mysql面试题1
MySQL實訓_01
 1. 創建數據庫,名稱為cdadb;(如果已有,則省略)
 2. 創建數據表customer(客戶)、deposite(存款)、bank(銀行),表結構如下:
 customer的表結構:
 屬性名稱 類型與長度 中文含義 備注
 c_id char(6) 客戶標識 主鍵,非空
 name varchar(30) 客戶姓名 非空
 location Varchar(30) 工作地點 
 salary decimal(8,2) 工資 
 bank的表結構:
 屬性名稱 類型與長度 中文含義 備注
 b_id char(5) 銀行標識 主鍵,非空
 bank_name char(30) 銀行名次 非空
 deposite的表結構:
 屬性名稱 類型與長度 中文含義 備注
 d_id int 存款流水號 主鍵,非空,自增
 c_id char(6) 客戶標識 外鍵,關聯customer表的c_id
 b_id char(5) 銀行標識 外鍵,關聯bank表的b_id
 dep _date date 存入日期 
 dep_type char(1) 存款期限 1,3,5分別代表1年期、3年期和5年期
 amount decimal(8,2) 存款金額 
 3. 錄入數據如下:
 customer的數據如下,注意最后一條記錄用你的學號和你的姓名代替。
 c_id name location salary
 101001 孫楊 廣州 1234
 101002 郭海 南京 3526
 101003 盧江 蘇州 6892
 101004 郭惠 濟南 3492
 你的學號 你的姓名 北京 6324
bank的數據如下:
 b_id bank_name
 B0001 工商銀行
 B0002 建設銀行
 B0003 中國銀行
 B0004 農業銀行
 deposite的數據如下:
 d_id c_id b_id dep_date dep_type amount
 1 101001 B0001 2011-04-05 3 42526
 2 101002 B0003 2012-07-15 5 66500
 3 101003 B0002 2010-11-24 1 42366
 4 101004 B0004 2008-03-31 1 62362
 5 101001 B0003 2002-02-07 3 56346
 6 101002 B0001 2004-09-23 3 353626
 7 101003 B0004 2003-12-14 5 36236
 8 101004 B0002 2007-04-21 5 26267
 9 101001 B0002 2011-02-11 1 435456
 10 101002 B0004 2012-05-13 1 234626
 11 101003 B0003 2001-01-24 5 26243
 12 101004 B0001 2009-08-23 3 45671
 4. 更新customer表的salary屬性,將salary低于5000的客戶的salary變為原來的2倍.
 5. 對deposite表進行統計,按銀行統計存款總數,顯示為b_id,total.
 6. 對deposite、customer、bank進行查詢,查詢條件為location在廣州、蘇州、濟南的客戶,存款在300000至500000之間的存款記錄,顯示客戶姓名name、銀行名稱bank_name、存款金額amount.
關系型數據庫和MySQL作業
作業:使用下面的SQL在MySQL中創建數據庫、二維表并插入數據,然后完成下面的查詢操作。
create database hrs default charset utf8mb4;use hrs;create table tb_dept ( dno int not null comment '編號', dname varchar(10) not null comment '名稱', dloc varchar(20) not null comment '所在地', primary key (dno) );insert into tb_dept values (10, '會計部', '北京'),(20, '研發部', '成都'),(30, '銷售部', '重慶'),(40, '運維部', '深圳');create table tb_emp ( eno int not null comment '員工編號', ename varchar(20) not null comment '員工姓名', job varchar(20) not null comment '員工職位', mgr int comment '主管編號', sal int not null comment '員工月薪', comm int comment '每月補貼', dno int comment '所在部門編號', primary key (eno), foreign key (dno) references tb_dept(dno), foreign key (mgr) references tb_emp(eno) );insert into tb_emp values (7800, '張三豐', '總裁', null, 9000, 1200, 20),(2056, '喬峰', '分析師', 7800, 5000, 1500, 20),(3088, '李莫愁', '設計師', 2056, 3500, 800, 20),(3211, '張無忌', '程序員', 2056, 3200, null, 20),(3233, '丘處機', '程序員', 2056, 3400, null, 20),(3251, '張翠山', '程序員', 2056, 4000, null, 20),(5566, '宋遠橋', '會計師', 7800, 4000, 1000, 10),(5234, '郭靖', '出納', 5566, 2000, null, 10),(3344, '黃蓉', '銷售主管', 7800, 3000, 800, 30),(1359, '胡一刀', '銷售員', 3344, 1800, 200, 30),(4466, '苗人鳳', '銷售員', 3344, 2500, null, 30),(3244, '歐陽鋒', '程序員', 3088, 3200, null, 20),(3577, '楊過', '會計', 5566, 2200, null, 10),(3588, '朱九真', '會計', 5566, 2500, null, 10);查詢月薪最高的員工姓名和月薪
mysql> select ename as 姓名,sal as 月薪 from tb_emp where sal = (select max(sal) f rom tb_emp);查詢員工的姓名和年薪((月薪+補貼)*13)
mysql> select ename as 姓名,(sal+ifnull(comm,0))*13 as 年薪 from tb_emp;查詢所有部門的名稱和人數
mysql> select dname as 部門,total as 人數 from tb_dept d left join (select dno,count(dno) as total from tb_emp group by dno) e on d.dno = e.dno;查詢月薪最高的員工(Boss除外)的姓名和月薪
mysql> select ename as 姓名,sal as 月薪 from tb_emp where sal = (select max(sal) from tb_emp where job !='總裁');查詢月薪超過平均月薪的員工的姓名和月薪
mysql> select ename as 姓名,sal as 月薪 from tb_emp where sal > (select avg(sal) from tb_emp);查詢月薪超過其所在部門平均月薪的員工的姓名、部門編號和月薪
mysql> select e.ename as 姓名,d.dno as 部門編號,e.sal as 月薪 from tb_emp e inner join (select dno,avg(sal) as avgsal from tb_emp group by dno) as d on d.dno =e.dno where e.sal > d.avgsal;查詢部門中月薪最高的人姓名、月薪和所在部門名稱
# 1 部門中月薪最高的拿出來 SELECT dno,max(sal) as maxsal from tb_emp GROUP BY dno;# 2 部門中月薪最高的人 部門編號 拿出來 SELECT ename,sal,t1.dno from tb_emp t1 INNER JOIN (SELECT dno,max(sal) as maxsal from tb_emp GROUP BY dno) t2 on t1.dno=t2.dno where sal=maxsal; # 3 根據上表 拿出 部門名稱 mysql> select e.ename as 姓名,p.dname as 部門名稱,e.sal as 月薪 from tb_emp e inner join (select dno,max(sal) as maxsal from tb_emp group by dno) d on d.dno = e.dno inner join tb_dept p on d.dno =p.dno where e.sal = d.maxsal;查詢主管的姓名和職位
SELECT eno,ename,job from tb_emp where eno in (SELECT DISTINCT mgr from tb_emp where mgr is not null);查詢月薪排名4~6名的員工排名、姓名和月薪
有員工的部門編號和人數
SELECT dno,COUNT(dno) as 人數 from tb_emp GROUP BY dno;總結
 
                            
                        - 上一篇: 抽卡模拟系统(包含图形界面,结果可存储到
- 下一篇: 2021中国AI产业10大趋势,一分钟看
