Oracle
CREATE TABLE
In Oracle, the CREATE TABLE command is used to create a new table in the database. This is an essential part of the Data Definition Language (DDL), which focuses on defining and managing database structures. For beginners, understanding how to define a table and specify its columns, data types, and constraints is crucial. The following example will demonstrate how to create tables for a library system, focusing on authors, books, libraries, memberships, and rentals.
Here’s a step-by-step guide with code samples for creating these tables:
Creating the
authorTable- The
authortable will store information about authors such as their ID, name, and nationality.
CREATE TABLE author ( author_id NUMBER GENERATED BY DEFAULT AS IDENTITY, first_name VARCHAR2(50) NOT NULL, last_name VARCHAR2(50) NOT NULL, nationality VARCHAR2(50), birth_date DATE, PRIMARY KEY (author_id) );- The
Creating the
booksTable- The
bookstable will store details about books such as title, genre, publication date, and the author who wrote them.
CREATE TABLE books ( book_id NUMBER GENERATED BY DEFAULT AS IDENTITY, title VARCHAR2(100) NOT NULL, genre VARCHAR2(50), publication_date DATE, author_id NUMBER, PRIMARY KEY (book_id), FOREIGN KEY (author_id) REFERENCES author(author_id) );- The
Creating the
libraryTable- The
librarytable represents different library branches with details such as branch name and location.
CREATE TABLE library ( library_id NUMBER GENERATED BY DEFAULT AS IDENTITY, branch_name VARCHAR2(100) NOT NULL, location VARCHAR2(100), PRIMARY KEY (library_id) );- The
Creating the
membershipTable- The
membershiptable will store information about library members including their name, contact details, and join date.
CREATE TABLE membership ( membership_id NUMBER GENERATED BY DEFAULT AS IDENTITY, first_name VARCHAR2(50) NOT NULL, last_name VARCHAR2(50) NOT NULL, join_date DATE DEFAULT SYSDATE, email VARCHAR2(100), phone_number VARCHAR2(20), PRIMARY KEY (membership_id) );- The
Creating the
rentalsTable- The
rentalstable will track the borrowing of books by library members, including when a book was borrowed and returned.
CREATE TABLE rentals ( rental_id NUMBER GENERATED BY DEFAULT AS IDENTITY, book_id NUMBER NOT NULL, membership_id NUMBER NOT NULL, rental_date DATE DEFAULT SYSDATE, return_date DATE, PRIMARY KEY (rental_id), FOREIGN KEY (book_id) REFERENCES books(book_id), FOREIGN KEY (membership_id) REFERENCES membership(membership_id) );- The
Additional Notes:
- In Oracle, the
NUMBERdata type is used for numeric values, andVARCHAR2is used for variable-length strings. - The
PRIMARY KEYconstraint ensures that each row in a table can be uniquely identified. - The
FOREIGN KEYconstraint establishes a link between two tables, enforcing referential integrity.
By following these examples, students can begin creating well-structured tables that form the foundation of a relational database.
To gain complete access, login with gmail or outlook, no need of signup. click here


Comments Not Found