DDL
用户表 (Users)
图书表 (Books)
图书类别表 (BookCategories)
图书与类别关联表 (BookCategoryRelations)
借阅记录表 (BorrowRecords)
供应商表 (Suppliers)
采购记录表 (PurchaseRecords)
CREATE TABLE Users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
email VARCHAR(100),
phone VARCHAR(20),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Books (
book_id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
author VARCHAR(100) NOT NULL,
publisher VARCHAR(100),
publication_year YEAR,
ISBN VARCHAR(13) UNIQUE,
stock_quantity INT NOT NULL DEFAULT 0
);
CREATE TABLE BookCategories (
category_id INT PRIMARY KEY AUTO_INCREMENT,
category_name VARCHAR(100) NOT NULL UNIQUE
);
CREATE TABLE `bookcategoryrelations` (
`relation_id` int(11) NOT NULL AUTO_INCREMENT,
`book_id` int(11) DEFAULT NULL,
`category_id` int(11) DEFAULT NULL,
PRIMARY KEY (`relation_id`),
UNIQUE KEY `book_id` (`book_id`,`category_id`),
KEY `bookcategoryrelations_ibfk_2` (`category_id`),
CONSTRAINT `bookcategoryrelations_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `books` (`book_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `bookcategoryrelations_ibfk_2` FOREIGN KEY (`category_id`) REFERENCES `bookcategories` (`category_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
);
CREATE TABLE BorrowRecords (
record_id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
book_id INT,
borrow_date DATE NOT NULL,
return_date DATE,
FOREIGN KEY (user_id) REFERENCES Users(user_id),
FOREIGN KEY (book_id) REFERENCES Books(book_id)
);
CREATE TABLE Suppliers (
supplier_id INT PRIMARY KEY AUTO_INCREMENT,
supplier_name VARCHAR(100) NOT NULL,
contact_name VARCHAR(100),
phone VARCHAR(20),
email VARCHAR(100)
);
CREATE TABLE `purchaserecords` (
`record_id` int(11) NOT NULL AUTO_INCREMENT,
`supplier_id` int(11) DEFAULT NULL,
`book_id` int(11) DEFAULT NULL,
`purchase_date` date NOT NULL,
`quantity` int(11) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`record_id`),
KEY `supplier_id` (`supplier_id`),
KEY `book_id` (`book_id`),
CONSTRAINT `purchaserecords_ibfk_1` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers` (`supplier_id`),
CONSTRAINT `purchaserecords_ibfk_2` FOREIGN KEY (`book_id`) REFERENCES `books` (`book_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DML
INSERT INTO Users (username, password, email, phone)
VALUES ('user1', 'pass1', 'user1@example.com', '1234567890');
INSERT INTO Users (username, password, email, phone)
VALUES ('user2', 'pass2', 'user2@example.com', '0987654321');
INSERT INTO Users (username, password, email, phone)
VALUES ('user3', 'pass3', NULL, '1112223333');
INSERT INTO Users (username, password, email, phone)
VALUES ('user4', 'pass4', 'user4@example.com', NULL);
INSERT INTO Users (username, password, email, phone)
VALUES ('user5', 'pass5', 'user5@example.com', '5556667777');
INSERT INTO Users (username, password, email, phone)
VALUES ('user6', 'pass6', NULL, '9998887777');
INSERT INTO Users (username, password, email, phone)
VALUES ('user7', 'pass7', 'user7@example.com', '7778889999');
INSERT INTO BookCategoryRelations (book_id, category_id)
VALUES (1, 1);
INSERT INTO BookCategoryRelations (book_id, category_id)
VALUES (2, 2);
INSERT INTO BookCategoryRelations (book_id, category_id)
VALUES (3, 3);
INSERT INTO BookCategoryRelations (book_id, category_id)
VALUES (4, 4);
INSERT INTO BookCategoryRelations (book_id, category_id)
VALUES (5, 5);
INSERT INTO BookCategoryRelations (book_id, category_id)
VALUES (6, 6);
INSERT INTO BookCategoryRelations (book_id, category_id)
VALUES (7, 7);
INSERT INTO Books (title, author, publisher, publication_year, ISBN, stock_quantity)
VALUES ('Book 1', 'Author 1', 'Publisher 1', 2020, '1234567890123', 10);
INSERT INTO Books (title, author, publisher, publication_year, ISBN, stock_quantity)
VALUES ('Book 2', 'Author 2', 'Publisher 2', 2019, '2345678901234', 15);
INSERT INTO Books (title, author, publisher, publication_year, ISBN, stock_quantity)
VALUES ('Book 3', 'Author 3', 'Publisher 3', 2021, '3456789012345', 20);
INSERT INTO Books (title, author, publisher, publication_year, ISBN, stock_quantity)
VALUES ('Book 4', 'Author 4', 'Publisher 4', 2018, '4567890123456', 25);
INSERT INTO Books (title, author, publisher, publication_year, ISBN, stock_quantity)
VALUES ('Book 5', 'Author 5', 'Publisher 5', 2022, '5678901234567', 30);
INSERT INTO Books (title, author, publisher, publication_year, ISBN, stock_quantity)
VALUES ('Book 6', 'Author 6', 'Publisher 6', 2023, '6789012345678', 35);
INSERT INTO Books (title, author, publisher, publication_year, ISBN, stock_quantity)
VALUES ('Book 7', 'Author 7', 'Publisher 7', 2024, '7890123456789', 40);
INSERT INTO BookCategories (category_name)
VALUES ('Category 1');
INSERT INTO BookCategories (category_name)
VALUES ('Category 2');
INSERT INTO BookCategories (category_name)
VALUES ('Category 3');
INSERT INTO BookCategories (category_name)
VALUES ('Category 4');
INSERT INTO BookCategories (category_name)
VALUES ('Category 5');
INSERT INTO BookCategories (category_name)
VALUES ('Category 6');
INSERT INTO BookCategories (category_name)
VALUES ('Category 7');
INSERT INTO BorrowRecords (user_id, book_id, borrow_date, return_date)
VALUES (1, 1, '2024-07-01', '2024-07-15');
INSERT INTO BorrowRecords (user_id, book_id, borrow_date, return_date)
VALUES (2, 2, '2024-07-05', '2024-07-20');
INSERT INTO BorrowRecords (user_id, book_id, borrow_date, return_date)
VALUES (3, 3, '2024-07-10', NULL);
INSERT INTO BorrowRecords (user_id, book_id, borrow_date, return_date)
VALUES (4, 4, '2024-07-15', NULL);
INSERT INTO BorrowRecords (user_id, book_id, borrow_date, return_date)
VALUES (5, 5, '2024-07-20', NULL);
INSERT INTO BorrowRecords (user_id, book_id, borrow_date, return_date)
VALUES (6, 6, '2024-07-25', NULL);
INSERT INTO BorrowRecords (user_id, book_id, borrow_date, return_date)
VALUES (7, 7, '2024-07-30', NULL);
INSERT INTO Suppliers (supplier_name, contact_name, phone, email)
VALUES ('Supplier 1', 'Contact 1', '1234567890', 'upplier1@example.com');
INSERT INTO Suppliers (supplier_name, contact_name, phone, email)
VALUES ('Supplier 2', 'Contact 2', '0987654321', 'upplier2@example.com');
INSERT INTO Suppliers (supplier_name, contact_name, phone, email)
VALUES ('Supplier 3', 'Contact 3', '1112223333', 'upplier3@example.com');
INSERT INTO Suppliers (supplier_name, contact_name, phone, email)
VALUES ('Supplier 4', 'Contact 4', '4445556666', 'upplier4@example.com');
INSERT INTO Suppliers (supplier_name, contact_name, phone, email)
VALUES ('Supplier 5', 'Contact 5', '7778889999', 'upplier5@example.com');
INSERT INTO Suppliers (supplier_name, contact_name, phone, email)
VALUES ('Supplier 6', 'Contact 6', '0001112222', 'upplier6@example.com');
INSERT INTO Suppliers (supplier_name, contact_name, phone, email)
VALUES ('Supplier 7', 'Contact 7', '3334445555', 'upplier7@example.com');
INSERT INTO PurchaseRecords (supplier_id, book_id, purchase_date, quantity, price)
VALUES (1, 1, '2024-06-01', 50, 20.00);
INSERT INTO PurchaseRecords (supplier_id, book_id, purchase_date, quantity, price)
VALUES (2, 2, '2024-06-05', 40, 18.00);
INSERT INTO PurchaseRecords (supplier_id, book_id, purchase_date, quantity, price)
VALUES (3, 3, '2024-06-10', 30, 25.00);
INSERT INTO PurchaseRecords (supplier_id, book_id, purchase_date, quantity, price)
VALUES (4, 4, '2024-06-15', 20, 30.00);
INSERT INTO PurchaseRecords (supplier_id, book_id, purchase_date, quantity, price)
VALUES (5, 5, '2024-06-20', 15, 22.00);
INSERT INTO PurchaseRecords (supplier_id, book_id, purchase_date, quantity, price)
VALUES (6, 6, '2024-06-25', 10, 15.00);
INSERT INTO PurchaseRecords (supplier_id, book_id, purchase_date, quantity, price)
VALUES (7, 7, '2024-06-30', 5, 10.00);