MySQL约束

来源:互联网 发布:淘宝后台设置发货地 编辑:程序博客网 时间:2024/05/17 08:46

以下内容为个人学习总结,可能存在错误, 如发现请指出,不胜感激。

数据库中的约束主要完成对数据的检验,确保数据库表数据的合法性、完整性,如果有相互依赖数据,保证该数据不被误删除。


数据库中常用的五类约束:

not null:非空约束,指定某列数据不能为null

unique:唯一约束,指定某列或几列组合的数据不能重复,但是可以存在多个null

primary key:主键约束,指定某列或几列的数据不能重复、唯一

foreign key:外键,指定该列记录属于主表中的一条记录,参照另一条数据,一般以主键为外键

check:检查,指定一个表达式,用于检验指定数据。但是MySQL不支持check约束,但可以使用check约束,而没有任何效果。


MySQL中的约束保存在information_schema数据库的table_constraints表中,可以通过该表查询约束信息。

use information_schema;select * from table_constraints;select * from information_schema.table_constraints where table_name = '';

1、not null约束

非空约束用于确保当前列的值不为空值,非空约束只能出现在表对象的列上。

Null类型特征:

    所有的类型的值都可以是null,包括int、float等数据类型。

    空字符串""是不等于null,0也不等于null。

# 使用示例:drop table if exists test_table;create table test_table(    # 为id列和name列加上非空约束    id int not null,    name varchar(50) not null,    password varchar(50) default '123456');

如果表已经创建了,可以通过下面的方法来增加或删除非空约束:

# 增加非空约束:alter table test_table modify password varchar(50) not null;# 删除非空约束:alter table test_table modify password varchar(50) null default '123456';

2、unique约束

唯一约束是指表的列或列组合不能重复,保证数据的唯一性。虽然唯一约束不允许出现重复的值,但是可以为多个null。

同一个表可以有多个唯一约束,多个列组合的约束。

在创建唯一约束的时候,如果不给唯一约束名称,就默认和列名相同。

唯一约束不仅可以在一个表内创建,而且可以同时多表创建组合唯一约束。

MySQL会给唯一约束的列上默认创建一个唯一索引。

# 使用示例:drop table if exists test_table;create table test_table(    id int not null,    name varchar(50),    password varchar(50),    # 表级约束语法, 表示名字和密码组合不能重复,constraint是为这个约束取名    constraint test_table_uk unique(name, password));drop table if exists test_table;create table test_table(    id int not null,    # 列级约束语法,列级约束不可以使用constraint取名    name varchar(50) unique,    password varchar(50));
如果表已经创建了,可以通过下面的方法来增加、修改、删除唯一约束:
# 增加唯一约束:alter table test_table add unique(name, password);# 修改唯一约束:alter table test_table modify name varchar(25) unique;# 删除唯一约束alter table test_table drop index name;

3、primary key约束

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

如果是多列组合的主键约束,那么这些列都不允许为空值,并且组合的值不允许重复。

每个表最多只允许一个主键,建立主键约束可以在列级别创建,也可以在表级别上创建。MySQL的主键名总是PRIMARY。

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

# 使用示例:drop table if exists test_table;create table test_table(    # 列级约束的语法    id int primary key,    name varchar(50),    password varchar(50));drop table if exists test_table;create table test_table(    id int,    name varchar(50),    password varchar(50),    # 表级约束的语法    constraint test_table_pk primary key(id)); drop table if exists test_table;create table test_table(    id int,    name varchar(50),    password varchar(50),    # 组合模式的写法    constraint test_table_pk primary key(id, name));

如果表已经创建了,可以通过下面的方法来增加,修改,删除主键约束:

# 添加主键约束alter table test_table add primary key(id, name);# 修改主键约束# MySql中修改主键需要先删除原有的主键再添加# 删除主键约束alter table test_table drop primary key; 
在创建表时我们可以设置主键自增:
drop table if exists test_table;create table test_table(    # auto_increment自增模式,设置自增后在插入数据的时候就不需要给该列插入值了。    id int unsigned not null auto_increment primary key,    name varchar(50),    password varchar(50));# 设置自增长的开始值,默认从1开始ALTER TABLE TEST_TABLE AUTO_INCREMENT = 10001;

4、foreign key约束

外键约束是保证一个或两个表之间的参照完整性,外键是构建于一个表的两个字段或是两个表的两个字段之间的参照关系。

也就是说从表的外键值必须在主表中能找到或者为空。

当主表的记录被从表参照时,主表的记录将不允许删除,如果要删除数据,需要先删除从表中依赖该记录的数据,或者级联删除子表数据。

注意:外键约束的参照列,在主表中引用的只能是主键或唯一键约束的列,假定引用的主表列不是唯一的记录,那么从表引用的数据就不确定记录的位置。

同一个表可以有多个外键约束。

# 使用示例:# 创建主表,部门drop table if exists Department_table;create table Department_table(    id int unsigned not null auto_increment primary key,    name varchar(50))engine=innodb;# 创建从表,员工drop table if exists Employee_table;create table Employee_table(    id int unsigned not null auto_increment primary key,    name varchar(50),    # 列级外键约束使用references,这种方式外键约束其实没有被创建,我们进入information_schema.table_constraints表查看,但是这表SQL在执行过程中不会出现任何错误和警告。    Department int references Department_table(id))engine=innodb;# 创建表级外键约束drop table if exists Employee_table;create table Employee_table(    id int unsigned not null auto_increment primary key,    name varchar(50),    Department int unsigned not null,    # 表级外键约束使用foreign key ... references ...    constraint Employee_table_fk foreign key(Department) references Department_table(id),    index(Department))engine=innodb;
多列外键组合,必须用表级别约束语法:
# 创建主表,部门drop table if exists Department_table;create table Department_table(    id int unsigned not null auto_increment,    name varchar(50),    primary key(id, name))engine=innodb;# 创建从表,员工drop table if exists Employee_table;create table Employee_table(    id int unsigned not null auto_increment primary key,    name varchar(50),    Department_id int unsigned not null,    Department_name varchar(50),    # 表级别联合外键,前后的列顺序要对应    foreign key(Department_id, Department_name) references Department_table(id, name))engine=innodb;

如果表已经创建了,可以通过下面的方法来增加,修改,删除外键约束:

# 增加外键约束alter table Employee_table add foreign key(Department) references Department_table(id);# 修改外键约束可以先删除再添加# 删除外键约束:alter table Employee_table drop foreign key 外键约束名称;
级联操作:on delete/on update,用于定义delete,update操作。以下是update,delete操作的各种约束类型:
    CASCADE:外键表中外键字段值会被更新,或所在的列会被删除。
    RESTRICT:RESTRICT也相当于no action,即不进行任何操作;即,拒绝主表update外键关联列,delete记录。
    set null:被主表的外键关联字段被update,delete时,从表的外键列被设置为null。
# 使用示例:create table Employee_table(    id int unsigned not null auto_increment primary key,    name varchar(50),    Department_id int unsigned not null,    Department_name varchar(50),    foreign key(Department_id, Department_name) references Department_table(id, name) on delete cascade)engine=innodb;
5、check约束

MySQL可以使用check约束,但check约束对数据验证没有作用。

drop table if exists test_table;create table test_table(    id int unsigned not null auto_increment primary key,    name varchar(50),    age int,    check(age > 0));# age的数据小于0时,不会报错。insert into test_table(name, age) values('abc', -4);

0 0
原创粉丝点击