- 创建视图
ST_CO_SR ,它包含学号、姓名、性别、课程号、课程名、成绩等列,并输出该视图的所有记录。
create view ST_CO_SR as
select a.stno, a.stname, c.cno, cname, grade
from student a
inner join score b on a.stno = b.stno
inner join course c on c.cno = b.cno;
select *
from ST_CO_SR;
- 输出
- 创建视图
ST_computer ,它包含学生姓名、课程名、成绩等列,且专业为计算机,并输出该视图的所有记录。
create view ST_computer as
select stname,
cname,
grade
from student a
inner join score b on a.stno = b.stno
inner join course c on c.cno = b.cno
where speciality = '计算机';
select *
from ST_computer;
- 输出
- 创建视图
ST_AV ,它包含学生姓名、平均分等列,并输出该视图的所有记录。
create view ST_AV as
select stname, avg(grade) as "avgScore"
from student a
inner join
score b on a.stno = b.stno
group by stname;
select *
from ST_AV;
- 输出
- 写出在
teacher 表的tno 列上建立聚集索引的语句。
sp_helpconstraint teacher;
drop index teacher.PK__teacher__DC10824F0AD2A005;
alter table teacher
drop constraint PK__teacher__DC10824F0AD2A005;
create clustered index index_teacher_tnos on teacher (tno);
- 输出
- 写出在
course 表的credit 列上建立非聚集索引的语句,并设置填充因子。
create nonclustered index index_course_credit on course (credit)
with (PAD_Index = on,fillfactor = 5);
- 输出
- 在
score 表的grade 列添加CHECK 约束,限制grade 列的值为0-100 。
alter table score
add constraint check_grade check (grade between 0 and 100);
- 输出
- 使用
T-SQL 语句在student 表的stsex 列添加DEFAULT 约束,使stsex 列的默认值为男 。
alter table student
add constraint default_value default ('男') for stsex;
- 输出
- 删除
student 表的stno 列的PRIMARY KEY 约束,然后在该列添加PRIMARY KEY 约束。
sp_helpconstraint student;
alter table student
drop constraint PK__student__312D77347F60ED59;
alter table student
add constraint pk_student_stno primary key (stno);
- 输出
- 在
score 表的stno 列添加FOREIGN KEY 约束。
alter table score[主表]
add constraint fk_score_stno foreign key (stno) references student[从表] (stno);
- 输出
|