当前位置: 首页 > news >正文

微商城网站建设代理商宁波方正建设监理网站

微商城网站建设代理商,宁波方正建设监理网站,一起装修网官方网站,vps服务器中的网站不显示图片目录 mysql 表的约束 NULL/NOT NULL DEFAULT comment zerofill PRIMARY KRY 删除主键 添加主键 复合主键 AUTO_INCREMENT UNIQUE KEY FOREIGN KEY mysql 表的约束 约束是 mysql 为了保证数据正确的一种手段#xff0c;而前面在谈数据类型的时候#xff0c;数据类…目录 mysql 表的约束 NULL/NOT NULL DEFAULT comment zerofill PRIMARY KRY 删除主键 添加主键 复合主键 AUTO_INCREMENT UNIQUE KEY FOREIGN KEY mysql 表的约束 约束是 mysql 为了保证数据正确的一种手段而前面在谈数据类型的时候数据类型其实就是一种约束但是 mysql 的约束可不仅仅是数据类型mysql 有各种各样的约束下面就看一下 mysql 中其他的约束。 NULL/NOT NULL null / not null 约束是mysql常见的一种约束之一not null可以保证用户输入的数据不为空。 创建一张表里面添加 not null 约束 mysql use restraint; Database changed mysql create table t1(- name varchar(12),- age tinyint not null); Query OK, 0 rows affected (0.01 sec) ​ mysql desc t1; ----------------------------------------------- | Field | Type       | Null | Key | Default | Extra | ----------------------------------------------- | name | varchar(12) | YES |     | NULL   |       | | age   | tinyint(4) | NO   |     | NULL   |       | ----------------------------------------------- 2 rows in set (0.01 sec) 这里表已经创建好了其中 name 并没有设置约束而age 设置了 not null 约束通过 desc 查看表结构发现在 Null 那一列发现 name 的 Null 列显示 yes 表示可以为 null 但是 age 显示 No 表示不能为空。 插入数据测试 not null 约束 mysql insert into t1(name, age) values(张三, 27); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t1(age) values(27); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t1; ------------- | name   | age | ------------- | 张三   | 27 | | NULL   | 27 | ------------- 2 rows in set (0.00 sec) 上面使用全列插入还有只插入了 age 发现插入成功但是只插入了 age 的那一条记录发现name为 NULL。 不插入age 和 插入 age 为NULL 测试 mysql insert into t1(name) values(27); ERROR 1364 (HY000): Field age doesnt have a default value mysql insert into t1(name,age) values(27, null); ERROR 1048 (23000): Column age cannot be null 上面两条记录均插入失败但是仔细观察发现这两条记录插入的报错信息却是不一样的没有插入 age 的那条报错显示没有默认的 value 但是插入为 null 的报错信息显示不能为 null。 其实这里发现 not null 约束和 default 约束还有一点点联系下面看 default 约束会让两者进行对比。 DEFAULT default 约束就是在填表的时候可以直接忽略掉该列然后可以让 default 默认填写但是如果插入了数据就会按照插入的数据来看。 下面通过查看表创建来看一下 default 和 not null。 创建一张表里面有 id 为 int 类型且无约束name varchar(12) 为 not null 还有 age tinyint 为 default 22 mysql create table t2(- id int,- name varchar(12) not null,- age tinyint default 22); Query OK, 0 rows affected (0.00 sec) ​ mysql desc t2; ----------------------------------------------- | Field | Type       | Null | Key | Default | Extra | ----------------------------------------------- | id   | int(11)     | YES |     | NULL   |       | | name | varchar(12) | NO   |     | NULL   |       | | age   | tinyint(4) | YES |     | 22     |       | ----------------------------------------------- 3 rows in set (0.00 sec) 这里创建成功同时也看到 Null 里面 name 不为空default 里面 age 默认为 22和我们设置的一样。 下面在看一下表的创建sql语句 mysql show create table t2 \G *************************** 1. row ***************************Table: t2 Create Table: CREATE TABLE t2 (id int(11) DEFAULT NULL,name varchar(12) NOT NULL,age tinyint(4) DEFAULT 22 ) ENGINEInnoDB DEFAULT CHARSETutf8 1 row in set (0.00 sec) mysql 通过语法分析词法分析以及各种优化等帮我们记录的该表创建sql语句就是这样的而 id 里面在我们写的时候并没有添加 default 但是mysql自动帮我们添加 default null。 插入数据 mysql insert into t2(id, name, age) values(1, 张三, 10); Query OK, 1 row affected (0.01 sec) ​ mysql insert into t2(name, age) values(张三, 10); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t2(name) values(张三); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t2(name, age) values(张三, NULL); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t2; -------------------- | id   | name   | age | -------------------- |   1 | 张三   |   10 | | NULL | 张三   |   10 | | NULL | 张三   |   22 | | NULL | 张三   | NULL | -------------------- 4 rows in set (0.00 sec) 这里分别是全列插入不插入 id 不插入 id 和 age不插入 id 且插入 age 为 NULL上面发现均插入成功且而没有插入 id 则 id 均为 null就是像上面的sql 创建语句一样 id 默认为 null而没有插入 age age就是默认 22还有就是插入 age 为 NULL 也插入成功。 那么通过上面的测试现在假设我们将一列字段的约束设置为即有 not null 约束也有 default 约束会怎么样 mysql create table t3(- id int not null,- name varchar(12) default 张三,- age tinyint not null default 20); Query OK, 0 rows affected (0.01 sec) ​ mysql desc t3; ----------------------------------------------- | Field | Type       | Null | Key | Default | Extra | ----------------------------------------------- | id   | int(11)     | NO   |     | NULL   |       | | name | varchar(12) | YES |     | 张三   |       | | age   | tinyint(4) | NO   |     | 20     |       | ----------------------------------------------- 3 rows in set (0.00 sec) ​ mysql show create table t3\G *************************** 1. row ***************************Table: t3 Create Table: CREATE TABLE t3 (id int(11) NOT NULL,name varchar(12) DEFAULT 张三,age tinyint(4) NOT NULL DEFAULT 20 ) ENGINEInnoDB DEFAULT CHARSETutf8 1 row in set (0.00 sec) 创建完成后现在我们就是很好奇向 age 里面插入数据或者直接忽略此字段会怎么样 mysql insert into t3(id, name, age) values(1, 田七, 12); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t3(id, name) values(1, 田七); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t3; ----------------- | id | name   | age | ----------------- | 1 | 田七   | 12 | | 1 | 田七   | 20 | ----------------- 2 rows in set (0.01 sec) 上面的插入均成功了即使忽略了也可以默认那么下面我们插入 NULL 呢 mysql insert into t3(id, name, age) values(1, 田七, NULL); ERROR 1048 (23000): Column age cannot be null 这里插入失败了通过上面的测试我们发现了 not null 和 default 的区别 default 约束的是用户不插入数据的时候 not null 约束的是用户插入数据的时候 comment comment 其实并不是用来约束数据插入的而在我认为他就是一种软约束它约束的是用户看到 comment 的时候就知道该列里面填入的数据是何种类型或者样式。 comment 其实在数据类型的时候就以及用过了而且该约束还是比较简答的。 mysql create table t4(- name varchar(12) comment 用户名,- age tinyint comment 用户年龄); Query OK, 0 rows affected (0.01 sec) ​ mysql desc t4; ----------------------------------------------- | Field | Type       | Null | Key | Default | Extra | ----------------------------------------------- | name | varchar(12) | YES |     | NULL   |       | | age   | tinyint(4) | YES |     | NULL   |       | ----------------------------------------------- 2 rows in set (0.00 sec) 表创建好后并不能用 desc 查看到该约束想要查看该约束还是需要用查看该表创建时的sql语句 mysql show create table t4\G *************************** 1. row ***************************Table: t4 Create Table: CREATE TABLE t4 (name varchar(12) DEFAULT NULL COMMENT 用户名,age tinyint(4) DEFAULT NULL COMMENT 用户年龄 ) ENGINEInnoDB DEFAULT CHARSETutf8 1 row in set (0.00 sec) zerofill zerofill 该约束其实时用在整数类型的之前在创建 int 类型的时候后面总是会带一个括号里面写着 11其实该约束和那个括号就有关系而且该约束时显示约束并不会对用户插入数据等有影响只是在显示的时候会有不同的样子。 mysql create table t5(- num int zerofill); Query OK, 0 rows affected (0.01 sec) ​ mysql desc t5; ------------------------------------------------------------- | Field | Type                     | Null | Key | Default | Extra | ------------------------------------------------------------- | num   | int(10) unsigned zerofill | YES |     | NULL   |       | ------------------------------------------------------------- 1 row in set (0.00 sec) 这里看到确实添加了 zero fill 并且默认给我们添加了 unsigned 下面插入数据查看我们就知道了 mysql insert into t5 values(1); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t5 values(11); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t5 values(111); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t5; ------------ | num       | ------------ | 0000000001 | | 0000000011 | | 0000000111 | ------------ 3 rows in set (0.00 sec) 这里发现插入后这些数字前面均加了一下 0 这个就是 zerofill 的显示约束而且 int 后面的那个数字就表示可以显示多少位数字如果是有符号整型那么就是 11 位如果是无符号就是 10位我们也可以改变该 int 后面的数字来查看 mysql alter table t5 modify num int(4) zerofill; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 ​ mysql desc t5; ------------------------------------------------------------ | Field | Type                     | Null | Key | Default | Extra | ------------------------------------------------------------ | num   | int(4) unsigned zerofill | YES |     | NULL   |       | ------------------------------------------------------------ 1 row in set (0.00 sec) 修改完成后继续查看插入后的数据 mysql select * from t5; ------ | num | ------ | 0001 | | 0011 | | 0111 | ------ 3 rows in set (0.00 sec) 现在就变成 最高显示4位的整数了而 zerofill 就是帮助如果没有超出该显示范围就用 0 来补充。 下面插入一个大于 4 位数的数据 mysql select * from t5; -------- | num   | -------- |   0001 | |   0011 | |   0111 | | 111111 | -------- 4 rows in set (0.00 sec) 这里发现如果超出的话就是正常显示也不会发生截断。 PRIMARY KRY 主键约束在一张表里面只能有一个主键而且主键不能为空且不能重复下面看一下如何设置主键 mysql create table t6(- id int primary key,- name varchar(12) not null,- age tinyint default 18- ); Query OK, 0 rows affected (0.01 sec) ​ mysql desc t6; ----------------------------------------------- | Field | Type       | Null | Key | Default | Extra | ----------------------------------------------- | id   | int(11)     | NO   | PRI | NULL   |       | | name | varchar(12) | NO   |     | NULL   |       | | age   | tinyint(4) | YES |     | 18     |       | ----------------------------------------------- 3 rows in set (0.00 sec) 设置 id 为主键后 Null 列显示 id 不能为空且 key 列 显示为 pri 也就是 primary key 主键默认为空。 插入数据 mysql insert into t6(id, name, age) values(1, 天蓬元帅, 28); Query OK, 1 row affected (0.01 sec) ​ mysql insert into t6(id, name, age) values(2, 玉皇大帝, 106); Query OK, 1 row affected (0.01 sec) ​ mysql select * from t6; ------------------------ | id | name         | age | ------------------------ | 1 | 天蓬元帅     |   28 | | 2 | 玉皇大帝     | 106 | ------------------------ 2 rows in set (0.00 sec) ​ 这里插入 id 分别为 1 和 2 显示插入成功查询数据同样显示插入成功下面插入相同的 id 查看是否可以插入 mysql insert into t6(id, name, age) values(2, 女儿国王, 18); ERROR 1062 (23000): Duplicate entry 2 for key PRIMARY 这里显示插入失败了报错原因就是已经有主键了。 那么如果我们直接省略掉该列看一下 mysql insert into t6(name, age) values(东海龙王, 18); ERROR 1364 (HY000): Field id doesnt have a default value 由于主键也是不能为空所以也不能插入。 那么如果表已经创建好了但是还想为其插入主键怎么办 删除主键 alter table table_name drop primary key; 因为只有一个主键所以删除可以直接 drop primary key。 mysql alter table t6 drop primary key; Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 ​ mysql desc t6; ----------------------------------------------- | Field | Type       | Null | Key | Default | Extra | ----------------------------------------------- | id   | int(11)     | NO   |     | NULL   |       | | name | varchar(12) | NO   |     | NULL   |       | | age   | tinyint(4) | YES |     | 18     |       | ----------------------------------------------- 3 rows in set (0.00 sec) 这里看到删除成功了。 删除主键后我们继续插入相同的 id 看是否能插入 mysql insert into t6(id, name, age) values(2, 女儿国王, 18); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t6; ------------------------ | id | name         | age | ------------------------ | 1 | 天蓬元帅     |   28 | | 2 | 玉皇大帝     | 106 | | 2 | 女儿国王     |   18 | ------------------------ 3 rows in set (0.00 sec) 这里插入就直接成功了。 添加主键 alter table table_name add primary key(列名); mysql alter table t6 add primary key(id); ERROR 1062 (23000): Duplicate entry 2 for key PRIMARY 上面我们添加主键失败了为什么 因为我们在前面插入了相同的 id 所以为 id 添加主键就会失败所以我们需要修改 id 不重复就好了 mysql update t6 set id3 where name女儿国王; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 ​ mysql select * from t6; ------------------------ | id | name         | age | ------------------------ | 1 | 天蓬元帅     |   28 | | 2 | 玉皇大帝     | 106 | | 3 | 女儿国王     |   18 | ------------------------ 3 rows in set (0.00 sec) 修改成功后我们继续添加主键 mysql alter table t6 add primary key(id); Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 ​ mysql desc t6; ----------------------------------------------- | Field | Type       | Null | Key | Default | Extra | ----------------------------------------------- | id   | int(11)     | NO   | PRI | NULL   |       | | name | varchar(12) | NO   |     | NULL   |       | | age   | tinyint(4) | YES |     | 18     |       | ----------------------------------------------- 3 rows in set (0.00 sec) ​ 这里添加主键再次成功下面我们插入相同的 id 看一下 mysql insert into t6 (id, name, age) values(3, 东海龙王, 99); ERROR 1062 (23000): Duplicate entry 3 for key PRIMARY 这里插入相同数据后就失败了。 复合主键 复合主键就是多列共成主键我们之前说的是主键只能有一个但是一个主键既可以由一列构成也可以由多列构成。 添加复合主键 mysql create table t7(- student_id char(5),- course_id tinyint unsigned,- primary key(student_id, course_id)- ); Query OK, 0 rows affected (0.00 sec) ​ mysql desc t7; ------------------------------------------------------------ | Field     | Type               | Null | Key | Default | Extra | ------------------------------------------------------------ | student_id | char(5)             | NO   | PRI | NULL   |       | | course_id | tinyint(3) unsigned | NO   | PRI | NULL   |       | ------------------------------------------------------------ 2 rows in set (0.00 sec) ​ 这里看到设置了复合主键 key 列里面的两列都是 PRI这里并不是两个都是主键而是这两个形成一个主键。 看到上面的创建复合主键的方法我们也就知道在添加一个主键的时候也可以这样写。 下面看一下这个复合主键是怎么样子的 mysql insert into t7(student_id, course_id) values(11111, 12); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t7(student_id, course_id) values(22222, 12); Query OK, 1 row affected (0.00 sec) mysql select * from t7; ----------------------- | student_id | course_id | ----------------------- | 11111     |       12 | | 22222     |       12 | ----------------------- 2 rows in set (0.00 sec) 这里看到我们插入 student_id 没有重复但是 course_id 重复了但是插入成功下麦呢看一下 student_id 重复 mysql insert into t7(student_id, course_id) values(22222, 13); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t7; ----------------------- | student_id | course_id | ----------------------- | 11111     |       12 | | 22222     |       12 | | 22222     |       13 | ----------------------- 3 rows in set (0.00 sec) 下面我们插入了 student_id 重复但是 course_id 不重复也插入成功了。 下面让 student_id 和 course_id 都重复 mysql insert into t7(student_id, course_id) values(22222, 13); ERROR 1062 (23000): Duplicate entry 22222-13 for key PRIMARY 这里插入相同的数据显示报错了这里看到复合主键将这些数据都连接在一起了所以对于复合主键来说如果都重复才算重复只有其中一个重复或者是都不重复就不算重复。 AUTO_INCREMENT 自增键一般都是和主键共同使用下面还是先直接看一下自增键的使用然后在得出结论。 mysql create table t8(- id int unsigned primary key auto_increment,- name varchar(12) default 张三); Query OK, 0 rows affected (0.01 sec) ​ mysql desc t8; ------------------------------------------------------------- | Field | Type             | Null | Key | Default | Extra         | ------------------------------------------------------------- | id   | int(10) unsigned | NO   | PRI | NULL   | auto_increment | | name | varchar(12)     | YES |     | 张三   |               | ------------------------------------------------------------- 2 rows in set (0.00 sec) ​ 创建好后查看到该表的结构一般自增都在一张表中只能由一个自增键下面我们可以在创建一张表看一下是否可以由多个自增键 mysql create table t9(- id1 primary key auto_increment,- id2 auto_increment); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near primary key auto_increment, id2 auto_increment) at line 2 这里就直接报错了不能有多个自增键。 下面我们插入数据看一下 mysql insert into t8(name) values(张三); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t8(name) values(李四); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t8(name) values(王五); Query OK, 1 row affected (0.00 sec) ​ mysql select *from t8; ------------ | id | name   | ------------ | 1 | 张三   | | 2 | 李四   | | 3 | 王五   | ------------ 3 rows in set (0.00 sec) ​ 这里插入后我们没有插入 id 但是 id 从1开始依次递增那么我们主动插入 id mysql insert into t8(id, name) values(1000, 赵六); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t8; -------------- | id   | name   | -------------- |   1 | 张三   | |   2 | 李四   | |   3 | 王五   | | 1000 | 赵六   | -------------- 4 rows in set (0.00 sec) 这里主动插入后也成功了那么我们继续忽略 id 列呢 mysql insert into t8(name) values(田七); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t8(name) values(王小二); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t8; ----------------- | id   | name     | ----------------- |   1 | 张三     | |   2 | 李四     | |   3 | 王五     | | 1000 | 赵六     | | 1001 | 田七     | | 1002 | 王小二   | ----------------- 6 rows in set (0.00 sec) 这里看到插入的数据是从上一次的数据增加的这里我们可以看一下该表的创建 sql 就知道了 mysql show create table t8\G *************************** 1. row ***************************Table: t8 Create Table: CREATE TABLE t8 (id int(10) unsigned NOT NULL AUTO_INCREMENT,name varchar(12) DEFAULT 张三,PRIMARY KEY (id) ) ENGINEInnoDB AUTO_INCREMENT1003 DEFAULT CHARSETutf8 1 row in set (0.00 sec) 这里创建 sql 里面底下有一句 auto_increment1003而且下一次插入的话确实是从 1003 开始所以我们知道我们也可以在创建的时候加这一句 mysql create table t9(- id int primary key auto_increment,- name varchar(12) default 张三- )auto_increment100; Query OK, 0 rows affected (0.00 sec) ​ mysql show create table t9\G *************************** 1. row ***************************Table: t9 Create Table: CREATE TABLE t9 (id int(11) NOT NULL AUTO_INCREMENT,name varchar(12) DEFAULT 张三,PRIMARY KEY (id) ) ENGINEInnoDB AUTO_INCREMENT100 DEFAULT CHARSETutf8 1 row in set (0.00 sec) 这里创建好后看到自增就是从100开始下面插入数据查看 mysql insert into t9(name) values(张三); Query OK, 1 row affected (0.00 sec) ​ mysql insert into t9(name) values(李四); Query OK, 1 row affected (0.01 sec) ​ mysql insert into t9(name) values(王五); Query OK, 1 row affected (0.00 sec) ​ mysql select * from t9; ------------- | id | name   | ------------- | 100 | 张三   | | 101 | 李四   | | 102 | 王五   | ------------- 3 rows in set (0.00 sec) UNIQUE KEY 唯一键和主键很容易弄混有时候不知道该使用主键还是唯一键唯一键和主键功能上的区别并不大只是主键不能为空而唯一键可以为空。 唯一键不能重复但是唯一键可以为空但是如果设置唯一键后在设置一个 NOT NULL那么就和主键的功能上没有区别了。 下面还是直接看一下唯一键然后说一下唯一键和主键的意义上的区别。 mysql create table tt1(- id int primary key,- telephone char(11) unique,- name varchar(12));Query OK, 0 rows affected (0.00 sec) ​ mysql desc tt1; ------------------------------------------------------------ | Field     | Type       | Null | Key | Default | Extra         | ------------------------------------------------------------ | id       | int(11)     | NO   | PRI | NULL   | auto_increment | | telephone | char(11)   | YES | UNI | NULL   |               | | name     | varchar(12) | YES |     | NULL   |               | ------------------------------------------------------------ 3 rows in set (0.00 sec) ​ 这里设置了一个自增一个唯一键下面插入数据 mysql insert into tt1(telephone, name) values(12345678901, 贾宝玉); Query OK, 1 row affected (0.00 sec) ​ mysql insert into tt1(telephone, name) values(12345678902, 林黛玉); Query OK, 1 row affected (0.00 sec) ​ mysql insert into tt1(telephone, name) values(12345678903, 王熙凤); Query OK, 1 row affected (0.01 sec) ​ mysql select * from tt1; ---------------------------- | id | telephone   | name     | ---------------------------- | 1 | 12345678901 | 贾宝玉   | | 2 | 12345678902 | 林黛玉   | | 3 | 12345678903 | 王熙凤   | ---------------------------- 3 rows in set (0.01 sec) ​ 这里插入数据成功了那么下面插入telephone 重复数据 mysql insert into tt1(telephone, name) values(12345678903, 贾母); ERROR 1062 (23000): Duplicate entry 12345678903 for key telephone 这里插入重复 telephone 然后失败这里的唯一键不能重复那么能不能为空 mysql insert into tt1(telephone, name) values(NULL, 贾母); Query OK, 1 row affected (0.00 sec) ​ mysql select * from tt1; ---------------------------- | id | telephone   | name     | ---------------------------- | 1 | 12345678901 | 贾宝玉   | | 2 | 12345678902 | 林黛玉   | | 3 | 12345678903 | 王熙凤   | | 5 | NULL         | 贾母     | ---------------------------- 4 rows in set (0.00 sec) ​ 这里插入 null 成功所以唯一键可以为空但是主键不能为空那么为telephone添加not null 和主键还有没有区别 mysql alter table tt1 modify telephone char(11) not null unique; ERROR 1138 (22004): Invalid use of NULL value 上面添加not null 失败为什么 因为我们插入了贾母 telephone 为空所以失败了我们将该数据跟新 mysql update tt1 set telephone12345678904 where id5; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 跟新成功后下面重新添加 not null: mysql alter table tt1 modify telephone char(11) not null unique; Query OK, 0 rows affected, 1 warning (0.04 sec) Records: 0 Duplicates: 0 Warnings: 1 ​ mysql desc tt1; ------------------------------------------------------------ | Field     | Type       | Null | Key | Default | Extra         | ------------------------------------------------------------ | id       | int(11)     | NO   | PRI | NULL   | auto_increment | | telephone | char(11)   | NO   | UNI | NULL   |               | | name     | varchar(12) | YES |     | NULL   |               | ------------------------------------------------------------ 3 rows in set (0.01 sec) 这里修改成功了然后我们在测试一下 mysql insert into tt1(telephone, name) values(12345678901, 薛宝钗); ERROR 1062 (23000): Duplicate entry 12345678901 for key telephone 上面插入重复数据失败下面插入NULL mysql insert into tt1(telephone, name) values(NULL, 薛宝钗); ERROR 1048 (23000): Column telephone cannot be null 这里插入 NULL 也失败了现在到功能上讲和主键是没有区别的但是这两个的意义却是不同的其中主键是为了保证每一行记录的唯一性而唯一键为了让逻辑更通顺保证其他的值的唯一但是其实主键还有索引而唯一键没有。 FOREIGN KEY 外键约束外键就是其他表的主键一般从表里面的外键就来自于主表里面的主键下面还是直接看一下外键。 下面直接创建两张表其中学生表中存储的是学生 id name 和 class_id 班级号class 表里面存的是班级 id 和班级名学生表通过班级 id 和 班级表里面的班级 id 关联起来。 mysql create table student(- student_id char(5) primary key,- name varchar(12) not null,- class_id char(5)); Query OK, 0 rows affected (0.01 sec) ​ mysql create table class(- class_id char(5) primary key,- class_name varchar(12)); Query OK, 0 rows affected (0.01 sec) ​ mysql desc student; ---------------------------------------------------- | Field     | Type       | Null | Key | Default | Extra | ---------------------------------------------------- | student_id | char(5)     | NO   | PRI | NULL   |       | | name       | varchar(12) | NO   |     | NULL   |       | | class_id   | char(5)     | YES |     | NULL   |       | ---------------------------------------------------- 3 rows in set (0.00 sec) ​ mysql desc class; ---------------------------------------------------- | Field     | Type       | Null | Key | Default | Extra | ---------------------------------------------------- | class_id   | char(5)     | NO   | PRI | NULL   |       | | class_name | varchar(12) | YES |     | NULL   |       | ---------------------------------------------------- 2 rows in set (0.00 sec) ​ 上面并没有通过外键关联起来。 下面插入数据 mysql insert into class values(22001, 物联网01班); Query OK, 1 row affected (0.00 sec) ​ mysql insert into class values(22002, 物联网02班); Query OK, 1 row affected (0.00 sec) ​ mysql insert into class values(22003, 计算机01班); Query OK, 1 row affected (0.01 sec) ​ mysql insert into student values(00001, 豹子头, 22001); Query OK, 1 row affected (0.00 sec) ​ mysql insert into student values(00002, 黑旋风, 22001); Query OK, 1 row affected (0.01 sec) ​ mysql insert into student values(00003, 及时雨, 22002); Query OK, 1 row affected (0.00 sec) ​ mysql select * from class; -------------------------- | class_id | class_name     | -------------------------- | 22001   | 物联网01班     | | 22002   | 物联网02班     | | 22003   | 计算机01班     | -------------------------- 3 rows in set (0.00 sec) ​ mysql select * from student; --------------------------------- | student_id | name     | class_id | --------------------------------- | 00001     | 豹子头   | 22001   | | 00002     | 黑旋风   | 22001   | | 00003     | 及时雨   | 22002   | --------------------------------- 3 rows in set (0.01 sec) 上面就是插入数据那么我们继续插入数据 mysql insert into student values(00004, 智多星, 22006); Query OK, 1 row affected (0.00 sec) ​ mysql select * from student; --------------------------------- | student_id | name     | class_id | --------------------------------- | 00001     | 豹子头   | 22001   | | 00002     | 黑旋风   | 22001   | | 00003     | 及时雨   | 22002   | | 00004     | 智多星   | 22006   | --------------------------------- 4 rows in set (0.00 sec) 这里插入了一个 22006 但是班级表中没有这个班级根据我们现实合理吗显然不合理。 下面我们删除掉一个班级 mysql delete from class where class_id22001; Query OK, 1 row affected (0.00 sec) ​ mysql select * from class; -------------------------- | class_id | class_name     | -------------------------- | 22002   | 物联网02班     | | 22003   | 计算机01班     | -------------------------- 2 rows in set (0.00 sec) 这里删除成功了也是不合理所以我们需要添加外键约束 但是在添加之前我们需要删掉从表student 表然后重新创建 mysql create table student(- Sid char(5) primary key,- name varchar(12) not null,- id char(5),- foreign key(id) references class(class_id)- ); Query OK, 0 rows affected (0.01 sec) ​ mysql desc student; ----------------------------------------------- | Field | Type       | Null | Key | Default | Extra | ----------------------------------------------- | Sid   | char(5)     | NO   | PRI | NULL   |       | | name | varchar(12) | NO   |     | NULL   |       | | id   | char(5)     | YES | MUL | NULL   |       | ----------------------------------------------- 3 rows in set (0.01 sec) ​ 这里创建成功那么现在重新插入 mysql insert into student(Sid, name, id) values(00001, 王熙凤, 22001); Query OK, 1 row affected (0.00 sec) ​ mysql insert into student(Sid, name, id) values(00002, 贾宝玉, 22002); Query OK, 1 row affected (0.00 sec) ​ mysql insert into student(Sid, name, id) values(00003, 贾母, 22003); Query OK, 1 row affected (0.00 sec) ​ mysql select * from student; ------------------------- | Sid   | name     | id   | ------------------------- | 00001 | 王熙凤   | 22001 | | 00002 | 贾宝玉   | 22002 | | 00003 | 贾母     | 22003 | ------------------------- 3 rows in set (0.00 sec) 这里插入成功了那么下面插入班级表里面没有的班级 mysql insert into student(Sid, name, id) values(00004, 赵姨娘, 22004); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (restraint.student, CONSTRAINT fk_class_id FOREIGN KEY (id) REFERENCES class (class_id)) 这里插入失败了下面删除掉任意一个班级 mysql delete from class where class_id22001; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (restraint.student, CONSTRAINT fk_class_id FOREIGN KEY (id) REFERENCES class (class_id)) 上面两条语句都失败了这就是外键约束的作用。 那么下面说一下我对外键约束的理解 表与表之间有关系所以需要联系起来。 但是只有联系没有用还是需要约束所以还有外键约束。 虽然外键约束时一个但是外键约束是由联系和约束组成的。
http://www.zqtcl.cn/news/266360/

