約束是什麼用來幹嗎?
約束實際上就是表中數據的限制條件
做用:表在設計的時候加入約束的目的就是爲了保證表中的記錄完整和有效
好比一些字段的惟一性,將一些字段約束成外鍵mysql
A、非空約束
用not null約束的字段不能爲null值,必須給定具體的數據
建立表,給字段添加非空約束(建立用戶表,用戶名不能爲空)sql
create table blog (
id int(12) not null;
)
insert插入數據時若是插入id爲null直接報錯數據庫
B、惟一性約束
unique約束的字段,具備惟一性,不可重複,但能夠爲null
建立表,保證郵箱地址惟一(列級約束)
create table blog (
id int(12) not null UNIQUE;
)
表級約束
create table blog (
id int(12) not null;
UNIQUE(id);
)
若是插入相同 id 會報錯
使用表級約束,給多個字段聯合約束
聯合約束,表示兩個或以上的字段同時與另外一條記錄相等,則報錯
create table blog (
id int(12) not null;
name varchar(20) not null;
unique(id,name);
)
若是與聯合字段都相同,則報錯
表級約束能夠給約束起名字(方便之後經過這個名字來刪除這個約束)
create table blog (
id int(12) not null;
name varchar(20) not null;
CONSTRAINT t_id_name unique(id,name);
)
constraint是約束關鍵字,t_user_email_unique本身取的名字ide
C、主鍵約束(primary key)PK
表設計時必定要有主鍵
主鍵約束
主鍵字段
主鍵值
表中的某個字段添加主鍵約束後,該字段爲主鍵字段,主鍵字段中出現的每個數據都稱爲主鍵值
主鍵約束與「not null unique」區別:給某個字段添加主鍵約束以後,該字段不能重複也不能爲空,效果和」not null unique」約束相同,可是本質不一樣。
主鍵約束除了能夠作到」not null unique」以外,還會默認添加」索引——index」
一張表應該有主鍵字段,若是沒有,表示該表無效
主鍵值:是當前行數據的惟一標識、是當前行數據的身份
即便表中兩行記錄相關數據相同,但因爲主鍵值不一樣,因此也認爲是兩行不一樣的記錄
按主鍵約束的字段數量分類:不管是單一主鍵仍是複合主鍵,一張表主鍵約束只能有一個(約束只能有一個,但能夠做用到好幾個字段)
單一主鍵:給一個字段添加主鍵約束
複合主鍵:給多個字段聯合添加一個主鍵約束(只能用表級定義)
1)單一主鍵(列級定義)
mysql> create table t_user(
-> id int(10) primary key,
-> name varchar(30)
-> );
Query OK, 0 rows affected (0.07 sec)
2)單一主鍵(表級定義)
mysql> create table t_user(
-> id int(10),
-> name varchar(30) not null,
-> constraint t_user_id_pk primary key(id)
-> );
Query OK, 0 rows affected (0.01 sec)
3)複合主鍵(表級定義)
mysql> create table t_user(
-> id int(10),
-> name varchar(30) not null,
-> email varchar(128) unique,
-> primary key(id,name)
-> );
Query OK, 0 rows affected (0.05 sec)oop
在MySQL數據庫提供了一個自增的數字,專門用來自動生成主鍵值,主鍵值不用用戶維護,自動生成,自增數從1開始,以1遞增(auto_increment)
mysql> create table t_user(
-> id int(10) primary key auto_increment,
-> name varchar(30) not null
-> );
Query OK, 0 rows affected (0.03 sec)
插入兩行記錄,id主鍵值會自動增長
mysql> insert into t_user(name) values('jay');
Query OK, 1 row affected (0.04 sec)設計
mysql> insert into t_user(name) values('man');
Query OK, 1 row affected (0.00 sec)blog
mysql> select * from t_user;
+----+------+
| id | name |
+----+------+
| 1 | jay |
| 2 | man |
+----+------+
2 rows in set (0.00 sec)索引
D、外鍵約束(foreign key)FK
只能是表級定義
foreign key(classno) references t_class(cno)
外鍵約束主要用來維護兩個表之間數據的一致性
外鍵約束
外鍵字段
外鍵值rem
外鍵約束、外鍵字段、外鍵值之間的關係:某個字段添加外鍵約束以後,該字段稱爲外鍵字段,外鍵字段中每一個數據都是外鍵值it
按外鍵約束的字段數量分類:a、單一外鍵:給一個字段添加外鍵約束
b、複合外鍵:給多個字段聯合添加一個外鍵約束
一張表能夠有多個外鍵字段(與主鍵不一樣)
栗子:存儲學生班級 信k 息
mysql> drop table if exists t_student;
mysql> drop table if exists t_class;
mysql> create table t_class(
-> cno int(10) primary key,
-> cname varchar(128) not null unique
-> );
mysql> create table t_student(
-> sno int(10) primary key auto_increment,
-> sname varchar(30) not null,
-> classno int(3),
-> foreign key(classno) references t_class(cno)
-> );
mysql> insert into t_class(cno,cname) values(100,'aaaaaaxxxxxx');
mysql> insert into t_class(cno,cname) values(200,'oooooopppppp');
mysql> insert into t_student(sname,classno) values('jack',100);
mysql> insert into t_student(sname,classno) values('lucy',100);
mysql> insert into t_student(sname,classno) values('king',200);
班級表t_class:
mysql> select from t_class;
+-----+--------------+
| cno | cname |
+-----+--------------+
| 100 | aaaaaaxxxxxx |
| 200 | oooooopppppp |
+-----+--------------+
學生表t_student:
mysql> select from t_student;
+-----+-------+---------+
| sno | sname | classno |
+-----+-------+---------+
| 1 | jack | 100 |
| 2 | lucy | 100 |
| 3 | king | 200 |
+-----+-------+---------+
上表中找出每一個學生的班級名稱:
mysql> select s.,c. from t_student s join t_class c on s.classno=c.cno;+-----+-------+---------+-----+--------------+| sno | sname | classno | cno | cname |+-----+-------+---------+-----+--------------+| 1 | jack | 100 | 100 | aaaaaaxxxxxx || 2 | lucy | 100 | 100 | aaaaaaxxxxxx || 3 | king | 200 | 200 | oooooopppppp |+-----+-------+---------+-----+--------------+這就是數據實體的一對多關係模型:在多的那一方加外鍵來約束