一、实验目的
- 掌握SELECT语句的基本语法
- 多表连接查询
- GROUP BY的使用方法。
- ORDER BY的使用方法。
二、实验步骤、内容、结果
实验内容:
实验4.1数据库的查询
目的与要求
(1)掌握SELECT语句的基本语法。
(2)掌握子查询的表示。
(3)掌握连接查询的表示。
(4)掌握SELECT语句的GROUP BY子句的作用和使用方法。
(5)掌握SELECT语句的ORDER BY子句的作用和使用方法。
(6)掌握SELECT语句的LIMIT子句的作用和使用方法。
实验准备
(1)了解SELECT语句的基本语法格式。
(2)了解SELECT语句的执行方法。
(3)了解子查询的表示方法。
(4) 了解连接查询的表示。
(5)了解SELECT语句的GROUPBY子句的作用和使用方法。(6)了解SELECT语句的ORDER BY子句的作用。
- 了解SELECT语句的LIMIT子句的作用。
实验内容
- SELECT语句的基本使用
- 对于实验2给出的数据库表结构,查询每个雇员的所有数据。
- 查询每个雇员的姓名、地址和电话。
- 查询EmployeeID为000001的雇员的地址和电话。
- 查询Employees表中女雇员的地址和电话,使用As子句将结果中各列的标题分别指定为地址、电话。
- 查询Employees.表中雇员的姓名和性别,要求Sex值为1时显示为"男",为0时显示为"女"。
- 计算每个雇员的实际收入。
- 获得雇员总数。
- 找出所有姓王的雇员的部门号。
- 找出所有收入为2009---3000元的雇员号码。
- 子查询的使用
- 查找在财务部工作的雇员情况。
- 查找研发部年龄不低于市场部所有雇员年龄的雇员姓名。
- 查找比广告部所有的雇员收入都高的姓名。
- 连接查询的使用
- 查询每个雇员的情况及其薪水的情况。
- 使用内连接的方法查询名字为"王林"的雇员所在部门。
- 查找广告部收入在2000元以上的雇员姓名及其薪水详情。
- GROUP BY、ORDER BY 和LIMIT子句的使用。
- 查找Employees中男性和女性的人数。
- 查找雇员数超过2人的部门名称和雇员数量。
- 将Employees表中的雇员号码由大到小排列。
- 返回Employees表中的前5位雇员的信息。
实验4.2视图的使用
目的和要求
(1)熟悉规图的概念和作用
(2)掌握视图的创建方法
(3)掌握如何查询和修改视图
实验准备
(1)了解视图的概念.
(2)了解创建视图的方法。
(3)了解对视图的操作。
实验内容
1.创建视图
- 创建YGGL数据库上的视图DS_ VIEW,视图包含Departments表的全部列。
- 创建YGGL数据库上的视图Employees_view,视图包含员工号码、姓名和实际收入,使用如下SQL语句:
2.查询视图
(1)从视图DS_VIEW中查询出部门号为3的部门名称
(2)从视图Employees_view查询出姓名为"王林"的员工的实际收入
3.更新视图
在更新视图前需要了解可更新视图的概念,了解什么视图是不可以进行修改的。更新视图真正更新的是与视图关联的表。
(1)向视图DS_VIEW中插入一行数据:6,财务部,财务管理
(2)修改视图DS_VIEW,将部门号为5的部门名称修改为"生产车间"
(3)修改视图Employees_view中号码为000001的员工的姓名为"王浩"
(4)删除视图DS_VIEW中部门号为"1"的数据
4.删除视图
删除DS_VIEW。
实验步骤:
打开Navicat Premium 15编辑器,编辑代码运行
代码:
#创建YGGL数据库
create database YGGL;
#使用数据库
use YGGL;
/*
实验一
*/
#创建表Employees
create table Employees
(
EmployeeID char(6) not null,
Name char(10) not null,
Education char(4) not null,
Birthday date not null,
Sex char(2) not null default '1',
WorkYear tinyint(1),
Address varchar(20),
PhoneNumber char(12),
DepartmentID char(3) not null,
primary key(EmployeeID)
)engine=innodb;
#创建表Departments
create table Departments
(
DepartmentID char(3) not null primary key,
DepartmentName char(20) not null,
Note text(16)
);
#创建表Salary
create table Salary(
EmployeeID char(6) not null,
InCome char(8) not null,
OutCome float(8) not null,
primary key(EmployeeID)
);
/*
create table Departments
(
DepartmenetID char(3) not null primary key,
DepartmentName char(20) not null,
Note text(16),
部门号 char(3) not null,
部门名称 char(20) not null,
备注 text(16)
);
create table Salary(
EmployeeID char(6) not null,
Income char(8) not null,
Outcome float(8) not null,
编号 char(6) not null,
收入 char(8) not null,
支出 float(8) not null,
primary key(EmployeeID)
);
*/
/*
实验二
*/
#向表Employees中插入数据
insert into Employees values('000001','王林','大专','1966-01-23',1,8,'中山路32-1-508','83355668',2);
insert into Employees values('010008','伍容华','本科','1976-03-28',1,3,'北京东路100-2','83321321',1);
insert into Employees values('020010','王向容','硕士','1982-12-09',1,2,'四牌楼10-0-108','83792361',1);
insert into Employees values('020018','李丽','大专','1960-07-30',0,6,'中山路102-2','83413301',1);
insert into Employees values('102201','刘明','本科','1972-10-18',1,3,'虎踞路100-2','83606608',5);
insert into Employees values('102208','朱俊','硕士','1965-09-28',1,2,'牌楼巷5-3-106','84708817',5);
insert into Employees values('108991','钟敏','硕士','1979-08-10',0,4,'中山路10-3-105','83346722',3);
insert into Employees values('111006','张石兵','本科','1974-10-01',1,1,'解放路34-1-203','84563418',5);
insert into Employees values('210678','林涛','大专','1977-04-02',1,2,'中山北路24-35','83467336',3);
insert into Employees values('302566','李玉珉','本科','1968-09-20',1,3,'热和路209-3','58765991',4);
insert into Employees values('308759','叶凡','本科','1978-11-18',1,2,'北京西路3-7-52','83308901',4);
insert into Employees values('504209','陈琳琳','大专','1969-09-03',0,5,'汉中路120-4-12','84468158',4);
#向表Salary中插入数据
insert into Salary values('000001','2100.8','123.09');
insert into Salary values('010008','1582.62','88.03');
insert into Salary values('102201','2569.88','185.65');
insert into Salary values('111006','1978.01','79.58');
insert into Salary values('504209','2066.15','108.0');
insert into Salary values('302566','2980.7','210.2');
insert into Salary values('108991','3259.98','281.52');
insert into Salary values('020010','2860.0','198.0');
insert into Salary values('020018','2347.68','180.0');
insert into Salary values('308759','2531.98','199.08');
insert into Salary values('210678','2240.0','121.0');
insert into Salary values('102208','1980.0','100.0');
/*
insert into Departments values(
('1','财务部','NULL'),
('2','人力资源部','NULL'),
('3','经理办公室','NULL'),
('4','研发部','NULL'),
('5','市场部','NULL'),
('','','')
);
*/
#向表 Departments中插入数据
insert into Departments values('1','财务部','NULL');
insert into Departments values('2','人力资源部','NULL');
insert into Departments values('3','经理办公室','NULL');
insert into Departments values('4','研发部','NULL');
insert into Departments values('5','市场部','NULL');
#insert into Departments values('','','');
/*
实验三
*/
#删除表Employees的第一行和表Salary的第一行
DELETE FROM `yggl`.`employees` WHERE `EmployeeID` = '000001';
DELETE FROM `yggl`.`salary` WHERE `EmployeeID` = '000001';
/*
3.插入数据
(1)
*/
insert into Employees values('000001','王林','大专','1966-01-23','1',8,'中山路32-1-508','83355668','2');
/*
(2)
*/
insert into Salary set EmployeeID='000001',InCome=2100.8,OutCome=123.09;
/*
(3)
*/
replace into Departments values('1','广告部','负责推广产品');
#查看表Salary
select * from Departments;
/*
思考与练习
*/
#创建空表Employees2
create table Employees2
(
EmployeeID char(6) not null,
Name char(10) not null,
Education char(4) not null,
Birthday date not null,
Sex char(2) not null default '1',
WorkYear tinyint(1),
Address varchar(20),
PhoneNumber char(12),
DepartmentID char(3) not null,
primary key(EmployeeID)
)engine=innodb;
#(2)INSERT INTO 语句还可以通过 SELECT 子句来添加其他表中的数据,但是 SELECT 子句中的列要与添加表的列数目和数据类型都﹣﹣对应。假设有另一个空表Employees2,结构和 Employees 表完全相同,使明 INSERT INTO 语句将 Employees 表中数据添加到Employees2中,语句如下:
insert into Employees2 select * from Employees;
/*查看Employees2表中的变化,如图实验3.2所示。
可见,这时表Employees2中已经有了表 Employees 的全部数据
*/
select * from Employees2;
/*
4.使用 SQL 语句修改表数据
(1)使用 SQL 命令修改表 Salary 中的某个记录的字段值:
*/
update Salary set InCome=2890 where EmployeeID='102201';
/*
(2)将所有职工收入增加100:
*/
update Salary set InCome =InCome+100;
/*说明:可以在界面工具中观察数据的变化,验证操作是否成功。
(3)使用 SQL 命令删除表 Employees 中编号为102201的职工信息:
*/
delete from Employees where EmployeeID='102201';
/*
(4)删除所有收入大于2500的员工信息:
*/
delete from Employees where EmployeeID=(select EmployeeID from Salary where InCome >2500);
/*
(5)使用TRUNCATE TABLE
*/
truncate table Salary;
/*
实验4.1 数据库的查询
1 .SELECT语句的基本使用
(1)对于实验2给出的数据库表结构,查询每个雇员的所有数据。
*/
use YGGL;
select * from Employees;
/*
(2)查询每个雇员的姓名、地址和电话。
*/
select Name,Address,PhoneNumber from Employees;
/*
(3)查询EmployeeID为000001的雇员的地址和电话。
*/
select Address,PhoneNumber from Employees where EmployeeID='000001';
/*
(4)查询Employees表中女雇员的地址和电话,使用As子句将结果中各列的标题分别指定为地址、电话。
*/
select Address as 地址,PhoneNumber as 电话 from Employees where sex='0';
/*
(5)查询Employees.表中雇员的姓名和性别,要求Sex值为1时显示为"男",为0时显示为"女"。
*/
select Name as 姓名,
case
when Sex='1' then '男'
when Sex='0' then '女'
end as 性别
from Employees;
/*
(6)计算每个雇员的实际收入。
*/
select EmployeeID,InCome-OutCome as 实际收入 from Salary;
/*
(7)获得雇员总数。
*/
select COUNT(*) from Employees;
/*
(8)找出所有姓王的雇员的部门号。
*/
select DepartmentID from Employees where name like '王%';
/*
(9)找出所有收入为2009---3000元的雇员号码。
*/
select EmployeeID from Salary where InCome between 2000 and 3000;
/*
2.子查询的使用
(1)查找在财务部工作的雇员情况。
*/
select * from Employees
where DepartmentID=
(select DepartmentID
from Departments
where DepartmentName='广告部');
/*
(2)查找研发部年龄不低于市场部所有雇员年龄的雇员姓名。
*/
select Name from
Employees where DepartmentID in
(select DepartmentID
from Departments
where DepartmentName='研发部')
and
Birthday <=ALL
(select Birthday
from Employees
where DepartmentID in
(select DepartmentID
from Departments
where DepartmentName='市场部'));
/*
(3)查找比广告部所有的雇员收入都高的姓名。
*/
select Name
from Employees
where EmployeeID in
(select EmployeeID
from Salary where InCome>
all (select InCome
from Salary
where EmployeeID in
(select EmployeeID
from Employees
where DepartmentID=
(select DepartmentID
from Departments
where DepartmentName='广告部')
)
)
);
/*
3.连接查询的使用
(1)查询每个雇员的情况及其薪水的情况。
*/
select Employees.*,Salary.* from Employees,Salary where Employees.EmployeeID=Salary.EmployeeID;
/*
(2)使用内连接的方法查询名字为"王林"的雇员所在部门。
*/
select DepartmentName from Departments join Employees on Departments.DepartmentID=Employees.DepartmentID where Employees.Name='王林';
/*
(3)查找广告部收入在2000元以上的雇员姓名及其薪水详情。
*/
select Name,InCome,OutCome from Employees,Salary,Departments where Employees.EmployeeID=Salary.EmployeeID and Employees.DepartmentID=Departments.DepartmentID and DepartmentName='广告部' and InCome > 2000;
/*
4.GROUP BY、ORDER BY 和LIMIT子句的使用。
(1)查找Employees中男性和女性的人数。
*/
select Sex,COUNT(sex) from Employees group by Sex;
/*
(2)查找雇员数超过2人的部门名称和雇员数量。
*/
select DepartmentName,COUNT(*) AS 人数 from Employees,Departments where Employees.DepartmentID=
Departments.DepartmentID group by Employees.DepartmentID having COUNT(*) >2;
/*
(3)将Employees表中的雇员号码由大到小排列。
*/
select EmployeeID from Employees order by EmployeeID DESC;
/*
(4)返回Employees表中的前5位雇员的信息。
*/
select * from Employees limit 5;
/*
实验4.2
*/
/*
11.创建视图
(1)创建YGGL数据库上的视图DS_ VIEW,视图包含Departments表的全部列。
*/
create or replace view DS_VIEW as select * from Departments;
/*
(2)创建YGGL数据库上的视图Employees_view,视图包含员工号码、姓名和实际收入,
*/
create or replace view Employees_view(EmployeeID,Name,RealIncome) as select Employees.EmployeeID,Name,InCome-OutCome from Employees,Salary where Employees.EmployeeID=Salary.EmployeeID
/*
2.查询视图
(1)从视图DS_VIEW中查询出部门号为3的部门名称
*/
select DepartmentName from DS_VIEW where DepartmentID='3';
/*
(2)从视图Employees_view查询出姓名为"王林"的员工的实际收入
*/
select RealIncome from Employees_view where Name='王林';
/*
3.更新视图
(1)向视图DS_VIEW中插入一行数据:6,财务部,财务管理
*/
insert into DS_VIEW values('6','财务部','财务管理');
/*
(2)修改视图DS_VIEW,将部门号为5的部门名称修改为"生产车间"
*/
update DS_VIEW set DepartmentName='生产车间' where DepartmentID='5';
/*
(3)修改视图Employees_view中号码为000001的员工的姓名为"王浩"
*/
update Employees_view set Name='王浩' where EmployeeID='000001';
/*
(4)删除视图DS_VIEW中部门号为"1"的数据
*/
delete from DS_VIEW where DepartmentID='1';
/*
4.删除视图
删除DS_VIEW。
*/
drop view DS_VIEW