如何在 Oracle 中使用主鍵約束和外部索引鍵約束來增強資料庫完整性?
問題
您想在 Oracle 中使用主鍵約束和外部索引鍵約束來增強資料庫完整性。
解決方案
任何在定義了完整性的表上執行的 DML 語句(INSERT、UPDATE 或 DELETE)都有助於確保表中的行保持其完整性。
讓我們看一些示例,這些示例展示了主鍵約束的執行方式。customers 表的主鍵是 customer_id 列,這意味著儲存在 customer_id 列中的每個值都必須是唯一的。如果您嘗試插入主鍵值重複的行,資料庫將返回錯誤 ORA-00001,如下例所示。
示例
INSERT INTO customers (customer_id, first_name, last_name, dob, phone) VALUES (1, 'Roger', 'Federer', '01-JAN-83', '001-001-0001'); INSERT INTO customers ( * ERROR at line 1: ORA-00001: unique constraint (STORE.CUSTOMERS_PK) violated
如果您嘗試將主鍵值更新為表中已存在的值,資料庫將返回相同的錯誤。
示例
UPDATE customers SET customer_id = 1 WHERE customer_id = 2; ERROR at line 1: ORA-00001: unique constraint (STORE.CUSTOMERS_PK) violated
外部索引鍵關係是指一個表中的列在另一個表中被引用的關係。例如,products 表中的 product_type_id 列引用 product_types 表中的 product_type_id 列。product_types 表稱為父表,products 表稱為子表,反映了 products 表中 product_type_id 列對 product_types 表中 product_type_id 列的依賴性。
如果您嘗試將不存在的 product_type_id 插入到 products 表中,資料庫將返回錯誤 ORA-02291。此錯誤表示資料庫找不到匹配的父鍵值。
示例
INSERT INTO products (product_id, product_type_id, name, description, price) VALUES (999, 999, 'Test product', 'Test Product', 23); INSERT INTO products ( * ERROR at line 1: ORA-02291: integrity constraint (STORE.PRODUCTS_FK_PRODUCT_TYPES) violated - parent key not found
如果您嘗試將 products 表中某一行的 product_type_id 更新為不存在的父鍵值,資料庫將返回相同的錯誤。
示例
UPDATE products SET product_type_id = 999 WHERE product_id = 999; UPDATE products * ERROR at line 1: ORA-02291: integrity constraint (STORE.PRODUCTS_FK_PRODUCT_TYPES) violated - parent key not found
最後,如果您嘗試刪除父表中具有從屬子行的行,資料庫將返回錯誤 ORA-02292。例如,如果您嘗試從 product_types 表中刪除 product_type_id 為 1 的行,資料庫將返回此錯誤,因為 products 表包含 product_type_id 為 1 的行。
示例
DELETE FROM product_types WHERE product_type_id = 1; DELETE FROM product_types * ERROR at line 1: ORA-02292: integrity constraint (STORE.PRODUCTS_FK_PRODUCT_TYPES) violated - child record found
上述問題使用的表定義。
示例
--------------create scripts---------------------------- create table customers ( customer_id integer generated by default on null as identity, email_address varchar2(255 char) not null, full_name varchar2(255 char) not null) ; create table stores ( store_id integer generated by default on null as identity , store_name varchar2(255 char) not null, web_address varchar2(100 char), physical_address varchar2(512 char), latitude number, longitude number, logo blob, logo_mime_type varchar2(512 char), logo_filename varchar2(512 char), logo_charset varchar2(512 char), logo_last_updated date) ; create table products ( product_id integer generated by default on null as identity , product_name varchar2(255 char) not null, unit_price number(10,2), product_details blob, product_image blob, image_mime_type varchar2(512 char), image_filename varchar2(512 char), image_charset varchar2(512 char), image_last_updated date) ; --------------------------constraints-------------------------------------- alter table customers add constraint customers_pk primary key (customer_id); alter table customers add constraint customers_email_u unique (email_address); alter table stores add constraint stores_pk primary key (store_id); alter table stores add constraint store_name_u unique (store_name); alter table stores add constraint store_at_least_one_address_c check ( web_address is not null or physical_address is not null ); alter table products add constraint products_pk primary key (product_id); alter table products add constraint products_json_c check ( product_details is json );