03-----完整性约束

重点:
not null 与 default

unique

primary

auto_increment

foreign key

一、介绍

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

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

主要分为:

PRIMARY KEY (PK)    #标识该字段为该表的主键,可以唯一的标识记录
FOREIGN 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

);

验证1:

mysql> create table t11(id int);# id字段默认可以为空
Query OK, 0 rows affected (0.05 sec)

mysql> desc t11;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES   |          | NULL    |       |
+-------+---------+------+-----+---------+-------+
row in set (0.03 sec)
mysql> insert into t11 values(); #给t11表插一个空的值
Query OK, 1 row affected (0.00 sec)

#查询结果如下
mysql> select * from t11;
+------+
| id   |
+------+
| NULL |
+------+
row in set (0.00 sec)

默认值可以为空
默认值可以空

验证2:

mysql> create table t12(id int not null);#设置字段id不为空
Query OK, 0 rows affected (0.03 sec)

mysql> desc t12;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
row in set (0.01 sec)

mysql> insert into t12 values();#不能插入空
ERROR 1364 (HY000): Field 'id' doesn't have a default value

设置not null,插入值时不能为空
设置not null,插入值时不能为空

验证3:

# 第一种情况
mysql> create table t13(id int default 1);
Query OK, 0 rows affected (0.03 sec)

mysql> desc t13;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |           | 1              |              |
+-------+---------+------+-----+---------+-------+
row in set (0.01 sec)

mysql> insert into t13 values();
Query OK, 1 row affected (0.00 sec)

mysql> select * from t13;
+------+
| id   |
+------+
|    1  |
+------+
row in set (0.00 sec)


# 第二种情况
mysql> create table t14(id int not null default 2);
Query OK, 0 rows affected (0.02 sec)

mysql> desc t14;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO      |         | 2               |             |
+-------+---------+------+-----+---------+-------+
row in set (0.01 sec)

mysql> select * from t14;
+----+
| id |
+----+
|  2 |
+----+
row in set (0.00 sec)
设置id字段有默认值后,则无论id字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值

猜你喜欢

转载自www.cnblogs.com/edeny/p/9394708.html