主要内容:

  一、完整性约束

  二、表关系

1️⃣ 完整性约束

  (1)何为完整性约束?  

  约束条件与数据类型的宽度一样,都是可选参数。

  作用:用于保证数据的完整性和一致性

  (2)分类主要有以下五类:

    1、not null 与 default

    2、unique

    3、primary key

    4、auto_increment

    5、foreign key

  1. PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录
  2. FOREIGN KEY (FK) 标识该字段为该表的外键
  3. NOT NULL 标识该字段不能为空
  4. UNIQUE KEY (UK) 标识该字段的值是唯一的
  5. AUTO_INCREMENT 标识该字段的值自动增长(整数类型,而且为主键)
  6. DEFAULT 为该字段设置默认值
  7.  
  8. UNSIGNED 无符号
  9. ZEROFILL 使用0填充

说明:

  1. 1. 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值
  2. 2. 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值
  3. sex enum('male','female') not null default 'male'
  4. age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20
  5. 3. 是否是key
  6. 主键 primary key
  7. 外键 foreign key
  8. 索引 (index,unique...)

  (3)not null 与 default 

    是否可空,null表示空,非字符串
    not null - 不可空
    null - 可空

    默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值,如下:
    create table tb1(
    nid int not null defalut 2,
    num int not null
    )

    not null的实例如下:

  1. ==================not null====================
  2. mysql> create table t1(id int); #id字段默认可以插入空
  3. mysql> desc t1;
  4. +-------+---------+------+-----+---------+-------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +-------+---------+------+-----+---------+-------+
  7. | id | int(11) | YES | | NULL | |
  8. +-------+---------+------+-----+---------+-------+
  9. mysql> insert into t1 values(); #可以插入空
  10.  
  11. mysql> create table t2(id int not null); #设置字段id不为空
  12. mysql> desc t2;
  13. +-------+---------+------+-----+---------+-------+
  14. | Field | Type | Null | Key | Default | Extra |
  15. +-------+---------+------+-----+---------+-------+
  16. | id | int(11) | NO | | NULL | |
  17. +-------+---------+------+-----+---------+-------+
  18. mysql> insert into t2 values(); #不能插入空
  19. ERROR 1364 (HY000): Field 'id' doesn't have a default value

    default的实例

  1. ==================default====================
  2. #设置id字段有默认值后,则无论id字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值
  3. mysql> create table t3(id int default 1);
  4. mysql> alter table t3 modify id int not null default 1;
  5. mysql> create table t8(
  6. -> id int,
  7. -> name char(6),
  8. -> sex enum('male','female') not null default 'male'
  9. -> );
  10. Query OK, 0 rows affected (0.41 sec)
  11.  
  12. mysql> insert into t8(id,name) values(1,'cc');
  13. Query OK, 1 row affected (0.08 sec)
  14.  
  15. mysql> desc t8;
  16. +-------+-----------------------+------+-----+---------+-------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +-------+-----------------------+------+-----+---------+-------+
  19. | id | int(11) | YES | | NULL | |
  20. | name | char(6) | YES | | NULL | |
  21. | sex | enum('male','female') | NO | | male | |
  22. +-------+-----------------------+------+-----+---------+-------+
  23. 3 rows in set (0.01 sec)
  24.  
  25. mysql> select * from t8;
  26. +------+------+------+
  27. | id | name | sex |
  28. +------+------+------+
  29. | 1 | cc | male |
  30. +------+------+------+
  31. 1 row in set (0.00 sec)
  32. mysql> insert into department values
  33. -> (1,'student'),
  34. -> (2,'school');
  35. Query OK, 2 rows affected (0.06 sec)
  36. Records: 2 Duplicates: 0 Warnings: 0

  

  (4) unique 

    单列唯一

      方法一:

  1. mysql> create table department(
  2. -> id int unique,
  3. -> name char(10) unique
  4. -> );
  5. Query OK, 0 rows affected (0.59 sec)

      方法二:

  1. mysql> create table department(
  2. -> id int,
  3. -> name char(10),
  4. -> unique(id),
  5. -> unique(name)
  6. -> );
  7. Query OK, 0 rows affected (0.49 sec)

    联合唯一(一组里的某个参数不同即可)

  1. mysql> create table services(
  2. -> id int unique,
  3. -> ip char(14),
  4. -> port int,
  5. -> unique(ip,port)
  6. -> );
  7. Query OK, 0 rows affected (0.48 sec)
  8.  
  9. mysql> desc services;
  10. +-------+----------+------+-----+---------+-------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +-------+----------+------+-----+---------+-------+
  13. | id | int(11) | YES | UNI | NULL | |
  14. | ip | char(14) | YES | MUL | NULL | |
  15. | port | int(11) | YES | | NULL | |
  16. +-------+----------+------+-----+---------+-------+
  17. 3 rows in set (0.06 sec)
  18.  
  19. mysql> insert into services values
  20. -> (1,'192.168.0.1',80),
  21. -> (2,'192.168.0.1',81),
  22. -> (2,'192.168.0.2',81);
  23. ERROR 1062 (23000): Duplicate entry '' for key 'id'
  24. mysql> insert into services values
  25. -> (1,'192.168.0.1',80),
  26. -> (2,'192.168.0.1',81),
  27. -> (3,'192.168.0.2',81);
  28. Query OK, 3 rows affected (0.10 sec)
  29. Records: 3 Duplicates: 0 Warnings: 0
  30.  
  31. mysql> select * from services;
  32. +------+-------------+------+
  33. | id | ip | port |
  34. +------+-------------+------+
  35. | 1 | 192.168.0.1 | 80 |
  36. | 2 | 192.168.0.1 | 81 |
  37. | 3 | 192.168.0.2 | 81 |
  38. +------+-------------+------+
  39. 3 rows in set (0.00 sec)

  (5)primary key --> not  null  unique   

  1.   primary key字段的值不为空且唯一
      约束:not null nique
      存储引擎(innodb):对于innodb存储引擎来说,一张表必须有一个主键
      一个表中可以:
      单列做主键
      多列做主键(复合主键)
      但一个表内只能有一个主键primary key
  2.  
  3.   单列主键:
      
  1. mysql> create table t9(
  2. -> id int primary key,
  3. -> name char(13)
  4. -> );
  5. Query OK, 0 rows affected (0.44 sec)
  6.  
  7. mysql> desc t9;
  8. +-------+----------+------+-----+---------+-------+
  9. | Field | Type | Null | Key | Default | Extra |
  10. +-------+----------+------+-----+---------+-------+
  11. | id | int(11) | NO | PRI | NULL | |
  12. | name | char(13) | YES | | NULL | |
  13. +-------+----------+------+-----+---------+-------+
  14. 2 rows in set (0.01 sec)
  15.  
  16. mysql> insert into t9 values
  17. -> (1,'cc'),
  18. -> (2,'cc2');
  19. Query OK, 2 rows affected (0.07 sec)
  20. Records: 2 Duplicates: 0 Warnings: 0
  21.  
  22. mysql> insert into t9 values
  23. -> (2,'cc3');
  24. ERROR 1062 (23000): Duplicate entry '' for key 'PRIMARY'

  多列主键

  1. mysql> create table t10(
  2. -> ip char(13),
  3. -> port int,
  4. -> primary key(ip,port)
  5. -> );
  6. Query OK, 0 rows affected (0.41 sec)
  7.  
  8. mysql> desc t9;
  9. +-------+----------+------+-----+---------+-------+
  10. | Field | Type | Null | Key | Default | Extra |
  11. +-------+----------+------+-----+---------+-------+
  12. | id | int(11) | NO | PRI | NULL | |
  13. | name | char(13) | YES | | NULL | |
  14. +-------+----------+------+-----+---------+-------+
  15. 2 rows in set (0.00 sec)
  16.  
  17. mysql> desc t10;
  18. +-------+----------+------+-----+---------+-------+
  19. | Field | Type | Null | Key | Default | Extra |
  20. +-------+----------+------+-----+---------+-------+
  21. | ip | char(13) | NO | PRI | | |
  22. | port | int(11) | NO | PRI | 0 | |
  23. +-------+----------+------+-----+---------+-------+
  24. 2 rows in set (0.01 sec)
  25.  
  26. mysql> insert into t10 values(
  27. -> '1.1.1.1',88),
  28. -> ('1.1.1.1',88);
  29. ERROR 1062 (23000): Duplicate entry '1.1.1.1-88' for key 'PRIMARY'
  30. mysql> insert into t10 values(
  31. -> '1.1.1.1',88),
  32. -> ('1.1.1.1',89);
  33. Query OK, 2 rows affected (0.06 sec)
  34. Records: 2 Duplicates: 0 Warnings: 0
  35.  
  36. mysql> select * from t10;
  37. +---------+------+
  38. | ip | port |
  39. +---------+------+
  40. | 1.1.1.1 | 88 |
  41. | 1.1.1.1 | 89 |
  42. +---------+------+
  43. 2 rows in set (0.00 sec)

  

  (6) auto_increment

  约束字段为自动增长,被约束的字段必须被key约束。

  1. mysql> create table t11(
  2. -> id int primary key auto_increment,
  3. -> name char(12)
  4. -> );
  5. Query OK, 0 rows affected (0.56 sec)
  6.  
  7. mysql> insert into t11(name) values
  8. -> ('cc1'),
  9. -> ('cc2'),
  10. -> ('cc3'),
  11. -> ('cc4');
  12. Query OK, 4 rows affected (0.08 sec)
  13. Records: 4 Duplicates: 0 Warnings: 0
  14.  
  15. mysql> select * from t11;
  16. +----+------+
  17. | id | name |
  18. +----+------+
  19. | 1 | cc1 |
  20. | 2 | cc2 |
  21. | 3 | cc3 |
  22. | 4 | cc4 |
  23. +----+------+
  24. 4 rows in set (0.00 sec)
  25.  
  26. mysql> insert into t11(name) values
  27. -> ('qq1'),
  28. -> ('qq2'),
  29. -> ('qq3');
  30. Query OK, 3 rows affected (0.09 sec)
  31. Records: 3 Duplicates: 0 Warnings: 0
  32.  
  33. mysql> select * from t11;
  34. +----+------+
  35. | id | name |
  36. +----+------+
  37. | 1 | cc1 |
  38. | 2 | cc2 |
  39. | 3 | cc3 |
  40. | 4 | cc4 |
  41. | 5 | qq1 |
  42. | 6 | qq2 |
  43. | 7 | qq3 |
  44. +----+------+
  45. 7 rows in set (0.00 sec)
  46. 了解:
  47. mysql> show variables like 'auto_inc%';
  48. +--------------------------+-------+
  49. | Variable_name | Value |
  50. +--------------------------+-------+
  51. | auto_increment_increment | 1 |
  52. | auto_increment_offset | 1 |
  53. +--------------------------+-------+
  54. 2 rows in set (0.00 sec)
  55. #基于会话级别
  56. set session auth_increment_increment=2 #修改会话级别的步长
  57. #基于全局级别的
  58. set global auth_increment_increment=2 #修改全局级别的步长(所有会话都生效)
  59.  
  60. # 步长:
  61. auto_increment_increment默认为1
  62. 设置步长
  63. mysql> set session auto_increment_increment = 5; # 零时
  64. Query OK, 0 rows affected (0.00 sec)
  65.  
  66. mysql> set global auto_increment_increment=5; # 全局
  67. Query OK, 0 rows affected (0.00 sec)
  68. mysql> show variables like "auto_inc%";
  69. +--------------------------+-------+
  70. | Variable_name | Value |
  71. +--------------------------+-------+
  72. | auto_increment_increment | 5 |
  73. | auto_increment_offset | 1 |
  74. +--------------------------+-------+
  75. 2 rows in set (0.00 sec)
  76.  
  77. #起始偏移量(起始偏移量 <= 步长)
  78. auto_increment_offset 默认1
  79. set global auto_increment_office
  80.  
  81. mysql> create table t12(
  82. -> id int primary key auto_increment,
  83. -> name char(18)
  84. -> );
  85. Query OK, 0 rows affected (0.50 sec)
  86. mysql> insert into t12(name) values
  87. -> ('cc'),
  88. -> ('sc');
  89. Query OK, 2 rows affected (0.07 sec)
  90. Records: 2 Duplicates: 0 Warnings: 0
  91.  
  92. mysql> select * from t12;
  93. +----+------+
  94. | id | name |
  95. +----+------+
  96. | 1 | cc |
  97. | 6 | sc |
  98. +----+------+
  99. 2 rows in set (0.00 sec)
  100. 使用极少,我们需要改回来
  101. mysql> set global auto_increment_increment=1;
  102. Query OK, 0 rows affected (0.00 sec)
  103.  
  104. mysql> set global auto_increment_offset=1;
  105. Query OK, 0 rows affected (0.00 sec)
  106.  
  107. 清空表数据 truncate
  108. mysql> select * from t12;
  109. +----+------+
  110. | id | name |
  111. +----+------+
  112. | 1 | cc |
  113. | 6 | sc |
  114. +----+------+
  115. 2 rows in set (0.00 sec)
  116.  
  117. mysql> truncate t12;
  118. Query OK, 0 rows affected (0.31 sec)
  119.  
  120. mysql> select * from t12;

  (7)foreign key(外键),建立表之间的关系

    如何理解foreign key?

    假如员工信息表有三个字段:工号 姓名 部门

    公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费。该如何解决这个问题呢?

    解决办法:     

      我们可以先定义一个部门表,然后让员工信息表关联该表,如何关联,即使用 foreign key

    注意:

      表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一。

    第一步,先建立被关联的表,并且保证被关联的子段唯一,此处的被关联的表是部门表(dep)如下:   

  1. mysql> create table dep( # 父表,即被关联的表
  2. -> id int primary key,
  3. -> name char(20),
  4. -> comment char(16)
  5. -> );
  6. Query OK, 0 rows affected (0.42 sec)
  7. mysql> desc dep;
  8. +---------+----------+------+-----+---------+-------+
  9. | Field | Type | Null | Key | Default | Extra |
  10. +---------+----------+------+-----+---------+-------+
  11. | id | int(11) | NO | PRI | NULL | |
  12. | name | char(20) | YES | | NULL | |
  13. | comment | char(16) | YES | | NULL | |
  14. +---------+----------+------+-----+---------+-------+
  15. 3 rows in set (0.01 sec)

    第二步,建立关联的表(此处指员工表),如下:  

  1. mysql> create table emp( # 子表
  2. -> id int primary key,
  3. -> name char(13),
  4. -> sex enum('male','female'),
  5. -> dep_id int,
  6. -> foreign key(dep_id) references dep(id)
  7. -> );
  8. Query OK, 0 rows affected (0.54 sec)
  9. mysql> desc emp;
  10. +--------+-----------------------+------+-----+---------+-------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +--------+-----------------------+------+-----+---------+-------+
  13. | id | int(11) | NO | PRI | NULL | |
  14. | name | char(13) | YES | | NULL | |
  15. | sex | enum('male','female') | YES | | NULL | |
  16. | dep_id | int(11) | YES | MUL | NULL | |
  17. +--------+-----------------------+------+-----+---------+-------+
  18. 4 rows in set (0.01 sec)

    第三步,先向被关联表中插入数据,再向关联表中插入数据(即先向部门表中插入数据,再向员工表插入数据)

  1. mysql> insert into dep values
  2. -> (1,'销售','部门1'),
  3. -> (2,'财务','部门2'),
  4. -> (3,'售后','部门3');
  5. Query OK, 3 rows affected (0.07 sec)
  6. Records: 3 Duplicates: 0 Warnings: 0
  7.  
  8. mysql> insert into emp values
  9. -> (1,'cc','male',1),
  10. -> (2,'sc','male',2),
  11. -> (3,'ssc','female',3),
  12. -> (4,'sscc','female',3);
  13. Query OK, 4 rows affected (0.06 sec)
  14. Records: 4 Duplicates: 0 Warnings: 0
  15.  
  16. mysql> select * from emp
  17. -> ;
  18. +----+------+--------+--------+
  19. | id | name | sex | dep_id |
  20. +----+------+--------+--------+
  21. | 1 | cc | male | 1 |
  22. | 2 | sc | male | 2 |
  23. | 3 | ssc | female | 3 |
  24. | 4 | sscc | female | 3 |
  25. +----+------+--------+--------+
  26. 4 rows in set (0.00 sec)
  27. mysql> select * from dep;
  28. +----+--------+---------+
  29. | id | name | comment |
  30. +----+--------+---------+
  31. | 1 | 销售 | 部门1 |
  32. | 2 | 财务 | 部门2 |
  33. | 3 | 售后 | 部门3 |
  34. +----+--------+---------+
  35. 3 rows in set (0.00 sec)

    第四步(删除数据才需要),先删除关联表中的数据,再删除被关联表中的数据

  1. mysql> delete from emp where dep_id=3;
  2. Query OK, 2 rows affected (0.06 sec)
  3. mysql> select * from emp;
  4. +----+------+------+--------+
  5. | id | name | sex | dep_id |
  6. +----+------+------+--------+
  7. | 1 | cc | male | 1 |
  8. | 2 | sc | male | 2 |
  9. +----+------+------+--------+
  10. 2 rows in set (0.00 sec)
  11.  
  12. mysql> delete from dep where id=3;
  13. Query OK, 1 row affected (0.07 sec)
  14. mysql> select * from dep;
  15. +----+--------+---------+
  16. | id | name | comment |
  17. +----+--------+---------+
  18. | 1 | 销售 | 部门1 |
  19. | 2 | 财务 | 部门2 |
  20. +----+--------+---------+
  21. 2 rows in set (0.00 sec)

  注意:当表中包含 FOREIGN KEY约束时,插入数据要先被关联表,后关联表;而要删除数据时,

  顺序正好相反,先删除关联表中的数据,后删除被关联表中的数据。切记切记!!!

