You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookStore/sql/bookstore.sql

107 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

DROP DATABASE IF EXISTS book; ##
CREATE DATABASE book DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ##
USE book; ##
##t_user
CREATE TABLE t_user(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL UNIQUE,
`password` VARCHAR(32) NOT NULL,
`email` VARCHAR(50),
`address` VARCHAR(200)
);
##
INSERT INTO t_user(username,`password`,email,address) VALUES('admin','admin','admin@admin.com','湖北武汉');
##
SELECT * FROM t_user;
-- 创建名为t_book的表
CREATE TABLE t_book(
-- 定义名为id的字段数据类型为INT整数类型将其设置为主键并且设置为自增AUTO_INCREMENT这样每插入一条新记录时该字段的值会自动按顺序递增用于为每本图书生成唯一的标识符
`id` INT PRIMARY KEY AUTO_INCREMENT,
-- 定义名为name的字段数据类型为VARCHAR可变长度字符串长度为100用于存储图书的书名信息
`name` VARCHAR(100),
-- 定义名为price的字段数据类型为DECIMAL精确数值类型总长度为11位其中小数部分占2位用于存储图书的价格信息
`price` DECIMAL(11,2),
-- 定义名为author的字段数据类型为VARCHAR长度为100用来记录图书的作者姓名
`author` VARCHAR(100),
-- 定义名为classification的字段数据类型为VARCHAR长度为20可用于存储图书的分类信息例如文学、科技等类别
`classification` VARCHAR(20),
-- 定义名为sales的字段数据类型为INT用于记录图书的销量情况
`sales` INT,
-- 定义名为stock的字段数据类型为INT用于表示图书的库存数量
`stock` INT,
-- 定义名为imgpath的字段数据类型为VARCHAR长度为200可用于存储图书封面图片的路径信息方便在应用中展示图书封面图片
`imgpath` VARCHAR(200)
);
-- 向t_book表中插入一条图书数据记录指定要插入值的字段为name, author, classification, price, sales, stock, imgpath并对应给出具体的值分别为图书的书名、作者、分类、价格、销量、库存以及封面图片路径
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('解忧杂货店','东野圭吾','文学',27.20,100,100,'static/img/default.jpg');
-- 同样是向t_book表中插入图书数据记录以下每条INSERT语句的结构和作用与上面类似只是对应不同的图书信息
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('边城','沈从文','文学',23.00,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('中国哲学史','冯友兰','文学',44.5,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('苏东坡传','林语堂','文学',19.30,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('百年孤独','马尔克斯','文学',29.50,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('扶桑','严歌苓','文学',19.8,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('给孩子的诗','北岛','文学',22.20,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('为奴十二年','所罗门','文学',16.5,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('平凡的世界','路遥','文学',55.00,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('悟空传','今何在','文学',14.00,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('硬派健身','斌卡','文学',31.20,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('从晚清到民国','唐德刚','文学',39.90,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('三体','刘慈欣','文学',56.5,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('看见','柴静','文学',19.50,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('活着','余华','文学',11.00,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('小王子','安托万','文学',19.20,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('我们仨','杨绛','文学',11.30,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification,price, sales, stock, imgpath) VALUES('生命不息,折腾不止','罗永浩','文学',25.20,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification,price, sales, stock, imgpath) VALUES('皮囊','蔡崇达','文学',23.90,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('恰到好处的幸福','毕淑敏','文学',16.40,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('大数据预测','埃里克','文学',37.20,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('人月神话','布鲁克斯','文学',55.90,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('C语言入门经典','霍尔顿','文学',45.00,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('数学之美','吴军','文学',29.90,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('Java编程思想','埃史尔','文学',70.50,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('设计模式之禅','秦小波','文学',20.20,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('图解机器学习','杉山将','文学',33.80,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('艾伦图灵传','安德鲁','文学',47.20,100,100,'static/img/default.jpg');
INSERT INTO t_book (name, author, classification, price, sales, stock, imgpath) VALUES('教父','马里奥普佐','文学',29.00,100,100,'static/img/default.jpg');
-- 创建名为t_order的表
CREATE TABLE t_order(
-- 定义名为order_id的字段数据类型为VARCHAR可变长度字符串长度为50并且将该字段设置为主键用于唯一标识每条订单记录
`order_id` VARCHAR(50) PRIMARY KEY,
-- 定义名为create_time的字段数据类型为DATETIME用于存储日期和时间信息用来记录订单创建的时间
`create_time` DATETIME,
-- 定义名为price的字段数据类型为DECIMAL精确数值类型总长度为11位其中小数部分占2位用于存储订单的价格信息
`price` DECIMAL(11,2),
-- 定义名为status的字段数据类型为INT整数类型可能用于表示订单的状态比如不同的数值对应不同的订单处理阶段等情况
`status` INT,
-- 定义名为user_id的字段数据类型为INT该字段将作为外键与另一个表t_user中的id字段建立关联关系
`user_id` INT,
-- 使用FOREIGN KEY关键字为user_id字段添加外键约束指定它参照t_user表中的id字段意味着t_order表中的user_id值必须在t_user表的id字段值中存在以此建立起两张表之间的关联通常表示订单所属的用户
FOREIGN KEY(`user_id`) REFERENCES t_user(`id`)
);
-- 创建名为t_order_item的表
CREATE TABLE t_order_item(
-- 定义名为id的字段数据类型为INT整数类型并将该字段设置为主键同时设置为自增AUTO_INCREMENT这样每当插入一条新记录时该字段的值会自动按顺序递增常用于为每条记录生成唯一的标识符
`id` INT PRIMARY KEY AUTO_INCREMENT,
-- 定义名为name的字段数据类型为VARCHAR长度为100可用于存储商品名称等相关信息比如订单中具体商品的名字
`name` VARCHAR(100),
-- 定义名为count的字段数据类型为INT用于记录商品的数量比如某个商品在该订单中的购买数量
`count` INT,
-- 定义名为price的字段数据类型为DECIMAL总长度11位小数部分占2位可能用于存储单个商品的价格信息
`price` DECIMAL(11,2),
-- 定义名为total_price的字段数据类型为DECIMAL总长度11位小数部分占2位用于存储该商品的总价可能是通过商品单价乘以数量计算得出
`total_price` DECIMAL(11,2),
-- 定义名为order_id的字段数据类型为VARCHAR长度为50该字段将作为外键与t_order表中的order_id字段建立关联关系
`order_id` VARCHAR(50),
-- 使用FOREIGN KEY关键字为order_id字段添加外键约束指定它参照t_order表中的order_id字段意味着t_order_item表中的order_id值必须在t_order表的order_id字段值中存在以此建立起两张表之间的关联表明该订单项所属的订单
FOREIGN KEY(`order_id`) REFERENCES t_order(`order_id`)
);