How to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle?


Problem:

You want to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle.

Solution

Any DML statement (an INSERT, UPDATE, or DELETE) executed on tables defined with integrity, helps to ensures that the rows in the tables maintain their integrity.

Let’s look at some examples that show the enforcement of a primary key constraint. The customers table’s primary key is the customer_id column, which means that every value stored in the customer_id column must be unique. If you try to insert a row with a duplicate value for a primary key, the database returns the error ORA-00001, as in below example.

Example

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

If you attempt to update a primary key value to a value that already exists in the table, the database returns the same error.

Example

UPDATE customers    SET customer_id = 1  WHERE customer_id = 2;   ERROR at line 1: ORA-00001: unique constraint (STORE.CUSTOMERS_PK) violated

A foreign key relationship is one in which a column from one table is referenced in another. For example, the product_type_id column in the products table references the product_type_id column in the product_types table. The product_types table is known as the parent table, and the products table is known as the child table, reflecting the dependence of the product_type_id column in the products table on the product_type_id column in the product_types table.

If you try to insert a row into the products table with a nonexistent product_type_id, the database will return the error ORA-02291. This error indicates the database couldn’t find a matching parent key value.

Example

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

If you attempt to update the product_type_id of a row in the products table to a nonexistent parent key value, the database returns the same error.

Example

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

Finally, if you attempt to delete a row in the parent table that has dependent child rows, the database returns error ORA-02292. For example, if you attempt to delete the row whose product_type_id is 1 from the product_types table, the database will return this error because the products table contains rows whose product_type_id is 1.

Example

 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

Table definitions Used for above problem.

Example

--------------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 );

Updated on: 05-Dec-2020

397 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements