Constraints include:
- 1. The primary key constraint
primary key
; - 2. Non-null constraints
not null
; - 3. The only constraint
unique
; - 4. foreign key constraints
foreign key
.
Wherein the primary key constraint comprises a non-empty and the only constraint constraint.
I use the table:
mysql> create table stu(
-> id int,
-> name varchar(20),
-> age int,
-> score double(4,2),
-> address_id int
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> create table address(
-> id int primary key auto_increment, # id自增
-> description varchar(20)
-> );
Query OK, 0 rows affected (0.02 sec)
insert into address(description)values('France'),('England'),('China');
Non-empty constraint
create:
- Adding NOT NULL constraint when creating tables
name varchar(20) not null
. - Created later added
alter table stu modify name varchar(20) not null
.
delete:
alter table stu modify name varchar(20)
。
example:
创建表以后:
mysql> alter table stu modify name varchar(20) not null;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,2) | YES | | NULL | |
| address_id | int(11) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
创建表时:
mysql> create table stu(
-> id int,
-> name varchar(20) not null,
-> age int,
-> score double(4,2),
-> address_id int)
-> ;
Query OK, 0 rows affected (0.02 sec)
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,2) | YES | | NULL | |
| address_id | int(11) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
删除非空约束:
mysql> alter table stu modify name varchar(20);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,2) | YES | | NULL | |
| address_id | int(11) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec
The primary key constraint
create:
- When you create a table, write directly on the primary key constraint:
id int primary key
or re-write the finalprimary key (id)
. - Then add a primary key constraint in the future to create the table:
alter table stu modify id int primary key
andalter table stu add primary key (id)
.
Note: Useprimary key(id)
the time, will set the default value of 0.
delete:
- Use
drop primary key
, no attention at this time usedmodify
to modify.
example:
创建好以后添加主键约束:
方式一:
mysql> alter table stu modify id int primary key;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,2) | YES | | NULL | |
| address_id | int(11) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
方式二:
mysql> alter table stu add primary key (id);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 0 | |
| name | varchar(20) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,2) | YES | | NULL | |
| address_id | int(11) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
这两种方式一个有默认值,一个没有默认值。
创建时添加主键约束:
mysql> create table stu(
-> id int primary key,
-> name varchar(20) not null,
-> age int,
-> score double(4,2),
-> address_id int
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,2) | YES | | NULL | |
| address_id | int(11) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
Foreign key constraint
create:
- When you create a table of creation:
constraint FK_stu_address foreign key (address_id) references address(id)
. Where:constraint
is the key constraintFK_stu_address
is the name of the foreign key,addresss_id
foreign key column name,address
is the primary table name, according to their actual situation changes,constraint FK_stu_address
this can not write. - After you've created to add:
add constraint FK_stu_address foreign key (address_id) references address(id)
.
delete:
alter table stu drop foreign key FK_stu_address
。
Cascade: when modifying the primary table, modify the corresponding data will follow from the table, the line followed by the direct use.
on update cascade
Cascading updateon delete cascade
Cascading delete- Use small example:
mysql> alter table stu add constraint FK_stu_address foreign key (address_id) REFERENCES address(id) on update cascade;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
example:
创建以后添加外键:
mysql> alter table stu add constraint FK_stu_address foreign key (address_id) REFERENCES address(id);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
创建时添加外键:
mysql> create table stu(
-> id int,
-> name varchar(20),
-> age int,
-> score double(4,2),
-> address_id int,
-> primary key(id),
-> constraint FK_stu_address # 这一行可以不写
-> foreign key(address_id) # 外键列名
-> REFERENCES address(id) # 对应主表中的东西
-> );
Query OK, 0 rows affected (0.02 sec)
删除外键:
mysql> alter table stu drop foreign key FK_stu_address;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0