资源描述:
《大三 sql 课后习题答案.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第二章3.上机练习题02程序代码如下:CREATEDATABASESTUDENT1ONPRIMARY(NAME=STUDENT1_data,FILENAME='E:DATASTUDENT1.mdf',SIZE=3,MAXSIZE=unlimited,FILEGROWTH=15%)LOGON(NAME=STUDENT1_log,FILENAME='E:DATASTUDENT1.ldf',SIZE=2,MAXSIZE=30,FILEGROWTH=2)03程序代码如下:createdatabasestudentsonprimary(name=students1,filename=
2、'E:DATAstudents1.mdf',size=5,maxsize=75,filegrowth=10%),(name=students12,filename='E:DATAstudents2.ndf',size=10,maxsize=75,filegrowth=1)logon(name=studentslog1,filename='E:DATAstudentslog1.ldf',size=5,maxsize=30,filegrowth=1),(name=studentslog2,filename='E:DATAstudentslog2.ldf',size=5,
3、maxsize=30,filegrowth=1)第三章:3上机练习题01程序代码如下:--创建表book的Transact-SQL语句:USEtest01GOCREATETABLEbook(book_idnchar(6)NOTNULL,book_namenchar(30)NULL,pricenumeric(10,2)NULL,CONSTRAINTPK_bookPRIMARYKEYCLUSTERED(book_idASC))ONPRIMARY--创建表uthor的Transact-SQL语句:CREATETABLEdbo.author(anthor_namenchar(4)NOTNU
4、LL,book_idnchar(6)NOTNULL,addressnchar(30)NOTNULL)ON[PRIMARY]--设置book中的book_id为主键,author表中的book_id为外键ALTERTABLEdbo.authorWITHCHECKADDCONSTRAINTFK_book_authorFOREIGNKEY(book_id)REFERENCESdbo.book(book_id)02程序代码如下:--利用Transact-SQL语句创建表booksales的代码。USEtest01GOCREATETABLEbooksales(book_idnchar(6)N
5、OTNULL,sellnumintNOTNULL,selldatedatetimeNOTNULL)ONPRIMARY--利用insert语句为表booksales添加数据:INSERTINTObooksalesVALUES('m00011',7,20/12/2008)INSERTINTObooksales(book_id,sellnum,selldate)VALUES('m00017',3,17/11/2008)--利用update语句为表booksales更新数据:UPDATEbooksalesSETsellnum=11WHEREbook_id='m00011'--利用delet
6、e语句删除表booksales的数据:DELETEFROMbooksalesWHEREbook_id='m00011'03程序代码如下:USEtest01GOCREATERULEsellnum_ruleAS@sellnum>=0EXECsp_bindrule'sellnum_rule','booksales.sellnum'04程序代码如下:--删除年以前的数据DELETEFROMbooksalesWHEREselldate<'1/1/2009'--删除所有数据TruncateTablebooksales第四章3上机练习题01程序代码如下:DECLARE@booknamenchar
7、(16)set@bookname='SQLServer数据库编程'02程序代码如下:USEtest01GOSETNOCOUNTONDECLARE@startdatedatetime,@enddatedatetimeSET@startdate='1/7/200812:12AM'SET@enddate='11/10/200912:00AM'SELECTDATEDIFF(year,@startdate,@enddate)SELECTDATEDIFF(month,@start