亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

《sql---教學(xué)反饋系統(tǒng)-階段項目2》

系統(tǒng) 4110 0
    /*

a) 創(chuàng)建數(shù)據(jù)庫

使用T-SQL創(chuàng)建數(shù)據(jù)庫feedback,要求:①一個主要文件(存放在第一個硬盤分區(qū)C:\project文件夾下),初始大小為10M,最大為200M,文件自動增長率為15% 

                                   ②一個次要數(shù)據(jù)文件(分別存放在第二個硬盤分區(qū)D上) 

                                   ③一個日志文件(存放在第三個硬盤分區(qū)E:上)  ④檢查數(shù)據(jù)庫是否已存在,如果存在則先刪除

*/



use master

if exists(select * from sysdatabases where name = 'feedback')

drop database feedback

go



create database feedback

on primary 

(

	name = 'feedback_data',                             --文件名稱

	filename = 'C:\project\feedback_data.mdf',     --文件存儲位置

	size = 10mb,                                   --初始大小

	maxsize = 200mb,                               --最大文件大小

	filegrowth = 15%                               --可以通過  百分比 % 指定自動增長率 或 通過  數(shù)字+ mb指定

),

(

	name = 'feedback_data1',                           --文件名稱

	filename = 'C:\project\feedback_data.ndf'    --文件存儲位置

)

log on 

(

	name = 'feedback_log',                              --文件名稱

	filename = 'C:\project\feedback_data.ldf'     --文件存儲位置

)

go



-------------------------------------------建表-----------------------------------------------------

USE feedback

GO





/*新建 usertype 表*/

if exists(select * from sysobjects where name = 'usertype')

  drop table usertype

GO

create table usertype

(

	usertypeid INT IDENTITY (1, 1) NOT NULL ,

	utypename VARCHAR (20) NOT NULL 

)

GO



/*新建 methodtype 表*/

if exists(select * from sysobjects where name = 'methodtype')

  drop table methodtype

GO

create table methodtype

(

	methodtypeid INT IDENTITY (1, 1) NOT NULL ,

	typename VARCHAR (20) NOT NULL,

	description VARCHAR (100) NULL

)

GO



/*新建 item 表*/

if exists(select * from sysobjects where name = 'item')

  drop table item

GO

create table item

(

	itemid INT IDENTITY (1, 1) NOT NULL ,

	itemname VARCHAR (60) NOT NULL,

	methodtypeid INT NOT NULL,

	usertypeid INT NOT NULL

)

GO



/*新建 template 表*/

if exists(select * from sysobjects where name = 'template')

  drop table template

GO

create table template

(

	templateid INT IDENTITY (1, 1) NOT NULL ,

	templatename VARCHAR (30) NOT NULL,

	status INT NOT NULL,

	usertypeid INT NOT NULL,

	usecount INT 

)

GO



/*新建 templateanditem 表*/

if exists(select * from sysobjects where name = 'templateanditem')

  drop table templateanditem

GO

create table templateanditem

(

	id INT IDENTITY (1, 1) NOT NULL,

	templateid INT NOT NULL,

	itemid INT NOT NULL

)

GO



/*新建 classtype 表*/

if exists(select * from sysobjects where name = 'classtype')

  drop table classtype

GO

create table classtype

(

	ctypeid INT IDENTITY (1, 1) NOT NULL,

	ctypename VARCHAR (20) NOT NULL

)

GO



/*新建 classinfo 表*/

if exists(select * from sysobjects where name = 'classinfo')

  drop table classinfo

GO

create table classinfo

(

	classid INT IDENTITY (1, 1) NOT NULL,

	classname VARCHAR (30) NOT NULL,

	startdate DATETIME NOT NULL,

	status INT NOT NULL,

	ctypeid INT NOT NULL

)

GO



/*新建 userinfo 表*/

if exists(select * from sysobjects where name = 'userinfo')

  drop table userinfo 

GO

create table userinfo 

(

	userid INT IDENTITY (1, 1) NOT NULL,

	username VARCHAR (20) NOT NULL,

	usertypeid INT NOT NULL

)

GO



/*新建 courseinfo 表*/

if exists(select * from sysobjects where name = 'courseinfo')

  drop table courseinfo 

GO

create table courseinfo 

(

	courseid INT IDENTITY (1, 1) NOT NULL,

	coursename VARCHAR (30) NOT NULL

)

GO



/*新建 activeinfo 表*/

if exists(select * from sysobjects where name = 'activeinfo')

  drop table activeinfo  

GO

create table activeinfo  

(

	activeid INT IDENTITY (1, 1) NOT NULL,

	activename VARCHAR (50) NOT NULL,

	activedate DATETIME NOT NULL,

	usertypeid INT NOT NULL,

	userid INT NOT NULL,

	courseid INT NULL,

	templateid INT NOT NULL,

	status INT NOT NULL

)

GO



/*新建 activeandclass 表*/

if exists(select * from sysobjects where name = 'activeandclass')

  drop table activeandclass   

GO

create table activeandclass   

(

	acid INT IDENTITY (1, 1) NOT NULL,

	activeid INT NOT NULL,

	classid INT NOT NULL,

	useramount INT NOT NULL,

	status INT NOT NULL,

	total INT NOT NULL,

	avg decimal(18, 2) NOT NULL

)

