- 1、本文档共9页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
实验二 通过SQL语句创建与管理数据表
一、实验目的
(1)掌握查询分析器的使用。
(2)掌握通过SQL语句创建表的方法。
(3)掌握通过SQL语句修改表结构的方法。
(4)掌握通过SQL语句添加、修改、删除表数据的方法。
二、实验内容
1、通过SQL语句删除表
用SQL语句在数据库StudentStudent_info中删除实验一创建的studentStudent表、courseCourse表、SC表。
drop table SCdrop table Studentdrop table Course2、通过SQL语句创建表
用SQL语句在数据库StudentStudent_info中创建实验一中的studentStudent表、courseCourse表、SC表,结构如实验一中表2、表3、表4所示
表2:
create table Student(
Sno char(8) not null primary key, --学号
Sname varchar(8) not null, --学生姓名
Sex char(2) not null default 男, --性别
Birth smalldatetime not null, --出生年月
Classno char(3) not null, --班级号
Entrance_date smalldatetime not null, --入学时间
Homeaddr varchar(40) not null, --家庭住址
)
表3:
create table Course(
Cno char(3) not null primary key, --课程号
Cname varchar(20) not null, --课程名称
Total_perior smallint, --总学时
Credit tinyint, --学分
check (Total_perior3 and Credit0 and Credit=6)
)
表4:
create table SC(
primary key(Sno,Cno),
Sno char(8) not null foreign key references StudentSno), --学号
Cno char(3) not null foreign key references CourseCno), --课程号
Grade tinyint, --成绩
check(Grade=0 and Grade=100)
)
3、通过SQL语句管理表结构
(1)添加和删除列
a. 给studentStudent表增加身高(以米单位)statureStature列 ,类型为numeric(4,2),允许为空值,且身高值需小于3.0米。
alter table Studentadd Stature numeric(4,2),
constraint ck_Stature check(Stature3.0)
b. 给studentStudent表增加所在系Sdept列,字符型,长度2,不允许为空值。
alter table Studentadd Sdept char(8) not null
c. 给studentStudent表增加邮政篇码Postcode列,字符型,长度为6,可以为空,若不为空时,则要求其值只能出现数字,不能是其它字符。
alter table Studentadd Postcode char(6),
constraint ck_ps check(Postcode like [0-9][0-9][0-9][0-9][0-9][0-9])
d.删除studentStudent表中身高statureStature列。
alter table Studentdrop ck_Stature
alter table Studentdrop column Stature
(2)添加和删除约束
a.在studentStudent表添加约束:入学时间必须在出生年月之后。
alter table Studentadd constraint ck_data check(BirthEntrance_date)
b.给SC表的成绩gradeGrade列增加默认值约束,默认值为0.
alter table SCadd constraint ck_grade default(0
文档评论(0)