溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

sql中常見的5種約束是什么

發(fā)布時間:2020-12-01 14:10:12 來源:億速云 閱讀:720 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章將為大家詳細講解有關(guān)sql中常見的5種約束是什么,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

sql完整性約束有:非空約束,唯一約束,主鍵約束,外鍵約束,條件約束等

完整性約束是保證用戶所做的修改不會破壞數(shù)據(jù)的一致性,是保護數(shù)據(jù)正確性和相容性的一種手段。

常見的5種約束:

NOT NULL:非空約束C,指定的列不允許為空值

UNIQUE:唯一約束U,指定的列中沒有重復(fù)值,或該表中每一個值或者每一組值都將是唯一的

PRIMARY KEY:主鍵約束P,唯一的標識出表的每一行,且不允許空值,一個表只能有一個主鍵約束

FOREIGN KEY:外鍵約束R,一個表中的列引用了其它表中的列,使得存在依賴關(guān)系,可以指向引用自身的列

CHECK:條件約束C,指定該列是否滿足某個條件

非空約束 NK

create table member(
    mid number,    
    name varchar2(200) not null
    );

插入空報錯:

SQL> insert into member(mid,name) values(1,null);
*
第 1 行出現(xiàn)錯誤:
ORA-01400: 無法將 NULL 插入 ("SCOTT"."MEMBER"."NAME")

唯一約束 UK

drop table member purge;create table member(
    mid number,
    name varchar2(200) not null,
    email varchar2(50) unique
    );

插入報錯

SQL> insert into member(mid,name,email) values (1,'scott','scott@163.com');

已創(chuàng)建 1 行。

SQL> insert into member(mid,name,email) values (2,'jack','scott@163.com');
insert into member(mid,name,email) values (2,'jack','scott@163.com')
*
第 1 行出現(xiàn)錯誤:
ORA-00001: 違反唯一約束條件 (SCOTT.SYS_C0010891)

給約束指定名字

這錯誤并沒有像之前的非空約束那樣,準確告訴用戶哪個字段出現(xiàn)了問題,我們可以給這個約束指定一個名字

create table member(
    mid number,
    name varchar2(200) not null,
    email varchar2(50),    
    constraint uk_email unique(email)
    );

主鍵約束 PK

主鍵約束=非空約束+唯一約束

create table member(
    mid number,
    name varchar2(200) not null,
    email varchar2(50),    
    constraint pk_number primary key(mid),    
    constraint uk_email unique(email)
    );

插入錯誤的數(shù)據(jù)報錯:

SQL> insert into member(mid,name,email) values (1,'jack','scott@163.com');

已創(chuàng)建 1 行

SQL> insert into member(mid,name,email) values (1,'jack','jack@163.com');
insert into member(mid,name,email) values (1,'jack','jack@163.com')
*
第 1 行出現(xiàn)錯誤:
ORA-00001: 違反唯一約束條件 (SCOTT.PK_NUMBER)

檢查約束 CK

create table member(
   mid number,    
   name varchar2(200) not null,    
   age number check(age between 0 and 200),    
   sex varchar2(10),    
   email varchar2(50),    
   constraint pk_number primary key(mid),    
   constraint uk_email unique(email),    
   constraint ck_sex check(sex in('男','女'))    
   );

插入錯誤數(shù)據(jù)報錯:

SQL> insert into member(mid,name,age,sex,email) values (1,'Jack','300','無','jack@163.com');
insert into member(mid,name,age,sex,email) values (1,'Jack','300','無','jack@163.com')
*
第 1 行出現(xiàn)錯誤:
ORA-02290: 違反檢查約束條件 (SCOTT.CK_SEX)

主外鍵約束  FK

create table member(
    mid number,
    name varchar2(200) not null,    
    constraint pk_mid primary key(mid)
    );    
create table advice(
       adid number,
       content clob not null,
       mid number,       
       constraint pk_adid primary key(adid),       
       constraint fk_mid foreign key(mid) references member(mid)
       );

正確插入數(shù)據(jù),mid在附表中已經(jīng)存在

insert into member(mid,name) values (1,'Scott');
insert into member(mid,name) values (2,'Jack');
insert into advice(adid,content,mid) values (1,'test',1);
insert into advice(adid,content,mid) values (2,'test',2);
commit;

插入一條父表中沒有的mid數(shù)據(jù)

SQL> insert into advice(adid,content,mid) values (3,'test',3);
insert into advice(adid,content,mid) values (3,'test',3)
*
第 1 行出現(xiàn)錯誤:
ORA-02291: 違反完整約束條件 (SCOTT.FK_MID) - 未找到父項關(guān)鍵字

采用主外鍵約束會在表的刪除和數(shù)據(jù)刪除方面存在新的問題

刪除父表得先刪除子表

SQL> drop table member;drop table member           
*
第 1 行出現(xiàn)錯誤:
ORA-02449: 表中的唯一/主鍵被外鍵引用

強制性刪除父表,這樣就不能使用purge選項了,并且字表中的主外鍵約束也將不復(fù)存在

SQL> drop table member cascade constraint;
表已刪除。
SQL> insert into advice(adid,content,mid) values (3,'test',3);

已創(chuàng)建 1 行

刪除父表的數(shù)據(jù)的先刪除字表中的數(shù)據(jù)

SQL> delete from member where mid=1;
delete from member where mid=1
*
第 1 行出現(xiàn)錯誤:
ORA-02292: 違反完整約束條件 (SCOTT.FK_MID) - 已找到子記錄

除了這種方法外還可以設(shè)置級聯(lián)操作子句

on delete cascade

create table advice(
       adid number,
       content clob not null,
       mid number,       
       constraint pk_adid primary key(adid),       
       constraint fk_mid foreign key(mid) references member(mid) on delete cascade
       );

這樣的話,刪除附表數(shù)據(jù),會把字表中的數(shù)據(jù)也給刪除。

on delete set null

create table advice(       
adid number,       
content clob not null,       
mid number,       
constraint pk_adid primary key(adid),       
constraint fk_mid foreign key(mid) references member(mid) on delete set null      
 );

這樣刪除父表中的數(shù)據(jù),子表中的對應(yīng)的關(guān)聯(lián)字段將被設(shè)置為空

SQL> delete from member where mid=1;

已刪除 1 行。

SQL> select * from advice;

      ADID CONTENT         MID
---------- ---------- ----------
    test
    test            2

查看約束

SQL>  select constraint_name,constraint_type,table_name  from user_constraints; 

CONSTRAINT_NAME            C   TABLE_NAME
------------------------------ - ------------------------------
FK_DEPTNO                  R       EMP
PK_EMP                     P       EMP
PK_DEPT                    P       DEPT

R外鍵約束,C檢查約束,Q唯一約束,P主鍵約束

修改約束

增加約束

alter table member add constraint pk_mid primary key(mid);
alter table member add constraint ck_age check(age between 0 and 200);
alter table member modify (name varchar2(50) not null);

需要注意的是非空約束使用的是modify的方式

禁用/啟用約束

非外鍵

alter table member disable constraint ck_age;
alter table member enable constraint ck_age;

外鍵

alter table member disable constraint pk_mid cascade;

刪除約束

alter table member drop constraint pk_mid cascade;
alter table member drop constraint ck_age;

關(guān)于sql中常見的5種約束是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI