濮阳杆衣贸易有限公司

主頁 > 知識庫 > 基于MySQL數(shù)據(jù)庫的數(shù)據(jù)約束實例及五種完整性約束介紹

基于MySQL數(shù)據(jù)庫的數(shù)據(jù)約束實例及五種完整性約束介紹

熱門標(biāo)簽:淄博400電話申請 百度地圖標(biāo)注后不顯示 俄國地圖標(biāo)注app 電銷機器人各個細節(jié)介紹 溫州瑞安400電話怎么申請 電銷機器人 行業(yè) 昆明電信400電話辦理 電話機器人市場趨勢 南昌高頻外呼系統(tǒng)哪家公司做的好

為了防止不符合規(guī)范的數(shù)據(jù)進入數(shù)據(jù)庫,在用戶對數(shù)據(jù)進行插入、修改、刪除等操作時,DBMS自動按照一定的約束條件對數(shù)據(jù)進行監(jiān)測,使不符合規(guī)范的數(shù)據(jù)不能進入數(shù)據(jù)庫,以確保數(shù)據(jù)庫中存儲的數(shù)據(jù)正確、有效、相容。

#數(shù)據(jù)約束

#五種完整性約束:
#NOT NULL :非空約束,指定某列不能為空;
#UNIQUE : 唯一約束,指定某列或者幾列組合不能重復(fù)
#PRIMARY KEY :主鍵,指定該列的值可以唯一地標(biāo)識該列記錄
#FOREIGN KEY :外鍵,指定該行記錄從屬于主表中的一條記錄,主要用于參照完整性
#CHECK :檢查,指定一個布爾表達式,用于指定對應(yīng)的值必須滿足該表達式(mysql不支持check約束)
#--------------------------------NOT NULL 非空約束 ---------------------------
create table test4
(
  #建立非空約束
id int not null,
name varchar(55) default 'ABCD' not null,
#默認值就是null
age int null
);
#取消非空約束
 alter table test4
 modify name varchar(55) default 'ABCD' not null,
#增加非空約束
 alter table test4
 modify age int not null;
#--------------------------------UNIQUE : 唯一約束--------------------------------
#列級約束語法建立約束
 create table test_unique
 (
 #建立行級唯一約束
 id int not null unique,
 age int
 );
 #表級約束語法格式
 create table unique_test3
 (
test6_id int not null,
test6_name varchar(255),
test6_pass varchar(255),
#使用表級約束語法建立唯一約束,指定test6_id和test6_name兩列組合不能重復(fù)
constraint test6_unique unique(test6_id,test6_name),
#使用表級約束語法建立唯一約束,約束名為test6_unique_2,test6_pass不能重復(fù)
constraint test6_unique_2 unique(test6_pass)
 );
 #add關(guān)鍵字增加唯一約束
 alter table test4
 add unique(id,name,age);
 #modify關(guān)鍵字刪除或者增加唯一約束
 alter table test4
 modify age varchar(255) not null;
 alter table test4
 modify age varchar(255) not null unique;
 #對大部分數(shù)據(jù)庫而言,刪除約束使用: alter table 表名 drop constraint 約束名
 #但是Mysql不采取此方式,而是: alter table 表名 drop index 約束名
 #--------------------------------PRIMARY KEY : 主鍵約束--------------------------------
 #主鍵約束相當(dāng)于非空約束和唯一約束。
 #每個表只允許擁有一個主鍵,但是這個主鍵可以由多個數(shù)據(jù)列組成,這些列組合不能重復(fù)
 #標(biāo)準(zhǔn)SQL允許給主鍵自行命名,但是對于Mysql來說自己的名字沒有任何作用,總是默認名為PRIMARY
 create table primary_test
 (
#使用列級語法建立主鍵約束
test_id int primary key,
test_name varchar(255)
 );
 #使用表級語法建立主鍵約束
 create table primary_test2
 (
test_id int not null,
test_name varchar(255),
test_pass varchar(255),
#指定主鍵約束名為test2_pk,對大部分數(shù)據(jù)庫有效,但是對mysql無效,此主鍵約束名仍為PRIMARY
constraint test2_pk primary key (test_id)
 );
 #以多列組合創(chuàng)立主鍵
 create table primary_test3
 (
test_id int,
test_name varchar(255),
primary key(test_id,test_name)
 );
 #使用列級約束語法
 alter table primary_test3
 modify test_id int primary key();
 #使用表級約束語法
 alter table primary_test3
 add primary key(test_id,test_name);
 #刪除主鍵約束:alter table 表名 drop primary key;
 #主鍵列自增長特性:如果某個數(shù)據(jù)列的類型是整型,而且該列作為主鍵列,則可指定該列具有自增長功能
 #mysql使用auto_increment來設(shè)置自增長,向該表插入記錄時可不為該列指定值,由系統(tǒng)生成
  create table primary_test3
 (
//建立主鍵約束、設(shè)置自增長
test_id int auto_increment primary key,
test_name varchar(255)
 );
 #外鍵約束 FOREIGN KEY
 #Mysql中只有表級語法建立的外鍵約束才可以生效
 #為保證參照主表的存在,先建立主表
 create table teacher_tb
 (
t_id int auto_increment,
t_name varchar(255),
primary key(t_id)
 );
 create table student_tb
 (
s_id int auto_increment primary key,
s_name varchar(255) not null,
t_java int,
foreign key(t_java) references teacher_tb(t_id)
 );
