Oracle
CHECK Constraint
In Oracle, the CHECK constraint is used to ensure that all values in a column satisfy a specific condition. This is useful when you want to enforce domain integrity by limiting the values that can be stored in a column. The CHECK constraint helps to maintain data consistency by rejecting values that evaluate to false for the specified condition.
Below are examples of how to use the CHECK constraint for tables designed for a library system.
Adding a
CHECKConstraint to theauthorTable- Let’s assume that for each new author, we want to ensure their age is at least 18 before they are added to the library system.
CREATE TABLE author ( author_id NUMBER GENERATED BY DEFAULT AS IDENTITY, first_name VARCHAR2(50) NOT NULL, last_name VARCHAR2(50) NOT NULL, age NUMBER CHECK (age >= 18), bio CLOB, PRIMARY KEY (author_id) );Defining a
CHECKConstraint in thebooksTable- In the
bookstable, thepublished_datecolumn could have aCHECKconstraint to ensure that the date is not in the future, meaningpublished_datecannot be greater than today's date.
CREATE TABLE books ( book_id NUMBER GENERATED BY DEFAULT AS IDENTITY, title VARCHAR2(150) NOT NULL, author_id NUMBER NOT NULL, isbn VARCHAR2(20) NULL, published_date DATE CHECK (published_date <= SYSDATE), PRIMARY KEY (book_id), FOREIGN KEY (author_id) REFERENCES author(author_id) );- In the
Using a
CHECKConstraint in thelibraryTable- For the
librarytable, you can check that thelocationis one of the valid regions, such as 'North', 'South', 'East', or 'West'.
CREATE TABLE library ( library_id NUMBER GENERATED BY DEFAULT AS IDENTITY, branch_name VARCHAR2(100) NOT NULL, location VARCHAR2(100) CHECK (location IN ('North', 'South', 'East', 'West')), PRIMARY KEY (library_id) );- For the
Setting
CHECKConstraints for Memberships- In the
membershiptable, thestatusof the membership can be restricted to specific values like 'Active', 'Inactive', or 'Suspended'.
CREATE TABLE membership ( membership_id NUMBER GENERATED BY DEFAULT AS IDENTITY, member_name VARCHAR2(100) NOT NULL, join_date DATE DEFAULT SYSDATE, status VARCHAR2(20) CHECK (status IN ('Active', 'Inactive', 'Suspended')), PRIMARY KEY (membership_id) );- In the
Using
CHECKin therentalsTable- In the
rentalstable, you could set aCHECKconstraint for therental_statuscolumn to ensure it only accepts 'Pending', 'Returned', or 'Overdue'.
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) CHECK (rental_status IN ('Pending', 'Returned', 'Overdue')), PRIMARY KEY (rental_id), FOREIGN KEY (book_id) REFERENCES books(book_id), FOREIGN KEY (member_id) REFERENCES membership(membership_id) );- In the
Additional Notes:
- Automatic Values: The
DEFAULTconstraint is useful for assigning automatic values to columns when no specific data is provided during anINSERT. - Data Integrity: By setting default values, you help maintain data consistency and avoid unnecessary NULL values in important columns.
- Flexibility:
DEFAULTconstraint allows tables to be flexible and reduces the burden on users to input every column manually.
The DEFAULTconstraint is a handy tool for streamlining data entry and ensuring that key columns always have valid values, even if some are not explicitly provided by users.
To gain complete access, login with gmail or outlook, no need of signup. click here


Comments Not Found