Oracle
UNIQUE Constraint
In Oracle, the UNIQUE constraint ensures that all values in a column (or group of columns) are distinct, meaning no duplicate values are allowed. This constraint is typically used to enforce data integrity, ensuring that key data, such as identification numbers, remains unique across the table. Unlike a primary key, a table can have multiple UNIQUE constraints, and NULL values are allowed unless otherwise restricted.
Below are examples of how to use the UNIQUE constraint in tables for a library system that tracks authors, books, libraries, memberships, and rentals.
Using
UNIQUEConstraint in theauthorTable- In the
authortable, we may want to ensure that no two authors have the same combination offirst_nameandlast_name. This prevents duplicate author entries.
CREATE TABLE author ( author_id NUMBER GENERATED BY DEFAULT AS IDENTITY, first_name VARCHAR2(50) NOT NULL, last_name VARCHAR2(50) NOT NULL, bio CLOB, CONSTRAINT unique_author_name UNIQUE (first_name, last_name) );- In the
Defining a
UNIQUEConstraint in thebooksTable- In the
bookstable, theisbn(International Standard Book Number) must be unique, as each book is identified by its specificisbn.
CREATE TABLE books ( book_id NUMBER GENERATED BY DEFAULT AS IDENTITY, title VARCHAR2(150) NOT NULL, author_id NUMBER NOT NULL, isbn VARCHAR2(20) UNIQUE, published_date DATE, PRIMARY KEY (book_id), FOREIGN KEY (author_id) REFERENCES author(author_id) );- In the
Setting a
UNIQUEConstraint in thelibraryTable- The
branch_nameshould be unique in thelibrarytable, as no two library branches can have the same name within the system.
CREATE TABLE library ( library_id NUMBER GENERATED BY DEFAULT AS IDENTITY, branch_name VARCHAR2(100) NOT NULL UNIQUE, location VARCHAR2(100), PRIMARY KEY (library_id) );- The
Enforcing Uniqueness in the
membershipTable- For the
membershiptable, you could enforce uniqueness on the combination ofmember_nameandjoin_dateto prevent duplicate records for the same member joining on the same date.
CREATE TABLE membership ( membership_id NUMBER GENERATED BY DEFAULT AS IDENTITY, member_name VARCHAR2(100) NOT NULL, join_date DATE NOT NULL, status VARCHAR2(20) DEFAULT 'Active', CONSTRAINT unique_member_name_date UNIQUE (member_name, join_date), PRIMARY KEY (membership_id) );- For the
Applying a
UNIQUEConstraint in therentalsTable- In the
rentalstable, to avoid duplicate rental entries for the same book and member, you can add aUNIQUEconstraint onbook_idandmember_id.
CREATE TABLE rentals ( rental_id NUMBER GENERATED BY DEFAULT AS IDENTITY, book_id NUMBER NOT NULL, member_id NUMBER NOT NULL, rental_date DATE NOT NULL, return_date DATE NULL, rental_status VARCHAR2(20) DEFAULT 'Pending', CONSTRAINT unique_book_member UNIQUE (book_id, member_id), PRIMARY KEY (rental_id), FOREIGN KEY (book_id) REFERENCES books(book_id), FOREIGN KEY (member_id) REFERENCES membership(membership_id) );- In the
Additional Notes:
- Multiple
UNIQUEConstraints: A table can have multipleUNIQUEconstraints on different columns or combinations of columns. - NULL Values: Unlike a primary key, columns with a
UNIQUEconstraint can acceptNULLvalues, but only oneNULLis allowed per column. - Data Integrity: The
UNIQUEconstraint is crucial in maintaining data integrity by preventing the duplication of key information in your tables.
The UNIQUE constraint is a powerful tool for ensuring that key pieces of data remain distinct across a table, providing flexibility for both individual and composite columns.
To gain complete access, login with gmail or outlook, no need of signup. click here


Comments Not Found