GO



/*新建 result 表*/

if exists(select * from sysobjects where name = 'result')

  drop table result   

GO

create table result   

(

	resultid INT IDENTITY (1, 1) NOT NULL,

	ip VARCHAR (15) NOT NULL,

	acid INT NOT NULL,

	itemid INT NOT NULL,

	userresult INT NOT NULL,

	answer VARCHAR (500) NULL

)

GO



/*新建 score 表*/

if exists(select * from sysobjects where name = 'score')

  drop table score   

GO

create table score   

(

	scoreid INT IDENTITY (1, 1) NOT NULL,

	acid INT NOT NULL,

	itemid INT NOT NULL,

	total INT NOT NULL,

	avg decimal(18, 2) NOT NULL,

	numexcellent INT NOT NULL,

	numpoorest INT NOT NULL

)

GO



----------------------------------------------------------------------------------------------------



/*usertype表約束*/

alter table usertype add constraint PK_usertypeid primary key (usertypeid)  --主鍵約束

alter table usertype add constraint UQ_utypename unique (utypename) --唯一約束



/* methodtype 表約束*/

alter table methodtype add constraint PK_methodtypeid primary key (methodtypeid)  --主鍵約束

alter table methodtype add constraint UQ_typename unique (typename) --唯一約束



/* item 表約束*/

alter table item add constraint PK_itemid primary key (itemid)  --主鍵約束

alter table item add constraint UQ_itemname unique (itemname) --唯一約束

alter table item add constraint FK_item_methodtypeid foreign key (methodtypeid) references methodtype(methodtypeid)    --外鍵約束

alter table item add constraint FK_item_usertypeid foreign key (usertypeid) references usertype(usertypeid)    --外鍵約束



/* template 表約束*/

alter table template add constraint PK_templateid primary key (templateid)  --主鍵約束

alter table template add constraint UQ_templatename unique (templatename) --唯一約束

alter table template add constraint FK_template_usertypeid foreign key (usertypeid) references usertype(usertypeid)    --外鍵約束

alter table template add constraint DF_status default(0) for status --默認(rèn)

alter table template add constraint DF_usecount default(0) for usecount --默認(rèn)



/* templateanditem 表約束*/

alter table templateanditem add constraint PK_id primary key (id)  --主鍵約束

alter table templateanditem add constraint FK_templateanditem_itemid foreign key (itemid) references item(itemid)    --外鍵約束

alter table templateanditem add constraint FK_templateanditem_templateid foreign key (templateid) references template(templateid)    --外鍵約束

alter table templateanditem add constraint UQ_itemid_templateid unique (itemid, templateid) --唯一約束



/* classtype 表約束*/

alter table classtype add constraint PK_ctypeid primary key (ctypeid)  --主鍵約束

alter table classtype add constraint UQ_ctypename unique (ctypename) --唯一約束



/* classinfo 表約束*/

alter table classinfo add constraint PK_classid primary key (classid)  --主鍵約束

alter table classinfo add constraint UQ_classname unique (classname) --唯一約束

alter table classinfo add constraint DF_startdate default(getDate()) for startdate --默認(rèn)

alter table classinfo add constraint DF_classinfo_status default(0) for status --默認(rèn)

alter table classinfo add constraint FK_classinfo_ctypeid foreign key (ctypeid) references classtype(ctypeid)    --外鍵約束



/* userinfo 表約束*/

alter table userinfo add constraint PK_userid primary key (userid)  --主鍵約束

alter table userinfo add constraint UQ_username unique (username) --唯一約束

alter table userinfo add constraint FK_userinfo_usertypeid foreign key (usertypeid) references usertype(usertypeid)    --外鍵約束



/* courseinfo 表約束*/

alter table courseinfo add constraint PK_courseid primary key (courseid)  --主鍵約束

alter table courseinfo add constraint UQ_coursename unique (coursename) --唯一約束



/* activeinfo 表約束*/

alter table activeinfo add constraint PK_activeid primary key (activeid)  --主鍵約束

alter table activeinfo add constraint UQ_activename unique (activename) --唯一約束

alter table activeinfo add constraint DF_activedate default(getDate()) for activedate --默認(rèn)

alter table activeinfo add constraint FK_activeinfo_userid foreign key (userid) references userinfo(userid)    --外鍵約束

alter table activeinfo add constraint FK_activeinfo_courseid foreign key (courseid) references courseinfo(courseid)    --外鍵約束

alter table activeinfo add constraint FK_activeinfo_templateid foreign key (templateid) references template(templateid)    --外鍵約束

alter table activeinfo add constraint DF_activeinfo_status default(0) for status --默認(rèn)



/* activeandclass 表約束*/

alter table activeandclass add constraint PK_acid primary key (acid)  --主鍵約束

alter table activeandclass add constraint FK_activeandclass_activeid foreign key (activeid) references activeinfo(activeid)    --外鍵約束

alter table activeandclass add constraint FK_activeandclass_classid foreign key (classid) references classinfo(classid)    --外鍵約束

alter table activeandclass add constraint UQ_activeid_classid unique (activeid, classid) --唯一約束

alter table activeandclass add constraint DF_activeandclass_useramount default(0) for useramount --默認(rèn)

alter table activeandclass add constraint DF_activeandclass_status default(0) for status --默認(rèn)

