数据库课程设计--人事管理系统代码

–人事管理系统
–创建数据库
create database personal_management_system
on primary
(name=‘pms_data’,–逻辑名
filename=‘D:\sql server\pms_data.mdf’,–储存路径
size=20mb,–大小
maxsize=50mb,–最大大小
filegrowth=10%)–增加方式
log on
(name=‘pms_log’,–日志名
filename=‘D:\sql server\pms_log.ldf’,–日志储存路径
size=10mb,–大小
maxsize=30mb,–最大大小
filegrowth=10%)–增长方式
–1创建表

–1.1创建公司表
use personal_management_system
go
create table 公司
(cno nchar(5) primary key,–公司代号
cname nvarchar(10) not null ,–公司名
ceo nvarchar(10) not null,–法定代表人
birth_time date not null,–诞生时间
work nvarchar(10) not null,–公司从事的业务
stoke nvarchar(10) not null–是否上市
)
公司表设计图:
数据库课程设计--人事管理系统代码
–1.2创建部门表

create table 部门
(dno nchar(5) primary key,–部门号
dname nvarchar(10) not null,–部门名
manager nvarchar(10) not null,–管理人员
subject nvarchar(10) not null,–主营业务
cno nchar(5) not null,–外键
)
–部门表设计图
数据库课程设计--人事管理系统代码
–创建外键约束
use personal_management_system
go
alter table 部门
add constraint pk_部门_公司 foreign key (cno) references 公司(cno)
–1.3创建员工表

create table 员工
(sno nchar(10) primary key,–员工代号
sname nvarchar(10) not null,–员工姓名
sage int check(sage>=23 and sage<=50) not null,–员工年龄
ssex nchar(1) check(ssex=‘男’ or ssex=‘女’) not null,–性别
entrytime date not null,–入职时间
education nvarchar(10) not null,–学历
weeding nchar(2) check(weeding=‘已婚’ or weeding=‘未婚’) not null,–婚姻状况
dno nchar(5) not null,–部门号
wno nchar(5) not null,–岗位号
)
–员工表
数据库课程设计--人事管理系统代码

–use personal_management_system
–go
–alter table 员工
–add constraint ch_education check(education=‘学士’ or education=‘硕士’ or
–education=‘博士’ or education=‘博士后’)
–创建外键约束用于连接部门和员工
alter table 员工
add constraint FK_员工_部门 foreign key(dno) references 部门(dno)
–1.4创建岗位表

create table 岗位
(wno nchar(5) primary key,–岗位代号
wname nvarchar(10) not null,–岗位名
wlevel nchar(3) check(wlevel like ‘T%’) not null,–岗位级别
minsalary int check(minsalary>0) not null,–最小工资
maxsalary int check(maxsalary >0) not null ,–最大工资
)
–岗位表
数据库课程设计--人事管理系统代码

--创建外键约束用于连接员工和岗位
alter table 岗位
add cnostraint FK_员工_岗位 foreign key(wno) references 岗位(wno)
数据库关系图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190110193449762.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODMzNjU0Ng==,size_16,color_FFFFFF,t_70)
--视图的创建
--1.5员工基本信息表
use personal_management_system
go
create view 员工基本信息表
as
	select sname,ssex,sage,entrytime,education,cname,dname,wname from 公司,
	员工,部门,岗位 where 公司.cno=部门.cno and 部门.dno=员工.dno and 
	员工.wno=岗位.wno
--查询员工基本信息表
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190110200434731.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODMzNjU0Ng==,size_16,color_FFFFFF,t_70
select * from 员工基本信息表
--1.6创建 员工婚姻状况表
use personal_management_system
go
create view 员工婚姻状况表
as 
	select sname,ssex,sage,weeding from 员工
--
select * from 员工婚姻状况表
--1.7员工学历信息表
use personal_management_system
go
create view 员工学历信息表
as
	select sname,ssex,sage,education from 员工
--
select * from 员工学历信息表
--1.8企业工作岗位表
use personal_management_system
go
create view 企业工作岗位表
as 
	select cname,wname as 岗位 ,wlevel as 岗位级别,minsalary,maxsalary from 公司,部门,员工,岗位
	where 公司.cno=部门.cno and 部门.dno=员工.dno and 员工.wno=岗位.wno