相关文章:

  • 网站建设推广重要性河北高端网站建设
  • 网站的seo方案怎么做wordpress自动转内链
  • 番禺手机网站制作推广wordpress远程数据库
  • 企业网站seo外包 s深圳国内设计网站
  • 临海高端营销型网站建设地址免费网站alexa排名查询
  • 做企业网站的轻量级cms建设电子商务网站流程图
  • 淘宝网站设计分析国内在线免费服务器
  • wordpress网站文章加密网站建设 博采网络
  • 哪个网站做美食好一点网络运维个人工作总结
  • 做网红用哪个网站教人做策划的网站
  • 百度免费网站怎样建设wordpress模板目录结构
  • 长沙简单的网站建设公司wordpress+手机应用
  • 用spl做网站wordpress不用缓存
  • 微网站模板标签网站被攻击怎么让百度重新蜘蛛自动抓
  • 自己想做一个网站网页背景怎么设置
  • 国外做项目的网站软件定制外包平台
  • 做网站要用什么软件房地产建设网站
  • 龙岗爱联有学网站建设装饰公司简介
  • pc端网站怎么做自适应哪个公司网站备案快
  • 品牌网站建设黑白I狼J烟台开发区建设业联合网站
  • 做视频网站可以自学吗php html5企业网站源码
  • 阿里云怎么部署网站引流推广平台是什么意思
  • 江山建设工程信息网站营销网讯
  • 网站制作公司 沈阳上海建设主管部门网站
  • 网站建设前期如何做好市场定位分析网络推广主要工作内容
  • 做一个网站的流程是什么金融网站建设方案
  • 汽车维修保养网站模板北京网站建设知名公司排名
  • 网站建设案例分享网络推广网
  • 广州知名网站推广app软件开发制作公司电话
  • 泉州专业网站建设seo是指什么职位