阅读目录

  • 一 介绍
  • 二 not null与default
  • 三 unique
  • 四 primary key
  • 五 auto_increment
  • 六 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...)
二 not null与default

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

默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
create table tb1(
nid int not null defalut 2,
num int 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
  20.  
  21. ==================default====================
  22. #设置id字段有默认值后,则无论id字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值
  23. mysql> create table t3(id int default 1);
  24. mysql> alter table t3 modify id int not null default 1;
  25.  
  26. ==================综合练习====================
  27. mysql> create table student(
  28. -> name varchar(20) not null,
  29. -> age int(3) unsigned not null default 18,
  30. -> sex enum('male','female') default 'male',
  31. -> hobby set('play','study','read','music') default 'play,music'
  32. -> );
  33. mysql> desc student;
  34. +-------+------------------------------------+------+-----+------------+-------+
  35. | Field | Type | Null | Key | Default | Extra |
  36. +-------+------------------------------------+------+-----+------------+-------+
  37. | name | varchar(20) | NO | | NULL | |
  38. | age | int(3) unsigned | NO | | 18 | |
  39. | sex | enum('male','female') | YES | | male | |
  40. | hobby | set('play','study','read','music') | YES | | play,music | |
  41. +-------+------------------------------------+------+-----+------------+-------+
  42. mysql> insert into student(name) values('egon');
  43. mysql> select * from student;
  44. +------+-----+------+------------+
  45. | name | age | sex | hobby |
  46. +------+-----+------+------------+
  47. | egon | 18 | male | play,music |
  48. +------+-----+------+------------+

验证

三 unique

  1. ============设置唯一约束 UNIQUE===============
  2. 方法一:
  3. create table department1(
  4. id int,
  5. name varchar(20) unique,
  6. comment varchar(100)
  7. );
  8.  
  9. 方法二:
  10. create table department2(
  11. id int,
  12. name varchar(20),
  13. comment varchar(100),
  14. constraint uk_name unique(name)
  15. );
  16.  
  17. mysql> insert into department1 values(1,'IT','技术');
  18. Query OK, 1 row affected (0.00 sec)
  19. mysql> insert into department1 values(1,'IT','技术');
  20. ERROR 1062 (23000): Duplicate entry 'IT' for key 'name'
  1. mysql> create table t1(id int not null unique);
  2. Query OK, 0 rows affected (0.02 sec)
  3.  
  4. mysql> desc t1;
  5. +-------+---------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+---------+------+-----+---------+-------+
  8. | id | int(11) | NO | PRI | NULL | |
  9. +-------+---------+------+-----+---------+-------+
  10. row in set (0.00 sec)

not null+unique的化学反应

  1. create table service(
  2. id int primary key auto_increment,
  3. name varchar(20),
  4. host varchar(15) not null,
  5. port int not null,
  6. unique(host,port) #联合唯一
  7. );
  8.  
  9. mysql> insert into service values
  10. -> (1,'nginx','192.168.0.10',80),
  11. -> (2,'haproxy','192.168.0.20',80),
  12. -> (3,'mysql','192.168.0.30',3306)
  13. -> ;
  14. Query OK, 3 rows affected (0.01 sec)
  15. Records: 3 Duplicates: 0 Warnings: 0
  16.  
  17. mysql> insert into service(name,host,port) values('nginx','192.168.0.10',80);
  18. ERROR 1062 (23000): Duplicate entry '192.168.0.10-80' for key 'host'

联合唯一

四 primary key

primary key字段的值不为空且唯一

一个表中可以:

单列做主键
多列做主键(复合主键)

但一个表内只能有一个主键primary key

  1. ============单列做主键===============
  2. #方法一:not null+unique
  3. create table department1(
  4. id int not null unique, #主键
  5. name varchar(20) not null unique,
  6. comment varchar(100)
  7. );
  8.  
  9. mysql> desc department1;
  10. +---------+--------------+------+-----+---------+-------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +---------+--------------+------+-----+---------+-------+
  13. | id | int(11) | NO | PRI | NULL | |
  14. | name | varchar(20) | NO | UNI | NULL | |
  15. | comment | varchar(100) | YES | | NULL | |
  16. +---------+--------------+------+-----+---------+-------+
  17. rows in set (0.01 sec)
  18.  
  19. #方法二:在某一个字段后用primary key
  20. create table department2(
  21. id int primary key, #主键
  22. name varchar(20),
  23. comment varchar(100)
  24. );
  25.  
  26. mysql> desc department2;
  27. +---------+--------------+------+-----+---------+-------+
  28. | Field | Type | Null | Key | Default | Extra |
  29. +---------+--------------+------+-----+---------+-------+
  30. | id | int(11) | NO | PRI | NULL | |
  31. | name | varchar(20) | YES | | NULL | |
  32. | comment | varchar(100) | YES | | NULL | |
  33. +---------+--------------+------+-----+---------+-------+
  34. rows in set (0.00 sec)
  35.  
  36. #方法三:在所有字段后单独定义primary key
  37. create table department3(
  38. id int,
  39. name varchar(20),
  40. comment varchar(100),
  41. constraint pk_name primary key(id); #创建主键并为其命名pk_name
  42.  
  43. mysql> desc department3;
  44. +---------+--------------+------+-----+---------+-------+
  45. | Field | Type | Null | Key | Default | Extra |
  46. +---------+--------------+------+-----+---------+-------+
  47. | id | int(11) | NO | PRI | NULL | |
  48. | name | varchar(20) | YES | | NULL | |
  49. | comment | varchar(100) | YES | | NULL | |
  50. +---------+--------------+------+-----+---------+-------+
  51. rows in set (0.01 sec)

单列主键

  1. ==================多列做主键================
  2. create table service(
  3. ip varchar(15),
  4. port char(5),
  5. service_name varchar(10) not null,
  6. primary key(ip,port)
  7. );
  8.  
  9. mysql> desc service;
  10. +--------------+-------------+------+-----+---------+-------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +--------------+-------------+------+-----+---------+-------+
  13. | ip | varchar(15) | NO | PRI | NULL | |
  14. | port | char(5) | NO | PRI | NULL | |
  15. | service_name | varchar(10) | NO | | NULL | |
  16. +--------------+-------------+------+-----+---------+-------+
  17. rows in set (0.00 sec)
  18.  
  19. mysql> insert into service values
  20. -> ('172.16.45.10','','mysqld'),
  21. -> ('172.16.45.11','','mariadb')
  22. -> ;
  23. Query OK, 2 rows affected (0.00 sec)
  24. Records: 2 Duplicates: 0 Warnings: 0
  25.  
  26. mysql> insert into service values ('172.16.45.10','','nginx');
  27. ERROR 1062 (23000): Duplicate entry '172.16.45.10-3306' for key 'PRIMARY'

多列主键

五 auto_increment

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

  1. #不指定id,则自动增长
  2. create table student(
  3. id int primary key auto_increment,
  4. name varchar(20),
  5. sex enum('male','female') default 'male'
  6. );
  7.  
  8. mysql> desc student;
  9. +-------+-----------------------+------+-----+---------+----------------+
  10. | Field | Type | Null | Key | Default | Extra |
  11. +-------+-----------------------+------+-----+---------+----------------+
  12. | id | int(11) | NO | PRI | NULL | auto_increment |
  13. | name | varchar(20) | YES | | NULL | |
  14. | sex | enum('male','female') | YES | | male | |
  15. +-------+-----------------------+------+-----+---------+----------------+
  16. mysql> insert into student(name) values
  17. -> ('egon'),
  18. -> ('alex')
  19. -> ;
  20.  
  21. mysql> select * from student;
  22. +----+------+------+
  23. | id | name | sex |
  24. +----+------+------+
  25. | 1 | egon | male |
  26. | 2 | alex | male |
  27. +----+------+------+
  28.  
  29. #也可以指定id
  30. mysql> insert into student values(4,'asb','female');
  31. Query OK, 1 row affected (0.00 sec)
  32.  
  33. mysql> insert into student values(7,'wsb','female');
  34. Query OK, 1 row affected (0.00 sec)
  35.  
  36. mysql> select * from student;
  37. +----+------+--------+
  38. | id | name | sex |
  39. +----+------+--------+
  40. | 1 | egon | male |
  41. | 2 | alex | male |
  42. | 4 | asb | female |
  43. | 7 | wsb | female |
  44. +----+------+--------+
  45.  
  46. #对于自增的字段,在用delete删除后,再插入值,该字段仍按照删除前的位置继续增长
  47. mysql> delete from student;
  48. Query OK, 4 rows affected (0.00 sec)
  49.  
  50. mysql> select * from student;
  51. Empty set (0.00 sec)
  52.  
  53. mysql> insert into student(name) values('ysb');
  54. mysql> select * from student;
  55. +----+------+------+
  56. | id | name | sex |
  57. +----+------+------+
  58. | 8 | ysb | male |
  59. +----+------+------+
  60.  
  61. #应该用truncate清空表,比起delete一条一条地删除记录,truncate是直接清空表,在删除大表时用它
  62. mysql> truncate student;
  63. Query OK, 0 rows affected (0.01 sec)
  64.  
  65. mysql> insert into student(name) values('egon');
  66. Query OK, 1 row affected (0.01 sec)
  67.  
  68. mysql> select * from student;
  69. +----+------+------+
  70. | id | name | sex |
  71. +----+------+------+
  72. | 1 | egon | male |
  73. +----+------+------+
  74. row in set (0.00 sec)
  1. #在创建完表后,修改自增字段的起始值
  2. mysql> create table student(
  3. -> id int primary key auto_increment,
  4. -> name varchar(20),
  5. -> sex enum('male','female') default 'male'
  6. -> );
  7.  
  8. mysql> alter table student auto_increment=3;
  9.  
  10. mysql> show create table student;
  11. .......
  12. ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
  13.  
  14. mysql> insert into student(name) values('egon');
  15. Query OK, 1 row affected (0.01 sec)
  16.  
  17. mysql> select * from student;
  18. +----+------+------+
  19. | id | name | sex |
  20. +----+------+------+
  21. | 3 | egon | male |
  22. +----+------+------+
  23. row in set (0.00 sec)
  24.  
  25. mysql> show create table student;
  26. .......
  27. ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
  28.  
  29. #也可以创建表时指定auto_increment的初始值,注意初始值的设置为表选项,应该放到括号外
  30. create table student(
  31. id int primary key auto_increment,
  32. name varchar(20),
  33. sex enum('male','female') default 'male'
  34. )auto_increment=3;
  35.  
  36. #设置步长
  37. sqlserver:自增步长
  38. 基于表级别
  39. create table t1
  40. id int。。。
  41. engine=innodb,auto_increment=2 步长=2 default charset=utf8
  42.  
  43. mysql自增的步长:
  44. show session variables like 'auto_inc%';
  45.  
  46. #基于会话级别
  47. set session auth_increment_increment=2 #修改会话级别的步长
  48.  
  49. #基于全局级别的
  50. set global auth_increment_increment=2 #修改全局级别的步长(所有会话都生效)
  51.  
  52. #!!!注意了注意了注意了!!!
  53. If the value of auto_increment_offset is greater than that of auto_increment_increment, the value of auto_increment_offset is ignored.
  54. 翻译:如果auto_increment_offset的值大于auto_increment_increment的值,则auto_increment_offset的值会被忽略
  55. 比如:设置auto_increment_offset=3auto_increment_increment=2
  56.  
  57. mysql> set global auto_increment_increment=5;
  58. Query OK, 0 rows affected (0.00 sec)
  59.  
  60. mysql> set global auto_increment_offset=3;
  61. Query OK, 0 rows affected (0.00 sec)
  62.  
  63. mysql> show variables like 'auto_incre%'; #需要退出重新登录
  64. +--------------------------+-------+
  65. | Variable_name | Value |
  66. +--------------------------+-------+
  67. | auto_increment_increment | 1 |
  68. | auto_increment_offset | 1 |
  69. +--------------------------+-------+
  70.  
  71. create table student(
  72. id int primary key auto_increment,
  73. name varchar(20),
  74. sex enum('male','female') default 'male'
  75. );
  76.  
  77. mysql> insert into student(name) values('egon1'),('egon2'),('egon3');
  78. mysql> select * from student;
  79. +----+-------+------+
  80. | id | name | sex |
  81. +----+-------+------+
  82. | 3 | egon1 | male |
  83. | 8 | egon2 | male |
  84. | 13 | egon3 | male |
  85. +----+-------+------+

步长increment与起始偏移量offset:auto_increment_increment,auto_increment_offset

六 foreign key

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

公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费

解决方法:

我们完全可以定义一个部门表

然后让员工信息表关联该表,如何关联,即foreign key

  1. #表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一
  2. create table department(
  3. id int primary key,
  4. name varchar(20) not null
  5. )engine=innodb;
  6.  
  7. #dpt_id外键,关联父表(department主键id),同步更新,同步删除
  8. create table employee(
  9. id int primary key,
  10. name varchar(20) not null,
  11. dpt_id int,
  12. constraint fk_name foreign key(dpt_id)
  13. references department(id)
  14. on delete cascade
  15. on update cascade
  16. )engine=innodb;
  17.  
  18. #先往父表department中插入记录
  19. insert into department values
  20. (1,'欧德博爱技术有限事业部'),
  21. (2,'艾利克斯人力资源部'),
  22. (3,'销售部');
  23.  
  24. #再往子表employee中插入记录
  25. insert into employee values
  26. (1,'egon',1),
  27. (2,'alex1',2),
  28. (3,'alex2',2),
  29. (4,'alex3',2),
  30. (5,'李坦克',3),
  31. (6,'刘飞机',3),
  32. (7,'张火箭',3),
  33. (8,'林子弹',3),
  34. (9,'加特林',3)
  35. ;
  36.  
  37. #删父表department,子表employee中对应的记录跟着删
  38. mysql> delete from department where id=3;
  39. mysql> select * from employee;
  40. +----+-------+--------+
  41. | id | name | dpt_id |
  42. +----+-------+--------+
  43. | 1 | egon | 1 |
  44. | 2 | alex1 | 2 |
  45. | 3 | alex2 | 2 |
  46. | 4 | alex3 | 2 |
  47. +----+-------+--------+
  48.  
  49. #更新父表department,子表employee中对应的记录跟着改
  50. mysql> update department set id=22222 where id=2;
  51. mysql> select * from employee;
  52. +----+-------+--------+
  53. | id | name | dpt_id |
  54. +----+-------+--------+
  55. | 1 | egon | 1 |
  56. | 3 | alex2 | 22222 |
  57. | 4 | alex3 | 22222 |
  58. | 5 | alex1 | 22222 |
  59. +----+-------+--------+

示范

  1. 1 foreign key 2
  2. 则表1的多条记录对应表2的一条记录,即多对一
  3.  
  4. 利用foreign key的原理我们可以制作两张表的多对多,一对一关系
  5. 多对多:
  6. 1的多条记录可以对应表2的一条记录
  7. 2的多条记录也可以对应表1的一条记录
  8.  
  9. 一对一:
  10. 1的一条记录唯一对应表2的一条记录,反之亦然
  11.  
  12. 分析时,我们先从按照上面的基本原理去套,然后再翻译成真实的意义,就很好理解了

辅助理解

  1. #一对多或称为多对一
  2. 三张表:出版社,作者信息,书
  3.  
  4. 一对多(或多对一):一个出版社可以出版多本书
  5.  
  6. 关联方式:foreign key
  1. =====================多对一=====================
  2. create table press(
  3. id int primary key auto_increment,
  4. name varchar(20)
  5. );
  6.  
  7. create table book(
  8. id int primary key auto_increment,
  9. name varchar(20),
  10. press_id int not null,
  11. foreign key(press_id) references press(id)
  12. on delete cascade
  13. on update cascade
  14. );
  15.  
  16. insert into press(name) values
  17. ('北京工业地雷出版社'),
  18. ('人民音乐不好听出版社'),
  19. ('知识产权没有用出版社')
  20. ;
  21.  
  22. insert into book(name,press_id) values
  23. ('九阳神功',1),
  24. ('九阴真经',2),
  25. ('九阴白骨爪',2),
  26. ('独孤九剑',3),
  27. ('降龙十巴掌',2),
  28. ('葵花宝典',3);
  1. 单张表:用户表+相亲关系表,相当于:用户表+相亲关系表+用户表
  2. 多张表:用户表+用户与主机关系表+主机表
  3.  
  4. 中间那一张存放关系的表,对外关联的字段可以联合唯一

其它例子

  1. #一对一
  2. 两张表:学生表和客户表
  3.  
  4. 一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系
  5.  
  6. 关联方式:foreign key+unique
  1. 例一:一个用户只有一个博客
  2.  
  3. 用户表:
  4. id name
  5. egon
  6. alex
  7. wupeiqi
  8.  
  9. 博客表
  10. fk+unique
  11. id url name_id
  12. xxxx 1
  13. yyyy 3
  14. zzz 2
  15.  
  16. 例二:一个管理员唯一对应一个用户
  17. 用户表:
  18. id user password
  19. egon xxxx
  20. alex yyyy
  21.  
  22. 管理员表:
  23. fk+unique
  24. id user_id password
  25. 1 xxxxx
  26. 2 yyyyy

其他例子

七 作业

练习:账号信息表,用户组,主机表,主机组

  1. #用户表
  2. create table user(
  3. id int not null unique auto_increment,
  4. username varchar(20) not null,
  5. password varchar(50) not null,
  6. primary key(username,password)
  7. );
  8.  
  9. insert into user(username,password) values
  10. ('root',''),
  11. ('egon',''),
  12. ('alex','alex3714')
  13. ;
  14.  
  15. #用户组表
  16. create table usergroup(
  17. id int primary key auto_increment,
  18. groupname varchar(20) not null unique
  19. );
  20.  
  21. insert into usergroup(groupname) values
  22. ('IT'),
  23. ('Sale'),
  24. ('Finance'),
  25. ('boss')
  26. ;
  27.  
  28. #主机表
  29. create table host(
  30. id int primary key auto_increment,
  31. ip char(15) not null unique default '127.0.0.1'
  32. );
  33.  
  34. insert into host(ip) values
  35. ('172.16.45.2'),
  36. ('172.16.31.10'),
  37. ('172.16.45.3'),
  38. ('172.16.31.11'),
  39. ('172.10.45.3'),
  40. ('172.10.45.4'),
  41. ('172.10.45.5'),
  42. ('192.168.1.20'),
  43. ('192.168.1.21'),
  44. ('192.168.1.22'),
  45. ('192.168.2.23'),
  46. ('192.168.2.223'),
  47. ('192.168.2.24'),
  48. ('192.168.3.22'),
  49. ('192.168.3.23'),
  50. ('192.168.3.24')
  51. ;
  52.  
  53. #业务线表
  54. create table business(
  55. id int primary key auto_increment,
  56. business varchar(20) not null unique
  57. );
  58. insert into business(business) values
  59. ('轻松贷'),
  60. ('随便花'),
  61. ('大富翁'),
  62. ('穷一生')
  63. ;
  64.  
  65. #建关系:user与usergroup
  66.  
  67. create table user2usergroup(
  68. id int not null unique auto_increment,
  69. user_id int not null,
  70. group_id int not null,
  71. primary key(user_id,group_id),
  72. foreign key(user_id) references user(id),
  73. foreign key(group_id) references usergroup(id)
  74. );
  75.  
  76. insert into user2usergroup(user_id,group_id) values
  77. (1,1),
  78. (1,2),
  79. (1,3),
  80. (1,4),
  81. (2,3),
  82. (2,4),
  83. (3,4)
  84. ;
  85.  
  86. #建关系:host与business
  87.  
  88. create table host2business(
  89. id int not null unique auto_increment,
  90. host_id int not null,
  91. business_id int not null,
  92. primary key(host_id,business_id),
  93. foreign key(host_id) references host(id),
  94. foreign key(business_id) references business(id)
  95. );
  96.  
  97. insert into host2business(host_id,business_id) values
  98. (1,1),
  99. (1,2),
  100. (1,3),
  101. (2,2),
  102. (2,3),
  103. (3,4)
  104. ;
  105.  
  106. #建关系:user与host
  107.  
  108. create table user2host(
  109. id int not null unique auto_increment,
  110. user_id int not null,
  111. host_id int not null,
  112. primary key(user_id,host_id),
  113. foreign key(user_id) references user(id),
  114. foreign key(host_id) references host(id)
  115. );
  116.  
  117. insert into user2host(user_id,host_id) values
  118. (1,1),
  119. (1,2),
  120. (1,3),
  121. (1,4),
  122. (1,5),
  123. (1,6),
  124. (1,7),
  125. (1,8),
  126. (1,9),
  127. (1,10),
  128. (1,11),
  129. (1,12),
  130. (1,13),
  131. (1,14),
  132. (1,15),
  133. (1,16),
  134. (2,2),
  135. (2,3),
  136. (2,4),
  137. (2,5),
  138. (3,10),
  139. (3,11),
  140. (3,12);

作业:

MySQL四-2:完整性约束的更多相关文章

  1. 三.mysql表的完整性约束

    mysql表的完整性约束 什么是约束 not null    不能为空的 unique      唯一 = 不能重复 primary key 主键 = 不能为空 且 不能重复 foreign key ...

  2. mysql 四 表操作

    表介绍 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 id,name,qq,age称为字段,其余的,一行内容称为一条记录 本节重点: 1 创建 ...

  3. MySQL四:表操作

    阅读目录 表介绍 一 创建表 二 查看表结构 三 数据类型 四 表完整性约束 五 修改表ALTER TABLE 六 复制表 七 删除表 八 完整性约束 九 数据类型 表介绍 表相当于文件,表中的一条记 ...

  4. MySQL中的完整性约束

    对于已经创建好的表,虽然字段的数据类型决定所能存储的数据类型,但是表中所存储的数据是否合法并没有检查. MySQL支持的完整性约束: NOT NULL                 约束字段的值不能 ...

  5. Mysql 四种事务隔离介绍以及锁机制

    还有很多不太懂,这里收集几份大佬文章“飞机票”,待我整理好了,再好好写一篇文章吧. MySQL的四种事务隔离级别 https://www.cnblogs.com/huanongying/p/70215 ...

  6. MySQL四种事务隔离级别详解

    本文实验的测试环境:Windows 10+cmd+MySQL5.6.36+InnoDB 一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做 ...

  7. mysql三-3:完整性约束

    一.完整性约束介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIG ...

  8. 百万年薪python之路 -- MySQL数据库之 完整性约束

    MySQL完整性约束 一. 介绍 为了防止不符合规范的数据进入数据库,在用户对数据进行插入.修改.删除等操作时,DBMS自动按照一定的约束条件对数据进行监测,使不符合规范的数据不能进入数据库,以确保数 ...

  9. Mysql 四种事务隔离级别

    一.前提 时过一年重新拾起博文记录,希望后面都能坚持下来. 接着之前MySql的学习,先记录下这篇. 以下都是基于mysql8 innodb存储引擎进行分析的. 二.事务的ACID特性 A(Atomi ...

随机推荐

  1. 天梯L2-003-测试点

    测试点3无法过,题目说是正整数用了int,结果得用double输入才能AC.

  2. Xamarin XAML语言教程使用Progress属性数据绑定设置进度条进度

    Xamarin XAML语言教程使用Progress属性数据绑定设置进度条进度 开发者除了可以为ProgressBar定义的Progress属性直接赋双精度类型的值外,还可以通过数据绑定的方式为该属性 ...

  3. Beginning iOS 8 Programming with Swift-TableView

    UITableView控件使用 使用UITableView,在控件库中,拖拽一个Table View到ViewController中,在Controller的后台代码中需要继承UITableViewD ...

  4. iOS duplicate symbol for architecture arm64 解决办法

    导致这个问题的原因有多种: 1.重复定义了const常量. 2.多个第三方库同时用到了某个函数库. 暂时列举这几种,以后遇到了其他原因再加.

  5. A Beginner’s Guide to the OUTPUT Clause in SQL Server

    原文 A Beginner’s Guide to the OUTPUT Clause in SQL Server T-SQL supports the OUTPUT clause after the ...

  6. ife2015-task2-1-2-3

    task2-1.html <!DOCTYPE html><html><head lang="en"> <meta charset=&quo ...

  7. espresso 元素遮挡问题。

    在使用espresso进行测试的时候,点击一个横向列表的时候会在点击的项目下出现对应的横线. 实现方式是在FrameLayout下放两个TextView, 一个TextView包含下划线,默认是Fra ...

  8. JComboBox添加item的赋值类型问题!不一致的话会导致不能更改jcombobox的选择值

    在用swing做页面的时候,往往需要设置字体样式,那么,如何用一种方法设置字体之后,在后面的其他页面就不需要再次设置字体了呢? 下面这个方法就可以解决了: JComboBox在对它进行添加子项的时候, ...

  9. session的作用范围(转)

    session是在服务器端建立的,浏览器访问服务器会有一个jsessionid,浏览器端通过 jsessionid定位服务器端的session,session的创建和销毁由服务器端控制.当浏览器关闭后 ...

  10. Kwickserver

    Kwickserver 欢迎来到Kwickserver的主页 Kwickserver是什么? Kwickserver是一个易于安装的和易于使用的服务器应用程序,从CD安装在PC兼容的硬件和坚持webi ...