#如果使用表級約束語法,則需要使用foreign key指定本表的外鍵列,如果創(chuàng)建外鍵約束時沒有指定約束名,
#則mysql會為該外鍵約束命名為table_name_ibfk_n,其中table_name是從表的表名,n是從1開始的整數(shù)
 create table teacher_tb2
 (
t_id int auto_increment,
t_name varchar(255),
primary key(t_id)
 );
 create table student_tb2
 (
s_id int auto_increment primary key,
s_name varchar(255) not null,
t_java int,
constraint student_teacher_fk foreign key(t_java) references teacher_tb2(t_id)
 );
 #建立多列組合外鍵約束
 create table teacher_tb5
 (
t_name varchar(255),
t_pass varchar(255),
primary key(t_name,t_pass)
 );
 create table student_tb5
 (
s_id int auto_increment primary key,
s_name varchar(255) not null,
t_java_pass varchar(255),
t_java_name varchar(255),
foreign key(t_java_name,t_java_pass) 
  references teacher_tb5(t_name,t_pass)
 );
 #刪除外鍵約束
 alter table student_tb2
 drop foreign key student_teacher_fk;
 #增加外鍵約束
 alter table student_tb2
 add foreign key(t_java) references teacher_tb2(t_id);
 #外鍵約束參照自身,自約束
 create table foreign_test9
 (
foreign_id int auto_increment primary key,
foreign_name varchar(255),
refer_id int,
foreign key(refer_id) references foreign_test9(foreign_id)
 );
 #定義當(dāng)刪除主表記錄時,從表記錄也隨之刪除
 #on delete cascade 把參照該主表記錄的從表記錄全部級聯(lián)刪除
 #on delete set null 把參照該主表記錄的從表記錄從表設(shè)為null        e
 create table teacher_tb8
 (
t_id int auto_increment,
t_name varchar(255),
primary key(t_id)
 );
 create table student_tb8
 (
s_id int auto_increment primary key,
s_name varchar(255) not null,
t_java int,
constraint student_teacher_fk foreign key(t_java) references teacher_tb8(t_id) on delete cascade
 );

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

您可能感興趣的文章:
  • mysql完整性約束實例詳解
  • MySQL約束超詳解
  • MySQL中常見的六個約束類型詳解
  • MySQL約束類型及舉例介紹
  • MySQL學(xué)習(xí)之?dāng)?shù)據(jù)庫表五大約束詳解小白篇

標(biāo)簽:葫蘆島 洛陽 拉薩 嘉峪關(guān) 吐魯番 安徽 甘南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于MySQL數(shù)據(jù)庫的數(shù)據(jù)約束實例及五種完整性約束介紹》,本文關(guān)鍵詞  基于,MySQL,數(shù)據(jù)庫,的,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于MySQL數(shù)據(jù)庫的數(shù)據(jù)約束實例及五種完整性約束介紹》相關(guān)的同類信息!
  • 本頁收集關(guān)于基于MySQL數(shù)據(jù)庫的數(shù)據(jù)約束實例及五種完整性約束介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    米林县| 南川市| 阳东县| 霍州市| 武宣县| 孟村| 隆子县| 时尚| 绥宁县| 霍城县| 祁东县| 江川县| 康保县| 长寿区| 惠水县| 玛多县| 石棉县| 大埔区| 郑州市| 海兴县| 吉木萨尔县| 乐昌市| 阿坝| 镶黄旗| 潢川县| 南华县| 娱乐| 黄山市| 泰兴市| 托克逊县| 嫩江县| 广宗县| 东乌珠穆沁旗| 清远市| 远安县| 新宾| 建昌县| 绥滨县| 确山县| 扎鲁特旗| 云南省|