服务器有了怎么做网站wordpress注册错误
MySQL讲义第10讲——完整性约束之主键(PRIMARY KEY)约束
文章目录
- MySQL讲义第10讲——完整性约束之主键(PRIMARY KEY)约束
 - 一、定义主键的原则
 - 二、创建表时定义主键约束
 - 1、定义列时同时定义主键
 - 2、在定义完所有列之后定义主键
 - 3、定义多列主键
 
- 三、创建表之后添加主键
 - 1、添加列同时作为主键
 - 2、把一个已经存在的列定义为主键
 
- 四、删除主键约束
 
主键(primary key)是指表中的一个字段或多个字段的组合,其值能够唯一区分表中的每个行。主键用来表示一个特定的行。如果一个表没有定义主键,当对表进行更新操作时将非常不方便,因为在没有主键的情况下,可能无法准确定位要修改的行。
一、定义主键的原则
主键必须满足以下要求:
 (1)表中的任两行不能有相同的主键值。
 (2)每行都必须具有一个主键值(主键列不允许为 NULL)。
对主键的要求,最关键的一点是:记录一旦插入到表中,主键最好不要再修改,因为主键是用来唯一定位记录的。选取主键的一个基本原则是:不使用任何业务相关的字段作为主键。身份证号、手机号、邮箱地址等这些看上去可以唯一的字段,均不可用作主键。主键最好是完全与业务无关的字段,一般把这个字段命名为 id。
对于大部分应用来说,通常使用自增类型的字段作为主键,数据库会在插入数据时自动为每一条记录分配一个自增整数,这样我们就完全不用担心主键重复,也不用自己预先生成主键。
二、创建表时定义主键约束
1、定义列时同时定义主键
创建表时,定义列的同时指定主键,语法规则如下:
create table 表名 (<字段名> <数据类型> PRIMARY KEY [AUTO_INCREMENT],....
);
 
举例:
mysql> create table emp001(->     id int primary key auto_increment,->     name char(20),->     salary decimal(8,1)-> );
Query OK, 0 rows affected (0.02 sec)mysql> desc emp001;
+--------+--------------+------+-----+---------+----------------+
| Field  | Type         | Null | Key | Default | Extra          |
+--------+--------------+------+-----+---------+----------------+
| id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| name   | char(20)     | YES  |     | NULL    |                |
| salary | decimal(8,1) | YES  |     | NULL    |                |
+--------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)mysql> show create table emp001\G
*************************** 1. row ***************************Table: emp001
Create Table: CREATE TABLE `emp001` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` char(20) DEFAULT NULL,`salary` decimal(8,1) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
 
2、在定义完所有列之后定义主键
语法格式为:
create table 表名 (<字段定义>... ,PRIMARY KEY (字段名)
);
 
举例:
mysql> create table stu001(->     s_no char(11),->     s_name char(20),->     birth datetime,->     primary key(s_no)-> );
Query OK, 0 rows affected (0.04 sec)mysql> desc stu001;
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| s_no   | char(11) | NO   | PRI | NULL    |       |
| s_name | char(20) | YES  |     | NULL    |       |
| birth  | datetime | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)mysql> show create table stu001\G
*************************** 1. row ***************************Table: stu001
Create Table: CREATE TABLE `stu001` (`s_no` char(11) NOT NULL,`s_name` char(20) DEFAULT NULL,`birth` datetime DEFAULT NULL,PRIMARY KEY (`s_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
 
3、定义多列主键
多列主键又称复合主键,即主键有多个列组成,语法格式如下:
create table 表名 (<字段定义>... ,PRIMARY KEY (字段1 [,字段2,...])
);
 
举例:
mysql> create table score(->     stu_no char(11),->     course_no char(10),->     socre int,->     primary key(stu_no,course_no)-> );
Query OK, 0 rows affected (0.02 sec)mysql> desc score;
+-----------+----------+------+-----+---------+-------+
| Field     | Type     | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| stu_no    | char(11) | NO   | PRI | NULL    |       |
| course_no | char(10) | NO   | PRI | NULL    |       |
| socre     | int(11)  | YES  |     | NULL    |       |
+-----------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql> show create table score\G
*************************** 1. row ***************************Table: score
Create Table: CREATE TABLE `score` (`stu_no` char(11) NOT NULL,`course_no` char(10) NOT NULL,`socre` int(11) DEFAULT NULL,PRIMARY KEY (`stu_no`,`course_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
 
三、创建表之后添加主键
1、添加列同时作为主键
添加一个新列,同时把该列指定为主键,语法格式为:
alter table 表名 add 列名 类型 primary key [auto_increment];
 
举例:
mysql> create table t11(name char(11));
Query OK, 0 rows affected (0.02 sec)mysql> alter table t11 add id int primary key auto_increment first;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc t11;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| name  | char(11) | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)mysql> show create table t11\G
*************************** 1. row ***************************Table: t11
Create Table: CREATE TABLE `t11` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` char(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
 
2、把一个已经存在的列定义为主键
可以把一个已经存在的列定义为主键,语法格式如下:
alter table 表名 add primary key(列名);
 
举例:
mysql> create table t22(->     id int not null,->     name char(20)-> );
Query OK, 0 rows affected (0.01 sec)mysql> alter table t22 add primary key(id);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc t22;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   | PRI | NULL    |       |
| name  | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> show create table t22\G
*************************** 1. row ***************************Table: t22
Create Table: CREATE TABLE `t22` (`id` int(11) NOT NULL,`name` char(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
 
四、删除主键约束
删除主键约束的语法格式如下:
mysql> alter table t22 drop primary key;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc t22;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   |     | NULL    |       |
| name  | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> show create table t22\G
*************************** 1. row ***************************Table: t22
Create Table: CREATE TABLE `t22` (`id` int(11) NOT NULL,`name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
