实验目的及要求:
|
实验内容及步骤:
(1)、数据库中主要包括三个关系,分别是:学生信息,课程信息和选课信息。数据库XSGL包含下列3 个表: a) student:学生基本信息。 b)course:课程信息表。 c)sc:学生选课表。 创建XSGL数据库 create database XSGL on primary (name=XSGL, filename='D:\database\XSGL.mdf', size=10, maxsize=100, filegrowth=5) log on (name=XSGL_log, filename='D:\database\XSGL_log.ldf', size=10, maxsize=50, filegrowth=5) 创建学生基本信息表 use XSGL create table student (sno char(10) primary key, sname char(8) not null, ssex char(2) default '男' check(ssex='男' or ssex='女'), sage int check(sage>=10 and sage<=100), sdept char(10)); 注释以上代码创建课程信息表 use XSGL create table course (cno char(2) primary key, cname char(30) unique, cpno char(2), credit int); 注释以上代码继续创建学生选课表 use XSGL create table sc (sno char(10), cno char(2), grade smallint, primary key(sno,cno), foreign key(sno) references student(sno), foreign key(cno) references course(cno)) (2)、输入表中的记录 分别在student表、course表和sc表中输入如下表中的记录:
(3)、向创建的表中输入数据,测试所创建的完整性约束是否起作用 测试以下约束为例,将学生年龄更改为111,测试约束是否起作用 sage int check(sage>=10 and sage<=100), (4)、用SQL语言ALTER语句修改表结构;alter
use XSGL Alter table student add scome text;
use XSGL Alter table student drop column come;
右键单击SC表à选择“设计”à右键单击CNO行的钥匙图标à选择“关系”à复制标识的名称得到“FK__sc__cno__403A8C7D” use XSGL Alter table sc drop constraint FK__sc__cno__403A8C7D;
use XSGL alter table sc add constraint FK__sc__cno__403A8C7D foreign key (cno) references course(cno); (5)、重新定义一个简单表,然后用SQL语言DROP语句删除该表结构; 创建表d,仅有一列,列名为d use XSGL create table d (d int,); 删除表 use XSGL drop table d; (6)、用SQL语言CREATE INDEX语句定义表STUDENT的SNAME字段的降序唯一索引; use XSGL create index cao_jing on student (sname desc); (7)、用SQL语言DROP语句删除(6)中创建的索引; use XSGL drop index cao_jing on student; 注意:想要word版本可以去查看我的资源下载 |
数据库sql server实验报告 之 使用SQL语言进行建表和修改表结构(2/8)
于 2023-01-01 00:24:41 首次发布