综合案例: 

  1. mysql> create table dep2( # 被关联表
  2. -> id int primary key,
  3. -> name varchar(15) not null
  4. -> )engine=innodb;
  5. Query OK, 0 rows affected (0.36 sec)
  6.  
  7. mysql> create table emp2( # 关联表
  8. -> id int primary key,
  9. -> name varchar(13) not null,
  10. -> dep2_id int ,
  11. -> foreign key(dep2_id) references dep2(id)
  12. -> on delete cascade # 添加之后,可单独删除关联表数据
  13. -> on update cascade # 添加之后,可单独更新关联表数据
  14. -> )engine=innodb;
  15. Query OK, 0 rows affected (0.36 sec)
  16.  
  17. mysql> insert into dep2 values
  18. -> (1,'部门1'),
  19. -> (2,'部门2'),
  20. -> (3,'部门3');
  21. Query OK, 3 rows affected (0.08 sec)
  22. Records: 3 Duplicates: 0 Warnings: 0
  23.  
  24. mysql> insert into emp2 values
  25. -> (1,'cc1',1),
  26. -> (2,'cc2',2),
  27. -> (3,'cc2',2),
  28. -> (4,'cc2',3);
  29. Query OK, 4 rows affected (0.03 sec)
  30. Records: 4 Duplicates: 0 Warnings: 0
  31.  
  32. mysql> delete from emp2 where id=4;
  33. Query OK, 1 row affected (0.04 sec)
  34.  
  35. mysql> delete from emp2 where id=3;
  36. Query OK, 1 row affected (0.07 sec)
  37.  
  38. mysql> select * from emp2;
  39. +----+------+---------+
  40. | id | name | dep2_id |
  41. +----+------+---------+
  42. | 1 | cc1 | 1 |
  43. | 2 | cc2 | 2 |
  44. +----+------+---------+
  45. 2 rows in set (0.00 sec)
  46.  
  47. mysql> delete from dep2 where id=3;
  48. Query OK, 1 row affected (0.07 sec)
  49.  
  50. mysql> select * from dep2;
  51. +----+---------+
  52. | id | name |
  53. +----+---------+
  54. | 1 | 部门1 |
  55. | 2 | 部门2 |
  56. +----+---------+
  57. 2 rows in set (0.00 sec)
  58. mysql> delete from dep2 where id=2;
  59. Query OK, 1 row affected (0.15 sec)
  60.  
  61. mysql> select * from emp2;
  62. +----+------+---------+
  63. | id | name | dep2_id |
  64. +----+------+---------+
  65. | 1 | cc1 | 1 |
  66. +----+------+---------+
  67. 1 row in set (0.00 sec)
  68.  
  69. mysql> update emp2 set id=666 where id=1;
  70. Query OK, 1 row affected (0.09 sec)
  71. Rows matched: 1 Changed: 1 Warnings: 0
  72.  
  73. mysql> select * from emp2;
  74. +-----+------+---------+
  75. | id | name | dep2_id |
  76. +-----+------+---------+
  77. | 666 | cc1 | 1 |
  78. +-----+------+---------+
  79. 1 row in set (0.00 sec)

  清空表:

  1. delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。
  2.  
  3. truncate table t1; #数据量大,删除速度比上一条快,且直接从零开始,

