您的位置:首页 > 数据库

sql下员工工牌(YC0001)的简单实现和一些特殊查询

2013-05-07 21:51 141 查看
方法一: use master;
go
--判断数据库Test是否存在
if exists(select * from sys.databases where name='Test')
drop database Test; --存在则删除
go
--创建Test数据库
create database Test;
go
--使用Test数据库
use Test;
go
--判断数据库Test是否存在emp表
if exists(select * from sys.tables where name='emp')
drop table emp; --存在则删除
go
--创建emp表
create table emp(
eid int primary key identity(10001,1),
ecd varchar(20) null, -- 要求是YC0001 YC0002且有系统根据eid自动生成
);
go
--判断当前实例是否存在触发器tri_ecd
if exists(select * from sys.triggers where name='tri_ecd')
drop trigger tri_ecd;
go
--创建触发器用来实现工牌号的根据eid自动生成
create trigger tri_ecd -- 创建触发器tri_ecd
on emp --在emp表中
after insert -- 插入操作后触发此触发器,执行下面的操作
as
declare @eid int; --声明一个变量eid,用来存放最新插入的数据的eid
declare @ecd varchar(14); --声明一个变量ecd,用来存放截取的eid
--set @eid=(select max(eid) from emp); --从emp表中查询出最新插入的数据的eid --select @eid=@@identity; set @eid= (select ident_current(‘emp‘)); ---获取emp表中最新生成的标识列的值
set @ecd=substring(cast(@eid as varchar),2,len(@eid)); --讲整型的eid转为字符型,然后截取除掉前面的1
update emp set ecd='YC'+@ecd where eid=@eid; -- 更新emp表中的ecd列
go

insert into emp values('');
insert into emp values('');
insert into emp values('');

select * from emp; 效果: eid ecd 10001 YC0001 10002 YC0002 10003 YC0003 方法二:

create table dept(
did varchar(20) primary key,
dname varchar(20)
);
go

drop trigger tri_did;
go
create trigger tri_did
on dept
after insert
as
declare @did varchar(20);
set @did=(select top 1 did from dept where did!='1' order by did desc);
if((select count(*) from dept)<=1)
begin
update dept set did='YC10001';
end
else
begin
declare @temp varchar(20);
declare @num int;
set @temp=substring(@did,3,len(@did));
set @num=(cast(@temp as int)+1);
update dept set did=('YC'+cast(@num as varchar)) where did='1';
end
go

insert into dept values('1','a'); --注意:did列初始值必须是1
insert into dept values('1','b');
insert into dept values('1','c');
效果:
did dname
YC10001 a
YC10002 b
YC10003 c

create trigger tri_did
on dept
after insert
as
declare @did varchar(20);
select top 1 @did=did from dept where did!=1 order by did desc;
if((select count(*) from dept)<=1)
begin
update dept set did='S001'
end
else
begin
declare @temp varchar(10);
set @temp=stuff((cast((cast(replace(@did,'S',1) as int)+1) as varchar)),1,1,'S');
update dept set did=@temp where did='1';
end

--触发器 模拟日志
create table users(
uid int identity primary key,
uname varchar(20),
pwd varchar(20)
);
go

create table loginfo(
lid int identity primary key,
logininfo varchar(100),
ldate date
);
go

if exists(select * from sys.triggers where name='tri_loginfo')
drop trigger tri_loginfo;
go

create trigger tri_loginfo
on users
after insert,delete
as
declare @info varchar(100);

if exists(select 1 from inserted)
begin
select @info='用户 '+uname+' 添加成功' from users;
end;

if exists(select 1 from deleted)
begin
select @info='用户 '+uname+' 删除成功' from users;
end;

--insert into loginfo values((select ident_current('users')),@info,getDate()); --ident_current('users') users表最新生成的标识列的值
--insert into loginfo values((select @@identity),@info,getDate());
go

关闭标识列
set identity_insert 表名 on;

select case e.sex when 'M' then'男' when 'F' then '女' end 性别 from temp;
--替换显示
select sname,case sex when 'M' then'男' when 'F' then '女' end 性别 from stuinfo;

统计列印各科成绩,各分数段人数:课程名称,[100-85],[85-70],[70-60],[ <60] ,
select * from score
select cname,sum(case when score between 85 and 100 then 1 else 0 end) as [100 - 85],
sum(case when score between 70 and 85 then 1 else 0 end) as[85 - 70],
sum(case when score between 60 and 70 then 1 else 0 end) as [70 - 60],

sum(case when score < 60 then 1 else 0 end) as [60 -0] from score,course group by cname;

--去掉补考的
select cname,sum(case when score between 85 and 100 then 1 else 0 end) as [100 - 85],
sum(case when score between 70 and 85 then 1 else 0 end) as [85 - 70],
sum(case when score between 60 and 70 then 1 else 0 end) as [70 - 60],
sum(case when score < 60 then 1 else 0 end) as [60 -0] from (
select studentid,courseid,cid,cname,max(score) as score from
score,course where courseid=cid group by studentid,courseid,cid,cname) oo
) where courseid=cid group by cname;

查找学生姓名和出生日期,如果出生日期为空值,显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。
select sname 姓名,isnull(CONVERT(char(10),e.BirthDate),'日期不详') 出生日期 from stuinfo;
--查询选修了两个以上课程的学生姓名
--1.先从score中查出选修了两门以上的学生的学号,去掉补考的
--2.通过查出来的学号从stuinfo中查出姓名
select sname from stuinfo stu where stu.stuid in(
select sc.studentid from score sc group by sc.studentid having COUNT(distinct(courseid)>=2);

--查出没有选修1号课程的学生信息
select * from stuinfo where stuid not in(select sc.studentid from score sc where sc.courseid=1);

select * from stuinfo stu where not exists(
select * from score where stu.stuid=score.studentid and score.courseid=1);

--查询选修了所有课程的学生信息(不存在这样的课程,他没有参加选修)

select * from stuinfo where not exists(--不存在这样的学生不在
select * from course where not exists( --不存在这样的课程不在学生的选修课成绩单中
select * from score where score.courseid=course.cid and stuinfo.stuid=score.studentid));

select * from stuinfo where stuid in(
select studentid from score group by studentid having COUNT(distinct(courseid)=(select count(*) from course));

--查询至少选修了2号学生所有课程的学生信息
select * from stuinfo where not exists(
select * from course where course.cid in( --查出课程2号学生学修了的课程
select score.courseid from score where score.studentid=2) and not exists(
select * from score where score.studentid=stuinfo.stuid and cid=score.courseid));

--选修了2号课程以上的学生信息
select * from stuinfo where stuid in(select studentid from score where courseid=2) --选修了2号课程的

select * from stuinfo where stuid in(
select studentid from score group by studentid having count(distinct(courseid)>1 and studentid in(
select * from stuinfo where stuid in(select studentid from score where courseid=2)));

--5检索至少选修了1号和2的课程的学生信息
select * from stuinfo,score where studentid=stuid and courseid=1 and stuid in(select studentid from score where courseid=2);

select distinct * from stuinfo sf1,score sc1,score sc2 where stuid=sc1.studentid and stuid=sc2.studentid and sc1.courseid=1 and sc2.courseid=2;
--查询学生成绩,包括姓名,课程名,成绩,如有补考,只显示最高的一次

select sname,cname,max(score) from score,course,stuinfo where courseid=cid and studentid=stuid group by sname,cname
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