--
select distinct * from 企业工作岗位表
--1.9企业部门信息表
use personal_management_system
go
create view 企业
--
drop view 企业工作岗位表
--查询:
select * from 公司
select * from 部门
select * from 员工
select * from 岗位
--
select 公司.cno,cname,sname,sage,wname from 部门,公司,员工,岗位
where 公司.cno=部门.cno and  部门.dno=员工.dno and 
员工.wno=岗位.wno
--
--
use personal_management_system
go
create view 公司岗位表
as
	select cname as 公司 ,dname as 部门,manager as 部门管理人员,subject as 
主要工作 from 公司,部门 where 公司.cno=部门.cno
--
select * from 公司岗位表
--储存过程的定义
--定义储存过程
--1.定义储存过程
--1.1修改、对于转出、辞职、辞退、退休员工信息的删
--已知员工号,对员工进行修改,职位,部门进行修改
create procedure update_staff @number nchar(5),@department nchar(5),
@work nchar(5)
as 
update 员工 set [email protected],[email protected] from 员工 where [email protected]
--对转出 辞职 退休的员工进行的员工进行删除
--1.2根据员工号进行删除
create procedure delete_staff @number nchar(5)
as
	delete 员工 where [email protected]
--按条件查询员工的信息:
--查询公司部门员工的信息
create procedure query_depart_id @cname nvarchar(10),@dno nchar(5)
as 
	select 员工.* from 公司,部门,员工 where 公司.cno=部门.cno 
	and 部门.dno
	=员工.dno and [email protected] and 部门[email protected]
--
--实例:查询华为公司,H001部门的员工
use personal_management_system
go
exec query_depart_id '华为','H001'
--查询部门
--并对查询、统计的结果打印输出
--对输出公司博士学位的员工的个数

create procedure query_depart_phd @cname char(10),@edu char(10),
@num int output
as
	select @num=COUNT(education) from 员工,部门,公司 where 公司.cno=部门.cno
	and 部门.dno=员工.dno and [email protected] and [email protected]

–执行储存过程
–sample 2
–查询华为公司 学士学位人数
declare @num int
declare @edu char
set @edu=‘学士’
exec query_depart_phd ‘华为’,‘博士’,@num output
print ‘博士学位人数:’+ltrim(str(@num))+‘个’

–触发器的定义和使用
–1.1定义触发器显示删除的数据

use personal_management_system
go
create trigger trigger_delete_staff
on 员工
for delete
as

select * from deleted
declare @count varchar(5)
set @count=STR(@@rowcount)+'个学生被删除'

print '你正在删除员工的数据:'
return
--
delete 员工 where sno='044'
--1.2定义触发器用于岗位对插入数据
create trigger trigger_insert_work
on 岗位
for insert
as
	if (select minsalary from inserted )>(select maxsalary from inserted)
	    print '最小工资要小于等于最大工资'
	    rollback
--
--实例插入一组数据
--1.对公司表进行数据插入
insert into 公司 values('007','华为','胡厚崑','1987-09-09','信息技术'),
('001','阿里巴巴','张勇','1990-2-6','电子商务')
--2.对部门的信息进行插入
insert into 部门 values('H001','移动终端部','余承东','移动终端','007'),
('H002','通信技术','徐直军','通信技术服务','007')
--对员工信息进行插入

insert into 员工 values('001','韩东升',25,'男','2015-1-9','博士',
'已婚','H001','p9'),
('002','韩希',26,'女','2018-2-6','学士','未婚','H002','P8')
--先插入岗位后才能插入员工,
insert into 岗位 values('P9','UNITY3D','T2','15000','12000')
insert into 岗位 values('P8','C++','T3','12000','20000')
--创建触发器 用于对修改员工的数据进行提示
create trigger trigger_update 
on 员工
for update 
as	
	print '修改后的数据'
	select * from inserted 
	print '修改前的数据'
	select * from deleted
--创建触发器用于对部门信息修改的提醒
create trigger trigger_update_department
on 部门
for update 
as	
	print '你正在修改部门的信息'
--创建触发器用于对部门增加信息的提醒
create trigger trigger_insert_department
on 部门
for insert
as	
	print '你正在增加部门的信息'
	select * from inserted
--创建触发器用于对修改岗位信息的提醒
create trigger trigger_update_work
on 岗位
for update 
as
	
	print '你正在修改员工的信息'
	
--创建触发器用于对修改公司信息的提醒
create trigger trigger_update_company
on 公司
for update
as
	print '正在修改数据'
	--select * from inserted
	--select * from deleted
--
create trigger trigger_insert_company
On 公司
for insert
as	
	print '正在插入数据'
	select * from inserted
	select * from deleted
--
--
--