1、实验:表管理-创建及维护表一、实验目的与任务目的:旨在训练学生使用T-SQL语句创建及维护表,能根据给出的语法创建符合用户需求的表结构,通过表的创建,了解表的约束性规则及表数据的维护。任务:创建表及表数据的维护,在数据库中创建相应的表,并对表进行数据的添加、删除及修改。任务1:使用T-SQL语句创建数据库,脚本如下:CREATE DATABASE SchoolInfo -创建数据库任务2:创建表Department列名数据类型说明DepIDintPrimary KeyDepNamevarchar(20)NOT NULL答案:create table department(depid int,d
2、epname varchar(20) not null,primary key(depid)任务3:创建表Course列名数据类型说明CourseIDintPrimary KeyCourseNamevarchar(20)NOT NULL答案:create table course(courseid int ,coursename varchar(20) not null,primary key(courseid)任务4:创建表Student列名数据类型说明StuIDchar(10)Primary Key,学号以A,B或Z打头,后头跟两位数字代表的系号,再跟1位数字代表的班号,最后跟2位数字代表
3、的个人学号StuNamevarchar(20)NOT NULLStuAgeintStuAge在15到100之间StuSexchar(2)只能取值为男或女,默认男StuCityvarchar(20)无StuScorefloatStuScore在0到100之间DepIDint参照Department表主键答案:Create table Student(StuID char(10) constraint pkStuID primary key constraint chkStuID check(StuID like A,B,Z0-90-90-90-90-9) ,StuName varchar(20)
4、 not null,StuAge int constraint chkStuAge check(StuAge between 15 and 100) ,StuSex char(2) constraint chkStuSex check(StuSex in(男,女) constraint defStuSex default 男,StuCity varchar(20) ,StuScore float constraint chkStuScore check(StuScore between 0 and 100) ,DepID int constraint fkDepID foreign key r
5、eferences Department(DepID)任务5:创建表SC列名数据类型说明StuIDchar(10)主键,外键CourseIDint主键,外键Scorefloat介于0到100之间答案:Create table SC(StuID char(10) constraint fkStuID foreign key references Student(StuID) ,CourseID int constraint fkCourseID foreign key references Course(CourseID) Constraint pkStuCourse primary key(S
6、tuID,CourseID) , Score float constraint chkScore check(Score between 0 and 100)任务6:使用insert语句为各表添加若干记录(试插入违反约束性规则的记录查看执行的结果)答案:insert into Department values(1,computer)insert into Department values(2,English)insert into Student values(A00001,jianghao,18,男,jiangxi,60,1)insert into Student values(A000
7、02,gaoemng,19,男,feicheng,80,1)insert into Student values(A00003,linxucheng,19,男,jiashan,80,2)insert into Course values(1,Database)insert into Course values(2,English)insert into SC values(A00001,1,70)insert into SC values(A00002,1,85)insert into SC values(A00002,2,85)任务7:使用update语句将1系学生的StuScore值加1,
8、年龄加1岁。答案:update Studentset StuScore=StuScore+1,StuAge=StuAge+1where DepID=1任务8:删除SC表选择1号课程的记录。答案:delete from SCwhere CourseID=1三、内容与要求 课题名称表的创建及维护 (一)课题内容 按照任务1至任务8逐步完成,完成后学生可以独立管理表,对表创建,表数据的维护。 (二)课题要求按照实验任务书逐步完成实验(三)考核及报告要求 1. 考核能独立完成任一项任务。2. 报告要求提交一份完整的实验报告,实验报告中详细给出每个任务的解决方案,解决方案后附带每个任务的实验结果。四、主要仪器设备硬件:PC机软件:SQL SERVER 2005