源码见 https://github.com/DearMrJ/HrmSys
数据库文件:
create DATABASE HrmSys;
use HrmSys;
--alter table dept_inf add default (0) for mnumber with values
--创建表dept_inf
create table dept_inf(
ID INT IDENTITY(10,1) NOT NULL, --列自增
NAME VARCHAR(20) NOT NULL,
REMARK VARCHAR(30) NOT NULL,
mnumber int default (0),
PRIMARY KEY (ID)
)
set identity_insert dept_inf on;
insert into dept_inf(ID,NAME,REMARK) values(0,'无部门','待分配')
insert into dept_inf(ID,NAME,REMARK) values(1,'技术部','技术部'),(2,'运营部','运营部'),(3,'财务部','财务部'),(5,'总工办','总工办'),(6,'市场部','市场部'),(7,'教学部','教学部');
set identity_insert dept_inf off; --开启identity_insert.当 IDENTITY_INSERT 设置为 OFF 时,不能为表‘XXX’中的标识列插入显式值。
--创建表job_inf
create table job_inf(
ID INT IDENTITY(10,1) NOT NULL, --列自增
NAME VARCHAR(50) NOT NULL,
REMARK VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (ID)
)
set identity_insert job_inf on;
insert into job_inf(ID,NAME,REMARK) values(1,'实习生','实习生'),(2,'java开发工程师','java开发工程师'),(3,'java中级开发工程师','java中级开发工程师'),(4,'java高级开发工程师','java高级开发工程师'),(5,'系统管理员','系统管理员'),(6,'架构师','架构师'),(7,'主管','主管'),(8,'经理','经理'),(9,'总经理','总经理');
set identity_insert job_inf off; --开启identity_insert.当 IDENTITY_INSERT 设置为 OFF 时,不能为表‘XXX’中的标识列插入显式值。
--创建表user_inf
create table user_inf(
ID INT IDENTITY(2,1) NOT NULL,
loginname VARCHAR(20) NOT NULL,
PASSWORD VARCHAR(16) NOT NULL,
USERSTATUS INT NOT NULL DEFAULT '1',
createdate datetime default getdate(),
username VARCHAR(20) DEFAULT NULL,
PRIMARY KEY (ID)
)
set identity_insert user_inf on;
insert into user_inf(ID,loginname,PASSWORD,USERSTATUS,createdate,username) values(1,'admin','123456',2,default,'超级管理员');
set identity_insert user_inf off;
--创建表employee_inf
create table employee_inf(
ID INT IDENTITY(21,1) NOT NULL,
DEPT_ID INT NOT NULL,
JOB_ID INT NOT NULL,
NAME VARCHAR(20) NOT NULL,
CARD_ID VARCHAR(18) NOT NULL,
ADDRESS VARCHAR(50) NOT NULL,
POST_CODE VARCHAR(50) DEFAULT NULL,
TEL VARCHAR(16) DEFAULT NULL,
PHONE VARCHAR(11) NOT NULL,
QQ_NUM VARCHAR(10) default NULL,
EMAIL VARCHAR(50) NOT NULL,
SEX INT NOT NULL DEFAULT '1',
PARTY VARCHAR(10) DEFAULT NULL,
BIRTHDAY DATETIME DEFAULT NULL,
RACE VARCHAR(100) DEFAULT NULL,
EDUCATION VARCHAR(10) DEFAULT NULL,
SPECIALITY VARCHAR(20) DEFAULT NULL,
HOBBY VARCHAR(100) DEFAULT NULL,
REMARK VARCHAR(500) DEFAULT NULL,
createdate datetime default getdate(),
PRIMARY KEY(ID),
CONSTRAINT FK_EMP_DEPT FOREIGN KEY (DEPT_ID) REFERENCES dept_inf(ID),
CONSTRAINT FK_EMP_JOB FOREIGN KEY (JOB_ID) REFERENCES job_inf(ID),
)
set identity_insert employee_inf on;
insert into employee_inf(ID,DEPT_ID,JOB_ID,NAME,CARD_ID,ADDRESS,POST_CODE,TEL,PHONE,QQ_NUM,EMAIL,SEX,PARTY,BIRTHDAY,RACE,EDUCATION,SPECIALITY,HOBBY,REMARK,createdate)
VALUES (1,1,9,'gakki',20160331,'广州天河','510000','0668-7658284',13250352690,'285692983','[email protected]',1,'共青团员','1998-04-04 00:00:00','汉','本科','散打','唱歌','哈啤',DEFAULT),
(2,1,8,'佐佐木希',20166666,'广州越秀','510030','0668-7658284',18244967180,'3260167367','[email protected]',0,null,'1988-02-08 00:00:00','和','本科','美声','唱歌','sasaki',DEFAULT),
(3,1,8,'新垣结衣',20163333,'广州越秀','510030','0668-7658284',13068519745,'1776990136','[email protected]',0,null,'1988-06-11 00:00:00','和','本科','美声','美食','gakki',DEFAULT)
set identity_insert employee_inf OFF;
--创建表salary_inf
create table salary_inf(
id bigint identity(10,1) primary key not null,
employee_name varchar(20) not null,
job_id int not null,
dept_id int not null,
employee_id int not null,
basic_salary decimal default (0), --基本工资
tax decimal default (0),
bonus decimal default (0), --奖金
fine decimal default (0), --罚金
real_wage decimal , --实发工资
year varchar(4) , --工资年份,不设默认值,手动插入
month varchar(2), --工资月份,不设默认值,手动插入
createdate datetime default getdate(),
constraint salary_job_fk foreign key(job_id) references job_inf(ID) ,
constraint salary_dept_fk foreign key(dept_id) references dept_inf(ID) ,
constraint salary_employee_fk foreign key(employee_id) references employee_inf(ID) ,
)
#员工表触发器,修改部门人数
--drop trigger insert_employee
create trigger insert_employee
on employee_inf
after insert
as
begin
update dept_inf set mnumber = mnumber+1 from dept_inf d ,inserted i
where d.ID = i.DEPT_ID
end
--drop trigger delete_employee
create trigger delete_employee
on employee_inf
after delete
as
begin
update dept_inf set mnumber = mnumber-1 from dept_inf d ,deleted del
where d.ID = del.DEPT_ID
end;
--update trigger update_employee
--drop trigger update_employee
create trigger update_employee
on employee_inf
after update
as begin
update dept_inf set mnumber=mnumber+1 from dept_inf d ,inserted i where d.ID = i.DEPT_ID;
update dept_inf set mnumber=mnumber-1 from dept_inf d ,deleted del where d.ID =del.DEPT_ID;
end
--删除部门触发器
--drop trigger delete_dept
create trigger delete_dept
on dept_inf
instead of delete
as begin
update employee_inf set DEPT_ID = 0 from employee_inf e,deleted del where e.DEPT_ID = del.ID;
delete from dept_inf from dept_inf d , deleted del where d.ID = del.ID;
end