欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2019年01月04日 09:33:37   作者:李西召  
今天小編就為大家分享一篇關(guān)于基于MySQL數(shù)據(jù)庫(kù)的數(shù)據(jù)約束實(shí)例及五種完整性約束介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

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

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

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

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

相關(guān)文章

  • MySQL substr函數(shù)使用方法詳解

    MySQL substr函數(shù)使用方法詳解

    MySQL substr函數(shù)是指從一個(gè)內(nèi)容中,按照指定條件,「截取」一個(gè)字符串,這個(gè)內(nèi)容可以是數(shù)值或字符串,本文就來給大家講講MySQL substr函數(shù)的使用方法,需要的朋友可以參考下
    2023-07-07
  • bitronix 連接 MySQL 出現(xiàn)MySQLSyntaxErrorException 的解決方法

    bitronix 連接 MySQL 出現(xiàn)MySQLSyntaxErrorException 的解決方法

    這篇文章主要介紹了bitronix 連接 MySQL 出現(xiàn)MySQLSyntaxErrorException 的解決方法的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • explain分析sql效率的方法

    explain分析sql效率的方法

    下面小編就為大家?guī)硪黄猠xplain分析sql效率的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • 詳解mysql?bit/json/enum/set?的數(shù)據(jù)存儲(chǔ)

    詳解mysql?bit/json/enum/set?的數(shù)據(jù)存儲(chǔ)

    這篇文章主要介紹了mysql?bit/json/enum/set?的數(shù)據(jù)存儲(chǔ),探究的主要內(nèi)容為 int 類類型的存儲(chǔ), 浮點(diǎn)類類型的存儲(chǔ), char 類類型的存儲(chǔ), blob 類類型的存儲(chǔ), enum/json/set/bit 類類型的存儲(chǔ),需要的朋友可以參考下
    2023-09-09
  • Mysql select語(yǔ)句設(shè)置默認(rèn)值的方法

    Mysql select語(yǔ)句設(shè)置默認(rèn)值的方法

    這篇文章主要介紹了Mysql select語(yǔ)句設(shè)置默認(rèn)值的方法,需要的朋友可以參考下
    2014-05-05
  • Mysql日期查詢的詳細(xì)介紹

    Mysql日期查詢的詳細(xì)介紹

    這篇文章主要介紹了Mysql日期查詢的詳細(xì)介紹,根據(jù)時(shí)間段進(jìn)行查詢數(shù)據(jù)庫(kù)中單表中的數(shù)據(jù),如果感興趣的來了解一下
    2020-07-07
  • MySQL筆記之?dāng)?shù)據(jù)類型詳解

    MySQL筆記之?dāng)?shù)據(jù)類型詳解

    本篇文章對(duì)mysql數(shù)據(jù)類型進(jìn)行了詳細(xì)的介紹,需要的朋友參考下
    2013-05-05
  • mysql?事務(wù)解析

    mysql?事務(wù)解析

    這篇文章主要給大家分享的是mysql事務(wù)解析,事務(wù)(transaction)是業(yè)務(wù)邏輯的一個(gè)基本的單元組成,下面文章圍繞mysql事務(wù)的相關(guān)資料展開詳細(xì)內(nèi)容,需要的朋友可以參考一下希望對(duì)大家有所幫助
    2021-11-11
  • MySQL中一條update語(yǔ)句是如何執(zhí)行的

    MySQL中一條update語(yǔ)句是如何執(zhí)行的

    這篇文章主要給大家介紹了關(guān)于MySQL中一條update語(yǔ)句是如何執(zhí)行的相關(guān)資料,由于update涉及到數(shù)據(jù)的修改,所以很容易推斷,update語(yǔ)句比select語(yǔ)句會(huì)更復(fù)雜一些,需要的朋友可以參考下
    2022-03-03
  • getdata table表格數(shù)據(jù)join mysql方法

    getdata table表格數(shù)據(jù)join mysql方法

    今天小編就為大家分享一篇關(guān)于getdata table表格數(shù)據(jù)join mysql方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02

最新評(píng)論