diff --git "a/20230215 MySQL\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/20230215 MySQL\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..75f50cf7157d2dee504c6cc134a354e69482c6aa --- /dev/null +++ "b/20230215 MySQL\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,13 @@ +use test03_bookstore +create table class3( + stu_id int primary key, + stu_name varchar(5) not null, + stu_gender enum(' 男','女') not null, + stu_hobby set('崩','撤','卖','溜'), + stu_address varchar(20) not null, + stu_mail varchar(20) not null, + stu_QQ int not null, + stu_contact int not null + )default charset=utf8; + insert into class3 values(22443103,'坤',1,'崩','木叶村','123465@qq.com',20551,11); + select * FROM class3; \ No newline at end of file diff --git "a/20230218 MySQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" "b/20230218 MySQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..3f35ee7dcefd17647bfd1d11dbf60d9d33a71781 --- /dev/null +++ "b/20230218 MySQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,276 @@ +## 第1题 + +1、创建数据库test01_market + +2、创建表格customers + +| 字段名 | 数据类型 | +| --------- | ----------- | +| c_num | int(11) | +| c_name | varchar(50) | +| c_contact | varchar(50) | +| c_city | varchar(50) | +| c_birth | date | + +**要求3:**将c_contact字段移动到c_birth字段后面 + +**要求4:**将c_name字段数据类型改为 varchar(70) + +**要求5:**将c_contact字段改名为c_phone + +**要求6:**增加c_gender字段到c_name后面,数据类型为char(1) + +**要求7:**将表名改为customers_info + +**要求8:**删除字段c_city + +```mysql +create DATABASE test01_market +use test01_market + create table customers( + + c_num int, + c_name VARCHAR(50), + c_contact VARCHAR(50), + c_city VARCHAR(50), + c_birth DATE + ); +alter table customers modify column c_contact VARCHAR(50) after c_birth; +alter table customers modify column c_name varchar(70); +alter table customers change column c_contact c_phone VARCHAR(50); +alter table customers add column c_gender char(1) after c_name; +alter table customers rename customers_info; +alter table customers_info drop COLUMN c_city; +``` + +## 第2题 + +1、创建数据库test02_library + +2、创建表格books + +| 字段名 | 字段说明 | 数据类型 | 允许为空 | 唯一 | +| ------- | -------- | ------------- | -------- | ---- | +| b_id | 书编号 | int(11) | 否 | 是 | +| b_name | 书名 | varchar(50) | 否 | 否 | +| authors | 作者 | varchar(100) | 否 | 否 | +| price | 价格 | float | 否 | 否 | +| pubdate | 出版日期 | year | 否 | 否 | +| note | 说明 | varchar(100) | 是 | 否 | +| num | 库存 | int(11) | 否 | 否 | + +3、向books表中插入记录 + +1) 指定所有字段名称插入第一条记录 + +2)不指定字段名称插入第二记录 + +3)同时插入多条记录(剩下的所有记录) + +| b_id | b_name | authors | price | pubdate | note | num | +| ---- | ------------- | --------------- | ----- | ------- | -------- | ---- | +| 1 | Tal of AAA | Dickes | 23 | 1995 | novel | 11 | +| 2 | EmmaT | Jane lura | 35 | 1993 | joke | 22 | +| 3 | Story of Jane | Jane Tim | 40 | 2001 | novel | 0 | +| 4 | Lovey Day | George Byron | 20 | 2005 | novel | 30 | +| 5 | Old land | Honore Blade | 30 | 2010 | law | 0 | +| 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | +| 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | + +4、将小说类型(novel)的书的价格都增加5。 + +5、将名称为EmmaT的书的价格改为40。 + +6、删除库存为0的记录 + +```mysql +create database test02_library; +use test02_library; +create table books( + b_id int(11) UNIQUE, + b_name varchar(50), + authors varchar(100), + price float, + pubdate year, + note varchar(100) default null, + num int(11) +); +insert into books values(1,'Tal of AAA','Dickes',23,'1919','novel',11); +SELECT * FROM books; +insert into books(b_id,b_name,authors,price,pubdate,note,num) values(2,'EmmaT','Jane lura',35,'1993','joke',22); +SELECT * FROM books; +insert into books values(3,'Story of Jane','Jane Tim',40,'2001','novel',0); +SELECT * FROM books; +insert into books values(4,'Lovey Day','George Byron',20,'2005','novel3',30),(5,'Old land','Honore Blade',30,'2010','law',0), +(6,'The Battle','Upton Sara',30,'1999','medicine',40),(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); +SELECT * FROM books; +update books set price = price+5 where note = 'novel'; +SELECT * FROM books; +update books set price = 40 where b_name = 'EmmaT' +SELECT * FROM books; +delete from books where num = 0 +SELECT * FROM books; +``` + + + +## 第3题 + +1、创建数据库test03_bookstore + +2、创建book表 + +```mysql ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| title | varchar(100) | NO | | NULL | | +| author | varchar(100) | NO | | NULL | | +| price | double(11,2) | NO | | NULL | | +| sales | int(11) | NO | | NULL | | +| stock | int(11) | NO | | NULL | | +| img_path | varchar(100) | NO | | NULL | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----+-------------+------------+-------+-------+-------+----------------------------+ +| id | title | author | price | sales | stock | img_path | ++----+-------------+------------+-------+-------+-------+-----------------------------+ +| 1 | 解忧杂货店 | 东野圭吾 | 27.20 | 102 | 98 | upload/books/解忧杂货店.jpg | +| 2 | 边城 | 沈从文 | 23.00 | 102 | 98 | upload/books/边城.jpg | ++----+---------------+------------+-------+-------+-------+----------------------------+ +``` + +3、创建用户表users,并插入数据 + +```mysql ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| username | varchar(100) | NO | UNI | NULL | | +| password | varchar(100) | NO | | NULL | | +| email | varchar(100) | YES | | NULL | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----+----------+----------------------------------+--------------------+ +| id | username | password | email | ++----+----------+----------------------------------+--------------------+ +| 1 | admin | 112233 | admin@mxdx.com | ++----+----------+----------------------------------+--------------------+ +``` + +4、创建订单表orders + +```mysql ++--------------+--------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++--------------+--------------+------+-----+---------+-------+ +| id | varchar(100) | NO | PRI | NULL | | +| order_time | datetime | NO | | NULL | | +| total_count | int(11) | NO | | NULL | | +| total_amount | double(11,2) | NO | | NULL | | +| state | int(11) | NO | | NULL | | +| user_id | int(11) | NO | MUL | NULL | | ++--------------+--------------+------+-----+---------+-------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----------------+---------------------+-------------+--------------+-------+---------+ +| id | order_time | total_count | total_amount | state | user_id | ++----------------+---------------------+-------------+--------------+-------+---------+ +| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | ++----------------+---------------------+-------------+--------------+-------+---------+ +``` + +5、创建订单明细表order_items + +```mysql ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| count | int(11) | NO | | NULL | | +| amount | double(11,2) | NO | | NULL | | +| title | varchar(100) | NO | | NULL | | +| author | varchar(100) | NO | | NULL | | +| price | double(11,2) | NO | | NULL | | +| img_path | varchar(100) | NO | | NULL | | +| order_id | varchar(100) | NO | MUL | NULL | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----+-------+--------+---------+---------+-------+----------------+----------------+ +| id |count| amount| title | author | price | img_path | order_id | ++----+-------+--------+------------+----------+-------+----------------+----------------+ +| 1 | 1 | 27.20| 解忧杂货店 | 东野圭吾 | 27.20 | static/img/default.jpg|15294258455691 | +| 2 | 1 | 23.00| 边城 | 沈从文 | 23.00 | static/img/default.jpg|15294258455691 | ++----+-------+--------+------------+----------+-------+------------+----------------+ +``` + +参考答案: + +```mysql +use test03_bookstore +create table book( + b_id int(11) not NULL auto_increment PRIMARY key , + title varchar(100) not null, + author varchar(100) not null, + price double(11,2) not null, + sales int(11) not null, + stock int(11) not null, + img_path varchar(100) not null +); +desc book; +insert into book values(1,'解忧杂货店','东野圭吾',27.2,102,98,'解忧杂货店.jpg'),(2,'边城','沈从文 ',23,102,98,'边城.jpg '); +SELECT * FROM book; +create table users( + b_id int(11) not NULL auto_increment PRIMARY key, + username varchar(100) not NULL UNIQUE KEY, + password varchar(100) not NULL, + email varchar(100) default null +); +desc users; +insert into users(b_id,username,password,email) values(1,'admin',112233,'admin@mxdx.com'); +SELECT * FROM users; + +create table orders( +id varchar(100) not NULL PRIMARY key, +order_time datetime not null, +total_count int(11) not NULL, +otal_amount double(11,2) not null, +state int(11) not null, +user_id int(11) not null +); +desc orders; +insert into orders(id,order_time,total_count,otal_amount,state,user_id) values('15294258455691','2018-06-20 00:30:45',2,50.20,0,1); +SELECT * FROM orders; +create table order_items( +id int(11) not null auto_increment PRIMARY key, +count int(11) not null, +amount double(11,2) not null, +title varchar(100) not null, +author varchar(100) not null, +price double(11,2) not null, +img_path varchar(100) not null, +order_id varchar(100) not null +); +SELECT * FROM order_items; +insert into order_items VALUES( +1,1,27.20,'解忧杂货店','东野圭吾',27.20,'default.jpg','15294258455691'),(2,1,23.00,'边城','沈从文',23.00,'default.jpg','15294258455691'); +SELECT * FROM order_items; +``` +