一、實驗目的
- 掌握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