資料的完整性就是指資料庫中的資料在邏輯上的一緻性和準确性。資料完整性一般分為3種:域完整性、實體完整性和參照完整性。完整性限制是通過限制列資料、行資料和表之間的資料來保證資料的完整性。完整性限制定義在表上,存儲在資料字典中。
1、域完整性
域完整性又稱列完整性,是指确定一個資料集對某一列是否有效和确定是否允許空值。域完整性通常是通過使用有效性檢查來實作的,還可以通過限制資料類型、格式或者可能的取值範圍來實作。
Oracle可以通過check限制實作域完整性。check限制表示一個字段的輸入必須滿足check限制的條件,若不滿足,則資料無法正常輸入。
利用SQL語句操作check限制主要有如下幾種方式:
(1)利用SQL語句在建立表時建立check限制
Sql代碼
create table tableName(
columnName dataType [not null | null] [default expression] check(checkExpression),
columnName dataType [not null | null] [default expression] constraint checkName check(checkExpression),
...n
)
示例代碼:
create table person(
id int primary key,
sex varchar(4) check(sex='男' or sex='女'),
age int default 0 constraint ageCheck check(age>=0 and age<=125)
(2)利用SQL語句在修改時建立check限制
alter table tableName add(constraint checkName check(checkExpression), columnName dataType check(checkExpression))
alter table person add(constraint sexCheck check(sex='男' or sex='女'), age int check(age>=0 and age<=125))
(3)利用SQL語句删除check限制
alter table tableName drop constraint checkName;
alter table person drop constraint sexCheck;
2、實體完整性
實體完整性又稱行完整性,它有求表中的每一行都有一個唯一的辨別符,這個辨別符就是主關鍵字。通過索引、unique限制和primary key限制可以實作實體完整性。
為表建立主鍵可實作表的實體完整性,主鍵可以是一列或幾列的組合,一個表隻能有一個primary key限制,且primary key限制中的列不能為空值,當為表建立primary key時,Oracle會自動為primary key對應的列建立唯一索引,實作資料的唯一性。有了索引後,就可以實作對資料的快速通路。
如果要確定一個表中的非主鍵列的值不可重複,可以在該列加上unique限制,unique限制也會自動産生索引。
primary key限制和unique限制的差別如下:
(1)一個資料表隻能有一個primary key限制,但可以有多個unique限制。
(2)primary key對應字段的值不能為null,而unique限制對應列的值可以為null。
(3)primary key對應字段或字段的組合的值是不能重複的,而unique限制列的值為null的時候是可以重複的,也就是說unique限制列可以有多個null值。
利用SQL建立和删除primary key限制和unique限制
(1)在建立表的時候建立對應的限制
create table tableName(columnName dataType primary key, columnName dataType unique,...n);
create table tableName(columnName1 dataType, columnName2 dataType, ...n, primary key(columnName1, columnName2), unique(columnName3, columnName4));
create table person(id int primary key, name varchar(20) unique, age int);
create table person(id int, name varchar(20), age int, primary key(id), unique(name));
(2)在修改表的時候建立對應的限制
alter table tableName add(constraint pkName primary key(column1, column2), constraint uniqueName unique(column3, column4));
alter table person add(constraint personPk primary key(id), constraint nameUnique unique(name));
(3)删除primary key和unique限制
alter table tableName drop constraint constraintName;
alter table person drop constraint personPk;
3、參照完整性
參照完整性又稱為引用完整性,它可以保證從表(參照表)中的資料與主表(被參照表)中的資料的一緻性。參照完整性通過定義從表的外鍵與主表的主鍵相對應來實作。可以利用foreign key定義從表中的外鍵,利用primary key定義主表中的主鍵。
--如果主表中的一條記錄被從表中的外鍵所引用,那麼主表的這一條記錄既不能被删除也不能修改主關鍵字。
--如果從表的外鍵沒有進行not null限制,那麼從表的外鍵也是可以為null的。
利用SQL語句定義和删除表間的參照關系
(1)建立表時同時定義表間的參照關系
create table tableName(columnName dataType [foreign key] references referencesTableName(referencesColumn),...n);
--referencesTableName表示參照表的表名,referencesColumn表示參照的參照表中的字段名,即參照的是參照表中的哪個字段。
create table saleBill(id int primary key, saleBookBillId int references saleBookBill(id));
--該代碼表示saleBill表中的saleBookBillId字段是參照saleBookBill表的id字段。
(2)通過修改表定義外鍵限制
alter table tableName add(constraint constraintName foreign key(column1[, column2, ...n]) references refTableName(refColumn1[, refColumn2, ...n]));
alter table saleBill add(constraint saleBillFk foreign key(saleBookBillId) references saleBookBIll(id));
(3)利用SQL語句删除表間的參照關系
alter table saleBill drop constraint saleBillFk;