alter table activeandclass add constraint DF_activeandclass_total default(0) for total --默認(rèn)

alter table activeandclass add constraint DF_activeandclass_avg default(0.00) for avg --默認(rèn)



/* result 表約束*/

alter table result add constraint PK_resultid primary key (resultid)  --主鍵約束

alter table result add constraint FK_result_acid foreign key (acid) references activeandclass(acid)    --外鍵約束

alter table result add constraint FK_result_itemid foreign key (itemid) references item(itemid)    --外鍵約束

alter table result add constraint UQ_result_ip_acid_itemid unique (ip, acid, itemid) --唯一約束

alter table result add constraint DF_userresult default(0) for userresult --默認(rèn)



/* score 表約束*/

alter table score add constraint PK_scoreid primary key (scoreid)  --主鍵約束

alter table score add constraint FK_score_acid foreign key (acid) references activeandclass(acid)    --外鍵約束

alter table score add constraint FK_score_itemid foreign key (itemid) references item(itemid)    --外鍵約束

alter table score add constraint UQ_score_acid_itemid unique (acid, itemid) --唯一約束

alter table score add constraint DF_total default(0) for total --默認(rèn)

alter table score add constraint DF_avg default(0.00) for avg --默認(rèn)

alter table score add constraint DF_numexcellent default(0) for numexcellent --默認(rèn)

alter table score add constraint DF_numpoorest default(0) for numpoorest --默認(rèn)



/*

1添加反饋活動 

a) 使用存儲過程實現(xiàn)如下功能,根據(jù)實際傳遞的數(shù)據(jù)增加一項反饋活動,同時往activeandclass表中添加多條數(shù)據(jù)。

要求:注意事務(wù)的處理;檢查存儲過程是否已存在,如果存在則先刪除



b) 測試存儲過程,添加如下數(shù)據(jù)(其中參與班級應(yīng)根據(jù)表中實際數(shù)據(jù)需添加對應(yīng)的班級ID):

09級實訓(xùn)班講師反饋第一次  參與班級0901班/0902班/0903班/0904班

09級實訓(xùn)班講師反饋第2次  參與班級0901班/0902班/01實訓(xùn)班/02實訓(xùn)班

*/



/*

select * from activeinfo 

select * from classinfo

select * from activeandclass

*/



--a) 使用存儲過程實現(xiàn)如下功能,根據(jù)實際傳遞的數(shù)據(jù)增加一項反饋活動,同時往activeandclass表中添加多條數(shù)據(jù)。

--要求:注意事務(wù)的處理;檢查存儲過程是否已存在,如果存在則先刪除



-- 創(chuàng)建截取字符串存儲過程

use feedback

if exists(select name from sysobjects where name = 'proc_splitStr')

	drop procedure proc_splitStr

go

create procedure proc_splitStr

	-- 存儲過程參數(shù),不寫output默認(rèn)為輸入

	@bigStr varchar(100) output,

	@headStr varchar(20) output

as

	-- 聲明變量

	declare @position int 

	-- 給變量賦值

	set @position = charindex('/', @bigStr)



	if @position = 0    -- 判斷傳入的字符串是否含有'/'

		begin

			set @headStr = @bigStr

			set @bigStr = null

		end

	else  

		begin

			-- substring(字符串, 字符串中的起點, 字符數(shù))

			-- ( @headStr 參與班級0901班)/0902班/0903班/0904班 

			set @headStr = substring(@bigStr, 0, @position)

			-- @bigStr 0902班/0903班/0904班

			set @bigStr = substring(@bigStr, @position + 1, len(@bigStr)-@position)

		end

go



/*

	-- 測試

	declare @bigStr varchar(100) 

	declare @headStr varchar(20) 

	set @bigStr = '0901班/0902班/0903班/0904班'

	set @headStr = null

exec proc_splitStr @bigStr output, @headStr output 

	print @bigStr

	print @headStr

go

*/



-- 創(chuàng)建插入活動數(shù)據(jù)的存儲過程

use feedback

if exists(select * from sysobjects where name ='proc_addactive')

	drop proc proc_addactive

go

create proc proc_addactive

    @activename varchar(100),

    @usertypeid int,

	@userid int,

	@courseid int,

    @templateid int,

    @classname varchar(100),

    @amount int

as

   --聲明變量

    declare @identityNum int

    declare @classunit varchar(50)

    declare @classid int

    declare @sumerror int

    set @sumerror=0

    --開啟事務(wù)

    begin transaction

	insert into activeinfo(activename,usertypeid,userid,courseid,templateid) 

		values(@activename,@usertypeid,@userid,@courseid,@templateid)

    set @sumerror = @sumerror + @@error

    set @identityNum=@@identity 

    --截取班級的字符串

    while @classname is not null

		begin

           exec proc_splitStr @classname output,@classunit output

		   select @classid=classid from classinfo where classname=@classunit

		   --添加班級與活動的對象關(guān)系

		   insert into activeandclass(activeid,classid,useramount) values(@identityNum,@classid,@amount)		

		   set @sumerror = @sumerror + @@error

		end

   --判斷語句執(zhí)行的狀態(tài)

   if @sumerror=0

      begin

		--沒錯誤

		commit transaction

		print '儲存操作成功'

	  end

   else 

	  begin

		--中間存在問題

        rollback transaction

		print '儲存操作失敗'

	  end

