Oracle
DEFAULT Constraint
In Oracle, the DEFAULT constraint is used to automatically assign a default value to a column if no value is provided during an INSERT operation. This is useful when you want to ensure that specific fields have a default value, avoiding NULL values or providing common defaults for optional fields. The DEFAULT constraint helps to maintain data consistency without requiring the user to specify values for every column when adding records.
Below are examples of how to use the DEFAULT constraint for tables designed for a library system.
Adding a
DEFAULTConstraint to theauthorTable- Let’s assume that for each new author, if no value is provided for the
biocolumn, a default message is inserted.
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 DEFAULT 'Bio not available', PRIMARY KEY (author_id) );- Let’s assume that for each new author, if no value is provided for the
Defining a
DEFAULTValue in thebooksTable- In the
bookstable, thepublished_datecolumn could default to the current date if no value is provided, indicating the book was published on 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 DEFAULT SYSDATE, PRIMARY KEY (book_id), FOREIGN KEY (author_id) REFERENCES author(author_id) );- In the
Using a
DEFAULTConstraint in thelibraryTable- For the
librarytable, if no specific location is provided for a new library branch, it defaults to ‘Unknown Location’.
CREATE TABLE library ( library_id NUMBER GENERATED BY DEFAULT AS IDENTITY, branch_name VARCHAR2(100) NOT NULL, location VARCHAR2(100) DEFAULT 'Unknown Location', PRIMARY KEY (library_id) );- For the
Setting Default Values for Memberships
- In the
membershiptable, thejoin_datecould default to the current date, and thestatusof the membership could default to ‘Active’.
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) DEFAULT 'Active', PRIMARY KEY (membership_id) );- In the
Using
DEFAULTin therentalsTable- In the
rentalstable, you could set a default value for therental_statuscolumn to ‘Pending’ when a new rental record is inserted.
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', 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
NULLvalues in important columns. - Flexibility: The
DEFAULTconstraint allows tables to be flexible and reduces the burden on users to input every column manually.
The DEFAULT constraint 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