Oracle
NUll vs NOT NULL
In Oracle, columns in a table can be defined as either NULL or NOT NULL, which determines whether a column can store NULL (empty or unknown) values. By default, a column allows NULL values unless explicitly marked as NOT NULL. Using NOT NULL is useful when you want to enforce that a certain field must always contain data, while NULL is useful for optional fields. Understanding how and when to use NULL and NOT NULL is essential for maintaining data integrity.
Below are examples of how to use NULL and NOT NULL with tables designed for a library system.
Defining a Column as
NOT NULLin theauthorTable- The
first_nameandlast_namecolumns must always have values when creating or updating rows in theauthortable, so they are markedNOT NULL.
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 );- The
Allowing
NULLValues in thebooksTable- Some fields like
isbn(International Standard Book Number) might be optional, so they can be defined asNULLto indicate that these values are not required.
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, PRIMARY KEY (book_id), FOREIGN KEY (author_id) REFERENCES author(author_id) );- Some fields like
Using
NOT NULLin thelibraryTable- The
branch_nameis a required field and should never be left empty, while thelocationcan be left asNULL.
CREATE TABLE library ( library_id NUMBER GENERATED BY DEFAULT AS IDENTITY, branch_name VARCHAR2(100) NOT NULL, location VARCHAR2(100) NULL, PRIMARY KEY (library_id) );- The
Defining
NOT NULLConstraints in themembershipTable- In this case, the
member_nameandjoin_dateare required fields and cannot beNULL, butpreferencescan be leftNULLif not provided.
CREATE TABLE membership ( membership_id NUMBER GENERATED BY DEFAULT AS IDENTITY, member_name VARCHAR2(100) NOT NULL, join_date DATE NOT NULL, preferences CLOB NULL, PRIMARY KEY (membership_id) );- In this case, the
Handling
NULLin therentalsTable- For the
rentalstable, you might want to allowreturn_dateto beNULLif the book has not been returned yet.
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, PRIMARY KEY (rental_id), FOREIGN KEY (book_id) REFERENCES books(book_id), FOREIGN KEY (member_id) REFERENCES membership(membership_id) );- For the
Additional Notes:
- NULL: A column defined as
NULLallows empty or unknown values, which can be useful for optional data. - NOT NULL: A column defined as
NOT NULLensures that a value must always be provided duringINSERTorUPDATEoperations. - Data Integrity: Using
NOT NULLconstraints helps enforce data integrity by ensuring that important fields are always filled.
The NULL vs NOT NULL constraints are simple yet powerful tools for controlling the presence of data in your tables, allowing flexibility where needed and ensuring consistency where required.
To gain complete access, login with gmail or outlook, no need of signup. click here


Comments Not Found