在数据库中,“约束”指的是对表中数据的一种限制条件,它能够确保数据库中数据的准确性和有效性。比如有的数据是必填项,就像身份认证的时候,或者填注册信息的时候,手机号身份证这种就不能空着,所以就有了非空约束;又有的数据比如用户的身份证号码不能跟其他人的一样,所以就需要使用唯一约束等等。在MySQL中主要有6种约束:主键约束、外键约束、唯一约束、检查约束、非空约束和默认值约束。
视频讲解如下:
一、 主键约束(primary key)
主键是表里面的一个特殊字段,这个字段能够唯一标识该表中的每条信息。一张表只能定义一个主键,如果一个字段被定义成了主键,该列的值不允许为NULL,也不允许重复。下面是一个示例。
(1)创建表testprimarykey,并为表设置主键约束。
mysql> create table testprimarykey(id int primary key,name varchar(20)); # 提示:主键约束也可以在多个列上设定,例如: mysql> create table testprimarykey( id int ,name varchar(20),gender varchar(10), primary key(id, name) ); # 如果要在已经存在的表上添加主键约束,可以使用下面的语句: mysql> alter table testprimarykey add primary key(id,name);
(2)往testprimarykey表中插入数据。
mysql> insert into testprimarykey values(1,'Tom'); mysql> insert into testprimarykey values(2,'Mary'); mysql> insert into testprimarykey values(1,'Mike'); # 提示:当插入第3条数据时,会出现下面的错误。因为主键不允许重复。 ERROR 1062 (23000): Duplicate entry '1' for key 'testprimarykey.PRIMARY'
二、 外键约束(foreign key)
外键通常会和主键约束一起使用,用来确保数据的一致性。对于有关联关系的两张表,相关联字段中主键所在的表就是主表(父表),外键所在的表就是从表(子表),外键就是用来建立主表与从表的关联关系。当子表的某一个字段被定义为外键时,该列上的值必须在父表中存在或者为NULL值。下面是一个示例。
(1)创建用于外键约束的父表和子表。
mysql> create table testparent( id int primary key, name varchar(20) ); mysql> create table testchild( id int, name varchar(20), classes_id int, foreign key(classes_id) references testparent(id) ); # 提示:外键也可以使用多个字段组合进行设置,例如: mysql> create table classes( id int, name varchar(20), number int, primary key(id,name) ); mysql> create table student( id int auto_increment primary key, name varchar(20), classes_id int, classes_name varchar(20), foreign key(classes_id, classes_name) references classes(id, name) ); # 如果要在已存在的表上添加外键约束,可以使用下面的语句: mysql> alter table student add foreign key(classes_id, classes_name) references classes(id, name);
(2)往表testparent和表testchild中插入数据。
mysql> insert into testparent values(1,'Dev'); mysql> insert into testchild values(1,'Tom',1); mysql> insert into testchild values(2,'Mike',1); # 提示: 这三条insert语句都将成功插入数据。
(3)往表testchild中插入一条错误的数据。
mysql> insert into testchild values(3,'Mary',2); # 由于在表testparent中不存在“2”号记录,因此将输出下面的错误信息: ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`demo1`.`testchild`, CONSTRAINT `testchild_ibfk_1` FOREIGN KEY (`classes_id`) REFERENCES `testparent` (`id`))
三、 唯一约束(unique)
唯一约束就是指所有记录中字段的值不允许重复。值得注意的时,由于SQL中的NULL值是一个特殊值,因此如果一个字段被定义了唯一约束,该字段的值允许为NULL。下面是一个示例。
(1)创建新的表,并设置用户名、密码不能重复。
mysql> create table testunique( id int not null , name varchar(20), password varchar(10), unique(name,password) ); # 提示:如果想要在已经存在的表上添加唯一约束,可以使用下面的语句: mysql> mysql> alter table testunique add unique(name, password);
(2)往表testunique中插入数据。
mysql> insert into testunique values(1,'Tom','123456'); mysql> insert into testunique values(2,'Mary','123456'); mysql> insert into testunique values(3,'Mary','123456'); # 当插入第三条数据的时候,会出现下面的错误信息: ERROR 1062 (23000): Duplicate entry 'Mary-123456' for key 'testunique.name'
四、 检查约束(check)
MySQL提供了检查约束用来指定某列的可取值的范围,它通过限制输入到列中的值来强制域的完整性。下面是一个示例。
(1)创建新表,并添加检查约束用于检查薪水的范围。
mysql> create table testcheck( id int primary key, name varchar(25), salary float, check(salary>0 and salary<10000) );
(2)往表testcheck中插入数据
mysql> insert into testcheck values(1,'Tom',9000); mysql> insert into testcheck values(2,'Mike',15000); # 当插入第二条数据的时候,会出现下面的错误信息: ERROR 3819 (HY000): Check constraint 'testcheck_chk_1' is violated.
五、 非空约束(not null)
非空约束用于确保该字段的值不能为空值,非空约束只能出现在表对象的列上。下面是一个示例。
(1)创建新表,并设定name为非空约束,且默认值为“no name”
mysql> create table testnotnull( id int not null, name varchar(20) not null default 'no name', gender char );
(2)往表testnotnull中插入数据。
mysql> insert into testnotnull values(1,'Tom','F'); mysql> insert into testnotnull(id) values(2); # 提示:这两条语句都可以成功执行。 # 尽管在第二条语句中没有给出name的值,在这种情况下将会采用默认值“no name”。
(3)查询表testnotnull中的数据。
mysql> select * from testnotnull; # 输出的信息如下: +----+---------+--------+ | id | name | gender | +----+---------+--------+ | 1 | Tom | F | | 2 | no name | NULL | +----+---------+--------+
六、 默认值约束(default)
MySQL默认值约束用来指定某列的默认值。下面是一个示例。
(1)创建一张新的表test5.
mysql> create table test5(id int,name varchar(32),age int);
(2)在表中增加一个字段
mysql> alter table test5 add gender varchar(10) default 'Female'; # 提示:这里增加了一个gender字段用于表示性别,默认是“Female”。