




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、【精品文檔】如有侵權(quán),請(qǐng)聯(lián)系網(wǎng)站刪除,僅供學(xué)習(xí)與交流SQL實(shí)驗(yàn)(實(shí)驗(yàn)4至實(shí)驗(yàn)7的答案).精品文檔.實(shí)驗(yàn)實(shí)驗(yàn)41.用select 語(yǔ)句查詢departments和salary表中的所有數(shù)據(jù):select salary.*, departments.* from salary ,departments 2、查詢departments 中的departmentid:select departmentid from departments go3、查詢 salary中的 income,outcome:select income,outcome from salarygo4、查詢employees表中的
2、部門號(hào),性別,要用distinct消除重復(fù)行:select distinct(departmentid), sexfrom employees 5、查詢?cè)率杖敫哂?000元的員工號(hào)碼:select employeeid from salarywhere income>2000go6、查詢1970年以后出生的員工的姓名和住址:select name ,address from employees where birthday>1970go7、查詢所有財(cái)務(wù)部的員工的號(hào)碼和姓名:select employeeid ,namefrom employeeswhere departmentid
3、 in(select departmentid from departments where departmentname='財(cái)務(wù)部')go8、查詢employees員工的姓名,住址和收入水平,2000元以下顯示為低收入,20003000元顯示為中等收入,3000元以上顯示為高收入:select name ,address,case when income-outcome<2000 then '低收入'when income-outcome>3000 then '高收入'else '中等收入'end as '收
4、入等級(jí)'from employees,salarywhere employees.employeeid=salary.employeeidgo9、計(jì)算salary表中員工月收入的評(píng)價(jià)數(shù):select avg(income)as '平均收入' from salary10、查找employees表中最大的員工號(hào)碼:select max(employeeid)as '最大員工號(hào)碼' from employees11、計(jì)算salary表中的所有員工的總支出:select sum(outcome) as'總支出' from salary12、查詢財(cái)
5、務(wù)部雇員的最高實(shí)際收入:select max(income-outcome) from salary ,employees,departmentswhere salary.employeeid=employees.employeeid and employees.departmentid=departments.departmentid and departmentname='財(cái)務(wù)部'go13、查詢財(cái)務(wù)部雇員的最低實(shí)際收入:select min(income-outcome) from salary ,employees,departmentswhere salary.empl
6、oyeeid=employees.employeeid and employees.departmentid=departments.departmentid and departmentname='財(cái)務(wù)部'go14、找出所用地址中含有“中山”的雇員的號(hào)碼及部門號(hào):select employeeid ,departmentid from employeeswhere address like'%中山%'go15、查找員工號(hào)碼中倒數(shù)第二個(gè)數(shù)字為0的員工的姓名,地址和學(xué)歷:select education,address,name from employees wh
7、ere employeeid like'%0_'go16、使用into字句,由表employees創(chuàng)建“男員工1”表,包括編號(hào)和姓名:select employeeid,name into 男員工表from employees where sex='1'go17、用子查詢的方法查找收入在2500元以下的雇員的情況:select * from employees where employeeid in(select employeeid from salary where income<2500)go18、用子查詢的方法查找查找研發(fā)部比所有財(cái)務(wù)部雇員收入都高
8、的雇員的姓名:SELECT Name FROM Employees WHERE EmployeeID IN SELECT EmployeeID FROM Salary WHERE EmployeeID IN SELECT EmployeeId FROM Employees WHERE DepartmentID INSELECT DepartmentID FROM Departments WHERE DepartmentName='研發(fā)部'AND InCome>ALLSELECT InCome FROM Salary WHERE EmployeeID IN SELECT E
9、mployeeId FROM Employees WHERE DepartmentID INSELECT DepartmentID FROM Departments WHERE DepartmentName='財(cái)務(wù)部'19、 用子查詢的方法查找所有年齡比研發(fā)部雇員都大的雇員的姓名:select name from employees where Birthday<all ( select birthday from employees where departmentid in (select departmentid from departments where dep
10、artmentname='研發(fā)部'20、查詢每個(gè)員工的情況及其薪水的情況: select employees.*,departments.departmentname from employees,departments where employees.departmentid=departments.departmentid21、使用內(nèi)連接方法查找不在財(cái)務(wù)部工作的所有員工信息:select employees.* from employees inner join departments on employees.departmentid=departments.depart
11、mentidwhere departmentname!='財(cái)務(wù)部'22、使用外連接方法查找出所有員工的月收入:select employees.*,salary.incomefrom employees join salary on employees.employeeid=salary.employeeid23、查找財(cái)務(wù)部雇員的最高收入:select max(income)from salary where employeeid in(select employeeid from employees where departmentid in (select departme
12、ntid from departments where departmentname='財(cái)務(wù)部'24、查詢財(cái)務(wù)部雇員的最高實(shí)際收入:select max(income-outcome)from salary where employeeid in(select employeeid from employees where departmentid in (select departmentid from departments where departmentname='財(cái)務(wù)部'25、統(tǒng)計(jì)財(cái)務(wù)部收入在2500元以上的雇員人數(shù):select count(empl
13、oyeeid)from employees where departmentid in(select departmentid from departments where departmentname='財(cái)務(wù)部')and employeeid inselect employeeid from salary where income>2500)26、按部門列出在該部門工作的員工的人數(shù):select departmentid ,count(*) as 人數(shù)from employees group by departmentid27、按員工的學(xué)歷分組:select educa
14、tion ,count(*) as 人數(shù)from employees group by education28、按員工的工作年份分組,統(tǒng)計(jì)年份人數(shù):select workyear ,count(*) as 人數(shù)from employees group by workyear29、按各雇員的情況收入由低到高排列:select employees.* ,salary.incomefrom employees ,salary where employees.employeeid=salary.employeeidorder by income30、將員工信息按出生時(shí)間從小到大排列:select *f
15、rom employees order by birthday31、在order by 字句中使用子查詢,查詢員工姓名,性別和工齡信息,要求按實(shí)際收入從大到小排列:select name ,sex,workyear,income-outcomefrom salary ,employeeswhere salary.employeeid=employees.employeeidorder by income-outcome desc視圖部分1、創(chuàng)建view1:Create view view1 as select employees.employeeid,name,departmentname,(
16、income-outcome) as comefrom employees , departments , salary where employees.departmentid=departments.departmentid and employees.employeeid=salary.employeeid2、查詢視圖employeeid:3、向視圖view1中插入一行數(shù)據(jù):insert into view1 values('111111','謊言','1','30000')4、查看視圖(沒有影響)基本表:實(shí)驗(yàn)51、 定義一
17、個(gè)變量,用于描述YGGL數(shù)據(jù)庫(kù)的salary表中000001號(hào)員工的實(shí)際收入,然后查詢?cè)撟兞浚篸eclare hy int set hy=(select income-outcome from salary where employeeid='000001')select hy2、 使用運(yùn)算符“>”:select name from employees where birthday>'1974-10-10'3、 判斷姓名為“王林”的員工實(shí)際收入是否高于3000元,如果是則顯示“高收入”,否則顯示“收入不高于3000”:if(select income
18、 from salary,employees where salary.employeeid=employees.employeeid and ='劉明')>3000) select income as '高收入' from salary,employees where salary.employeeid=employees.employeeid and ='劉明'else select'收入不高于'4、使用循環(huán)輸出一個(gè)“*”三角形:declare i int decl
19、are j int set j=20set i=1while i<jbegin print(space(j-i)/2)+replicate('*',i)set i=i+2end4、 按部門進(jìn)行分類,使用if語(yǔ)句實(shí)現(xiàn):Create function hy1(departmentid1 char(3)returns char(10) as begin declare hy1 char(10)if(select departmentid from departments where departmentid1=departmentid)='1') set hy1
20、='財(cái)務(wù)部'if(select departmentid from departments where departmentid1=departmentid)='2')set hy1='人力資源部'if(select departmentid from departments where departmentid1=departmentid)='3')set hy1= '經(jīng)理辦公室'if(select departmentid from departments where departmentid1=departm
21、entid)='4') set hy1= '研發(fā)部'if(select departmentid from departments where departmentid1=departmentid)='5')set hy1='市場(chǎng)部'return hy1endselect employeeid,name,address,dbo.hy1(departmentid) from employeesselect employeeid,name,address,case departmentidwhen 1 then '財(cái)務(wù)部
22、9;when 2 then '人力資源部'when 3 then '經(jīng)理辦公室'when 4 then '研發(fā)部'when 5 then '市場(chǎng)部'end as 部門號(hào)from employees6、自定義一個(gè)函數(shù),計(jì)算一個(gè)數(shù)的階層:create function hy(hy2 int) returns intas begin declare i int set i=hy2declare j int set j=1while i>1beginset j=j*iset i=i-1endreturn(j)end declare
23、h int exec h=dbo.hy 4select h as 'jiecheng'7、/*生成隨機(jī)數(shù)*/select rand()8、/*平方*/select square(12)9、/*求財(cái)務(wù)部收入最高的員工姓名*/select max(name) from employees where employeeid in(select employeeid from salary where employeeid in (select employeeid from employees where departmentid in (select departmentid fr
24、om departments where departmentname='財(cái)務(wù)部')select avg(income) as '平均收入'from salary/*聚合函數(shù)與group by 一起使用*/select workyear ,count(*) as 人數(shù)from employees group by workyear/*將字符組成字符串*/select char(123)/*返回字符串左邊開始的個(gè)字符*/select left('abcdef',2)/*返回指定日期時(shí)間的天數(shù)*/select day(birthday)from em
25、ployees where employeeid='010000'/*獲取當(dāng)前時(shí)間*/select getdate()實(shí)驗(yàn)61、 創(chuàng)建索引:create unique index huangyan on employees(employeeid)2、 /*用create index 語(yǔ)句創(chuàng)建主鍵*/3、重建表employees中employeeid列上的索引alter index huangyanon employees rebuild4、刪除索引:5、創(chuàng)建一個(gè)新表,使用一個(gè)復(fù)合列作為主鍵,作為表的約束,并為其命名:create table employees5 ( emplo
26、yeeid char(6) not null,name char(5) not null,sex tinyint,education char(4),constraint yan primary key(employeeid,name)為新表添加一列:alter table employees5 add address char(10)6、創(chuàng)建新表student,性別只能包含男或女:create table student (號(hào)碼char(6) not null,性別char(2)not nullcheck(性別in ('男','女')7、創(chuàng)建新表:create
27、 table employees7(學(xué)號(hào)char(10) not null,出生日期datetime not nullcheck(出生日期>'1980-01-01')8、創(chuàng)建一個(gè)規(guī)則:9,創(chuàng)建salary2:create table salary2(employeeid char(6) not null primary key,income float not null,outcome float not null,foreign key(employeeid)references salary(employeeid)on update cascadeon delete
28、cascade10、添加一個(gè)外鍵,salary與employees有相關(guān)記錄,則拒絕更新employees:alter table salaryadd constraint kc_forforeign key(employeeid)references employees(employeeid)on delete no actionon update no action實(shí)驗(yàn)71、 工作年份大于6時(shí),跟換科室到經(jīng)理辦公室(根據(jù)員工):Create PROC UpdateDeptByYear(EmpId char(6) )ASBEGINDECLARE year intSELECT year=Wor
29、kYear From Employees WHERE EmployeeID=EmpIdIF(year>6) UPDATE Employees SET DepartmentID='3' WHERE EmployeeID=EmpIdENDEXEC UpdateDeptByYear '020010'SELECT * FROM Employees WHERE Employeeid='020010'2、 根據(jù)每個(gè)員工的學(xué)歷將收入提高元:CREATE PROC UpdateInComeByEdu Employeeid char(6)ASBEGINUPD
30、ATE SalarySET InCome=InCome+500FROM SalaryLEFT JOIN EmployeesON Salary.EmployeeID=Employees.EmployeeIDWHERE Salary.Employeeid=EmployeeidENDEXEC UpdateInComeByEdu '020010'SELECT * FROM Salary where EmployeeID='020010'3、游標(biāo):CREATE PROCEDURE Employees_bili AS BEGIN DECLARE i FLOAT DECLAR
31、E j FLOATDECLARE Education CHAR(10)DECLARE Employees_cursor CURSOR FOR SELECT Education FROM Employees SET i=0SET j=0OPEN Employees_cursor FETCH Employees_cursor INTO Education WHILE (FETCH_STATUS=0) BEGIN IF(Education!='大專' ) SET i=i+1 SET j=j+1 FETCH Employees_cursor INTO Education END CLO
32、SE Employees_cursor SELECT i AS'本科及以上員工所占員工數(shù)' SELECT j AS'員工總數(shù)'SELECT i/j AS'本科及以上員工所占比例'CLOSE Employees_cursor END EXEC Employees_bili4、使用命令的方式修改存儲(chǔ)過程的定義:5、對(duì)于YGGL數(shù)據(jù)庫(kù),表Employees的EmployeeID列與表Salary的EmployeeID列應(yīng)該滿足參照的完整性規(guī)則,請(qǐng)用觸發(fā)器實(shí)現(xiàn)兩個(gè)表的參照完整性:CREATE TRIGGER Salary_insert ON Salary
33、FOR INSERT,UPDATEASBEGINIF(SELECT EmployeeID FROM INSERTED) NOT IN(SELECT EmployeeID FROM Employees)ROLLBACKENDCREATE TRIGGER Employeesupdate ON EmployeesFOR UPDATEASBEGINUPDATE SalarySET EmployeeID=(SELECT EmployeeID FROM INSERTED)WHERE EmployeeID=(SELECT EmployeeID FROM DELETED)ENDCREATE TRIGGER E
34、mployeesdelete ON EmployeesFOR DELETEASBEGINDELETE FROM SalaryWHERE EmployeeID=(SELECT EmployeeID FROM DELETED)ENDINSERT INTO SalaryVALUES ('000005',2000,1000)UPDATE EmployeesSET EmployeeID='000000'WHERE EmployeeID=' 990230'DELETE FROM EmployeesWHERE EmployeeID='000000
35、9;6.當(dāng)修改表Employees時(shí),若將Employees表中員工的工作時(shí)間增加1年,則將收入增加500,若增加2年則增加1000,依次增加。若工作時(shí)間減少則無變化:CREATE TRIGGER EM_WORKYEAR ON EmployeesAFTER UPDATEASBEGINDECLARE i INT,j INTSET i=(SELECT WorkYear FROM INSERTED)SET j=(SELECT WorkYear FROM DELETED)IF(i>j)UPDATE SalarySET InCome=InCome+(i-j)*500WHERE EmployeeID IN(SELECT EmployeeID FROM INSERTED)ENDUPDATE Employees SET WorkYear=7WHERE Employeeid='990230'SELECT * FROM Employees WHERE Employeeid='990230'7.創(chuàng)建UPDATE觸發(fā)器,當(dāng)Salary表中Income值增加500時(shí),outcome值增加50:CREATE TRIGGER SA_INCOME ON SalaryFOR UPDATEASBEGINIF(SEL
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 提升商務(wù)英語(yǔ)考試聽說能力的綜合策略研究試題及答案
- 現(xiàn)代揚(yáng)塵測(cè)試題及答案
- 應(yīng)城一中高考試卷及答案
- 一年級(jí)下同步試卷及答案
- 一年級(jí)位置題及答案試卷
- 護(hù)士業(yè)務(wù)面試題及答案
- 未來智能充電技術(shù)的創(chuàng)新考核試題及答案
- 小學(xué)教師教學(xué)反思及改進(jìn)策略
- 家具設(shè)計(jì)師應(yīng)掌握的創(chuàng)新工具試題及答案
- 政策扶持與創(chuàng)業(yè)者創(chuàng)新能力試題及答案
- DB4403T339-2023城市級(jí)實(shí)景三維數(shù)據(jù)規(guī)范
- 設(shè)備維護(hù)工程師簡(jiǎn)歷
- 2023版押品考試題庫(kù)必考點(diǎn)含答案
- 挖孔樁基施工方案(水磨鉆)
- 變電檢修技能考試計(jì)算
- 國(guó)際經(jīng)濟(jì)法學(xué)(湘潭大學(xué))智慧樹知到答案章節(jié)測(cè)試2023年
- 以案說德發(fā)言四篇
- 大氣污染控制工程課后題答案解析
- 臨床試驗(yàn)倫理委員會(huì)倫理審查不同意見溝通的標(biāo)準(zhǔn)操作規(guī)程
- 梅毒診療指南(2023年)
- 高中物理3-3熱學(xué)練習(xí)題(含答案)
評(píng)論
0/150
提交評(píng)論