SQL Server实验课程指导书.docx
- 文档编号:9338775
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:30
- 大小:109.91KB
SQL Server实验课程指导书.docx
《SQL Server实验课程指导书.docx》由会员分享,可在线阅读,更多相关《SQL Server实验课程指导书.docx(30页珍藏版)》请在冰豆网上搜索。
SQLServer实验课程指导书
《SQLServer》实验指导书
实验一SQL语言调试
一、实验项目名称
SQL语言调试
二、实验目的和要求
1、熟悉SQLServer2000查询分析器的使用方法,掌握如何编写、调试和执行SQL语句
2、通过调试SQL语句,了解数据库开发环境的特点
3、熟练掌握DCL、DDL、DML语言和Select语句的语法规则
4、掌握SQLServer2000的数据类型及其使用
三、实验内容、方法和步骤
1、熟悉SQLServer2000查询分析器的使用方法
1)打开查询分析器使用界面:
开始(Start)—程序(Program)—MicrosoftSQLServer—查询分析器(QueryAnalyzer),选择服务器,选择验证方式,点击“OK”进行连接。
连接成功后,出现查询分析器使用界面
2)熟悉查询分析器使用方法:
ⅰ了解和熟悉文件(File)、编辑(Edit)、查询(Query)、工具(Tools)、窗口(Window)、帮助(Help)的作用。
ⅱ在主窗口中输入SQL关键字、保留字和非关键字,观察字体颜色的不同。
3)输入并运行一段简单的SQL语句:
ⅰ将主窗口中的字符删除,输入以下语句:
①usea
②useNorthwind
③selectordfromorders
④selectorderidfromorders
ⅱ用鼠标逐一选中各条语句,并分别按功能键Ctrl+F5、F5进行语法检测和执行,观察Grids和Messages窗口的信息,了解查询分析器的错误提示。
2、练习DCL语句
1)练习DCL语句
ⅰ在企业管理器(EnterpriseManager)中为数据库Northwind添加用户test_user
ⅱ用管理员登录,在查询分析器主窗口中输入以下语句:
usenorthwind
grantselectonsysuserstotest_user
denyselectoncategoriestotest_user
revokeselectoncustomerstotest_user
ⅲ断开查询分析器连接,以用户test_user身份重新登录,输入以下语句:
select*fromsysusers
select*fromcategories
select*fromcustomers
分别执行,看运行结果或错误提示。
ⅳ再次以管理员身份重新登录,数据库选择northwind,将语句
denyselectoncategoriestotest_user
修改为
revokeselectoncategoriestotest_user
执行,断开连接。
ⅴ以用户test_user身份登录,执行语句
Select*fromcategories
看运行结果。
断开连接。
ⅵ以管理员身份登录,数据库选择northwind,执行语句
revokeselectoncategoriestopublic
ⅶ断开连接,换成test_user登录,执行
Select*fromcategories
看错误提示。
断开连接。
ⅷ以管理员身份登录,将语句
revokeselectoncategoriestotest_user
修改为
Grantselectoncategoriestotest_user
执行,断开连接。
ⅸ以test_user登录,执行语句
Select*fromcategories
看运行结果。
*了解grant、deny、revoke权限,区分deny和revoke权限的差异。
2)DCL语句综合练习
ⅰYoucreatedaWindows2000securitygroupforusersoftheSalesReportingdatabaseonyourSQLServer2000installationandplacedthesalesmanagersinthisgroup.YouthengrantedthisgroupaccesstoSQLServer2000andtheSalesReportingdatabase.Inaddition,youmadethisgroupamemberofthedb_datawriteranddb_datareaderfixeddatabaseroles.Severalmembershavecomplainedthatalthoughtheycanaccessthedataineachtableandviewinthedatabase,theyareonlyabletoexecutecertainstoredprocedures,butnotall.Tomakemattersmorecomplicated,oneofthemembersofthisgroupcanexecuteallofthestoredprocedureswithoutaproblem.Whatarethelikelycausesofthisproblem?
ⅱYouneedtograntcertainuserstheabilitytoinsertnewdataintoahighlysecuredatabase.Theyalsorequireverylimitedlookuprightstothedata.Youareconcernedaboutsecurityforthisdata.Whatisthemostsecuremethodyoucanusetoallowtheuserstoperformtheirtask?
ⅲYouaredesigningasecuritystrategyforyourSQLServer2000installation.YouareonlyallowingaccesstoWindows2000usersandgroups.Isthereanyadvantagetoapplyingpermissionstouser-defineddatabasegroupsratherthandirectlytoWindowsgroups?
3、练习DDL语句,并掌握数据类型的定义
1)练习DDL语句
ⅰ打开查询分析器,创建数据库
createdatabasetest
onprimary
(
name='test_data',
filename='f:
\test.mdf',
size=3,
maxsize=10,
filegrowth=1
)
logon
(
name='test_log',
filename='f:
\test.ldf',
size=1,
maxsize=5,
filegrowth=10%
)
ⅱ修改数据库,设置数据库选项
alterdatabasetest
setread_only
alterdatabasetest
setread_write
alterdatabasetest
addfilegroupfg1
usetest
alterdatabasetest
addfile
(name='test_sec',
filename='f:
\test_sec.ndf',
size=3)
tofilegroupfg1
alterdatabasetest
removefilegroupfg1
alterdatabasetest
removefiletest_sec
ⅲ创建用户自定义数据类型:
Sp_addtypecity,’nvarchar(15)’,’null’
Sp_addtypezipcode,’varchar(6)’,’notnull’
ⅳ创建表,并使用自定义数据类型:
Createtabletest_table
(
Idintidentity(1,1)notnull,
Em_namevarchar(20),notnull,
Ageint,
Citycity,
Zipcode,zipcode,
Salaryfloat,
[rateoftaxation]float,
taxassalary*[rateoftaxation]
)
ⅴ修改表定义:
Altertabletest_table
Addremarkstext
Altertabletest_table
Altercolumnem_namevarchar(25)
ⅵ向表test_table中添加数据:
insertintotest_table
values(1,'Jhon',27,'Shenyang','110168',2087.2,0.25,521.8,'none')
看错误提示。
insertintotest_table(em_name,age,City,Zipcode,salary,[rateoftaxation],remarks)
values('Jhon',27,'Shenyang','110168',2087.20,0.250,'none')
理解表对象定义,对添加数据的限制。
2)DDL语句综合练习
ⅰ为沈阳理工大学汽车与交通学院创建学生信息数据库(student_info)。
ⅱ在已创建的数据库中,创建以下自定义数据类型:
学生姓名(stu_name)
地址(address)
邮政编码(zipcode)
出生日期(birthday)
学号(stu_id)
ⅲ建立学生信息表(stu_info),使用已创建的自定义数据类型,要求包含以下列:
学号、学生姓名、性别、年龄、出生日期、寝室号码、寝室电话、电子邮件、手机号码、家庭地址、邮政编码、家庭电话
ⅳ分别建立英语成绩表(English)、高数成绩表(Advanced_Mathematics)、机械制图成绩表(Mechanical_Drawing)、C语言成绩表(C_Program)、数据库原理成绩表(Principle_of_Database),要求包含以下列:
学号、平时成绩1、平时成绩2、平时成绩3、中期成绩、实验成绩、期末成绩、总评成绩
4、练习Select语句和DML语句
1)select语句和DML语句练习(使用Northwind和Pubs数据库)
ⅰ指定列查询:
所有列
SELECT*FROMcustomers
特定列
SELECTcustomerID,companyName,ContactNameFROMcustomers
加入字符串
SELECTLastName,FirstName,Title,'IDENTIFICATIONNUMBER:
',EmployeeIDFROMemployees
表达式
SELECTproductID,productName,(UnitPrice*1.1)ASpriceFROMProducts
SELECTlastname+','+SUBSTRING(firstname,1,1)ASname,TitleFromemployees
SELECTregion,isnull(Region,'thisisnull')FROMcustomers
ⅱWhere子句:
比较
SELECT*FROMcustomersWHERECity=’London’
SELECTproductname,unitpriceFROMproductsWHEREunitprice>100
基于范围
SELECTproductname,unitpriceFROMproducts
WHEREunitpriceBETWEEN100AND200
基于列表
SELECT*FROMEmployees
WHEREcityIN(‘London’,’Newyork’,’Berlin’)
基于字符
SELECT*FROMproducts
WHEREproductNameLIKE‘chef%’
ⅲ格式化结果集:
SELECTproductName,categoryID,unitpriceFROMproducts
ORDERBYcategoryIDASC,unitpriceASC(升序)
SELECT*FROMemployees
ORDERBYlastnameASC,firstnameDESC(降序)
ⅳ查询多重表:
内连接
USEpubs
SELECTa.au_fname,a.au_lname,p.pub_name
FROMauthorsASaINNERJOINpublishersASp
ONa.city=p.city
ORDERBYa.au_lnameASC,a.au_fnameASC
外连接
USEpubs
SELECTa.au_fname,a.au_lname,p.pub_name
FROMauthorsaLEFTOUTERJOINpublishersp
ONa.city=p.city
ORDERBYp.pub_nameASC,a.au_lnameASC,a.au_fnameASC
自连接
UsePubs
SELECTa.au_id,b.au_id,a.title_id
FROMtitleauthorasa
INNERJOINtitleauthorasb
ONa.title_id=b.title_id
WHEREa.au_id 交叉连接 UsePubs SELECTa.au_id,b.au_id,a.title_id FROMtitleauthorasa crossJOINtitleauthorasb 连接多个结果集 USENorthwind SELECTContactName,CompanyName,City,Phone FROMCustomers WHERECountryIN('USA','Canada') UNION SELECTContactName,CompanyName,City,Phone FROMCustomers ORDERBYCompanyName,ContactNameASC ⅴ高级查询技术: 嵌套子查询 USEpubs SELECTpub_name FROMpublishers WHEREpub_idIN(SELECTpub_idFROMtitlesWHEREtype=‘business’) 相关子查询 SELECTau_lname,au_fname FROMauthors WHERE100IN (SELECTroyaltyper FROMtitleauthor WHEREtitleauthor.au_ID=authors.au_id) ⅵ数据分组与汇总 聚集函数 SELECTSUM(ytd_sales)FROMtitles SELECTavg(price)FROMtitles SELECTavg(isnull(price))FROMtitles SELECTAVG(advance),SUM(ytd_sales) FROMtitles WHEREtype='business' 分组与汇总 USEpubs SELECTpub_id,total=SUM(ytd_sales) FROMtitles GROUPBYpub_id HAVINGSUM(ytd_sales)>40000 USEpubs SELECTpub_id,total=SUM(ytd_sales) FROMtitles GROUPBYpub_id WITHRollUp ⅶ从结果集创建表 SELECTproductnameASproduct, unitpriceASprice,(unitprice*1.1) AStax INTOpricetable FROMnorthwind.dbo.products ⅷDML语句练习 Insert语句练习 Update语句练习 UPDATEtitles SETprice=price*2 FROMtitlesINNERJOINpublishersONtitles.pub_id=publishers.pub_id ANDpub_name='NewMoonBooks’ Delete语句练习 . 2)select语句和DML语句综合练习 ⅰ向练习3中的学生信息表和各科成绩表中输入数据 ⅱ使用连接或子查询对学生信息表和各科成绩表进行查询,并用结果集生成学生成绩总表,要求包含以下列: 学号、科目、中期成绩、期末成绩、总评成绩 且所有数据已按学号和科目进行了排序。 ⅲ对学生成绩总表进行修改,要求: 缺成绩的科目,改成绩为0; 低于60分且高于55分的成绩,改为60分; 低于55分且高于50分的成绩,改为50分 ⅳ给出各科目的总分和平均分,并按科目给出成绩高于平均分的学生名单 5、综合项目设计 完成实验2、3、4综合练习,并为练习3、4的使用设置权限,并完成统计,要求: 1)从企业管理器中为学生信息数据库建立用户,并在查询分析器中设置权限: 将public的默认权限对所有用户表设置为revoke; 教务管理帐号: 可以读取各科成绩,可以修改学生信息表; 各科教师帐号: 只能读取和修改自己科目的内容; 学生帐号: 只能查询成绩。 2)完成统计: 教务管理帐号: 统计各学生所有科目的总分和平均分,并排序 教师帐号: 统计自己科目的总分和平均分,并作成绩分析 学生帐号: 统计自己不及格成绩的科目,并分别列出成绩。 实验报告要求: 用英文回答DCL语句综合练习三个问题。 写出select语句和DML语句综合练习ⅱ、ⅲ、ⅳ小题的语句。 实验二SQLServer数据库管理和数据库编程 一、实验项目名称 SQLServer数据库管理和数据库编程 二、实验目的和要求 1、掌握数据库的恢复技术 2、通过上机操作理解数据库的并发控制 3、熟练掌握数据库安全性策略 4、理解数据库的完整性要求,并能用于实际设计 三、实验内容、方法和步骤 1、掌握数据库的恢复技术 1)用企业管理器进行备份: 开始(Start)—程序(Program)—MicrosoftSQLServer—企业管理器(EnterpriseManager),选择要备份的数据库,鼠标右击,所有任务(AllTask)—备份数据库(BackupDatabase) *注意备份的选项: 如图: 2)用SQL语句进行备份: ⅰTocreatebackupdevicesusingTransact-SQL ①EnsurethatyouareloggedontotheLocalServerasAdministrator. ②ClickStart,pointtoPrograms,pointtoMicrosoftSQLServer,andthenclickQueryAnalyzer. ③IntheConnectToSQLServerdialogbox,connecttoLocalServerusingWindowsauthentication.ClickOK. ④Inputthesp_addumpdevicetocreatebackupdevices. ⑤Inthequerypane,typesp_helpdeviceandthenclicktheExecuteQuerybuttononthetoolbar.Noticethatthediskdevicesappear,alongwithinformationregardingthemaster,model,andtempdbdatabases. ⑥CloseSQLQueryAnalyzer.Donotsaveanychanges. ⅱToperformbackupsusingTransact-SQL ①backupdatabasetothebackupdevicewithoptionfullbackup,UsingBackupStatement. ②Changetheobjectsordataonthedatabase.Thenbackupdatabasetothebackupdevicewithoptiondifferentialbackup,UsingBackupStatement. 3)用企业管理器进行恢复: 开始(Start)—程序(Program)—MicrosoftSQLServer—企业管理器(EnterpriseManager),选择要备份的数据库,鼠标右击,所有任务(AllTask)—恢复数据库(RestoreDatabase) *注意恢复选项,如图: 4)用SQL语句进行恢复: ⅰRestoringaCompleteDatabase ⅱRestoringaDataFileorFilegroup ⅲRestorationUsingtheMoveOption ⅳRestoringtoStandbyMode ⅴRestoringtoaSpecifiedPointinTime 2、通过上机操作理解数据库的并发控制 1)TouseTransact-SQLtoconfigureasessiontimeout ⅰOpenQueryAnalyzerandconnecttoyourlocalserver. ⅱIntheEditorpaneoftheQuerywindow,enterthefollowingTransact-SQLcode: SELECT@@LOCK_TIMEOUT Inthisstateme
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SQL Server实验课程指导书 Server 实验 课程 指导书