From 22ab81fa930c00fc51f4e68c8193658ccadad667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E7=8F=8A=E7=8F=8A?= <3106036048@qq.com> Date: Sun, 19 Feb 2023 17:27:28 +0800 Subject: [PATCH] =?UTF-8?q?2.19=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230219 mysql.md" | 394 ++++++++++++++++++ 1 file changed, 394 insertions(+) create mode 100644 "49 \345\272\204\347\217\212\347\217\212/20230219 mysql.md" diff --git "a/49 \345\272\204\347\217\212\347\217\212/20230219 mysql.md" "b/49 \345\272\204\347\217\212\347\217\212/20230219 mysql.md" new file mode 100644 index 0000000..b6a0332 --- /dev/null +++ "b/49 \345\272\204\347\217\212\347\217\212/20230219 mysql.md" @@ -0,0 +1,394 @@ +# 作业 + +## 练习1 + +```mysql +-- 1、创建数据库test01_market +create database test01_market; + +-- 2、创建表格customers +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 c_num int(11); +desc 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字段后面 +alter table customers modify c_contact varchar(50) after c_birth; + +-- **要求4:**将c_name字段数据类型改为 varchar(70) +alter table customers modify column c_name varchar(70); + +-- **要求5:**将c_contact字段改名为c_phone +alter table customers change c_contact c_phone varchar(50); + +-- **要求6:**增加c_gender字段到c_name后面,数据类型为char(1) +alter table customers add c_gender char(1) after c_name; + +-- **要求7:**将表名改为customers_info +alter table customers rename customers_info; + +-- **要求8:**删除字段c_city +alter table customers_info drop column c_city; +``` + +## 练习2 + +```mysql +-- 1、创建数据库test02_library +create database test02_library; + +-- 2、创建表格books +create table books( + b_id int(10), + authors varchar(100), + price float, + pubdate year, + note varchar(100), + num int(11) +); +alter table books add b_name varchar(50) after b_id; +desc 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) 指定所有字段名称插入第一条记录 +insert into books(b_id,b_name,authors,price,pubdate,note,num) values(1,'Tal of AAA','Dickes','23','1995','novel','11'); +select * from books; +delete from books where b_id = 1; +2)不指定字段名称插入第二记录 +insert into books values (2,'EmmaT','Jane lura','35','1993','joke','22'); +3)同时插入多条记录(剩下的所有记录) +insert into books values (3,'Story of Jane','ane 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') + +/*| 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。 +update books set price =price+5 where note ='novel'; +-- 5、将名称为EmmaT的书的价格改为40。 +update books set price=40 where b_name ='emmat' +6、删除库存为0的记录 +delete from books where num =0; +``` + +## 练习3 + +```mysql +-- 1、创建数据库test01_market +create database test01_market; + +-- 2、创建表格customers +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 c_num int(11); +desc 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字段后面 +alter table customers modify c_contact varchar(50) after c_birth; + +-- **要求4:**将c_name字段数据类型改为 varchar(70) +alter table customers modify column c_name varchar(70); + +-- **要求5:**将c_contact字段改名为c_phone +alter table customers change c_contact c_phone varchar(50); + +-- **要求6:**增加c_gender字段到c_name后面,数据类型为char(1) +alter table customers add c_gender char(1) after c_name; + +-- **要求7:**将表名改为customers_info +alter table customers rename customers_info; + +-- **要求8:**删除字段c_city +alter table customers_info drop column c_city; + + +-- 1、创建数据库test02_library +create database test02_library; + +-- 2、创建表格books +create table books( + b_id int(10), + authors varchar(100), + price float, + pubdate year, + note varchar(100), + num int(11) +); +alter table books add b_name varchar(50) after b_id; +desc 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) 指定所有字段名称插入第一条记录 +insert into books(b_id,b_name,authors,price,pubdate,note,num) values(1,'Tal of AAA','Dickes','23','1995','novel','11'); +select * from books; +delete from books where b_id = 1; +2)不指定字段名称插入第二记录 +insert into books values (2,'EmmaT','Jane lura','35','1993','joke','22'); +3)同时插入多条记录(剩下的所有记录) +insert into books values (3,'Story of Jane','ane 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') + +/*| 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。 +update books set price =price+5 where note ='novel'; +-- 5、将名称为EmmaT的书的价格改为40。 +update books set price=40 where b_name ='emmat' +6、删除库存为0的记录 +delete from books where num =0; + + + + + + + + +-- 1、创建数据库test03_bookstore +create database test03_bookstore; +desc book; +-- 2、创建book表 +create table book( + id int(11), + title varchar(100), + author varchar(100), + price double(11,2), + sales int(11), + stock int(11), + img_path varchar(100) + ); + +alter table book add primary key(id); +alter table book modify title varchar (100) not null; +alter table book modify author varchar(100) not null; +alter table book modify price double(11,2) not null; +alter table book modify sales int(11) not null; +alter table book modify stock int(11) not null; +alter table book modify img_path varchar(100) not null; +/* ++----------+--------------+------+-----+---------+----------------+ +| 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 | ++----+---------------+------------+-------+-------+-------+----------------------------+ +​```*/ +select * from book; +use class3; +insert into book values(1,'解忧杂货店','东野圭吾','27.20','102','98','upload/books/解忧杂货店.jpg'); +-- 3、创建用户表users,并插入数据 +create table users( + id int(11), + username varchar(100), + password varchar(100), + email varchar(100) +); +/* +​```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 | | ++----------+--------------+------+-----+---------+----------------+ +​``` +*/ +alter table users add primary key (id); +select * from users; +desc users; +alter table users modify password varchar(100) not null; +alter table users modify email varchar(100) null; +alter table users add unique key(username); +alter table users drop primary key where = username; +alter table users modify id int(11) not null auto_increment; +/* +--尝试添加部分模拟数据,参考示例如下: + +​```mysql ++----+----------+----------------------------------+--------------------+ +| id | username | password | email | ++----+----------+----------------------------------+--------------------+ +| 1 | admin | 112233 | admin@mxdx.com | ++----+----------+----------------------------------+--------------------+ +​``` +*/ +create table test ( + id int, + username varchar(11), + password varchar(11), + email varchar(11) + ); +alter table test modify column email varchar(20); +insert into test values(1,'admin',112233,'admin@xdx.com'); +select * from test; +/* +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 | | ++--------------+--------------+------+-----+---------+-------+ +​```*/ +create table orders( + id varchar(100) not null , + order_time datetime not null, + total_ciunt int(11) , + total_amount double(11,2) not null , + state int(11) not null , + user_id int(11) not null +); + alter table orders add primary key(id); +alter table orders add key(user_id); +desc orders; +alter table orders modify column total_ciunt int(11) not null ; +/* +尝试添加部分模拟数据,参考示例如下: + +​```mysql ++----------------+---------------------+-------------+--------------+-------+---------+ +| id | order_time | total_count | total_amount | state | user_id | ++----------------+---------------------+-------------+--------------+-------+---------+ +| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | ++----------------+---------------------+-------------+--------------+-------+---------+ +​```*/ +insert into orders(id, order_time, total_ciunt, total_amount, state, user_id) + values (15294258455691,'2018-06-20 00:30:45 ',2,50.20,0,1); +select * from orders; +-- 5、创建订单明细表order_items +create table ordedr_items ( + id int(11) not null , + count int(11) not null, + amout 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 +); +alter table ordedr_items add primary key (id); +alter table ordedr_items add key (order_id); +desc ordedr_items; +alter table ordedr_items modify id int(11) not null auto_increment; + /*```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 | | ++----------+--------------+------+-----+---------+----------------+ +​``` + +尝试添加部分模拟数据,参考示例如下:*/ +insert into ordedr_items values (1,1,27.20,'解忧杂货店','东野圭吾','27.20','static/img/default.jpg','15294258455691 '); + (2,1,23.00,'边城','沈从文',23.00.'static/img/default.jpg','15294258455691'); +/* +​```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 | ++----+-------+--------+------------+----------+-------+------------+----------------+ +​``` +``` + -- Gitee