Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

- - Name: Amon Ahoure - - Assignment: M 0 2 Hands - On Lab - Constructing the Database - - Date: 1 / 2

-- Name: Amon Ahoure
-- Assignment: M02 Hands-On Lab - Constructing the Database
-- Date: 1/29/2024
CREATE TABLE CUSTOMERS (
CUSTOMER_ID NUMBER(6) NOT NULL,
FIRST_NAME VARCHAR2(25) NOT NULL,
LAST_NAME VARCHAR2(25) NOT NULL,
STREET_ADDRESS VARCHAR2(50) NOT NULL,
CITY VARCHAR2(50) NOT NULL,
ZIP_CODE VARCHAR2(10) NOT NULL,
COUNTRY CHAR(50) NOT NULL,
PHONE_NUMBER VARCHAR2(25) NOT NULL,
EMAIL VARCHAR2(50) NOT NULL,
DATE_OF_BIRTH DATE,
GENDER VARCHAR2(1)
);
CREATE TABLE BOOKS (
BOOK_ID NUMBER(6) NOT NULL,
BOOK_TITLE VARCHAR2(250) NOT NULL,
BOOK_DESCRIPTION VARCHAR2(2000) NOT NULL,
BOOK_CATEGORY_CD CHAR(2) NOT NULL,
BOOK_PRICE NUMBER(8,2) NOT NULL,
BOOK_REVIEWS NUMBER(5) NOT NULL,
USER_RATING NUMBER(2,1) NOT NULL
);
CREATE TABLE BOOK_CATEGORY (
BOOK_CATEGORY_CD CHAR(2) NOT NULL,
BOOK_CATEGORY_NAME VARCHAR2(100) NOT NULL,
BOOK_CATEGORY_DESCRIPTION VARCHAR2(2000) NOT NULL
);
CREATE TABLE ORDERS (
ORDER_NBR NUMBER(12) NOT NULL,
CUSTOMER_ID NUMBER(6) NOT NULL,
ORDER_DATE TIMESTAMP NOT NULL,
ORDER_TOTAL NUMBER(8,2) NOT NULL,
SALESPERSON NUMBER(6) NOT NULL
);
CREATE TABLE ORDER_ITEMS (
ORDER_ITEM_ID NUMBER(20) NOT NULL,
ORDER_NBR NUMBER(12) NOT NULL,
BOOK_ID NUMBER(6) NOT NULL,
UNIT_PRICE NUMBER(8,2) NOT NULL,
QUANTITY NUMBER(8) NOT NULL
);
ALTER TABLE CUSTOMERS ADD CONSTRAINT PK_CUSTOMERS PRIMARY KEY (CUSTOMER_ID);
ALTER TABLE BOOKS ADD CONSTRAINT PK_BOOKS PRIMARY KEY (BOOK_ID);
ALTER TABLE BOOK_CATEGORY ADD CONSTRAINT PK_BOOK_CATEGORY PRIMARY KEY (BOOK_CATEGORY_CD);
ALTER TABLE ORDERS ADD CONSTRAINT PK_ORDERS PRIMARY KEY (ORDER_NBR);
ALTER TABLE ORDER_ITEMS ADD CONSTRAINT PK_ORDER_ITEMS PRIMARY KEY (ORDER_ITEM_ID);
-- Adding foreign key constraint to the tables
ALTER TABLE BOOKS ADD CONSTRAINT FK_BOOK_BOOK_CAT FOREIGN KEY (BOOK_CATEGORY_CD)
REFERENCES BOOK_CATEGORY (BOOK_CATEGORY_CD);
ALTER TABLE ORDERS ADD CONSTRAINT FK_ORD_CUST FOREIGN KEY (CUSTOMER_ID)
REFERENCES CUSTOMERS (CUSTOMER_ID);
ALTER TABLE ORDERS ADD CONSTRAINT FK_ORD_EMP FOREIGN KEY (SALESPERSON)
REFERENCES EMPLOYEES (EMPLOYEE_ID);
ALTER TABLE ORDER_ITEMS ADD CONSTRAINT FK_ORD_ITM_ORD FOREIGN KEY (ORDER_NBR)
REFERENCES ORDERS (ORDER_NBR);
ALTER TABLE ORDER_ITEMS ADD CONSTRAINT FK_ORD_ITM_PROD FOREIGN KEY (BOOK_ID)
REFERENCES BOOKS (BOOK_ID);
ALTER TABLE ORDERS ADD CONSTRAINT FK_ORD_EMP FOREIGN KEY (SALESPERSON_ID)
REFERENCES EMPLOYEE (EMPLOYEE_ID);
ALTER TABLE ORDER_ITEMS ADD CONSTRAINT UK_ORD_ITM UNIQUE (ORDER_NBR, BOOK_ID);
/*
CREATE TABLE ORDER_ITEMS (
ORDER_ITEM_ID NUMBER(20) NOT NULL,
ORDER_NBR NUMBER(12) NOT NULL,
BOOK_ID NUMBER(6) NOT NULL,
UNIT_PRICE NUMBER(8,2) NOT NULL,
QUANTITY NUMBER(8) NOT NULL,
CONSTRAINT PK_ORDER_ITEMS PRIMARY KEY (ORDER_ITEM_ID),
CONSTRAINT FK_ORD_ITM_ORD FOREIGN KEY (ORDER_NBR) REFERENCES ORDERS (ORDER_NBR),
CONSTRAINT FK_ORD_ITM_PROD FOREIGN KEY (BOOK_ID) REFERENCES BOOKS (BOOK_ID),
CONSTRAINT UK_ORD_ITM UNIQUE (ORDER_NBR, BOOK_ID)
);
*/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions