mysql中可以通过在“create table”语句中,使用“7d7db3eee304faccc5db90e2b256afeb 744fc2083e98f2f4f987296bb2778d3e primary key [默认值]”语句来设置主键约束,使用“7d7db3eee304faccc5db90e2b256afeb 744fc2083e98f2f4f987296bb2778d3e not null”语句来设置非空约束。
mysql主键约束
主键(primary key)的完整称呼是“主键约束”,是 mysql 中使用最为频繁的约束。一般情况下,为了便于 dbms 更快的查找到表中的记录,都会在表中设置一个主键。
使用主键应注意以下几点:
每个表只能定义一个主键。
主键值必须唯一标识表中的每一行,且不能为 null,即表中不可能存在有相同主键值的两行数据。这是唯一性原则。
一个字段名只能在联合主键字段表中出现一次。
联合主键不能包含不必要的多余字段。当把联合主键的某一字段删除后,如果剩下的字段构成的主键仍然满足唯一性原则,那么这个联合主键是不正确的。这是最小化原则。
在创建表时设置主键约束
在创建数据表时设置主键约束,既可以为表中的一个字段设置主键,也可以为表中多个字段设置联合主键。但是不论使用哪种方法,在一个表中主键只能有一个。下面分别讲解设置单字段主键和多字段联合主键的方法。
1)设置单字段主键
在 create table 语句中,通过 primary key 关键字来指定主键。
在定义字段的同时指定主键,语法格式如下:
<字段名> <数据类型> primary key [默认值]
例 1
在 test_db 数据库中创建 tb_emp3 数据表,其主键为 id,sql 语句和运行结果如下。
mysql> create table tb_emp3 -> ( -> id int(11) primary key, -> name varchar(25), -> deptid int(11), -> salary float -> );query ok, 0 rows affected (0.37 sec)mysql> desc tb_emp3;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | no | pri | null | || name | varchar(25) | yes | | null | || deptid | int(11) | yes | | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.14 sec)
或者是在定义完所有字段之后指定主键,语法格式如下:
[constraint <约束名>] primary key [字段名]
例 2
在 test_db 数据库中创建 tb_emp4 数据表,其主键为 id,sql 语句和运行结果如下。
mysql> create table tb_emp4 -> ( -> id int(11), -> name varchar(25), -> deptid int(11), -> salary float, -> primary key(id) -> );query ok, 0 rows affected (0.37 sec)mysql> desc tb_emp4;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | no | pri | null | || name | varchar(25) | yes | | null | || deptid | int(11) | yes | | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.14 sec)
2)在创建表时设置联合主键
所谓的联合主键,就是这个主键是由一张表中多个字段组成的。
比如,设置学生选课数据表时,使用学生编号做主键还是用课程编号做主键呢?如果用学生编号做主键,那么一个学生就只能选择一门课程。如果用课程编号做主键,那么一门课程只能有一个学生来选。显然,这两种情况都是不符合实际情况的。
实际上设计学生选课表,要限定的是一个学生只能选择同一课程一次。因此,学生编号和课程编号可以放在一起共同作为主键,这也就是联合主键了。
主键由多个字段联合组成,语法格式如下:
primary key [字段1,字段2,…,字段n]
注意:当主键是由多个字段组成时,不能直接在字段名后面声明主键约束。
例 3
创建数据表 tb_emp5,假设表中没有主键 id,为了唯一确定一个员工,可以把 name、deptid 联合起来作为主键,sql 语句和运行结果如下。
mysql> create table tb_emp5 -> ( -> name varchar(25), -> deptid int(11), -> salary float, -> primary key(id,deptid) -> );query ok, 0 rows affected (0.37 sec)mysql> desc tb_emp5;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| name | varchar(25) | no | pri | null | || deptid | int(11) | no | pri | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+3 rows in set (0.14 sec)
在修改表时添加主键约束
主键约束不仅可以在创建表的同时创建,也可以在修改表时添加。但是需要注意的是,设置成主键约束的字段中不允许有空值。
在修改数据表时添加主键约束的语法格式如下:
alter table <数据表名> add primary key(<字段名>);
查看 tb_emp2 数据表的表结构,sql 语句和运行结果如下所示。
mysql> desc tb_emp2;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | no | | null | || name | varchar(30) | yes | | null | || deptid | int(11) | yes | | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.14 sec)
例 4
修改数据表 tb_emp2,将字段 id 设置为主键,sql 语句和运行结果如下。
mysql> alter table tb_emp2 -> add primary key(id);query ok, 0 rows affected (0.94 sec)records: 0 duplicates: 0 warnings: 0mysql> desc tb_emp2;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | no | pri | null | || name | varchar(30) | yes | | null | || deptid | int(11) | yes | | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.12 sec)
通常情况下,当在修改表时要设置表中某个字段的主键约束时,要确保设置成主键约束的字段中值不能够有重复的,并且要保证是非空的。否则,无法设置主键约束。
mysql非空约束
mysql 非空约束(not null)指字段的值不能为空。对于使用了非空约束的字段,如果用户在添加数据时没有指定值,数据库系统就会报错。可以通过 create table 或 alter table 语句实现。在表中某个列的定义后加上关键字 not null 作为限定词,来约束该列的取值不能为空。
比如,在用户信息表中,如果不添加用户名,那么这条用户信息就是无效的,这时就可以为用户名字段设置非空约束。
在创建表时设置非空约束
创建表时可以使用 not null 关键字设置非空约束,具体的语法格式如下:
<字段名> <数据类型> not null
例 1
创建数据表 tb_dept4,指定部门名称不能为空,sql 语句和运行结果如下所示。
mysql> create table tb_dept4 -> ( -> id int(11) primary key, -> name varchar(22) not null, -> location varchar(50) -> );query ok, 0 rows affected (0.37 sec)mysql> desc tb_dept3;+----------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+----------+-------------+------+-----+---------+-------+| id | int(11) | no | pri | null | || name | varchar(22) | no | | null | || location | varchar(50) | yes | | null | |+----------+-------------+------+-----+---------+-------+3 rows in set (0.06 sec)
在修改表时添加非空约束
如果在创建表时忘记了为字段设置非空约束,也可以通过修改表进行非空约束的添加。
修改表时设置非空约束的语法格式如下:
alter table <数据表名>change column <字段名><字段名> <数据类型> not null;
例 2
修改数据表 tb_dept4,指定部门位置不能为空,sql 语句和运行结果如下所示。
mysql> alter table tb_dept4 -> change column location -> location varchar(50) not null;query ok, 0 rows affected (0.15 sec)records: 0 duplicates: 0 warnings: 0mysql> desc tb_dept4;+----------+-------------+------+-----+----------+-------+| field | type | null | key | default | extra |+----------+-------------+------+-----+----------+-------+| id | int(11) | no | pri | null | || name | varchar(22) | no | | null | || location | varchar(50) | no | | null | |+----------+-------------+------+-----+----------+-------+3 rows in set (0.00 sec)
推荐教程:mysql视频教程
以上就是mysql主键非空约束怎么设置?的详细内容。