|
|
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`)
|
|
|
);
|