go



--b) 測試存儲過程,添加如下數(shù)據(jù)(其中參與班級應(yīng)根據(jù)表中實際數(shù)據(jù)需添加對應(yīng)的班級ID):

--09級實訓(xùn)班講師反饋第一次  參與班級0901班/0902班/0903班/0904班

--09級實訓(xùn)班講師反饋第2次  參與班級0901班/0902班/01實訓(xùn)班/02實訓(xùn)班



-- 添加班級

insert into classtype values('普通班')

insert into classtype values('沖刺班')

insert into classtype values('實訓(xùn)班')

-- 添加班級信息

insert into classinfo (classname,ctypeid) values ('0901班',1)

insert into classinfo (classname,ctypeid) values ('0902班',1)

insert into classinfo (classname,ctypeid) values ('0903班',1)

insert into classinfo (classname,ctypeid) values ('0904班',1)

insert into classinfo (classname,ctypeid) values ('01實訓(xùn)班',3)

insert into classinfo (classname,ctypeid) values ('02實訓(xùn)班',3)

-- 用戶類型

INSERT INTO usertype (utypename) VALUES ('教員')

INSERT INTO usertype (utypename) VALUES ('班主任')

INSERT INTO usertype (utypename) VALUES ('機房維護員')

INSERT INTO usertype (utypename) VALUES ('教務(wù)人員')

-- 用戶

insert into userinfo (username, usertypeid) values ('教員1', 1)

insert into userinfo (username, usertypeid) values ('教員2', 1)

insert into userinfo (username, usertypeid) values ('教員3', 1)

-- 課程

insert into courseinfo values ('語文')

insert into courseinfo values ('數(shù)學(xué)')

insert into courseinfo values ('英語')

-- 模板

insert into template (templatename, usertypeid, usecount) values ('理論課評定', 1, 20)

insert into template (templatename, usertypeid, usecount) values ('課外活動評定', 1, 20)



select * from classtype

select * from classinfo

select * from usertype

select * from userinfo

select * from courseinfo

select * from template



-- 測試存儲過程

--09級實訓(xùn)班講師反饋第一次  參與班級0901班/0902班/0903班/0904班

--09級實訓(xùn)班講師反饋第2次  參與班級0901班/0902班/01實訓(xùn)班/02實訓(xùn)班

exec proc_addactive '09級實訓(xùn)班講師反饋第一次',1,1,1,1,'0901班/0902班/0903班',50

exec proc_addactive '09級實訓(xùn)班講師反饋第2次',1,1,1,1,'0901班/0902班/01實訓(xùn)班/02實訓(xùn)班',50



select * from activeinfo

select * from activeandclass

select * from classinfo





--------------------------

/*

2批量發(fā)布反饋活動

a) 使用存儲過程實現(xiàn)如下功能,批量發(fā)布實際選中的多個反饋活動。

要求:注意事務(wù)的處理;檢查存儲過程是否已存在,如果存在則先刪除

*/



-- 定義批量發(fā)布反饋活動存儲過程

use feedback

if exists(select * from sysobjects where name ='proc_deployactive')

	drop proc proc_deployactive

go

create proc proc_deployactive

    @activeids varchar(100)		-- 反饋活動id(id1/id2/id3)

as

   --聲明用到的變量

    declare @activeid varchar(10)	-- 待發(fā)布反饋活動的id



    declare @sumerror int			-- 錯誤號

    set @sumerror=0

    --開啟事務(wù)

    begin transaction



    --截取班級的字符串

    while @activeids is not null

		begin

           exec proc_splitStr @activeids output,@activeid output



		   --添加班級與活動的對象關(guān)系

		   update activeinfo set status = 2 where activeid = @activeid

		   set @sumerror = @sumerror + @@error

		end

   --判斷語句執(zhí)行的狀態(tài)

   if @sumerror=0

      begin

		--沒錯誤

		commit transaction

		print '發(fā)布操作成功'

	  end

   else 

	  begin

		--中間存在問題

        rollback transaction

		print '發(fā)布操作失敗'

	  end

go



-- 測試

select * from activeinfo



exec proc_deployactive '1/2'



update activeinfo set status = 0 where activeid = 1

update activeinfo set status = 0 where activeid = 2



/*

3刪除反饋活動結(jié)果

a) 使用觸發(fā)器模擬簡易的刪除反饋活動結(jié)果的功能,要求刪除反饋活動結(jié)果的同時修改activeandclass表中“參與人數(shù)”字段useramount -1

b) 測試觸發(fā)器

*/



-- 添加數(shù)據(jù)

INSERT INTO methodtype (typename,description) VALUES ('answer','按回答評定')

INSERT INTO methodtype (typename,description) VALUES ('sorce','按分?jǐn)?shù)評定/評價標(biāo)準(zhǔn):5分[優(yōu)秀] 4分[良好] 3分[一般] 2分[差] 1分[很差]')



INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('對該教員有什么建議?',1,1)

INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('該教員哪方面對你有幫助?',1,1)



INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('講課是否活躍?',2,1)

INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('內(nèi)容是否詳細(xì)?',2,1)

INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('是否熱心幫助同學(xué)?',2,1)



INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('感覺班主任哪些方面需要改進',1,2)



INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('是否經(jīng)常開班會?',2,2)

INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('是否關(guān)心班級的相關(guān)事情?',2,2)



INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('對該管理的服務(wù)態(tài)度有什么建議?',1,3)



INSERT INTO item (itemname,methodtypeid,usertypeid) VALUES ('對該管理員的態(tài)度打多少分?',2,3)



select * from result

select * from activeandclass



insert into result (ip, acid, itemid, userresult, answer) values ('127.0.0.1', 1, 1, 1, '很好啊')

insert into result (ip, acid, itemid, userresult, answer) values ('127.0.0.1', 1, 2, 90, '不錯')

insert into result (ip, acid, itemid, userresult, answer) values ('127.0.0.1', 1, 3, 85, '不錯的事情')



--a) 使用觸發(fā)器模擬簡易的刪除反饋活動結(jié)果的功能,要求刪除反饋活動結(jié)果的同時

--修改activeandclass表中“參與人數(shù)”字段useramount -1



if exists(select * from sysobjects where name ='tgr_result_delete')

	drop trigger tgr_result_delete

go

create trigger tgr_result_delete 

on result

	for delete --刪除觸發(fā)

as 

	-- 定義變量

	declare @acid int

	declare @sumerror int

	

	set @sumerror = 0

	

	select @acid=acid from Deleted

	update activeandclass set useramount = useramount - 1 where acid = @acid

	set @sumerror = @sumerror + @@error

	

	if @sumerror = 0

		begin

			print 'activeandclass表中“參與人數(shù)”字段useramount -1'

		end

	else

		begin

			print 'activeandclass表更改失敗'

		end

go



--b) 測試觸發(fā)器

select * from activeandclass

insert into result (ip, acid, itemid, userresult, answer) values ('127.0.0.1', 1, 1, 1, '很好啊')



delete result where answer = '很好啊'



/*

4查看所有反饋活動

a) 使用視圖和函數(shù)實現(xiàn),查看所有反饋活動的功能。如下圖所示:

*/



select * from activeinfo

--select * from usertype

--select * from userinfo

--select * from courseinfo

select * from activeandclass

select * from classinfo



-- 創(chuàng)建根據(jù) activeid 獲取所有班級字符串的函數(shù)

if exists(select * from sysobjects where name = 'fun_getcnames')

	drop function fun_getcnames

go

create function fun_getcnames(@activeid int)

	returns varchar (100)

as

begin

	-- 變量聲明

	declare @result_classinfo varchar (200)

	declare @classnames varchar (200)

	

	set @classnames = ''

	-- 聲明一個游標(biāo)

   

	declare cur_classinfo cursor for

		-- 查詢語句

		select classname from activeandclass,classinfo 

			where activeandclass.classid = classinfo.classid and activeandclass.activeid = @activeid

	-- 打開游標(biāo)   

	Open cur_classinfo   

	-- 循環(huán)并提取記錄   

	Fetch Next From cur_classinfo Into @result_classinfo-- 取第一條記錄存入@result中   

	While ( @@Fetch_Status = 0 )     

	begin  

        set @classnames = @classnames + ' ' +@result_classinfo -- 處理結(jié)果  

 

		Fetch Next From cur_classinfo into @result_classinfo -- 下一條   

	end   

	-- 關(guān)閉游標(biāo)      

	close cur_classinfo

	-- 釋放游標(biāo)   

	deallocate cur_classinfo

	

	return @classnames

end

go



-- 測試函數(shù)

select activeid from activeinfo

select dbo.fun_getcnames(1) as 'result' 

select * from activeinfo



------------------- 創(chuàng)建視圖 ---------------------------

if exists(select * from sysobjects where name = 'view_activeinfos')

	drop view view_activeinfos

go

create view view_activeinfos

as

	select activename as '活動名稱', activedate as '活動時間', utypename as '被評價人類型',

		username as '被評價人姓名', coursename as '技能課程', dbo.fun_getcnames(activeid) as '參與班級'

	from activeinfo 

	left join usertype

		on activeinfo.usertypeid = usertype.usertypeid

	left join userinfo

		on userinfo.userid = activeinfo.userid

	left join courseinfo

		on courseinfo.courseid = activeinfo.courseid

go



-- 測試結(jié)果

select * from view_activeinfos



/*

5根據(jù)條件查詢反饋活動

a) 查詢某位教員(如,劉小林),在某一時間段內(nèi)(如,2009年度)被評價的反饋活動。如下圖所示:

提示:使用索引

*/



-------------------------------------------------------------------------------

/*

select username as '被評價人姓名' ,

	activename as '活動名稱', 

	activedate as '活動時間', 

	dbo.fun_getcnames(activeinfo.activeid) as '參與班級',

	--SUM() as '參與人數(shù)',

	--AVG() as '總平均分'

	userresult as '得分',

	useramount as '本班人數(shù)'--,

	--classname as '班級名稱'

from result

left join activeandclass

	on result.acid = activeandclass.acid

left join classinfo

	on classinfo.classid = activeandclass.classid 

left join activeinfo

	on activeinfo.activeid = activeandclass.activeid

left join usertype

	on activeinfo.usertypeid = usertype.usertypeid

left join userinfo

	on userinfo.userid = activeinfo.userid

left join courseinfo

	on courseinfo.courseid = activeinfo.courseid

--left join classinfo

--	on classinfo.classid = activeandclass.classid 

where username = '教員1'

-- 添加時間限制

	and convert(date, activedate) > convert(date, '2013') 

	and convert(date, activedate) < convert(date, '2014') 



*/



