Create The Database

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

-- Create the database

CREATE DATABASE OnlineBookstore;

-- Use the database

USE OnlineBookstore;

-- Create the "Books" table

CREATE TABLE Books (

BookID INT PRIMARY KEY,

Title VARCHAR(100),

Author VARCHAR(100),

Price DECIMAL(8, 2)

);

-- Create the "Customers" table

CREATE TABLE Customers (

CustomerID INT PRIMARY KEY,

Name VARCHAR(100),

Email VARCHAR(100)

);

-- Create the "Orders" table

CREATE TABLE Orders (

OrderID INT PRIMARY KEY,

CustomerID INT,

OrderDate DATE,

FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)

);

-- Create the "OrderItems" table

CREATE TABLE OrderItems (

OrderItemID INT PRIMARY KEY,

OrderID INT,

BookID INT,

Quantity INT,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),

FOREIGN KEY (BookID) REFERENCES Books(BookID)

);

-- Insert data into the "Books" table

INSERT INTO Books (BookID, Title, Author, Price)

VALUES

(1, 'The Great Gatsby', 'F. Scott Fitzgerald', 9.99),

(2, 'To Kill a Mockingbird', 'Harper Lee', 8.99),

(3, 'Pride and Prejudice', 'Jane Austen', 7.99);

-- Insert data into the "Customers" table

INSERT INTO Customers (CustomerID, Name, Email)

VALUES

(1, 'John Smith', 'john.smith@example.com'),

(2, 'Alice Johnson', 'alice.johnson@example.com');

-- Insert data into the "Orders" table

INSERT INTO Orders (OrderID, CustomerID, OrderDate)

VALUES

(1, 1, '2024-02-15'),

(2, 2, '2024-02-18');

-- Insert data into the "OrderItems" table

INSERT INTO OrderItems (OrderItemID, OrderID, BookID, Quantity)

VALUES

(1, 1, 1, 2),

(2, 1, 2, 1),

(3, 2, 3, 3);

You might also like