Basic operation of data table
Preface
In the database, the data table is the most basic operation object in the database and the basic unit of data storage. A data table is defined as a collection of columns, and data is stored in the table in the format of rows and columns. Each row represents a unique record, and each column represents a field in the record.
1. Constraints
In the previous article, I already have a certain understanding of the operation of mysql, and know how to create a table, so I will not describe the creation of a table.
1. Primary key constraints
1. Single field primary key
字段名 数据类型 primary key
Specify the primary key while
defining the columns Specify the primary key after defining all the columns
2. Multi-field combined primary key
2. Foreign key constraints
Grammar rules:constraint 外键名 foreign key (字段名) references 表名 (主键列)
3. Non-empty constraints
Grammar rules:字段名 数据类型 not null
4. Uniqueness constraint
Only one null value is allowed. Make sure that there are no duplicate values in one or more columns.
Grammar rules:字段名 数据类型 unique
5. Default constraints
Specify the default value of a column
Syntax rules:字段名 数据类型 default 默认值
6. Set table attributes to automatically increase
Grammar rules:字段名 数据类型 auto_increment
Two, modify the data table
1. Modify the field data type
Grammar rules:alter table 表名 modify 字段名 数据类型
2. Modify the field name
Grammar rules:alter table 表名 change 旧字段名 新字段名 新数据类型
3. Add fields
Grammar rules:alter table 表名 add 新字段名 数据类型
Specify where to add a new field
alter table 表名 add 新字段名 数据类型 first
alter table 表名 add 新字段名 数据类型 after 字段名
4. Delete the field
alter table 表名 drop 字段名;
5. Modify the arrangement position of the field
Grammar rules:alter table 表名 modify 字段1 数据类型 first/after 字段2
to sum up
I've been taking the 6th grade exam recently, and I have been writing for a long time. Come on, go for the 6th grade.