-- 定義獲取班級人數(shù)函數(shù)人數(shù)

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'fun_getclassmount')

	DROP function  fun_getclassmount 

go

create function fun_getclassmount(@activeid int)

	returns  int

as

begin

	-- 定義游標(biāo)

	declare class_usermount cursor read_only for

		select activeandclass.useramount

		from activeandclass,classinfo 

		where activeandclass.activeid=@activeid and activeandclass.classid=classinfo.classid

	--聲明變量接收游標(biāo)數(shù)據(jù)

	declare @useramount int,@mount int

	set @mount = 0

	--打開游標(biāo)

	open class_usermount

	--獲取游標(biāo)數(shù)據(jù)

	fetch next from class_usermount into @useramount

	--迭代遍歷

	while @@fetch_status = 0

	begin

		set @mount = @mount + @useramount

		--獲取游標(biāo)數(shù)據(jù)

		fetch next from class_usermount into @useramount

	end

	--關(guān)閉游標(biāo)

	close class_usermount

	--釋放游標(biāo)

	deallocate class_usermount



	return @mount

end

GO



-- 定義獲取平均分函數(shù)

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'fun_getrseultavg')

	DROP function  fun_getrseultavg 

go

create function fun_getrseultavg(@activeid int)

	returns  float

as

begin

	--定義游標(biāo)

	declare class_usermount cursor read_only for

		select activeandclass.total,activeandclass.useramount

		from activeandclass,classinfo 

		where activeandclass.activeid=@activeid and activeandclass.classid=classinfo.classid

	declare @useramount int,@mount int,@number int,@number_all int

	set @mount=0

	set @number_all=0

	-- 打開游標(biāo)

	open class_usermount

	-- 獲取游標(biāo)數(shù)據(jù)

	fetch next from class_usermount into @useramount,@number

	-- 迭代遍歷

	while @@fetch_status = 0

	begin

		--連接字符串

		set @mount = @mount + @useramount

		set @number_all = @number_all + @number

		--獲取游標(biāo)數(shù)據(jù)

		fetch next from class_usermount into @useramount,@number

	end

	--關(guān)閉游標(biāo)

	close class_usermount

	--釋放游標(biāo)

	deallocate class_usermount



	declare @avg float

	set @avg = @mount / @number_all



	return @avg

end

GO

-----------------------------------------------------------------------

--	測試函數(shù)

use feedback

select dbo.fun_getclassmount(1)

select * from activeinfo

select * from activeandclass

select * from classinfo



select username as '被評價人姓名',activename as '活動名稱',activedate as '活動日期',

dbo.fun_getclassmount(activeinfo.activeid) as '參與班級',

        dbo.fun_getclassmount(activeinfo.activeid) as '參與人數(shù)',

dbo.fun_getrseultavg(activeinfo.activeid) as '總平均分'

from (activeinfo left join userinfo on activeinfo.userid=userinfo.userid)

--left join activeandclass on activeinfo.activeid=activeandclass.activeid
  

?

教學(xué)反饋系統(tǒng)-階段項目2

第一部分案例描述

案例目的

???????? 學(xué)習(xí)并鞏固 SQL Server 數(shù)據(jù)庫編程技術(shù),包括存儲過程、觸發(fā)器、索引、視圖、事務(wù)、游標(biāo)、函數(shù)等,提高學(xué)生數(shù)據(jù)庫設(shè)計和數(shù)據(jù)庫編程的能力。

案例難度

???????? ★★★★

案例覆蓋技能點

1、 ?? 存儲過程

2、 ?? 觸發(fā)器

3、 ?? 索引

4、 ?? 視圖

5、 ?? 事務(wù)

6、 ?? 游標(biāo)

7、 ?? 函數(shù)

推薦案例完成時間

???????? ?0.5

適用課程和對象

???????? SQL SERVER 數(shù)據(jù)庫設(shè)計

第二部分需求和開發(fā)環(huán)境

使用技術(shù)和開發(fā)環(huán)境

???????? SQL Server 2005

項目背景

中國經(jīng)濟數(shù)年來持續(xù)高增長帶來了專業(yè)性職業(yè)人才的需求激增,職業(yè)教育作用日益顯現(xiàn),優(yōu)秀企業(yè)也孕育而生。他們的作用不僅僅為社會培養(yǎng)了專業(yè)人才,在產(chǎn)業(yè)經(jīng)營領(lǐng)域,他們也扮演了重要的角色。改革開放以來,隨著中國經(jīng)濟社會的發(fā)展,職業(yè)教育越來越受到國家的高度重視和社會的廣泛關(guān)注。隨著經(jīng)濟社會的發(fā)展,中國的職業(yè)教育取得了長足的發(fā)展,在職業(yè)教育理念的實踐群體中,若想更好地成為佼佼者,無疑是在 在規(guī)?;l(fā)展中保障 教學(xué)質(zhì)量是其中一個比較重要的方面。

案例需求

