Introduction to sql

Introduction to sql

sql language classification

Statement effect
DDL database definition language Database table view index stored procedure
DML database operation language Insert data insert delete data delete update data update
DQL database query language Query data select
DCL database control language Control user access

Glossary

Database server

The device running the database application, hardware + system software + mysql software

database
  • Default database

The database is the management unit of the table.

table

The table is the management unit of records.

Record (line)

The record is the carrier of information and the management unit of the field.

Field (column)

The field is composed of three parts, the name of the field + the type of the field + the field constraint.

Types of

Name, month, phone number, age, etc.

constraint

It cannot be empty, it will grow automatically.

dll

ddl library

SQL statements in the system are strictly case-sensitive. Generally, the sql command is uppercase, but the parameters are lowercase.
The database created by the sql statement is unique and can only be created once. You cannot use keywords such as create, select, etc. when creating the sql statement. Data and special symbols cannot be used alone.

  • Definition library

Create business database

# 格式:create 创建的类型(库还是表) 要创建的名称;
create database 数据库名;

View all databases

# 格式:show 查询的类型(库或者表);
show databases;

Insert picture description here

  • Select and enter the database
# 格式:use 数据库名;
use sqlproject01;

Query current library, call function

select database();
  • Delete database
# 格式:drop 要删除的类型(库或者表) 将要删除的名称;
drop database sqlproject01;

The location of the created database in the system

/var/lib/mysql/

ddl—>table

A table is a unit for recording data.

  • Create a table
    To create a table, you must first create a library and then enter the created library and then create a table.
# 格式:create table 表的名称 (列名1 类型(长度) 约束,列名2 类型(长度) 约束......);
create table t1 (id int);

Insert picture description here+ Query a table in a library

show tables;

Insert picture description here

  • Structure of the lookup table
desc t2;

Insert picture description here

  • Query all data in the table
# 格式: 查询 所有列 从 表名;
select * from t2;
  • Delete a table in a library
# 格式:删除 表 表名;
drop table t2;
  • Insert data in the data table
# 插入 从 表名 值 (值1,值2...);
insert into t1 values (1,"张三",18);

If you want to insert some data in a row

insert into t1 (id,name) values (4,"赵六");
insert into school.student1 (name,sex) values ("张三","f")

type of data

Commonly used data types are numeric, character, time and date types.

Numeric type

数值类型有:整型(int型)和float型(浮点数型)
float(7,2)代表数总共有7位,小数两位

String type

常用的有char(长度不可变)和varchar(长度可以增长),枚举类型enum("选项","选项"),集合类型set(多选,在界定的范围内选择)
create table t1 (id int,age int,love set("basketball","football","music"))

Time and date type

年year 日期date 时间time 日期和时间datetime

Insert picture description here
Insert picture description here+ Constraint type:
constraint type null and not null. As long as it is not null, then this column must be written.
Insert picture description here
Insert picture description here
Primary key: It must be unique and cannot be empty.

Insert picture description here
Insert picture description here
Self-increasingInsert picture description here
Insert picture description here

Insert picture description hereDefault value: default

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45671732/article/details/112889060