SQl Server2012版本的使用和基本操作
1. 使用 T-SQL 创建数据库
1)在 Query Analyzer 工具下建立学生选课数据库(XSXK)。
2)根据学生选课数据库的数据库物理结构设计,定义基本表、定义相应的约束条件和索 引。
3)数据库中各表的结构物理结构和要求如下:
(1) student(学生表)
| 字段名称 | 数据类型 | 长度大小 | 允许空 | 备注 |
|---|---|---|---|---|
| sno | char | 10 | 学号 | |
| sname | char | 8 | 姓名 | |
| ssex | char | 2 | 性别 | |
| sage | smallint | √ | 年龄 | |
| sdept | char | 30 | √ | 所在院系 |
| stel | char | 13 | √ | 联系电话 |
主键:sno
索引:sname(升序)
check 约束:年龄大于 18, 性别的取值只能为男或女
default 约束:性别默认为男 更改学生表的结构,取消姓名不允许为空的约束
(2)Course (课程表)
| 字段名称 | 数据类型 | 长度大小 | 允许空 | 说明 |
|---|---|---|---|---|
| cno | char | 10 | 课程编号 | |
| cname | char | 16 | 课程名称 | |
| ccredit | smallint | 学分 | ||
| cpno | char | 10 | √ | 先行课 |
主键:cno 索引: cno (升序)+ccredit(降序)
更改课程表的结构,增加属性列教师 ctech(类型是 char ,长度 20)
(3)SC(选课表)
| 字段名称 | 数据类型 | 长度大小 | 允许空 | 备注 |
|---|---|---|---|---|
| sno | char | 10 | 学号 | |
| cno | char | 10 | 课程编号 | |
| grade | smallint | √ | 成绩 |
主键:sno+cno
各个表的参照完整性约束:
(1) FK_SC_ student 主键表:student 外键表:SC 主键:sno 外键:sno
(2) FK_SC_ course 主键表:course 外键表:SC 主键:cno 外键:cno
2、实验结果(大小写无所谓)
1. 创建XSXK数据库
create database XSXK
on primary
( name = 选课管理_data,
filename = 'E:\SQL Sevser\选课管理_data',
size = 10MB,
maxsize= 50MB,
filegrowth = 10%
)
log on
( name = 选课管理_log,
filename = 'E:\SQL Sevser\选课管理_log.ldf',
size = 5MB,
maxsize= 20MB,
filegrowth = 5%
)
go
2. 创建student表
use XSXK
go
create table student(
sno char(10) primary key not null,
sname char(8) not null,
ssex char(2) not null,
sage smallint ,
sdept char(30) ,
stel char(13)
)
go
3. 创建Course表
use XSXK
go
create table Course(
cno char(10) primary key not null,
cname char(16) not null,
ccredit smallint not null,
cpno char(10)
)
go
4. 创建SC表
use XSXK
go
create table SC(
sno char(10) not null,
cno char(10) not null,
grade smallint,
constraint c1 primary key (sno,cno)
)
go
5. 姓名 降序
use XSXK
go
create UNIQUE nonclustered index idx_name on student(sname desc)
go
//删除索引
use XSXK
go
drop index student.idx_name
go
6. 检查约束
use XSXK
go
alter table student
add constraint CK_student_age CHECK(sage>18),
go
alter table student
add constraint CK_student_sex CHECK(ssex in('男','女'))
go
7.默认约束
use XSXK
go
alter table student
add constraint DE_student_sex default'男' for ssex
go
8. cno(升序) + ccredit(降序)
use XSXK
go
create unique nonclustered index idx_credit on Course(cno,ccredit desc)
go
9. 增加属性ctech 列
use XSXK
go
alter table Course
add ctech char(20)
go
10. 取消sname 不为空
use XSXK
go
alter table student
alter column sname
char(8)
null
go
11. 参照完整性约束
use XSXK
go
alter table SC
add foreign key(sno)references student(sno)
go
alter table SC
add foreign key(cno)references Course(cno)
go
结果图:


8303

被折叠的 条评论
为什么被折叠?