教學(xué)質(zhì)量是學(xué)校生存與發(fā)展的生命線,不斷提高課堂教學(xué)水平是學(xué)校和每一位教師的共同心愿。及時了解課堂教學(xué)的主體—學(xué)生對教學(xué)情況的評價及建議,有利于教師發(fā)現(xiàn)自己教學(xué)中的優(yōu)點以及不足,從而進一步改進教學(xué)方法,提高教學(xué)水平。為了更好的提高教學(xué)水平,建立學(xué)校與學(xué)員的更好勾通,院領(lǐng)導(dǎo)研究決定研發(fā)本系統(tǒng),并提供考核內(nèi)容管理、反饋項目管理、反饋表管理、數(shù)據(jù)統(tǒng)計分析等主要功能,本階段案例主要以反饋活動管理為主要分析目標(biāo), 詳細(xì)功能描述如下:

1、? 反饋活動管理

對學(xué)院內(nèi)部反饋活動進行管理和維護,包括對反饋活動的添加、修改、刪除、查看、批量刪除、發(fā)布、批量發(fā)布和關(guān)閉某個班的反饋活動等。反饋 活動 的詳細(xì)信息包括:反饋活動編號、反饋活動名稱、活動日期、被評價人類型、被評價人姓名、技能課程、本次反饋采用模板、參與班級。

反饋活動列表

?

添加反饋活動

?

???????? 查看反饋活動

???????? 系統(tǒng)基本模塊包括:

功能點

難度

?

添加反饋活動

★★★★

?

批量發(fā)布反饋活動

★★★

?

刪除反饋活動結(jié)果

★★★★

?

查看所有反饋活動

★★★★

?

根據(jù)條件查詢反饋活動

★★

?

?

功能點介紹

?

數(shù)據(jù)庫表結(jié)構(gòu)關(guān)系圖

?

?

1 人員類型表

表名

