Oracle
AUTO INCREMENT
In Oracle, the concept of auto-incremented columns is typically achieved using the IDENTITY column or a combination of sequences and triggers. Auto-increment is used to generate unique values automatically for a column, often used for primary keys, ensuring that every new row inserted has a distinct identifier. The IDENTITY option simplifies this process by auto-generating a value, eliminating the need for a manual sequence or trigger setup.
Below are examples demonstrating the use of auto-increment (IDENTITY) for tables related to a library system, including authors, books, libraries, memberships, and rentals.
Using Auto Increment in the
authorTable- In the
authortable, theauthor_idis an automatically incremented identifier.
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, PRIMARY KEY (author_id) );- In the
Defining Auto Increment in the
booksTable- The
book_idin thebookstable can also be auto-incremented using theIDENTITYclause, ensuring that every book entry has a unique identifier.
CREATE TABLE books ( book_id NUMBER GENERATED BY DEFAULT AS IDENTITY, title VARCHAR2(150) NOT NULL, author_id NUMBER NOT NULL, isbn VARCHAR2(20), published_date DATE, PRIMARY KEY (book_id), FOREIGN KEY (author_id) REFERENCES author(author_id) );- The
Auto Increment for the
libraryTable- In the
librarytable, thelibrary_idis auto-incremented to uniquely identify each library branch.
CREATE TABLE library ( library_id NUMBER GENERATED BY DEFAULT AS IDENTITY, branch_name VARCHAR2(100) NOT NULL, location VARCHAR2(100), PRIMARY KEY (library_id) );- In the
Using Auto Increment in the
membershipTable- The
membership_idfor each new member in themembershiptable is automatically incremented.
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) );- The
Auto Increment in the
rentalsTable- In the
rentalstable, therental_idis auto-incremented to ensure every rental record has a unique 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, 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:
IDENTITYColumns: Starting from Oracle 12c, theIDENTITYcolumn is the simplest way to implement auto-increment functionality, removing the need for sequences and triggers.GENERATED BY DEFAULT: This option allows the user to specify a value for the column during insertion. If no value is provided, Oracle will automatically generate the next value.- Simplifies Primary Key Generation: Auto-increment makes it easier to manage unique keys, particularly for tables that require unique identifiers for each record.
By using the IDENTITY column, Oracle allows for easy auto-increment functionality, making it ideal for creating unique identifiers in a variety of tables without the need for additional configuration or manual entry.
To gain complete access, login with gmail or outlook, no need of signup. click here


Comments Not Found