收藏 分销(赏)

数据库实验二-通过SQL语句创建与管理数据表.docx

上传人:二*** 文档编号:4511362 上传时间:2024-09-26 格式:DOCX 页数:9 大小:330.17KB
下载 相关 举报
数据库实验二-通过SQL语句创建与管理数据表.docx_第1页
第1页 / 共9页
亲,该文档总共9页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、实验二 通过SQL语句创建与管理数据表一、实验目的(1)掌握查询分析器的使用。(2)掌握通过SQL语句创建表的方法。(3)掌握通过SQL语句修改表结构的方法。(4)掌握通过SQL语句添加、修改、删除表数据的方法。二、实验内容1、通过SQL语句删除表 用SQL语句在数据库StudentStudent_info中删除实验一创建的studentStudent表、courseCourse表、SC表。drop table SC_20103322drop table Student_20103322drop table Course_201033222、通过SQL语句创建表用SQL语句在数据库Studen

2、tStudent_info中创建实验一中的studentStudent表、courseCourse表、SC表,结构如实验一中表2、表3、表4所示表2:create table Student_20103322(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 smalldatetim

3、e not null, -入学时间Homeaddr varchar(40) not null, -家庭住址)表3:create table Course_20103322(Cno char(3) not null primary key, -课程号Cname varchar(20) not null, -课程名称Total_perior smallint, -总学时Credit tinyint, -学分check (Total_perior3 and Credit0 and Credit=0 and Grade=100)3、通过SQL语句管理表结构(1)添加和删除列a. 给studentStu

4、dent表增加身高(以米单位)statureStature列 ,类型为numeric(4,2),允许为空值,且身高值需小于3.0米。alter table Student_20103322add Stature numeric(4,2),constraint ck_Stature check(Stature3.0)b. 给studentStudent表增加所在系Sdept列,字符型,长度2,不允许为空值。alter table Student_20103322add Sdept char(8) not nullc. 给studentStudent表增加邮政篇码Postcode列,字符型,长度为

5、6,可以为空,若不为空时,则要求其值只能出现数字,不能是其它字符。alter table Student_20103322add Postcode char(6),constraint ck_ps check(Postcode like 0-90-90-90-90-90-9)d.删除studentStudent表中身高statureStature列。alter table Student_20103322drop ck_Staturealter table Student_20103322drop column Stature(2)添加和删除约束a.在studentStudent表添加约束:入

6、学时间必须在出生年月之后。alter table Student_20103322add constraint ck_data check(BirthEntrance_date)b.给SC表的成绩gradeGrade列增加默认值约束,默认值为0.alter table SC_20103322add constraint ck_grade default(0) for Gradec.删除gradeGrade列的默认值约束alter table SC_20103322drop ck_grade4、 通过SQL语句添加、修改、删除表中数据(1)插入数据a.studentStudent表、course

7、Course表、SC表的记录见实验一的表5、表6、表7,其它数据可自行添加。要求studentStudent表和SC表中数据包括了每位同学自己的学号。insert into Student_20103322values (20110001,张虹,男,1992-09-11,051,2011-09-01,南京,计算机系,200413)insert into Student_20103322values (20110002,林红,女,1991-11-12,051,2011-09-01,北京,计算机系,100010)insert into Student_20103322values (2011010

8、3,赵青,男,1993-05-11,061,2011-09-01,上海,软件工程,200013)insert into Course_20103322values (001,高数,96,6)insert into Course_20103322values (002,C语言程序设计,80,5)insert into Course_20103322values (003,JAVA语言程序设计,48,3)insert into Course_20103322values (004,Visual Basic,48,4)insert into SC_20103322values (20110001,0

9、01,89)insert into SC_20103322values (20110001,002,78)insert into SC_20103322values (20110001,003,89)insert into SC_20103322values (20110002,002,60)insert into SC_20103322values (20110103,001,80)b.执行如下语句:insert into studentStudent(snoSno,snameSname,sexSex) values(20101101,赵青,男),该语句能成功执行吗?为什么?不能执行。因为有

10、不允许为Null的列存在。c. 执行如下语句:insert into sc values(20110103,005,80),该语句能成功执行吗?为什么?不能执行。因为Course_20103322表中的课号只出现了001、002和003,又因为SC_20103322表中Cno是外键,只能出现Course_20103322中Cno中的值,所以不能执行。(2)修改数据 a.使用T-SQL语句,将courseCourse表中的课程号为002的学分改为4,总学时改为64。update Course_20103322set Credit=4,Total_perior=64where Cno=002b.使用T-SQL语句,将SC表中的选修了002课程的同学的成绩*80%。update SC_20103322 set Grade=Grade*0.8where Cno=002(3)删除数据 a. 使用T-SQL语句,删除选修了“C语言程序设计”的学生的选课记录。deletefrom SC_20103322where Cno=002b. 使用T-SQL语句,删除所有的学生选课记录。deletefrom SC_20103322说明:删除后,请重新插入SC表中的记录。

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 通信科技 > 数据库/数据算法

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服