数据库操作之——约束

一 约束的种类

  primary key

  unique key

  not null

  zerofill

  default

  auto_increment

  foreign key

  unsigned  

  

  1 default。 默认

  示例一

mysql> create table t7(id int ,sex char(6) default 'male');
Query OK, 0 rows affected (0.28 sec)
mysql> desc t7;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| sex   | char(6) | YES  |     | male    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

   示例二

mysql> create table t1(id int primary key auto_increment,name char(6),sex char(5) default 'male');
Query OK, 0 rows affected (0.48 sec)
mysql> insert t1(name) values
    -> ('alex');
Query OK, 1 row affected (0.30 sec)

mysql> select * from t1;
+----+------+------+
| id | name | sex  |
+----+------+------+
|  1 | alex | male |
+----+------+------+
1 row in set (0.00 sec)

  2 auto_increment   自增

  针对所有表的设置:

  设置自增的步长:auto_increment_increment

  设置自增的初始值:auto_increment_offset

  

  针对单个表的设置:

  auto_increment

  

  查看回话级别变量

  show session variables like 'auto_inc%';

  查看全局级别变量

  show global variables like 'auto_inc%';

  设置基于回话级别:

  set session auto_increment_increment=2

  设置基于全局级别

  set global auto_increment_increment=2

  PS:

    如果自增的步长小于自增的初始值,则自增的初始值会被忽略!即自增的初始值一定要小于等于自增的步长!

    所以你会发现这个并没有多大卵用。

  设置完以后,不能生效。退出,重新登录,才能生效。

  示例 1:

  针对所有表的设置

mysql> set global auto_increment_increment=2;
Query OK, 0 rows affected (0.00 sec)

mysql> set global auto_increment_offset=3;
Query OK, 0 rows affected (0.00 sec)

mysql> q
Bye

C:Userszuo>mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 19
mysql> create table t2(id int primary key auto_increment,name char(6));
Query OK, 0 rows affected (0.28 sec)

mysql> insert t2(name) values
    -> ('alex'),
    -> ('egon'),
    -> ('wupeiqi');
Query OK, 3 rows affected, 1 warning (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 1
mysql> select * from t2;
+----+--------+
| id | name   |
+----+--------+
|  1 | alex   |
|  3 | egon   |
|  5 | wupeiq |
+----+--------+
3 rows in set (0.00 sec)
mysql> show global variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 2     |
| auto_increment_offset    | 3     |
+--------------------------+-------+
2 rows in set (0.00 sec)

  因为自增的步长小于自增的初始值,所以自增的初始值被忽略!

  

  示例 2:

  针对单个表的设置:

mysql> create table t3(id int primary key auto_increment,name char(4))auto_increment=10;
Query OK, 0 rows affected (0.26 sec)

mysql> insert t3(name) values
    -> ('egon'),
    -> ('alex');
Query OK, 2 rows affected (0.06 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t3;
+----+------+
| id | name |
+----+------+
| 10 | egon |
| 11 | alex |
+----+------+
2 rows in set (0.00 sec)

  3 foreign key 外键

  外键具有保持数据完整性和一致性的机制,目前MySQL只在InnoDB引擎下支持。

  B 存在外键 b_f_k,以 A 表的 a_k 作为参照列,则 A 为主表,B 为从表,A 中某记录更新或删除时将会联动 B 中外键与其关联对应的记录做更新或删除操作。

  举个例子:员工表、部门表。关系是:一个员工属于一个部门,一个部门有多个员工在这里,员工表中的部门编号就是外键。部门表是主键表(主表)员工表是外键表(从表)。

  注意:

  先创建主表,然后创建从表。

  主表中的操作(删除,修改)会影响到从表。

  示例:

  多对一的情形:

 create table dep(id int primary key auto_increment,name char(7));
Query OK, 0 rows affected (0.26 sec)

mysql> insert dep(name) values
    -> ('IT'),
    -> ('xx'),
    -> ('yy');
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> create table emp(id int primary key auto_increment,name char(6),dep_id int, foreign key(dep_id) references dep(id) on delete cascade on update cascade);
Query OK, 0 rows affected (0.26 sec)

mysql> insert emp(name,dep_id) values
    -> ('egon',1),
    -> ('alex',1),
    -> ('wupeiqi',1),
    -> ('sb',2);
Query OK, 4 rows affected, 1 warning (0.05 sec)
Records: 4  Duplicates: 0  Warnings: 1

mysql> select * from emp;
+----+--------+--------+
| id | name   | dep_id |
+----+--------+--------+
|  1 | egon   |      1 |
|  2 | alex   |      1 |
|  3 | wupeiq |      1 |
|  4 | sb     |      2 |
+----+--------+--------+
4 rows in set (0.00 sec)

mysql> show create table emp;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                     |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| emp   | CREATE TABLE `emp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(6) DEFAULT NULL,
  `dep_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `dep_id` (`dep_id`),
  CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`dep_id`) REFERENCES `dep` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> delete from dep where id =1;
Query OK, 1 row affected (0.06 sec)

mysql> select * from emp;
+----+------+--------+
| id | name | dep_id |
+----+------+--------+
|  4 | sb   |      2 |
+----+------+--------+
1 row in set (0.00 sec)

  多对多的情形:

  就没有主表,从表之分了。怎么办呢,需要通过一个第三方的中介,额外创建一个的table。

  

mysql> create table class(id int primary key auto_increment,name char(6),stu_id int);
Query OK, 0 rows affected (0.25 sec)

mysql> create table student(id int primary key auto_increment,name char(6),cla_id int );
Query OK, 0 rows affected (0.24 sec)
mysql> create table stu2cla(id int primary key auto_increment,stu_id int,cla_id int,foreign key(stu_id) references student(id) on delete cascade on update cascade,foreign key(cla_id) references class(id) on delete cascade on update cascade);
Query OK, 0 rows affected (0.30 sec)
mysql> insert class(name,stu_id) values
    -> ('python',1),
    -> ('python',2),
    -> ('python',3),
    -> ('python',4),
    -> ('linux',5),
    -> ('linux',6);
Query OK, 6 rows affected (0.05 sec)
Records: 6  Duplicates: 0  Warnings: 0
mysql> insert student(name,cla_id) values
    -> ('egon',1),
    -> ('alex',2),
    -> ('wupeiqi',3),
    -> ('yuanhao',4),
    -> ('egon',5),
    -> ('egon',6);
Query OK, 6 rows affected, 2 warnings (0.06 sec)
Records: 6  Duplicates: 0  Warnings: 2
mysql> insert stu2cla(stu_id,cla_id) values
    -> (1,2),
    -> (2,2),
    -> (2,1);
Query OK, 3 rows affected (0.30 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> show create table stu2cla;
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| stu2cla | CREATE TABLE `stu2cla` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `stu_id` int(11) DEFAULT NULL,
  `cla_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `stu_id` (`stu_id`),
  KEY `cla_id` (`cla_id`),
  CONSTRAINT `stu2cla_ibfk_1` FOREIGN KEY (`stu_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `stu2cla_ibfk_2` FOREIGN KEY (`cla_id`) REFERENCES `class` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
原文地址:https://www.cnblogs.com/654321cc/p/7724776.html