mysql实验5
實驗五 多表查詢
(一)實驗目的
1、掌握數據庫基礎知識、數據庫和表的基本操作
2、掌握外鍵建立。
3、掌握操作關聯表。
4、掌握多表查詢方法,包括子查詢,連接查詢,條件查詢。
(二)實驗要求
請大家將執行結果以圖片形式完成實驗報告,并在最后給出自己經過實驗得到的總結。
(三)實驗學時
2學時
(四)實驗內容
已知,有一個客戶表customer,一個銀行表bank和一個存款信息表deposite,請按要求對這三個表進行操作。表結構及表中內容如下所示。
表customer
| 屬性名稱 | 類型與長度 | 中文含義 | 備注 |
| c_id | char(6) | 客戶標識 | 主鍵 |
| name | varchar(30) | 客戶姓名 | 非空 |
| location | varchar(30) | 工作地點 | |
| salary | decimal(8,2) | 工資 |
表bank
| 屬性名稱 | 類型與長度 | 中文含義 | 備注 |
| b_id | char(6) | 銀行標識 | 主鍵 |
| bank_name | varchar(30) | 銀行名稱 | 非空 |
表deposite
| 屬性名稱 | 類型與長度 | 中文含義 | 備注 |
| d_id | int(11) | 存款流水號 | 主鍵,自增 |
| c_id | char(6) | 客戶標識 | 外鍵,關聯customer的c_id |
| b_id | char(6) | 銀行標識 | 外鍵,關聯bank的b_id |
| dep_date | date | 存入日期 | |
| amount | decimal(8,2) | 存款金額 |
customer的數據如下:
| c_id | name | location | salary |
| 101001 | 孫萌 | 廣州 | 1234 |
| 101002 | 王琦 | 南京 | 3526 |
| 101003 | 趙越 | 北京 | 6892 |
| 101004 | 童彤 | 海南 | 3492 |
bank的數據如下
| b_id | bank_name |
| B0001 | 工商銀行 |
| B0002 | 建設銀行 |
| B0003 | 中國銀行 |
| B0004 | 農業銀行 |
deposite表數據如下
| d_id | c_id | b_id | dep_date | amount |
| 1 | 101001 | B0001 | 2011-04-05 | 42526 |
| 2 | 101002 | B0003 | 2012-12-02 | 66500 |
| 3 | 101003 | B0002 | 2018-11-21 | 500000 |
| 4 | 101004 | B0004 | 2020-03-02 | 12987 |
| 5 | 101001 | B0002 | 2015-09-02 | 456 |
| 6 | 101002 | B0004 | 2018-09-02 | 99000 |
| 7 | 101003 | B0003 | 2016-06-22 | 35000 |
| 8 | 101004 | B0001 | 2016-03-12 | 67890 |
| 9 | 101001 | B0004 | 2019-11-02 | 400000 |
| 10 | 101002 | B0001 | 2017-08-22 | 12000 |
| 11 | 101003 | B0002 | 2015-09-02 | 12345.98 |
| 12 | 101004 | B0003 | 2014-10-13 | 5678.88 |
(1)啟動MySQL服務器。
mysql -uroot -proot(2)登錄MySQL服務器。
mysql -uroot -proot(3)創建數據庫exp07。
create database exp07(4)切換當前數據庫。
use exp07(5)按要求創建以上三張表格,插入相應數據。
create table customer( c_id char(6) primary key comment '客戶標識', name varchar(30) not null comment '客戶姓名', location varchar(30) comment '工作地點', salary decimal(8,2) comment '工資' ) create table bank( b_id char(6) primary key comment '銀行標識', bank_name varchar(30) not null comment '銀行名稱' ) create table deposite( d_id int(11) primary key auto_increment comment '存款流水號', c_id char(6) comment '客戶標識', b_id char(6) comment '銀行標識', dep_date date comment '存入日期', amount decimal(8,2) comment '存款金額' ) #關聯customer的c_id alter table deposite add constraint waic_id foreign key(c_id) references customer(c_id) on delete restrict on update cascade #關聯bank的b_id alter table deposite add constraint waib_id foreign key(b_id) references bank(b_id) on delete restrict on update cascade insert into customer (c_id,name,location,salary) values (101001,'孫萌','廣州',1234), (101002,'王琦','南京',3526), (101003,'趙越','北京',6892), (101004,'童彤','海南',3492) insert into bank (b_id,bank_name) values ('B0001','工商銀行'), ('B0002','建設銀行'), ('B0003','中國銀行'), ('B0004','農業銀行') insert into deposite (c_id,b_id,dep_date,amount) values (101001,'B0001','2011-04-05',42526), (101002,'B0003','2012-12-02',66500), (101003,'B0002','2018-11-21',500000), (101004,'B0004','2020-03-02',12987), (101001,'B0002','2015-09-02',456), (101002,'B0004','2018-09-02',99000), (101003,'B0003','2016-06-22',35000), (101004,'B0001','2016-03-12',67890), (101001,'B0004','2019-11-02',400000), (101002,'B0001','2017-08-22',12000), (101003,'B0002','2015-09-02',12345.98), (101004,'B0003','2014-10-13',5678.88)?(6)將數據表deposite中孫萌的存款金額加10000
update deposite as d join (select c_id from customer where name='孫萌') as c on c.c_id = d.c_id set d.amount = d.amount+10000 #查詢更改情況 select c.name,d.amount from customer c join deposite d on d.c_id = (select c_id from customer where name='孫萌')&&c.c_id=d.c_id(7)將數據表deposite中所屬賬戶為工商銀行并且存入日期為2011-04-05的人員的存款金額加100000
update deposite set amount=amount+100000 where b_id=(select b_id from bank where bank_name='工商銀行')&&dep_date='2011-04-05' #查詢更改情況 select dep_date,amount from deposite where b_id =(select b_id from bank where bank_name='工商銀行')&&dep_date='2011-04-05'(8)將數據表deposite中王琦的銀行標識改為建設銀行
update deposite set b_id=(select b_id from bank where bank_name='建設銀行') where c_id=(select c_id from customer where name='王琦') #查詢更改情況 select c.name,b.bank_name,d.b_id,d.c_id from customer c join bank b join deposite d on c.name='王琦'&&b.b_id=d.b_id&&d.c_id=c.c_id(9)將salary低于5000的建行客戶的salary變為原來的2倍.
update customer set salary=salary*2 where salary<5000 && c_id in (select c_id from deposite where b_id = (select b_id from bank where bank_name ='建設銀行')) #查詢更改情況 select c.name,c.salary,b.bank_name from customer c join bank b join deposite d where c.salary<5000 && b.b_id=d.b_id && c.c_id=d.c_id(10)查詢日期為2011-04-05這一天進行過存款的客戶ID,客戶姓名,銀行名稱,存款金額
select c.c_id,c.name,b.bank_name,d.amount from customer c join bank b join deposite d where d.dep_date='2011-04-05'&&b.b_id=d.b_id&&c.c_id=d.c_id(11)查詢趙越在建設銀行的存款信息(顯示信息:客戶ID,客戶姓名,銀行標識,銀行名稱,存款日期,存款金額)
select c.c_id,c.name,b.b_id,b.bank_name,d.dep_date,d.amount from customer c join bank b join deposite d where c.name='趙越'&&d.c_id=c.c_id&&b.b_id=d.b_id&&b.bank_name='建設銀行'(12)查詢在農業銀行存款前五名的客戶存款信息(顯示信息:客戶姓名,銀行名稱,存款金額)
from customer c join bank b join deposite d on c.c_id=d.c_id&&b.b_id=d.b_id&&b.bank_name='農業銀行' order by d.amount desc limit 5(13)查詢姓“童”的客戶的存款信息(顯示信息:客戶姓名,銀行名稱,存款金額)
select c.name,b.bank_name,d.amount from customer c join bank b join deposite d on c.c_id=d.c_id&&b.b_id=d.b_id&&c.name like '童%'(14)查詢孫萌的存款信息(顯示信息:客戶ID,客戶姓名,銀行名稱,存款金額)
(使用表連接、子查詢及where條件查詢三種方式實現)
#表連接 select c.c_id,c.name,b.bank_name,d.amount from customer c join bank b join deposite d on c.c_id=d.c_id&&b.b_id=d.b_id&&c.name='孫萌' #子查詢 select c.c_id,c.name,b.bank_name,d.amount from (select c_id,name from customer where name='孫萌') as c, (select b_id,amount from deposite where c_id=(select c_id from customer where name='孫萌')) as d, (select bank_name,b_id from bank where b_id in (select b_id from deposite where c_id=(select c_id from customer where name='孫萌'))) as b where b.b_id=d.b_id #where條件查詢 select c.c_id,c.name,b.bank_name,d.amount from customer as c, bank as b, deposite as d where c.name='孫萌'&&d.c_id=c.c_id&&b.b_id=d.b_id;(15) 查詢工商銀行存款大于等于一萬的客戶ID,姓名,銀行名稱,存款金額
(使用表連接、子查詢及where條件查詢三種方式實現)
#表連接 select c.c_id,c.name,b.bank_name,d.amount from customer c join bank b join deposite d on c.c_id=d.c_id && b.b_id=d.b_id && b.bank_name='工商銀行' && d.amount>10000 #子查詢 select c.c_id,c.name,b.bank_name,d.amount from(select c_id,name from customer where c_id in (select c_id from deposite where b_id =(select b_id from bank where bank_name='工商銀行'))) as c, (select b_id,c_id,amount from deposite where b_id=(select b_id from bank where bank_name='工商銀行')) as d, (select b_id,bank_name from bank where bank_name='工商銀行') as b where c.c_id=d.c_id #where條件查詢 select c.c_id,c.name,d.amount,b.bank_name from customer c,bank b,deposite d where d.amount>=10000 && b.bank_name='工商銀行' && d.c_id=c.c_id && b.b_id=d.b_id總結
- 上一篇: Python中最快的搜索引擎之一:Thr
- 下一篇: springboot RedisTemp