Sql | Teacher teaching management system(function & stored procedure & subquery and other examples)

一、设计并创建数据库( ),保存与教师授课有关的信息,包括:教师基本信息、课程基本信息、班级信息、授课信息等,设置各表的完整性约束条件(如主键、外键等)。录入以上信息。

1、设计并创建数据库( )

create database DB_teacher;

2、保存与教师授课有关的信息,包括:教师基本信息、课程基本信息、班级信息、授课信息等,设置各表的完整性约束条件(如主键、外键等)

create table teacher
(
	id int not null Identity(1,1) primary key,
	tname varchar(20) not null,
	tsex varchar(2) not null,
	tphone varchar(22) not null,
	tclass varchar(20) not null,
	tteaching varchar(50) not null,
	ttime int not null
);

3、录入以上信息(手动输入)
在这里插入图片描述

二、创建函数( ),根据输入课程名称和班级得出该课程的授课教师。

创建一个名叫select_teacher(
@teaching_name varchar(20),
@teaching_class varchar(20)
)函数

-- ================================================
-- Template generated from Template Explorer using:
-- Create Scalar Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		ZeroIce
-- Create date: Name & Class
-- Description:	Enter the course name and class to find the instructor of the course
-- =============================================
CREATE FUNCTION select_teacher
(
	@teaching_name varchar(20),
	@teaching_class varchar(20)
)
RETURNS varchar(20)
AS
BEGIN
	DECLARE  @name varchar(20) 
	SELECT @name = tname from teacher WHERE tclass = @teaching_class and  tteaching = @teaching_name
	RETURN @name
END
GO

测试:
在这里插入图片描述

三、创建存储过程( ),根据系名查询出该系所有总课时数超过300的教师。
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		ZeroIce
-- Create date: Dpart
-- Description:	Find out the teachers whose total class hours exceed 300 according to the department name
-- =============================================
CREATE PROCEDURE GetSumTeacher
	@Dpart VARCHAR(20)
AS
BEGIN
	SELECT tname AS '姓名' , @Dpart AS '系名' , SUM(ttime) AS '总课时' 
	FROM dbo.teacher 
	WHERE tdpart = @Dpart  
	GROUP BY  tname 
	HAVING SUM(ttime) > 300
END
GO

测试:
在这里插入图片描述

四、创建存储过程( ),根据输入教师号显示该教师的授课信息,并统计教授的课程门数、班级数和总课时数等。
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		ZeroIce
-- Create date:	Phone
-- Description:	According to the input teacher number display the teacher's teaching information, and statistics the number of courses taught, classes and total class hours.
-- =============================================
CREATE PROCEDURE GetEveryTeacher
	@Phone VARCHAR(20)
AS
BEGIN
	SELECT tname AS '姓名' , tteaching AS '课程' , COUNT(*) AS '班级数' ,SUM(ttime) AS  '总课时' 
	FROM dbo.teacher  
	WHERE tphone = @Phone
	GROUP BY tteaching , tname
END
GO

测试:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值