0% found this document useful (0 votes)
28 views3 pages

Book Stock

The document outlines the SQL commands to create a database named 'BookStock' with tables for Authors, Categories, Books, Customers, Orders, and OrderDetails. It includes commands for inserting sample data into these tables and retrieving information from them. The structure supports a book inventory and order management system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Book Stock

The document outlines the SQL commands to create a database named 'BookStock' with tables for Authors, Categories, Books, Customers, Orders, and OrderDetails. It includes commands for inserting sample data into these tables and retrieving information from them. The structure supports a book inventory and order management system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CREATE DATABASE BookStock;

USE BookStock;

CREATE TABLE Authors (


AuthorID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Birthdate Date
)Engine = InnoDB;

CREATE TABLE Categories (


CategoryID INT AUTO_INCREMENT PRIMARY KEY,
CategoryName VARCHAR(100) NOT NULL
)Engine = InnoDB;

CREATE TABLE Books (


BookID INT AUTO_INCREMENT PRIMARY KEY,
Title VARCHAR(200) NOT NULL,
AuthorID INT,
CategoryID INT,
Stock INT DEFAULT 0,
FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID),
FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)
)Engine = InnoDB;

CREATE TABLE Customers (


CustomerID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Email VARCHAR(100) UNIQUE NOT NULL
)Engine = InnoDB;

CREATE TABLE Orders (


OrderID INT AUTO_INCREMENT PRIMARY KEY,
CustomerID INT,
OrderDate DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
)Engine = InnoDB;

CREATE TABLE OrderDetails (


OrderDetailID INT AUTO_INCREMENT PRIMARY KEY,
OrderID INT,
BookID INT,
Quantity INT NOT NULL,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (BookID) REFERENCES Books(BookID)
)Engine = InnoDB;

Inserting Authors Information:

This code represents the authors’ information input.

INSERT INTO Authors (Name, Birthdate) VALUES


('J.K. Rowling', ‘1990-01-02’)

Inserting Categories:

This code represents the book categories input.

INSERT INTO Categories (CategoryName) VALUES


('Fantasy'),
('Science Fiction'),
('Mystery');

Inserting Books:
This code represents the book stock input.

INSERT INTO Books (Title, AuthorID, CategoryID, Stock) VALUES


('Harry Potter and the Sorcerer\'s Stone', 1, 1, 10),
('A Game of Thrones', 2, 1, 5),
('Dune', 1, 2, 3);

Inserting Customers:

This code represents the customers’ information input.

INSERT INTO Customers (Name, Email) VALUES


('Alice Smith', '[email protected]'),
('Bob Johnson', '[email protected]');

Inserting Orders:

This code represents customer orders input.


INSERT INTO Orders (CustomerID) VALUES
(1),
(2);

Inserting Order Details:

This code represents the order details input.

INSERT INTO OrderDetails (OrderID, BookID, Quantity) VALUES


(1, 1, 1),
(2, 2, 2);

Displaying Data:

This code retrieves filtered information from the database.

SELECT * FROM Authors;


SELECT * FROM Categories;
SELECT * FROM Books;
SELECT * FROM Customers;
SELECT * FROM Orders;
SELECT * FROM OrderDetails;

You might also like