2️⃣ 表关系

  1、如何分析两张表间的关系?

  实例如下,分析下面两张表:

  1. emp +----+------+--------+--------+
  2. | id | name | sex | dep_id |
  3. +----+------+--------+--------+
  4. | 1 | cc | male | 1 |p
  5. | 2 | sc | male | 2 |
  6. | 3 | ssc | female | 3 |
  7. | 4 | sscc | female | 3 |
  8. +----+------+--------+--------+
  1. dep
  2. +----+--------+---------+
  3. | id | name | comment |
  4. +----+--------+---------+
  5. | 1 | 销售 | 部门1 |
  6. | 2 | 财务 | 部门2 |
  7. | 3 | 售后 | 部门3 |
  8. +----+--------+---------+

分析流程如下:

  (1)先站在左表的角度去找

  1. 是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id

  (2)再站在右表的角度去找

  1. 是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id

  (3)总结:

  1. #多对一:
  2. 如果只有步骤1成立,则是左表多对一右表
  3. 如果只有步骤2成立,则是右表多对一左表
  4.  
  5. #多对多
  6. 如果步骤12同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系
  7.  
  8. #一对一:
  9. 如果12都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可

  2、建立表关系

    2.1、多对一(一对多)

    两张表:出版社、书

    关联方式:foreign key

  1. create table press(
  2. id int primary key auto_increment,
  3. name varchar(20)
  4. );
  5. create table book(
  6. id int primary key auto_increment,
  7. name varchar(20),
  8. press_id int not null,
  9. foreign key(press_id) references press(id)
  10. on delete cascade
  11. on update cascade
  12. );
  13. insert into press(name) values
  14. ('北京出版社'),
  15. ('知识产权无用出版社') ;
  16. insert into book(name,press_id) values
  17. ('九阳神功',1),
  18. ('九阴真经',1),
  19. ('葵花宝典',2);
  20. ('独孤九剑',2);

   

     2.2、多对多

  1.     三张表:出版社,作者信息,书
  1.     多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多   
  1.     如果步骤12同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来
      专门存放二者的关系。
  1.     关联方式:foreign key+一张新的表(出版社信息同上,不再追加)
  1. create table author(
  2. id int primary key auto_increment,
  3. name varchar(20)
  4. );
  5. 书籍表和插入的书与之前共用
  6. create table author2book(
  7. id int not null unique auto_increment,
  8. author_id int not null,
  9. book_id int not null,
  10. (constraint fk_author) foreign key(author_id) references author(id)
  11. on delete cascade,
  12. on update cascade,
  13. (constraint fk_book) foreign key(book_id) references book(id)
  14. on delete cascade
  15. on update cascade,
  16. primary key(author_id,book_id)
  17. );
  18. insert into autor(name) values('cc1'),('cc2'),('cc3');
  19. #每个作者与自己的代表作如下
  20. cc1:
  21. 九阳神功
  22. 九阴真经
  23. cc2:
  24. 九阳神功
  25. 葵花宝典
  26. cc3:
  27. 独孤九剑
  28. insert into author2book(author_id,book_id) values
  29. (1,1),
  30. (1,1),
  31. (2,1),
  32. (2,2),
  33. (3,2);

   

   2.3、一对一(一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系)

  1. 如果12都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,
  2. 就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可
  3. #一定是student来foreign key表customer,这样就保证了:
  4. #1 学生一定是一个客户,
  5. #2 客户不一定是学生,但有可能成为一个学生
  1. create table customer(
  2. id int primary key auto_increment,
  3. name varchar(20) not null,
  4. phone char(16) not null
  5. );
  6. create table student(
  7. id int primary key auto_increment,
  8. class_name varchar(20) not null,
  9. customer_id int unique, #该字段一定要是唯一的
  10. foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
  11. on delete cascade
  12. on update cascade
  13. );
  14. # 增加客户
  15. insert into customer(name,phone)
  16. ('cc',12312312),
  17. ('cc2',213231231),
  18. ('cc3',66666666);
  19. # 增加学生
  20. insert into student(class_name,customer_id) values
  21. ('class1',1),
  22. ('class2',2),
  23. ('class3',3);
  1.  
  1.  

