Mysql -- 完整性约束

  • not null 与 default
  • unique
  • primary
  • auto_increment
  • foreign key

    一、介绍

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

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

    主要分为:

    复制代码
    primary key (PK)    #标识该字段为该表的主键,可以唯一的标识记录
    foreing key (FK)    #标识该字段为该表的外键
    not null    #标识该字段不能为空
    unique key (UK)    #标识该字段的值是唯一的
    auto_increment    #标识该字段的值自动增长(整数类型,而且为主键)
    default    #为该字段设置默认值
    
    unsigned #无符号
    zerofill #使用0填充
    复制代码

    说明:

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

二、not null 与default

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

默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值

create table tb1(
    nid int not null defalut 2,
    num int not null

);

 

三、unique

意思是不同的.   在mysql 中称为  单列唯一

举例说明:创建公司部门表(每个公司都有唯一的一个部门)。


mysql> create table department(
    -> id int,
    -> name char(10)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into department values(1,'IT'),(2,'IT');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from department;
+------+------+
| id   | name |
+------+------+
|    1 | IT   |
|    2 | IT   |
+------+------+
2 rows in set (0.00 sec)
# 发现: 同时插入两个IT部门也是可以的,但这是不合理的,所以我们要设置name字段为unique 解决这种不合理的现象。

 

接下来,使用约束条件unique,来对公司部门的字段进行设置。


#第一种创建unique的方式
#例子1:
create table department(
    id int,
    name char(10) unique
);
mysql> insert into department values(1,'it'),(2,'it');
ERROR 1062 (23000): Duplicate entry 'it' for key 'name'

#例子2:
create table department(
    id int unique,
    name char(10) unique
);
insert into department values(1,'it'),(2,'sale');

#第二种创建unique的方式
create table department(
    id int,
    name char(10) ,
    unique(id),
    unique(name)
);
insert into department values(1,'it'),(2,'sale');
单列唯一

 

还有一个联合唯一     联合唯一 只要两列记录,有一列不同,既符合联合唯一的约束

# 创建services表
mysql> create table services(
    -> id int,
    -> ip char(15),
    -> port int,
    -> unique(id),
    -> unique(ip,port)
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> desc services;
+-------+----------+------+-----+---------+-------+
| Field | Type      | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id        | int(11)   | YES   | UNI  | NULL       |             |
| ip        | char(15) | YES   | MUL  | NULL       |             |
| port    | int(11) | YES   |          | NULL       |             |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

#联合唯一,只要两列记录,有一列不同,既符合联合唯一的约束
mysql> insert into services values
    -> (1,'192,168,11,23',80),
    -> (2,'192,168,11,23',81),
    -> (3,'192,168,11,25',80);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from services;
+------+---------------+------+
| id   | ip            | port |
+------+---------------+------+
|    1 | 192,168,11,23 |   80 |
|    2 | 192,168,11,23 |   81 |
|    3 | 192,168,11,25 |   80 |
+------+---------------+------+
3 rows in set (0.00 sec)

mysql> insert into services values (4,'192,168,11,23',80);
ERROR 1062 (23000): Duplicate entry '192,168,11,23-80' for key 'ip'
联合唯一

四、primary key

  设置主键的      也可以用 not null + unique  代替   但是比较推荐 primary key

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

约束:等价于 not null unique,字段的值不为空且唯一

存储引擎默认是(innodb):对于innodb存储引擎来说,一张表必须有一个主键。

 

五.auto_increment

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

 

清空表区分delete和truncate的区别:

delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。

truncate table t1;数据量大,删除速度比上一条快,且直接从零开始。

 

六. foreign key

关联表的   

on delete cascade      同步删除

on update cascade     同步更新

 

posted @ 2018-08-31 16:51  九月江  阅读(133)  评论(0编辑  收藏  举报