2实验二 通过SQL语句创建与管理数据表

更新时间:2023-12-10 10:12:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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

一、实验目的

(1)掌握查询分析器的使用。

(2)掌握通过SQL语句创建表的方法。 (3)掌握通过SQL语句修改表结构的方法。

(4)掌握通过SQL语句添加、修改、删除表数据的方法。

二、实验内容

1、通过SQL语句删除表

用SQL语句在数据库Student_info中删除实验一创建的Student表、Course表、SC表。 1、选择Student_info数据库,在该数据库环境中“新建查询”,然后完成删除操作

2、分别填写如下SQL语言 ①、drop table Student

②、drop table Course

③、drop table SC

3、删除操作完成

2、通过SQL语句创建表

用SQL语句在数据库Student_info中创建实验一中的Student表、Course表、SC表,结构如实验一中表2、表3、表4(即创建出空表即可)所示 ①、创建Student表

create table Student( Sno char(8) primary key, Sname varchar(8) not null, Sex char(2) not null,

Birth smalldatetime not null, Classno char(3) not null,

Entrance_date smalldatetime not null, Home_addr varchar(40) )

②、创建Course表

create table Course( Cno char(3) primary key, Cname varchar(20) not null,

Total_perior smallint check(Total_perior>0), Credit tinyint check(Credit<=6 and credit>0) )

③、创建SC表

create table SC( Sno char(8) not null, Cno char(3) not null,

Grade tinyint check(Grade>=0 and Grade<=100), primary key(Sno,Cno),

foreign key(Sno) references Student(Sno), foreign key(Cno) references Course(Cno) )

3、通过SQL语句管理表结构 (1)添加和删除列

a. 给Student表增加身高(以米单位)Stature列 ,类型为numeric(4,2),允许为空

值,且身高值需小于3.0米。

alter table Student add Stature numeric(4,2)

check(Stature<=3.0 and Stature>=0)

b. 给Student表增加所在系Sdept列,字符型,长度2,不允许为空值。

alter table Student add Sdept char(2) not null

c. 给Student表增加邮政篇码Postcode列,字符型,长度为6,可以为空,若不为空时,则要求其值只能出现数字,不能是其它字符。

alter table Student add Postcode char(6)

check(Postcode Like '[1-9][0-9][0-9][0-9][0-9][0-9]')

d.删除Student表中身高Stature列。

①、添加Stature列时就已知该列存在约束条件,若要删除该列,必须先删除约束条件,则

首先必须先找出约束条件的约束名称。以下有两种方法:

1、写入SQL语句找出

alter table Student drop column Stature

2、运用企业管理器找出 a、打开Student表

b、选择Stature行,单击右键,选择“CHECK约束”

c、约束名称显而易见

②、其次删除Stature约束

本文来源:https://www.bwwdw.com/article/zcz5.html

Top