当前位置:文档之家› 数据库及基本表的建立

数据库及基本表的建立

一、实验目的1、掌握SQL SERVER的查询分析器和企业管理器的使用;2、掌握创建数据库和表的操作;二、实验内容和要求1、练习使用SQL语句、企业管理器(Enterprise Manager)创建数据库;2、练习使用SQL语句、企业管理器(Enterprise Manager)创建数据库表;三、实验主要仪器设备和材料1.计算机及操作系统:PC机,Windows 2000/xp;2.数据库管理系统:SQL sever 2005;四、实验方法、步骤及结果测试题目1、创建数据库“学生情况”:实现代码及截图:SQL语句create database学生情况题目2、将数据库“学生情况”改名为“student”SQL语句alter database学生情况modify name=student题目3、创建基本表SQL语句S表:use Studentcreate table S(Sno char(10) primary key,Sname char(10) not null,Ssex char(2) check(Ssex='男'or Ssex='女'),Ssp char(20),Sdept char(20),Sbirth datetime,Sfrom varchar(30),Schg char(10),Spa char(8) default('团员'),Snation char(8) default('汉族'),);C表:use Studentcreate table C(Cno char(10) primary key,Cname char(10) unique,Tname char(8),Cdept char(20),CCredit real check(CCredit>=0and CCredit<=20),);SC表:use Studentcreate table Sc(Sno char(10),Cno char(10),Grade real check(Grade>=0and Grade<=100),Remark varchar(50),primary key(Sno,Cno),foreign key(Sno) references S(Sno),foreign key(Cno) references C(Cno),);题目4、用sql语句将C表中的ccredit改为整型use Student/*首先手动删除约束才可以修改*/alter table C alter column CCredit int;/*重新建立约束*/alter table C add check(CCredit>=0and CCredit<=20);题目5、用sql语句在“学生”表中添加一格“备注”字段remark,变长字符型,并保存结果alter table S add remark varchar(50);题目6. 用sql语句将“学生”表中“专业”字段数据类型改为varchar,长度为30并保存结果alter table S alter column Ssp varchar(30);题目7. 用sql语句删除“学生成绩”表中的“备注”字段并保存结果alter table Sc drop column Remark;题目8. 通过sql语句向s表中添加信息。

INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('001','李春刚','男','计算机应用','CS','1985-5-10','河源','','团员','汉',''); INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('002','东学婷','女','计算机应用','CS','1986-10-24','包头','转系','团员','蒙','');INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('003','张五男','男','电子商务','MA','1984-2-17','上海','退学','团员','汉',''); INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('004','刘%','男','电子商务','MA','1985-4-24','巴盟','','团员','汉',''); INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('005','吴惠','女','软件开发','CS','1985-2-10','通辽','','团员','汉',''); INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('006','王涛','男','软件开发','CS','1984-9-8','赤峰','','团员','满','');INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('007','郭凤丽','男','应用电子','IS','1984-3-2','广州','','团员','蒙',''); INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('008','贾惠','男','应用电子','IS','1983-2-2','深圳','','团员','汉','');INSERT INTO S(Sno,Sname,Ssex,Ssp,Sdept,Sbirth,Sfrom,Schg,Spa,Snation,remark) VALUES('009','刘一%','男','软件开发','CS','1985-9-9','东莞','','团员','满','');题目10. 通过sql语句添加学生选课信息INSERT INTO Sc(Sno,Cno,Grade) VALUES('001','02',85)INSERT INTO Sc(Sno,Cno,Grade) VALUES('001','03',75)INSERT INTO Sc(Sno,Cno,Grade) VALUES('001','04',90)INSERT INTO Sc(Sno,Cno,Grade) VALUES('002','05',50)INSERT INTO Sc(Sno,Cno,Grade) VALUES('003','01',85)INSERT INTO Sc(Sno,Cno,Grade) VALUES('003','02',75)INSERT INTO Sc(Sno,Cno,Grade) VALUES('004','03',60)INSERT INTO Sc(Sno,Cno,Grade) VALUES('004','02',92)INSERT INTO Sc(Sno,Cno,Grade) VALUES('005','01',85)INSERT INTO Sc(Sno,Cno,Grade) VALUES('005','04',46)INSERT INTO Sc(Sno,Cno,Grade) VALUES('006','05',95)INSERT INTO Sc(Sno,Cno,Grade) VALUES('006','03',52)INSERT INTO Sc(Sno,Cno,Grade) VALUES('007','04',96)INSERT INTO Sc(Sno,Cno,Grade) VALUES('007','05',85)INSERT INTO Sc(Sno,Cno,Grade) VALUES('008','04',76)INSERT INTO Sc(Sno,Cno,Grade) VALUES('008','05',68)题目9.通过sql语句向C表中添加数据,并保存结果INSERT INTO C(Cno,Cname,Tname,Cdept,CCredit) VALUES('01','计算机应用','王晓梅','IS',4)INSERT INTO C(Cno,Cname,Tname,Cdept,CCredit) VALUES('02','高等数学','李一','MA',6)INSERT INTO C(Cno,Cname,Tname,Cdept,CCredit) VALUES('03','网页制作','张铁柱','CS',6)INSERT INTO C(Cno,Cname,Tname,Cdept,CCredit) VALUES('04','软件工程','付大鹏','CS',4)INSERT INTO C(Cno,Cname,Tname,Cdept,CCredit) VALUES('05','数据库','白一格','CS',6)用sql语句在S中增加约束,要求学号要用S开头,后接4位数的字符串,请写出操作步骤以及sql语句。

相关主题