MySQL学习——约束

MySQL学习——约束

摘要:本文主要学习了数据库的约束。

primary key(主键)

定义

主键约束是一个列或者多个列,其值能唯一地标识表中的每一行。这样的一列或多列称为表的主键,通过它可以强制表的实体完整性。

主键约束相当于唯一约束和非空约束的组合,主键约束列不允许重复,也不允许出现空值。

每个表最多只允许一个主键,建立主键约束可以在列级别创建,也可以在表级别创建。

当创建主键的约束时,系统默认会在所在的列和列组合上建立对应的唯一索引。

主键自增

MySQL数据库提供了一个自增的数字,专门用来自动生成主键值,主键值不用用户维护,自动生成,自增数从1开始,以1递增。

使用方式是在主键后面添加 auto_increment 选项。

实例

在创建表时添加单列主键约束,主键自增:

1 create table test (
2   id int(11) primary key auto_increment,
3   phone int(11),
4   name varchar(50)
5 );

在创建表时添加复合主键约束:

1 mysql> create table test (
2     ->   id int(11),
3     ->   phone int(11),
4     ->   name varchar(50),
5     ->   primary key(id, phone)
6     -> );
7 Query OK, 0 rows affected (0.00 sec)
8 
9 mysql> 

在修改表时添加主键约束:

1 mysql> alter table test add primary key(id, phone);
2 Query OK, 0 rows affected (0.01 sec)
3 Records: 0  Duplicates: 0  Warnings: 0
4 
5 mysql> 

在修改表时删除主键约束:

1 mysql> alter table test drop primary key;
2 Query OK, 0 rows affected (0.01 sec)
3 Records: 0  Duplicates: 0  Warnings: 0
4 
5 mysql> 

foreign key(外键)

定义

外键约束用来在两个表的数据之间建立链接,它可以是一列或者多列。一个表可以有一个或多个外键。

外键对应的是参照完整性,一个表的外键可以为空值,若不为空值,则每一个外键的值必须等于另一个表中主键的某个值。

外键是表的一个字段,不是本表的主键,但对应另一个表的主键。定义外键后,不允许删除另一个表中具有关联关系的行。

外键的主要作用是保持数据的一致性、完整性。

主表和子表

主表(父表):对于两个具有关联关系的表而言,相关联字段中主键所在的表就是主表。

从表(子表):对于两个具有关联关系的表而言,相关联字段中外键所在的表就是从表。

实例

创建父表并定义主键约束:

1 mysql> create table man (
2     ->   id int(11) primary key,
3     ->   phone int(11),
4     ->   name varchar(50)
5     -> );
6 Query OK, 0 rows affected (0.01 sec)
7 
8 mysql> 

在创建表时添加外键约束:

 1 mysql> create table child (
 2     ->   id int(11) primary key,
 3     ->   manid int(11),
 4     ->   phone int(11),
 5     ->   name varchar(50),
 6     ->   constraint fk_man_id foreign key(manid) references man(id)
 7     -> );
 8 Query OK, 0 rows affected (0.00 sec)
 9 
10 mysql> 

在修改表时添加外键约束:

1 mysql> alter table child add constraint fk_man_id foreign key(manid) references man(id);
2 Query OK, 0 rows affected (0.01 sec)
3 Records: 0  Duplicates: 0  Warnings: 0
4 
5 mysql> 

在修改表时删除外键约束:

1 mysql> alter table child drop foreign key fk_man_id;
2 Query OK, 0 rows affected (0.00 sec)
3 Records: 0  Duplicates: 0  Warnings: 0
4 
5 mysql> 

unique key(唯一)

定义

唯一约束要求该列唯一,允许为空,但只能出现一个空值。唯一约束可以确保一列或者几列不出现重复值。

实例

在创建表时添加单列唯一约束:

1 mysql> create table test (
2     ->   id int(11) primary key,
3     ->   phone int(11) unique,
4     ->   name varchar(50)
5     -> );
6 Query OK, 0 rows affected (0.00 sec)
7 
8 mysql> 

在创建表时添加多列唯一约束:

1 mysql> create table test (
2     ->   id int(11) primary key,
3     ->   phone int(11),
4     ->   name varchar(50),
5     ->   unique(phone, name)
6     -> );
7 Query OK, 0 rows affected (0.00 sec)
8 
9 mysql> 

在修改表时添加唯一约束:

1 mysql> alter table test add unique uk_test_phone(phone);
2 Query OK, 0 rows affected (0.00 sec)
3 Records: 0  Duplicates: 0  Warnings: 0
4 
5 mysql> 

在修改表时删除唯一约束:

1 mysql> alter table test drop index uk_test_phone;
2 Query OK, 0 rows affected (0.00 sec)
3 Records: 0  Duplicates: 0  Warnings: 0
4 
5 mysql> 

default constraint(默认值)

定义

默认值约束用来指定某列的默认值。

实例

在创建表时添加默认值:

1 mysql> create table test (
2     ->   id int(11) primary key,
3     ->   phone int(11),
4     ->   name varchar(50) default 'Test'
5     -> );
6 Query OK, 0 rows affected (0.01 sec)
7 
8 mysql> 

在修改表时修改默认值:

1 mysql> alter table test change column name name varchar(30) default 'none';
2 Query OK, 1 row affected (0.00 sec)
3 Records: 1  Duplicates: 0  Warnings: 0
4 
5 mysql> 

not null constraint(非空)

定义

非空约束可以用来约束该列的取值不能为空,对于使用了非空约束的字段,如果用户在添加数据时没有指定值,数据库系统就会报错。

实例

在创建表时添加非空约束:

1 mysql> create table test (
2     ->   id int(11) primary key,
3     ->   phone int(11),
4     ->   name varchar(50) not null
5     -> );
6 Query OK, 0 rows affected (0.00 sec)
7 
8 mysql> 

在修改表时修改非空约束:

1 mysql> alter table test change column name name varchar(30) null;
2 Query OK, 0 rows affected (0.01 sec)
3 Records: 0  Duplicates: 0  Warnings: 0
4 
5 mysql> 
原文地址:https://www.cnblogs.com/shamao/p/11468174.html