MySQL database basic study notes 04-DDL language

DDL language

Data definition language, library and table management.
Library and table management:
create, modify, delete: create, alter, drop
library management
1. Create

create databaseif not exists)库名

2. Modify

rename database 原库名 to 新库名

3. Delete

drop databaseif exists)库名

Table management
1. Create

create table 表名(
	列名 类型 【(长度) 约束】
	...

2. Modify
Modify column name:

alter table 表名 change column 原列名 新列名 类型

Modify the type of column:

alter table 表名 modify column 列名 新类型

Add a new column:

alter table 表名 add column 新列名 类型

Delete column:

alter table drop column 列名

Modify the table name:

alter table 原表名 rename to 新表名

3. Delete

drop table 表名

4. Copy

create table 表名 like 被复制表名;    #复制表的结构
create table 表名
select * from 被复制表名;    #复制表的结构与数据

5. Constraint
A restriction, used to limit the data in the table, in order to ensure the accuracy and reliability of the data in the table
not null: non-empty constraint, used to ensure that the value of the field is not empty
default: default constraint, used Ensure that the field has a default value.
primary key: primary key constraint, used to ensure the uniqueness of the value of the field, and
unique: unique constraint, ensure that the value of the field is unique, can be empty
foreign key: foreign key constraint, use To limit the relationship between the two tables, it is used to ensure that the value of the field must come from the value of the associated column of the main table, and the foreign key constraint is added in the secondary table to reference the value of a column in the main table


Both the primary key and the unique 1. The uniqueness can be guaranteed
2. The primary key must not be empty, the only one can be empty
3. There can only be one primary key in a table, and there can be multiple uniques in a table
4. Allow combination of primary keys and combination of unique keys

Foreign key:
1. The foreign key relationship is required to be set from the table
2. The type of the foreign key column of the slave table and the type of the associated column of the
main table must be consistent or compatible, and the name is not required . 3. The associated column of the main table must be a key ( General primary key or unique)

Guess you like

Origin blog.csdn.net/qq_44708714/article/details/105868810
Recommended