MySQL study notes. Basic operations of data tables


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
Insert picture description here
defining the columns Specify the primary key after defining all the columns
Insert picture description here
2. Multi-field combined primary key
Insert picture description here

2. Foreign key constraints

Grammar rules:constraint 外键名 foreign key (字段名) references 表名 (主键列)

3. Non-empty constraints

Grammar rules:字段名 数据类型 not null
Insert picture description here

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
Insert picture description here

5. Default constraints

Specify the default value of a column
Syntax rules:字段名 数据类型 default 默认值
Insert picture description here

6. Set table attributes to automatically increase

Grammar rules:字段名 数据类型 auto_increment

Insert picture description here
Insert picture description here

Two, modify the data table

1. Modify the field data type

Grammar rules:alter table 表名 modify 字段名 数据类型
Insert picture description here

2. Modify the field name

Grammar rules:alter table 表名 change 旧字段名 新字段名 新数据类型
Insert picture description here

3. Add fields

Grammar rules:alter table 表名 add 新字段名 数据类型

Insert picture description here
Specify where to add a new field
alter table 表名 add 新字段名 数据类型 first

alter table 表名 add 新字段名 数据类型 after 字段名

Insert picture description here

4. Delete the field

 alter table 表名 drop 字段名;

Insert picture description here

5. Modify the arrangement position of the field

Grammar rules:alter table 表名 modify 字段1 数据类型 first/after 字段2

Insert picture description here

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.

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/108628317