diff --git "a/16 \346\236\227\346\210\220\351\270\277/2023021811\343\200\201DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.md" "b/16 \346\236\227\346\210\220\351\270\277/2023021811\343\200\201DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..292c7be95f372fa7b81bd5e288e43cce7c29dfd3 --- /dev/null +++ "b/16 \346\236\227\346\210\220\351\270\277/2023021811\343\200\201DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.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_bih字段后面 + +**要求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 c_contact varchar(50) after c_birth; +desc customers; + +alter table customers modify c_name varchar(70); + +alter table customers change c_contact c_phone varchar(50); + +alter table customers add c_gender char(1) after c_name; + +alter table customers rename TO customers_info; + +DESC customers_info; + +ALTER table customers_info drop 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 default charset utf8; +use test02_library; +create table books( + b_id int(11) not null unique auto_increment comment '书编号', + b_name varchar(50) not null comment '书名', + authors varchar(100) not null comment '作者', + price float not null comment '价格', + pubdate year not null comment '出版日期', + note varchar(100) comment '说明', + num int(11) not null comment '库存' +); +insert into books (b_id, b_name, authors, price, pubdate, note, num) values (1,'Tal of AAA','Dickes',23,1995,'novel',11); +desc books; +show create table books; +insert into books values (2,'EmmaT','Jane lura',35,1993,'joke',22); +insert into books values (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); +update books set price=+5 where note='novel'; +update books set price =40 where b_name ='EmmaT'; +delete from books where num=0; +``` + + + +## 第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 +create database test03_bookstore default charset utf8; +use test03_bookstore; +create table book( + id int(11) not null primary key auto_increment , + 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 +); +insert into book values (1,'解忧杂货店','东野圭吾',27.20,102,98,'upload/books/解忧杂货店.jpg'), + (2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); +create table users( + id int(11) not null primary key auto_increment, + username varchar(100) not null unique , + password varchar(100) not null , + email varchar(100) +) comment '用户表'; +insert into users values (1,'admin','112233','admin@mxdx.com'); +create table orders( + id varchar(100) not null primary key , + order_time datetime not null , + total_count int(11) not null , + total_amount double(11,2) not null , + state int(11) not null , + user_id int(11) not null +); +alter table orders add index(user_id); +insert into orders values ('15294258455691 ','2018-06-20 00:30:45',2,50.20,0,1); +create table order_items( + id int(11) not null primary key auto_increment, + 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 +); +alter table order_items add index (order_id); +insert into order_items values (1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg','15294258455691'), + (2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg','15294258455691'); +``` +