什么是主键?
作查询字段的适合做索引,加快查找速度
一个表中只能有一个primary key字段
对应的字段不容许重复,且不容许赋NULL值
如果有多个字段都为PRIMARY KEY,称为复合主键,必须一起创建。
主键字段的KEY标志是PRI
通常与AUTO——INCREMENT ----字段值自增长 ++
经常把表中能够唯一标示记录的字段设置为主键字段
添加主键的两种方式
mysql> create table t17(
-> stu_id char(9) primary key,
-> name char(10)
-> );
Query OK, 0 rows affected (0.25 sec) ----方式一
mysql> create table t18(
-> stu_id char(9),
-> name char(10),
-> primary key(stu_id)
-> );
Query OK, 0 rows affected (0.21 sec) ----方式二
mysql> desc t18;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| stu_id | char(9) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> insert into t17 values("1","ss");
Query OK, 1 row affected (0.12 sec)
mysql> insert into t17 values("1","ss");
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' ----主键约束
mysql> insert into t17 values("1","s");
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> alter table t17 drop primary key; -----删除主键
Query OK, 1 row affected (0.46 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> alter table t17 add primary key(stu_id); ----添加主键
Query OK, 0 rows affected (0.48 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加复合主键
mysql> create table t19(
-> cip char(15),
-> serport smallint(2),
-> status enum("yes","no"),
-> primary key(cip,serport));
Query OK, 0 rows affected (0.43 sec)
mysql> insert into t19 values("1.1.1.1",22,"no");
Query OK, 1 row affected (0.17 sec)
mysql> insert into t19 values("1.1.1.1",21,"yes");
Query OK, 1 row affected (0.10 sec)
删除主键
mysql> alter table t19 drop primary key;
Query OK, 2 rows affected (0.62 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc t19;
+---------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+-------+
| cip | char(15) | NO | | NULL | |
| serport | smallint(2) | NO | | NULL | |
| status | enum('yes','no') | YES | | NULL | |
+---------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
添加主键
mysql> alter table t19 add primary key(cip,serport);
Query OK, 0 rows affected (0.51 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t19;
+---------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+-------+
| cip | char(15) | NO | PRI | NULL | |
| serport | smallint(2) | NO | PRI | NULL | |
| status | enum('yes','no') | YES | | NULL | |
+---------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
自增长的两个条件:必须是主键且最好是整数类型的
mysql> create table t20(
-> stu_id int(2) primary key
-> auto_increment,
-> name char(10),
-> age tinyint(2)
-> );
Query OK, 0 rows affected (1.73 sec)
mysql> desc t20;
+--------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+----------------+
| stu_id | int(2) | NO | PRI | NULL | auto_increment |
| name | char(10) | YES | | NULL | |
| age | tinyint(2) | YES | | NULL | |
+--------+------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> insert into t20(name,age)values("bob",19);
Query OK, 1 row affected (0.30 sec)
mysql> select * from t20;
+--------+------+------+
| stu_id | name | age |
+--------+------+------+
| 1 | bob | 19 |
+--------+------+------+
1 row in set (0.00 sec)
mysql> insert into t20 values(null,"bosb",29);
Query OK, 1 row affected (0.64 sec)
mysql> select * from t20;
+--------+------+------+
| stu_id | name | age |
+--------+------+------+
| 1 | bob | 19 |
| 2 | bosb | 29 |
+--------+------+------+
2 rows in set (0.00 sec)
当自定义起自增长的数值时,再次的给该表赋值的时候,则在最大的值进行自动增长
mysql> select * from t20;
+--------+------+------+
| stu_id | name | age |
+--------+------+------+
| 1 | bob | 19 |
| 2 | bosb | 29 |
| 19 | zsb | 29 |
+--------+------+------+
3 rows in set (0.00 sec)
mysql> insert into t20 values(NULL,"assb",39);
Query OK, 1 row affected (0.09 sec)
mysql> select * from t20;
+--------+------+------+
| stu_id | name | age |
+--------+------+------+
| 1 | bob | 19 |
| 2 | bosb | 29 |
| 19 | zsb | 29 |
| 20 | assb | 39 |
+--------+------+------+
4 rows in set (0.00 sec)
什么时外键?
--当前表字段的值在另一个表中字段值的范围内选择。
使用外键的条件
--表的存储引擎必须是innodb
--字段类型要一致
--被参考字段必须要是索引类型的一种(primary key)
mysql> create table gzb(
-> ya_id int(2) primary key auto_increment, ----创建可以参照的表
-> name char(15),
-> bumen char(20)
-> )engine=innodb;
Query OK, 0 rows affected (0.29 sec)
mysql> insert into ygb(name,bumen)values("bob","tea"),("jack","tea");
Query OK, 2 rows affected (0.10 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from ygb;
+-------+------+-------+
| ya_id | name | bumen |
+-------+------+-------+
| 1 | bob | tea |
| 2 | jack | tea |
+-------+------+-------+
2 rows in set (0.00 sec)
mysql> create table gzb(
-> gz_id int(2),
-> name char(15),
-> pay float(7,2),
-> bumen char(20),
-> foreign key(gz_id) references ygb(ya_id) ------设置外键
-> on update cascade -----同步的更新
-> on delete cascade -----同步的删除
-> )
-> engine=innodb;
mysql> show create table gzb; -----查看外键的信息
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| gzb | CREATE TABLE `gzb` (
`gz_id` int(2) DEFAULT NULL,
`name` char(15) DEFAULT NULL,
`pay` float(7,2) DEFAULT NULL,
`bumen` char(20) DEFAULT NULL,
KEY `gz_id` (`gz_id`),
CONSTRAINT `gzb_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `ygb` (`ya_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into gzb values (1,"zhu",1333,"tea"); -----必须依赖参照物的表
Query OK, 1 row affected (0.06 sec)
mysql> insert into gzb values (5,"zhu",1333,"tea"); -----必须依赖参照物的表,否则会报错
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`zhuhaiyan`.`gzb`, CONSTRAINT `gzb_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `ygb` (`ya_id`) ON DELETE CASCADE ON UPDATE CASCADE)