191 lines
6.0 KiB
Markdown
191 lines
6.0 KiB
Markdown
# 综合案例2-记录的插入、更新和删除
|
||
|
||
本部分内容重点介绍了数据表中数据的插入、更新和删除操作。MySQL中可以灵活的对数据进行插入与更新,MySQL中对数据的操作没有任何提示,因此在更新和删除数据时,一定要谨慎小心,查询条件一定要准确,避免造成数据的丢失。此综合案例包含了对数据表中数据的基本操作,包括记录的插入、更新和删除。
|
||
|
||
## 1、案例目的
|
||
|
||
创建表books,对数据表进行插入、更新和删除操作,掌握表数据的基本操作。books表结构以及表中的记录,如表1和表2所示
|
||
|
||
|
||
|
||
表1 books表结构
|
||
|
||
| 字段名 | 数据类型 | 主键 | 外键 | 非空 | 唯一 | 自增 | 字段说明 |
|
||
| ------- | -------------- | ---- | ---- | ---- | ---- | ---- | -------- |
|
||
| b_id | int(11) | 是 | 否 | 是 | 是 | 否 | 书编号 |
|
||
| b_name | varchar(50) | 否 | 否 | 是 | 否 | 否 | 书名 |
|
||
| authors | varchar(100) | 否 | 否 | 是 | 否 | 否 | 作者 |
|
||
| price | float | 否 | 否 | 是 | 否 | 否 | 价格 |
|
||
| pubdate | year | 否 | 否 | 是 | 是 | 否 | 出版日期 |
|
||
| note | varchar(100) | 否 | 否 | 否 | 否 | 否 | 说明 |
|
||
| num | int(11) | 否 | 否 | 是 | 否 | 否 | 库存量 |
|
||
|
||
|
||
|
||
表2 books表中的记录
|
||
|
||
| b_id | b_name | authors | price | pubdate | note | num |
|
||
| ---- | ------------- | --------------- | ----- | ------- | -------- | ---- |
|
||
| 1 | Tale 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 |
|
||
|
||
|
||
|
||
## 2、案例操作过程
|
||
|
||
(1)登录MySQL数据库。
|
||
|
||
```mysql
|
||
mysql -h localhost -u root -p
|
||
```
|
||
|
||
截图:
|
||
|
||
![img](https://lsky.hhdxw.top/imghub/img/wps4BC6.tmp.jpg)
|
||
|
||
(2)创建数据库M_BOOK并选择使用此数据库。
|
||
|
||
```mysql
|
||
create database M_BOOK;
|
||
desc M_BOOK;
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315141208398](https://lsky.hhdxw.top/imghub/img/image-20220315141208398.png)
|
||
|
||
(3)按照表1创建表books,创建成功后用desc查看表结构。
|
||
|
||
```mysql
|
||
create table books(
|
||
b_id int(11) not null unique,
|
||
b_name varchar(50) not null,
|
||
authors varchar(100) not null,
|
||
price float not null,
|
||
pubdate year not null unique,
|
||
note varchar(100) null,
|
||
num int(11) not null,
|
||
primary key(b_id)
|
||
);
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315142332615](https://lsky.hhdxw.top/imghub/img/image-20220315142332615.png)
|
||
|
||
(4)使用select语句查询表中的数据(此处查询结果应为空)
|
||
|
||
```mysql
|
||
select * from books;
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315142310880](https://lsky.hhdxw.top/imghub/img/image-20220315142310880.png)
|
||
|
||
(5)将表2中的记录插入到books表中,分别使用不同的方法插入记录。
|
||
|
||
①指定所有字段名插入第1行记录,插入后用select语句查询插入结果
|
||
|
||
```mysql
|
||
insert into books (b_id,b_name,authors,price,pubdate,note,num) values(1,'Tale of AAA','DIckes',23,1995,'novel',11);
|
||
select * from books;
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315142912049](https://lsky.hhdxw.top/imghub/img/image-20220315142912049.png)
|
||
|
||
②不指定字段名插入第2行记录,插入后用select语句查询插入结果
|
||
|
||
```mysql
|
||
insert into books values(2,'EmmaT','Jane lura',35,1993,'joke',22);
|
||
select * from books;
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315143315288](https://lsky.hhdxw.top/imghub/img/image-20220315143315288.png)
|
||
|
||
③同时插入第3~7行记录,插入后用select语句查询插入结果
|
||
|
||
```mysql
|
||
insert into books (b_id,b_name,authors,price,pubdate,note,num)
|
||
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);
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315144009242](https://lsky.hhdxw.top/imghub/img/image-20220315144009242.png)
|
||
|
||
(6)使用select语句查询小说类型(novel)的书的所有信息。
|
||
|
||
```mysql
|
||
select * from books where note='novel';
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315144355595](https://lsky.hhdxw.top/imghub/img/image-20220315144355595.png)
|
||
|
||
(7)将小说类型(novel)的书的价格都增加5,并在更新后使用select语句查询小说类型(novel)的书的所有信息
|
||
|
||
```mysql
|
||
update books set price=price+5 where note='novel';
|
||
select * from books where note='novel';
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315145957535](https://lsky.hhdxw.top/imghub/img/image-20220315145957535.png)
|
||
|
||
(8)使用select语句查看书名为EmmaT的信息
|
||
|
||
```mysql
|
||
select * from books where b_name='EmmaT';
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315150138907](https://lsky.hhdxw.top/imghub/img/image-20220315150138907.png)
|
||
|
||
(9)将名为EmmaT的书价格改为40,并使用select语句查询更新后的该书信息。
|
||
|
||
```mysql
|
||
update books set price=40 where b_name='EmmaT';
|
||
select * from books where b_name='EmmaT';
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315150252053](https://lsky.hhdxw.top/imghub/img/image-20220315150252053.png)
|
||
|
||
(10)查询库存量为0的书的所有信息。
|
||
|
||
```mysql
|
||
select * from books where num=0;
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315150322666](https://lsky.hhdxw.top/imghub/img/image-20220315150322666.png)
|
||
|
||
(11)删除库存量为0的书的所有信息,并使用select语句查询库存量为0的书的所有信息(此处应为空)。
|
||
|
||
```mysql
|
||
delete from books where num=0;
|
||
select * from books where num=0;
|
||
```
|
||
|
||
截图:
|
||
|
||
![image-20220315150431488](https://lsky.hhdxw.top/imghub/img/image-20220315150431488.png) |