usertype (人員類型表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

usertypeid

類型編號

int

非空

主鍵,標(biāo)識列

utypename

類型名稱

Varchar(20)

非空

唯一

?

?

2 考核方式類型表

表名

methodtype (考核方式表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

methodtypeid

考核方式編號

int

非空

主鍵,標(biāo)識列

typename

考核方式名稱

Varchar(20)

非空

唯一

description

描述

Varchar(100)

?

?

?

3 考核項表

表名

item (考核項表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

itemid

考核項 編號

int

非空

主鍵,標(biāo)識列

itemname

考核項 名稱

Varchar(60)

非空

唯一

methodtypeid

考核方式編號

int

非空

外鍵

usertypeid

適用人員類型編號

int

非空

外鍵

?

4 反饋模板表

表名

template (人員類型表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

templateid

模板 編號

int

非空

主鍵,標(biāo)識列

templatename

模板 名稱

Varchar(30)

非空

唯一

status

狀態(tài)

int

非空

0-正常(默認(rèn)值)

1-刪除

usertypeid

適用人員類型編號

int

非空

外鍵

usecount

使用次數(shù)

int

非空

外鍵

?

5 反饋模板與考核項關(guān)聯(lián)表

表名

templateanditem (人員類型表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

id

編號

int

非空

主鍵,標(biāo)識列

templateid

模板編號

int

非空

外鍵

考核項 編號一起,唯一

itemid

考核項 編號

int

非空

外鍵

與模板編號一起,唯一

?

6 班級類型表

表名

classtype (班級類型表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

ctypeid

編號

int

非空

主鍵,標(biāo)識列

ctypename

類型名稱

Varchar(20)

非空

唯一

?

7 班級信息表

表名

classinfo (班級信息表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

classid

編號

int

非空

主鍵,標(biāo)識列

classname

班級名稱

Varchar(30)

非空

唯一

startdate

開班時間

datetime

非空

默認(rèn)系統(tǒng)日期

status

狀態(tài)

int

非空

0-正常(默認(rèn))

1-刪除

2-結(jié)業(yè)

ctypeid

班級類型編號

int

非空

外鍵

?

8 用戶信息表

表名

userinfo (用戶信息表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

userid

編號

int

非空

主鍵,標(biāo)識列

username

用戶名稱

Varchar(20)

非空

唯一

usertypeid

用戶 類型編號

int

非空

外鍵

?

9 課程信息表

表名

courseinfo (課程信息表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

courseid

編號

int

非空

主鍵,標(biāo)識列

coursename

課程名稱

Varchar(30)

非空

唯一

?

10 反饋活動信息表

表名

activeinfo (反饋活動信息表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

activeid

編號

int

非空

主鍵,標(biāo)識列

activename

活動名稱

Varchar(50)

非空

唯一

activedate

活動時間

datetime

非空

默認(rèn)系統(tǒng)日期

usertypeid

被評價人 類型編號

int

非空

?

userid

被評價人 編號

int

非空

外鍵

courseid

技能課程 編號

int

?

外鍵

templateid

反饋模板 編號

int

非空

外鍵

status

狀態(tài)

int

非空

0-正常(未發(fā)布,默認(rèn));1-刪除;2-已發(fā)布

?

11 反饋活動與班級關(guān)聯(lián)表

表名

activeandclass (反饋活動與班級關(guān)聯(lián)表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

acid

編號

int

非空

主鍵,標(biāo)識列

activeid

活動 編號

int

非空

外鍵

班級 編號一起,唯一

classid

班級 編號

int

非空

外鍵

活動 編號一起,唯一

useramount

參與人數(shù)

int

非空

默認(rèn)0

status

狀態(tài)

int

非空

0-正常(活動未開始,默認(rèn));1-刪除;2-進行中;3-關(guān)閉

total

總成績

int

非空

默認(rèn)0

avg

平均成績

decimal(18, 2)

非空

默認(rèn)0.00

?

12 反饋結(jié)果表

表名

result (反饋結(jié)果表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

resultid

編號

int

非空

主鍵,標(biāo)識列

ip

IP地址

Varchar(15)

非空

?

acid

反饋活動與班級關(guān)聯(lián)ID

int

非空

外鍵

itemid

考核項 編號

int

非空

外鍵

userresult

提交的成績

int

非空

默認(rèn)0

answer

按回答評定時的用戶答案

Varchar(500)

?

默認(rèn)null

?

13 成績表

表名

score (成績表)

列名

描述

數(shù)據(jù)類型

空/非空

約束條件

scoreid

編號

int

非空

主鍵,標(biāo)識列

acid

反饋活動與班級關(guān)聯(lián)ID

int

非空

外鍵

考核項 編號一起,唯一

itemid

考核項 編號

int

非空

外鍵

反饋活動與班級關(guān)聯(lián)ID號 一起,唯一

total

總成績

int

非空

默認(rèn)0

avg

平均成績

decimal(18, 2)

非空

默認(rèn)0.00

numexcellent

選”優(yōu)”的人數(shù)

int

非空

默認(rèn)0

numpoorest

選”很差”的人數(shù)

int

非空

默認(rèn)0

?

1 添加反饋活動

a) 使用存儲過程實現(xiàn)如下功能,根據(jù)實際傳遞的數(shù)據(jù)增加一項反饋活動,同時往 activeandclass 表中添加多條數(shù)據(jù)。

要求:注意事務(wù)的處理;檢查存儲過程是否已存在,如果存在則先刪除

b) 測試存儲過程,添加如下數(shù)據(jù)(其中參與班級應(yīng)根據(jù)表中實際數(shù)據(jù)需添加對應(yīng)的班級 ID ):

09 級實訓(xùn)班講師反饋第一次 ? 參與班級 0901 /0902 /0903 /0904

09 級實訓(xùn)班講師反饋第 2 ? 參與班級 0901 /0902 /01 實訓(xùn)班 /02 實訓(xùn)班

2 批量發(fā)布反饋活動

a) 使用存儲過程實現(xiàn)如下功能,批量發(fā)布實際選中的多個反饋活動。

要求:注意事務(wù)的處理;檢查存儲過程是否已存在,如果存在則先刪除

3 刪除反饋活動結(jié)果

a) 使用觸發(fā)器模擬簡易的刪除反饋活動結(jié)果的功能,要求刪除反饋活動結(jié)果的同時修改 activeandclass 表中“參與人數(shù)”字段 useramount -1

b) 測試觸發(fā)器

4 查看所有反饋活動

a) 使用視圖和函數(shù)實現(xiàn),查看所有反饋活動的功能。如下圖所示:

?

5 根據(jù)條件查詢反饋活動

a) 查詢某位教員(如,劉小林),在某一時間段內(nèi)(如, 2009 年度)被評價的反饋活動。如下圖所示:

?

提示:使用索引

第三部分考核評價點

序號

功能列表

功能描述

分?jǐn)?shù)

說明

1

批量刪除反饋活動

?

?

?

2

?

?

?

?

3

刪除反饋活動結(jié)果

?

?

?

4

查看所有反饋活動

?

?

?

5

根據(jù)條件查詢反饋活動

?

?

?

6

數(shù)據(jù)庫命名規(guī)范

?

?

?

?

?

?

《sql---教學(xué)反饋系統(tǒng)-階段項目2》


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久热re国产手机在线观看 | 欧美性性性性性色大片 | 女bbbbxxxx毛片视频丶 | 精品福利一区 | 久久精品六 | 国产亚洲视频在线观看 | 香蕉午夜 | 国产精品久久久久精 | 免费一区二区三区四区五区 | 国产高清自拍视频 | 亚洲国产精品高清在线一区 | 亚洲午夜网 | 久久精品操 | 97香蕉久久夜色精品国产 | 豆国产97在线 | 亚洲 | 亚洲成人在线视频播放 | 在线观看深夜观看网站免费 | 伊人五月天婷婷琪琪综合 | 国产欧美另类第一页 | 99精品久久久久中文字幕 | 精品国产一区二区三区香蕉事 | 成人毛片网 | 久久精品国内偷自一区 | 亚欧成人一区二区 | 婷婷亚洲综合五月天在线 | 香蕉网久久 | 久久精品免费i 国产 | 激情奇米网 | 色片网| 97超精品视频在线观看 | 九天玄帝诀高清300集免费观看 | 久久91亚洲精品久久91综合 | 欧美成人亚洲欧美成人 | 91精东果冻蜜桃星空麻豆 | 全部免费毛片在线 | 亚洲干综合 | 中文字幕在线观看一区二区 | 久久久精品中文字幕 | 久久久日韩精品国产成人 | 午夜一级精品免费毛片 | 四虎在线视频观看 |