十三:約束
維護(hù)數(shù)據(jù)的完整性
介紹
介紹
數(shù)據(jù)的完整性用于確保數(shù)據(jù)庫(kù)數(shù)據(jù)遵從一定的商業(yè)和邏輯規(guī)則,在oracle中,數(shù)據(jù)完整性可以使用約束、觸發(fā)器、應(yīng)用程序(過(guò)程、函數(shù))三種方法來(lái)實(shí)現(xiàn),在這三種方法中,因?yàn)榧s束易于維護(hù),并且具有最好的性能,所以作為維護(hù)數(shù)據(jù)完整性的首選。
約束
約束
約束用于確保數(shù)據(jù)庫(kù)數(shù)據(jù)滿足特定的商業(yè)規(guī)則。在oracle中,約束包括:not null、 unique, primary key, foreign key,和check五種。
使用
not null(非空)
如果在列上定義了not null,那么當(dāng)插入數(shù)據(jù)時(shí),必須為列提供數(shù)據(jù)。
unique(唯一)
當(dāng)定義了唯一約束后,該列值是不能重復(fù)的,但是可以為null。
primary key(主鍵)
用于唯一的標(biāo)示表行的數(shù)據(jù),當(dāng)定義主鍵約束后,該列不但不能重復(fù)而且不能為null。
需要說(shuō)明的是:一張表最多只能有一個(gè)主鍵,但是可以有多個(gè)unqiue約束。
foreign key(外鍵)
用于定義主表和從表之間的關(guān)系。外鍵約束要定義在從表上,主表則必須具有主鍵約束或是unique約束,當(dāng)定義外鍵約束后,要求外鍵列數(shù)據(jù)必須在主表的主鍵列存在或是為null。
check
用于強(qiáng)制行數(shù)據(jù)必須滿足的條件,假定在sal列上定義了check約束,并要求sal列值在1000-2000之間如果不在1000-2000之間就會(huì)提示出錯(cuò)。
商店售貨系統(tǒng)表設(shè)計(jì)案例
現(xiàn)有一個(gè)商店的數(shù)據(jù)庫(kù),記錄客戶及其購(gòu)物情況,由下面三個(gè)表組成:商品goods(商品號(hào)goodsId,商品名 goodsName,單價(jià) unitprice,商品類別category,供應(yīng)商provider);
客戶customer(客戶號(hào)customerId,姓名name,住在address,電郵email,性別sex,身份證cardId);
購(gòu)買purchase(客戶號(hào)customerId,商品號(hào)goodsId,購(gòu)買數(shù)量nums);
請(qǐng)用SQL語(yǔ)言完成下列功能:
1. 建表,在定義中要求聲明:
(1). 每個(gè)表的主外鍵;
(2). 客戶的姓名不能為空值;
(3). 單價(jià)必須大于0,購(gòu)買數(shù)量必須在1到30之間;
(4). 電郵不能夠重復(fù);
(5). 客戶的性別必須是 男 或者 女,默認(rèn)是男;
SQL> create table goods(goodsId char(8) primary key, --主鍵
goodsName varchar2(30),
unitprice number(10,2) check(unitprice>0),
category varchar2(8),
provider varchar2(30)
);
SQL> create table customer( customerId char(8) primary key, --主鍵
name varchar2(50) not null, --不為空
address varchar2(50),
email varchar2(50) unique,
sex char(2) default '男' check(sex in ('男','女')), -- 一個(gè)char能存半個(gè)漢字,兩位char能存一個(gè)漢字
cardId char(18)
);
SQL> create table purchase( customerId char(8) references customer(customerId),
goodsId char(8) references goods(goodsId),
nums number(10) check (nums between 1 and 30)
);
表是默認(rèn)建在SYSTEM表空間的
維護(hù)
商店售貨系統(tǒng)表設(shè)計(jì)案例(2)
如果在建表時(shí)忘記建立必要的約束,則可以在建表后使用alter table命令為表增加約束。但是要注意:增加not null約束時(shí),需要使用modify選項(xiàng),而增加其它四種約束使用add選項(xiàng)。
1. 增加商品名也不能為空
SQL> alter table goods modify goodsName not null;
2. 增加身份證也不能重復(fù)
SQL> alter table customer add constraint xxxxxx unique(cardId);
3. 增加客戶的住址只能是’海淀’,’朝陽(yáng)’,’東城’,’西城’,’通州’,’崇文’,’昌平’;
SQL> alter table customer add constraint yyyyyy check (address in (’海淀’,’朝陽(yáng)’,’東城’,’西城’,’通州’,’崇文’,’昌平’));
刪除約束
當(dāng)不再需要某個(gè)約束時(shí),可以刪除。
alter table 表名 drop constraint 約束名稱;
特別說(shuō)明一下:
在刪除主鍵約束的時(shí)候,可能有錯(cuò)誤,比如:
alter table 表名 drop primary key;
這是因?yàn)槿绻趦蓮埍泶嬖谥鲝年P(guān)系,那么在刪除主表的主鍵約束時(shí),必須帶上cascade選項(xiàng) 如像:
alter table 表名 drop primary key cascade;
顯示約束信息
1.顯示約束信息
通過(guò)查詢數(shù)據(jù)字典視圖user_constraints,可以顯示當(dāng)前用戶所有的約束的信息。
select constraint_name, constraint_type, status, validated from user_constraints where table_name = '表名';
2.顯示約束列
通過(guò)查詢數(shù)據(jù)字典視圖user_cons_columns,可以顯示約束所對(duì)應(yīng)的表列信息。
select column_name, position from user_cons_columns where constraint_name = '約束名';
3.當(dāng)然也有更容易的方法,直接用pl/sql developer查看即可。簡(jiǎn)單演示一下下...
表級(jí)定義 列級(jí)定義
列級(jí)定義
列級(jí)定義是在定義列的同時(shí)定義約束。
如果在department表定義主鍵約束
create table department4(dept_id number(12) constraint pk_department primary key,
name varchar2(12), loc varchar2(12));
表級(jí)定義
表級(jí)定義是指在定義了所有列后,再定義約束。這里需要注意:
not null約束只能在列級(jí)上定義。
以在建立employee2表時(shí)定義主鍵約束和外鍵約束為例:
create table employee2(emp_id number(4), name varchar2(15),
dept_id number(2), constraint pk_employee primary key (emp_id),
constraint fk_department foreign key (dept_id) references department4(dept_id));
聯(lián)系客服