1. 程式人生 > >mysql約束條件

mysql約束條件

表結構

mysql> create table t1(id int,name char(6),sex enum('male','female'));
Query OK, 0 rows affected (0.01 sec)

mysql> desc t1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | char(6)               | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

約束條件與資料型別的寬度一樣,都是可選引數

作用:用於保證資料的完整性和一致性 主要分為:

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'
age int unsigned NOT NULL default 20 必須為正值(無符號) 不允許為空 預設是20
3. 是否是key
主鍵 primary key
外來鍵 foreign key
索引 (index,unique...)

〔1〕null and deault  (空和預設值)

create table t1(id int not null)
create table t1(id int not null default 0)

not null :插入內容時不能為空,否則為報錯

default :如何插入時為空預設值為0

〔2〕unique (設定唯一約束)

單列唯一

方式一

mysql> create table t3(id int,name char(6)unique);
Query OK, 0 rows affected (0.03 sec)

mysql> desc t3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| name  | char(6) | YES  | UNI | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

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

mysql> insert into t3 values(2,'one');
ERROR 1062 (23000): Duplicate entry 'one' for key 'name'

方式二

mysql> create table t3(id int,name char(6),unique(name));
Query OK, 0 rows affected (0.01 sec)

mysql> desc t3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| name  | char(6) | YES  | UNI | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 建立的時候name欄位加了unique(唯一性)後,下面在第二次插入相同內容時就報錯了。

聯合唯一:

mysql> create table t4(id int,name char(6),unique(id,name));
Query OK, 0 rows affected (0.02 sec)


mysql> insert into t4 values(1,'one');
Query OK, 1 row affected (0.00 sec)
插入成功
mysql> insert into t4 values(1,'one');
ERROR 1062 (23000): Duplicate entry '1-one' for key 'id'
插入失敗,聯合唯一,id,name 同時和表內的id,name相同
mysql> insert into t4 values(1,'two');
Query OK, 1 row affected (0.00 sec)
插入成功,因為是聯合唯一,單列id相同沒事

〔3〕primary key (主鍵)

mysql> create table t5(id int primary key);
Query OK, 0 rows affected (0.08 sec)

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

mysql> insert into t5 values(1);
Query OK, 1 row affected (0.02 sec)

mysql> insert into t5 values(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

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

從約束角度來看:只是單純的一個不能為空並且唯一的欄位

預設引擎:intodb  他是需要一個主鍵的,如果沒有設定他會自己去尋找一個欄位唯一的,如果沒有他會使用隱藏欄位就沒有意義了。

也是可以設定聯合的 

〔4〕auto_increment 自動遞增 

mysql> create table t6(id int auto_increment,name char(6));
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column 
and it must be defined as a key
# 自動遞增的欄位必須是唯一的,加個nuique或者primary都可以
mysql> create table t6(id int primary key auto_increment,name char(6));
Query OK, 0 rows affected (0.10 sec)

mysql> desc t6;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| name  | char(6) | YES  |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
2 rows in set (0.01 sec)


mysql> insert into t6(name) values('one'),('two');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

#一次性指定name插入了兩個內容,效果如下:id自動遞增。

mysql> select * from t6;
+----+------+
| id | name |
+----+------+
|  1 | one  |
|  2 | two  |
+----+------+
2 rows in set (0.00 sec)

mysql> insert into t6 values(5,'five');
Query OK, 1 row affected (0.01 sec)


mysql> insert into t6(name) values('six');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t6;
+----+------+
| id | name |
+----+------+
|  1 | one  |
|  2 | two  |
|  5 | five |
|  6 | six  |
+----+------+
4 rows in set (0.00 sec)

# 如果指定了id傳值,下次遞增起點就是你傳值的那個位置。

mysql> truncate t6;
Query OK, 0 rows affected (0.02 sec)

# 清空的時候不要用delete,只會清空內容,不會充值auto_increment的初始值。
   使用truncate會全部清空並且重置

〔4〕foreign key 表之間的關聯

建立被關聯表(必須先建立)

mysql> create table f1(id int primary key,name char(6));
Query OK, 0 rows affected (0.16 sec)
# 被繫結的欄位必須唯一性,unique或者primary key 都可以

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

#插入內容,效果如下:
mysql> select * from f1;
+----+------+
| id | name |
+----+------+
|  1 | IT   |
|  2 | sale |
+----+------+
2 rows in set (0.00 sec)

建立關聯表 

mysql> create table f2(id int,name char(6),dep_id int,foreign key(dep_id) references f1(id) on delect cascade on update cascade);
Query OK, 0 rows affected (0.03 sec)
# 繫結格式 刪除同步 更新同步


mysql> insert into f2 values(1,'one',1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into f2 values(2,'two',1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into f2 values(3,'three',2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from f2;
+------+-------+--------+
| id   | name  | dep_id |
+------+-------+--------+
|    1 | one   |      1 |
|    2 | two   |      1 |
|    3 | three |      2 |
+------+-------+--------+
3 rows in set (0.00 sec)

# 這就是算繫結成功了

雖然確實繫結在一起了,但若是後期擴充套件的時候有時候會出現麻煩,可以單純邏輯上同步。