MySQL数据库篇之完整性约束和表关系的更多相关文章

  1. Python Django orm操作数据库笔记之外键和表关系

    外键 在MySQL中,表有两种引擎,一种是InnoDB,另外一种是myisam.如果使用的是InnoDB引擎,是支持外键约束的. 外键的使用 使用外键前需要先确保相应外键已存储在数据库中(flask中 ...

  2. Sqlserver 连接oracle和mysql数据库 已经oracle导入sqlserver表数据

    SQL Server2012创建连接服务器到ORACLE11G 8,百思考不知道原因啊??突然我发现如下:链接服务器—〉访问接口—〉OraOLEDB.Oracle—〉允许进程内没有勾上,但是我想上面的 ...

  3. 通过JSP网页连接MySQL数据库,从MySQL数据库中读出一张表并显示在JSP网页中

    1.安装所需软件 ①安装java和tomcat,建立JSP网页最基础的软件②安装MySQL数据库(下载地址:https://www.mysql.com/)③安装Navicat Premium来查看数据 ...

  4. linux mysql 数据库操作导入导出 数据表导出导入

    linux mysql 数据库操作导入导出 数据表导出导入 1,数据库导入 mysql -uroot -p show databases; create database newdb; use 数据库 ...

  5. MySQL数据库的全局锁和表锁

    1.概念 数据库锁设计的初衷是处理并发问题.作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则.而锁就是用来实现这些访问规则的重要数据结构. 2.锁的分类 根据加锁的范围, ...

  6. MySQL数据库基础(2)表结构管理

    目录 一.关系模型与数据表 二.MySQL数据类型 三.数据完整性约束 四.参照完整性约束 一.关系模型与数据表 概念 ①关系模型:是由若干个关系模式组成的集合,关系模式的实例称为关系,每个关系实际上 ...

  7. MySQL数据库篇之库的增删改查

    主要内容: 一.系统数据库介绍 二.创建数据库 三.数据库增删改查 四.MySQL添加注释 1️⃣ 系统数据库介绍 1.初识sql语句 有了mysql这个数据库软件,就可以将程序员从对数据的管理中解脱 ...

  8. Django中的app及mysql数据库篇(ORM操作)

    Django常见命令 在Django的使用过程中需要使用命令让Django进行一些操作,例如创建Django项目.启动Django程序.创建新的APP.数据库迁移等. 创建Django项目 一把我们都 ...

  9. Python学习日记(四十二) Mysql数据库篇 十

    前言 当我们自己去写SQL代码的时候有时候会因为不熟练会导致效率低,再之后要进行许多的优化,并且操作也较为繁琐.因此ORM框架就能够解决上面的问题,它能根据自身的一些规则来帮助开发者去生成SQL代码. ...

随机推荐

  1. eclipse显示/隐藏代码行号

    Window→Preferences→General→Editors→TextEditors→勾选Show line numbers

  2. 零基础学习hadoop到上手工作线路指导初级篇:hive及mapreduce

      此篇是在零基础学习hadoop到上手工作线路指导(初级篇)的基础,一个继续总结.五一假期:在写点内容,也算是总结.上面我们会了基本的编程,我们需要对hadoop有一个更深的理解:hadoop分为h ...

  3. ballerina 学习十六 错误&&异常处理

    ballerina 的error 处理和elxiir 以及rust 比较类似使用模式匹配,但是他的 error lifting 还是比较方便的 同时check 也挺好,异常处理没什么特殊的 throw ...

  4. k8s helm 私服chartmuseum minio s3 存储配置

    1. 安装minio 使用docker 安装 参考项目 https://github.com/rongfengliang/mino-thumbor-openresty 备注: 因为是一个集成项目可能会 ...

  5. 关于SQL的几道小题详解

    关于SQL的几道小题详解 当我们拿到题目的时候,并不是急于作答,那样会得不偿失的,而是分析思路,采用什么方法,达到什么目的,还要思考有没有简单的方法或者通用的方法等等,这样才会达到以一当十的效果,这样 ...

  6. 读取设置config.ini配置

    class CSenseIni { /************************************************************************/ /*写操作 * ...

  7. JS 实现关闭浏览器

        $('#exitSystem').on('click',function(){ if(confirm("确定要退出系统并关闭浏览器吗?")){ //关闭浏览器的方法只适用i ...

  8. [模板]LCA的倍增求法解析

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  9. FU-A 分包

    FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法 [原创] RFC3984是H.264的baseline码流在RTP方式下传输的规范,这里只讨论FU-A分包方式,以及从RTP包 ...

  10. 北京师范大学第十六届程序设计竞赛决赛 I 如何办好比赛

    链接:https://www.nowcoder.com/acm/contest/117/I来源:牛客网 如何办好比赛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...