mysql建表时外检怎么创建_MySQL创建表时加入的约束以及外键约束的的意义
1,創(chuàng)建表時加入的約束
a)?非空約束,not null
b)?唯一約束,unique
c)?主鍵約束,primary key
d)?外鍵約束,foreign key
1,非空約束,針對某個字段設(shè)置其值不為空,如:學(xué)生的姓名不能為空
drop table if existst_student;create tablet_student(
student_idint(10),
student_namevarchar(20) not null,
sexchar(2) default 'm',
birthday date,
emailvarchar(30),
classes_idint(3)
)
反例,如果插入時?student_name為空違反了約束則報錯
insert intot_student(student_id, birthday, email, classes_id)values(1002, '1988-01-01', 'qqq@163.com', 10)
2,唯一性約束,它可以使某個字段的值不能重復(fù),如:email不能重復(fù)
drop table if existst_student;create tablet_student(
student_idint(10),
student_namevarchar(20) not null,
sexchar(2) default 'm',
birthday date,
emailvarchar(30) unique,
classes_idint(3)
)
drop table if existst_student;create tablet_student(
student_idint(10),
student_namevarchar(20) not null,
sexchar(2) default 'm',
birthday date,
emailvarchar(30) ,
classes_idint(3) ,constraint email_unique unique(email)/*表級約束*/
3,主鍵約束
每個表應(yīng)該具有主鍵,主鍵可以標(biāo)識記錄的唯一性,主鍵分為單一主鍵和復(fù)合(聯(lián)合)主鍵,單一主鍵是由一個字段構(gòu)成的,復(fù)合(聯(lián)合)主鍵是由多個字段構(gòu)成的。
drop table if existst_student;create tablet_student()
student_idint(10) primary key,/*列級約束*/student_namevarchar(20) not null,
sexchar(2) default 'm',
birthday date,
emailvarchar(30) ,
classes_idint(3)
)
或者
drop table if existst_student;create tablet_student(
student_idint(10),
student_namevarchar(20) not null,
sexchar(2) default 'm',
birthday date,
emailvarchar(30) ,
classes_idint(3),CONSTRAINT p_id PRIMARY key (student_id)/*表級約束*/)
4,外鍵約束
外鍵主要是維護(hù)表之間的關(guān)系的,主要是為了保證參照完整性,如果表中的某個字段為外鍵字段,那么該字段的值必須來源于參照的表的主鍵。
首先建立班級表t_classes
drop table if existst_classes;create tablet_classes(
classes_idint(3),
classes_namevarchar(40),constraint pk_classes_id primary key(classes_id)
)
在t_student中加入外鍵約束
drop table if existst_student;create tablet_student(
student_idint(10),
student_namevarchar(20),
sexchar(2),
birthday date,
emailvarchar(30),
classes_idint(3),constraint student_id_pk primary key(student_id),constraint fk_classes_id foreign key(classes_id) referencest_classes(classes_id)
)
當(dāng)我們向t_student中加入數(shù)據(jù)
insert into
t_student(student_id, student_name, sex, birthday, email, classes_id)
values(1001, 'zhangsan', 'm', '1988-01-01', 'qqq@163.com', 10)
出現(xiàn)錯誤,因為在班級表中不存在班級編號為10班級,外鍵約束起到了作用
存在外鍵的表就是子表,參照的表就是父表,所以存在一個父子關(guān)系,也就是主從關(guān)系,主表就是班級表,從表就是學(xué)生表。
上面的例子中如果插入的時候把外鍵值設(shè)為null,可以插入成功。成功的插入了學(xué)生信息,但是classes_id沒有值,這樣會影響參照完整性,所以我們建議將外鍵字段設(shè)置為非空。
當(dāng)需要刪除班級數(shù)據(jù),也會報錯,因為子表(t_student)存在一個外鍵classes_id,它參照了父表(t_classes)中的主鍵,所以先刪除子表中的引用記錄,再修改父表中的數(shù)據(jù)。
因為子表(t_student)存在一個外鍵classes_id,它參照了父表(t_classes)中的主鍵,所以先刪除父表,那么將會影響子表的參照完整性,所以正確的做法是,先刪除子表中的數(shù)據(jù),再刪除父表中的數(shù)據(jù),采用drop table也不行,必須先drop子表,再drop父表
例如級聯(lián)更新
mysql對有些約束的修改比較麻煩,所以我們可以先刪除,再添加alter table t_student drop foreign keyfk_classes_id;alter table t_student add constraint fk_classes_id_1 foreign key(classes_id) references t_classes(classes_id) on update cascade;
級聯(lián)之后,我們只修改了父表中的數(shù)據(jù),但是子表中的數(shù)據(jù)也會跟著變動。
2,需不需要外鍵
主鍵和索引是不可少的,不僅可以優(yōu)化數(shù)據(jù)檢索速度,開發(fā)人員還省不其它的工作,
矛盾焦點:數(shù)據(jù)庫設(shè)計是否需要外鍵。這里有兩個問題:一個是如何保證數(shù)據(jù)庫數(shù)據(jù)的完整性和一致性;二是第一條對性能的影響。
正方觀點:
1,由數(shù)據(jù)庫自身保證數(shù)據(jù)一致性,完整性,更可靠,因為程序很難100%保證數(shù)據(jù)的完整性,而用外鍵即使在數(shù)據(jù)庫服務(wù)器當(dāng)機(jī)或者出現(xiàn)其他問題的時候,也能夠最大限度的保證數(shù)據(jù)的一致性和完整性。
eg:數(shù)據(jù)庫和應(yīng)用是一對多的關(guān)系,A應(yīng)用會維護(hù)他那部分?jǐn)?shù)據(jù)的完整性,系統(tǒng)一變大時,增加了B應(yīng)用,A和B兩個應(yīng)用也許是不同的開發(fā)團(tuán)隊來做的。他們?nèi)绾螀f(xié)調(diào)保證數(shù)據(jù)的完整性,而且一年以后如果又增加了C應(yīng)用呢?
2,有主外鍵的數(shù)據(jù)庫設(shè)計可以增加ER圖的可讀性,這點在數(shù)據(jù)庫設(shè)計時非常重要。
3,外鍵在一定程度上說明的業(yè)務(wù)邏輯,會使設(shè)計周到具體全面。
反方觀點:
1,可以用觸發(fā)器或應(yīng)用程序保證數(shù)據(jù)的完整性
2,過分強(qiáng)調(diào)或者說使用主鍵/外鍵會平添開發(fā)難度,導(dǎo)致表過多等問題
3,不用外鍵時數(shù)據(jù)管理簡單,操作方便,性能高(導(dǎo)入導(dǎo)出等操作,在insert,? ?update,? ?delete? ?數(shù)據(jù)的時候更快)
eg:在海量的數(shù)據(jù)庫中想都不要去想外鍵,試想,一個程序每天要insert數(shù)百萬條記錄,當(dāng)存在外鍵約束的時候,每次要去掃描此記錄是否合格,一般還不 止一個字段有外鍵,這樣掃描的數(shù)量是成級數(shù)的增長!我的一個程序入庫在3個小時做完,如果加上外鍵,需要28個小時!
結(jié)論:
1,在大型系統(tǒng)中(性能要求不高,安全要求高),使用外鍵;在大型系統(tǒng)中(性能要求高,安全自己控制),不用外鍵;小系統(tǒng)隨便,最好用外鍵。
2,用外鍵要適當(dāng),不能過分追求
3,不用外鍵而用程序控制數(shù)據(jù)一致性和完整性時,應(yīng)該寫一層來保證,然后個個應(yīng)用通過這個層來訪問數(shù)據(jù)庫。
需要注意的是:MySQL允許使用外鍵,但是為了完整性檢驗的目的,在除了InnoDB表類型之外的所有表類型中都忽略了這個功能。這可能有些怪異,實際上卻非常正常:對于數(shù)據(jù)庫的所有外鍵的每次插入、更新和刪除后,進(jìn)行完整性檢查是一個耗費時間和資源的過程,它可能影響性能,特別是當(dāng)處理復(fù)雜的或者是纏繞的連接樹時。因而,用戶可以在表的基礎(chǔ)上,選擇適合于特定需求的最好結(jié)合。所以,如果需要更好的性能,并且不需要完整性檢查,可以選擇使用MyISAM表類型,如果想要在MySQL中根據(jù)參照完整性來建立表并且希望在此基礎(chǔ)上保持良好的性能,最好選擇表結(jié)構(gòu)為innoDB類型
轉(zhuǎn)自;https://www.cnblogs.com/fuland/p/4280434.html
總結(jié)
以上是生活随笔為你收集整理的mysql建表时外检怎么创建_MySQL创建表时加入的约束以及外键约束的的意义的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 20日均线的意义?
- 下一篇: win2008 mysql_mysql5