关于sql server的问题,高手帮个忙啊,谢谢啦!!!

现有学生表和班级表的设计结构如下
学生表
序号 字段名称 名称说明 数据类型 备注说明
1 studentid 学生编号 整型 主键约束 自动增长
2 studntname 学生名称 字符型 非空约束
3 Studentsex 学生性别 字符型 只能填男或女
4 Class_id 班级编号 整型 该表的外键和班级表关联
班级表

序号 字段名称 字段说明 数据类型 备注说明
1 Class_id 班级编号 整型 主键约束
2 Class_name 班级名称 字符型 非空约束
3 Niid 年级编号 整型 该表外键

 根据学生表和班级表的设计用t_sql语句写出这俩个表的建表语句 ,并要加上字段的约束条件和这俩个表主外键约束
 选出学生表中所有男学生且学生编号(studentid) 大于等于10的学生信息并按姓名(studentname) 降序排序
 用内连接方式删除学生表中所有一年级的学生
 选出学生表student 中一班(class_id=1) 中前2条学生信息
 统计出学生表student中,四班(class_id=4)的所有学生数并用 stucounts作为列的别名
 写个触发器 当删除班级表的某一班级时侧把学生表对应的该班学生信息删掉
 写个视图student_class_view 选出班级编号为2的所有学生姓名,班级名称
 写个存储过程 student_ select_out_pro 传入参数@studentname ,返回学生学号参数@studentid
 删除student表中的索引 stuindex
 写个存储过程 student_insert_out_pro ,这个存储过程由三个输入参数@studentname,@studentsex,@classid 和一个输出参数@count, 通过这些输入参数向学生表中插入信息,并返回改表中所有学生总数 (该表主键是自动增长的)

高手帮个忙把语句写出来,非常感谢

呵,50分太少了,毕竟工作量挺大的,不过现在刚好有时间空,帮你弄弄吧:

--根据学生表和班级表的设计用t_sql语句写出这俩个表的建表语句 ,并要加上字段的约束条件和这俩个表主外键约束
create table tb_class
(
Class_id int primary key,
Class_name varchar(50) not null,
Niid int /*这里说有外键,但没见关联的表,你自己补上去外键吧*/
)
go

create table tb_student
(
studentid int identity(1,1) primary key,
studntname varchar(50) not null,
Studentsex varchar(10) check (Studentsex in ('男','女')),
Class_id int references tb_class(Class_id)
)
go

--选出学生表中所有男学生且学生编号(studentid) 大于等于10的学生信息并按姓名(studentname) 降序排序
select * from tb_student where Studentsex='男' and studentid >= 10 order by studntname desc
go
--用内连接方式删除学生表中所有一年级的学生
delete a from tb_student a inner join tb_class b on a.Class_id=b.Class_id where b.Class_name = '一年级'
go
--选出学生表student 中一班(class_id=1) 中前2条学生信息
select top 2 * from tb_student where class_id=1
go
--统计出学生表student中,四班(class_id=4)的所有学生数并用 stucounts作为列的别名
select count(*) as stucounts from tb_student where class_id=4
go
--写个触发器 当删除班级表的某一班级时侧把学生表对应的该班学生信息删掉
create trigger tr_class on tb_class
for delete
AS
begin
delete a from tb_student a inner join deleted b on a.Class_id=b.Class_id
end
go

--写个视图student_class_view 选出班级编号为2的所有学生姓名,班级名称
create view student_class_view
AS
select a.studntname,b.Class_name from tb_student a inner join tb_class b on a.Class_id=b.Class_id where a.Class_id=2
go

--写个存储过程 student_ select_out_pro 传入参数@studentname ,返回学生学号参数@studentid
create procedure student_select_out_pro
@studentname varchar(50),
@studentid int OUTPUT
as
begin
select @studentid=studentid from tb_student where studntname=@studentname
end
go

--删除student表中的索引 stuindex (这个索引好象你前面没说要建立喔,所以,执行这个语句会报对象不存在的)
drop index tb_student.stuindex
go

--写个存储过程 student_insert_out_pro ,这个存储过程由三个输入参数@studentname,@studentsex,@classid 和一个输出参数@count, 通过这些输入参数向学生表中插入信息,并返回改表中所有学生总数 (该表主键是自动增长的)
create procedure student_insert_out_pro
@studentname varchar(50),
@studentsex varchar(10),
@classid int,
@count int OUTPUT
as
begin
insert into tb_student(studntname,Studentsex,Class_id) values(@studentname,@studentsex,@classid)
select @count=count(*) from tb_student
end
go
温馨提示:答案为网友推荐,仅供参考
相似回答