①创建Student表
mysql> create table Student (
-> Sno int primary key,
-> Sname varchar(255),
-> Ssex varchar(10),
-> Sdept varchar(50)
-> );
Query OK, 0 rows affected (0.01 sec)
②创建Course表
mysql> create table Course (
-> Cno int primary key,
-> Cname varchar(255),
-> Tno int
-> );
Query OK, 0 rows affected (0.01 sec)
③创建Score表
mysql> create table Score (
-> Sno int,
-> Cno int,
-> Degree int,
-> primary key (Sno,Cno)
-> );
Query OK, 0 rows affected (0.01 sec)
④创建Teacher表
mysql> create table Teacher (
-> Tno int primary key,
-> Tname varchar(255),
-> Tsex varchar(10),
-> Prof varchar(50)
-> );
Query OK, 0 rows affected (0.00 sec)
①命令
mysql> insert into Student values(1,'张三','男','计科2003班');
Query OK, 1 row affected (0.01 sec)
②结果
#方法一
mysql> select s.Sname
-> from Student s
-> inner join Score SC on s.Sno = SC.Sno
-> inner join Course c on SC.Cno = c.Cno
-> where c.Cname = '数据库' and SC.Degree < 60;
Empty set (0.01 sec)
#方法二
mysql> select s.Sname,SC.Degree
-> from Student s
-> inner join Score SC on s.Sno = SC.Sno
-> inner join Course c on SC.Cno = c.Cno
-> where c.Cname = '数据库' and SC.Degree < 60;
Empty set (0.00 sec)
#方法三
mysql> select s.Sname, SC.Degree, C.Cname
-> from Student s
-> inner join Score SC on s.Sno = SC.Sno
-> inner join Course C on SC.Cno = C.Cno
-> where C.Cname = '数据库' and SC.Degree < 60;
Empty set (0.00 sec)
mysql> update Score SC
-> set Degree = Degree + 5
-> where SC.Sno in (select s.Sno from Student s where s.Ssex = '女')
-> and SC.Cno in (select c.Cno from Course c where c.Cname = '高等数学');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> delete from Score
-> where Sno = 1
-> and Cno in (select c.Cno from Course c where c.Cname = '数据库');
Query OK, 0 rows affected (0.01 sec)
①命令
mysql> DROP TABLE IF EXISTS tb_user_log;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE tb_user_log (
-> id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
-> uid INT NOT NULL COMMENT '用户ID',
-> artical_id INT NOT NULL COMMENT '视频ID',
-> in_time datetime COMMENT '进入时间',
-> out_time datetime COMMENT '离开时间',
-> sign_in TINYINT DEFAULT 0 COMMENT '是否签到'
-> ) CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> INSERT INTO tb_user_log(uid, artical_id, in_time, out_time, sign_in)
-> VALUES
-> (101,0,'2021-11-01 10:00:00','2021-11-01 10:00:42',1),
-> (102,9001,'2021-11-01 10:00:00','2021-11-01 10:00:09',0),
-> (103,9001,'2021-11-01 10:00:01','2021-11-01 10:01:50',0),
-> (101,9002,'2021-11-02 10:00:09','2021-11-02 10:00:28',0),
-> (103,9002,'2021-11-02 10:00:51','2021-11-02 10:00:59',0),
-> (104,9001,'2021-11-02 10:00:28','2021-11-02 10:00:50',0),
-> (101,9003,'2021-11-03 11:00:55','2021-11-03 11:01:24',0),
-> (104,9003,'2021-11-03 11:00:45','2021-11-03 11:00:55',0),
-> (105,9003,'2021-11-03 11:00:53','2021-11-03 11:00:59',0),
-> (101,9002,'2021-11-04 11:00:55','2021-11-04 11:00:59',0);
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
②结果
①命令
mysql> DROP TABLE IF EXISTS `Orderltems`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `Orderltems` (
-> prod_id VARCHAR(255) NOT NULL COMMENT '商品号',
-> order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
-> quantity INT(255) NOT NULL COMMENT '商品数量'
-> );
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> INSERT `Orderltems` VALUES('BRO1','a','105'),('BRO2','a2','1100'),('BR02','a2','200'),('BR04','a4','1121'),('BR017','a5','10'),('BR02','a2','19'),('BR017','a','75');
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
②结果
①命令
mysql> DROP TABLE IF EXISTS `Products`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `Products` (
-> `prod_id` VARCHAR(255) NOT NULL COMMENT '产品ID',
-> `prod_name` VARCHAR(255) NOT NULL COMMENT '产品名称'
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO `Products` VALUES ('a0001','egg'),
-> ('a0002','sockets'),
-> ('a0013','coffee'),
-> ('a0003','cola');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> DROP TABLE IF EXISTS `Orderltems`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `Orderltems` (
-> prod_id VARCHAR(255) NOT NULL COMMENT '产品id',
-> quantity INT(16) NOT NULL COMMENT '商品数量'
-> );
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> INSERT INTO `Orderltems` VALUES ('a0001',105),('a0002',1100),('a0002',200),('a0013',1121),('a0003',10),('a0003',19),('a0003',5);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
②结果
①命令
mysql> DROP TABLE IF EXISTS `Customers`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `Customers` (
-> cust_id VARCHAR(255) NOT NULL COMMENT '客户id',
-> cust_name VARCHAR(255) NOT NULL COMMENT '客户姓名'
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT `Customers` VALUES ('cust10','andy'),('cust1','ben'),('cust2' ,'tony'),('cust22','tom'),('cust221','an'),('cust2217','hex'),('cust40','ace');
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> DROP TABLE IF EXISTS `Orders`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `Orders` (
-> order_num VARCHAR(255) NOT NULL COMMENT '商品单号',
-> cust_id VARCHAR(255) NOT NULL COMMENT '顾客id'
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT `Orders` VALUES ('a1','cust10'),('a2','cust1'),('a3','cust2'),('a4','cust22'),('a5','cust221'),('a7','cust22171');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
②结果
①命令
mysql> DROP TABLE IF EXISTS tb_user_event;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE tb_user_event (
-> id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
-> uid INT NOT NULL COMMENT '用户ID',
-> product_id INT NOT NULL COMMENT '商品ID',
-> event_time datetime COMMENT '行为时间',
-> if_click TINYINT COMMENT '是否点击',
-> if_cart TINYINT COMMENT '是否加购物车',
-> if_payment TINYINT COMMENT '是否付款',
-> if_refund TINYINT COMMENT '是否退货退款'
-> ) CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> INSERT INTO tb_user_event (uid, product_id, event_time, if_click, if_cart, if_payment, if_refund) VALUES
-> (101,8001,'2021-10-01 10:00:00',0,0,0,0),
-> (102,8001,'2021-10-01 10:00:00',1,0,0,0),
-> (103,8001,'2021-10-01 10:00:00',1,1,0,0),
-> (104,8001,'2021-10-02 10:00:00',1,1,1,0),
-> (105,8001,'2021-10-02 10:00:00',1,1,1,0),
-> (101,8002,'2021-10-03 10:00:00',1,1,1,0),
-> (109,8001,'2021-10-04 10:00:00',1,1,1,1);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
②结果