BusinessEntity1D, FirstName, LastName, JobTitle, Gender FROM HumanResources.Employee, Person.Person WHERE Employee.BusinessEntityID = Person.BusinessEntityTD 60 --2/ Create view to displ
Trang 1
THU DAU MOT UNIVERSITY INSTITUTE OF ENGINEERING TECHNOLOGY
PAI HOC
\ rit DAU MOT
2009 THỦ DAU MOT UNIVERSITY
REPORT
DATABASE MANAGEMENT SYSTEM
TOPIC
HUMAN RESOURCE MANAGEMENT
Teacher: MS Neuyen Thi Thuy Group members:
1/ Duong Hoai Nam
Student code: 21280103E0020 2/ Nguyen Tan Dat
Student code: 21280103E0017 Class: D21EN.KTPM01
Oct - 2022
Trang 2TABLE OF CONTENTS
1.About the Database
2.Description the Database
3.Relationship diagram
CHAPTER 2 OPERATIONS ON THE DATABASTE co eSSSSe re
2 simple conditional views (on Ì tablÌe, multiple fables) ««<
2 views with complex conditions
oe
2.Create Stored procedures
1 procedure with default parameters
1 procedure with output parameter
2 procedures with input parameters
2 functions that return scalar type Co
2 functions that return a table
1 function that returns a self-defined table
4.Create Triggers and Transactions
1 Trigger Insert
1 Trigger Update
5.Create users
Create 2 users and decentralize these 2 ([ƒGFCHÍ HSGFS Ăc<<<<<<+ CHAPTER 3 CONCLUSION
1.Results
2,.Limitations (not yet done)
Trang 3INTRODUCTION
Databases are phrases that are used a lot in the fields of data, software
programming, information technology, websites, etc are extremely important
components for building and developing software and applications on the Internet mobile and PC platforms
In today's digital age, when enterprises effectively manage the database, resources will be optimized, processes will be streamlined and the performance of all
employees will be increased
So in this report, my team will demonstrate some operations on the database, with Microsoft's sample database Adventure Work2019
Trang 4
CHAPTER 1 DATABASE MANAGEMENT
1 About the Database
Adventure Works Database is a Microsoft product sample for an online transaction processing (OLTP) database The AdventureWorks Database supports a fictitious, multinational manufacturing company called Adventure Works Cycles
Microsoft highlight SQL Server features with the Adventure Works Database,
various versions of which can be downloaded at the SQL Server community projects and samples page on CodePlex, a Microsoft open source project site Downloads include Adventure Works Database 2008 and Adventure Works Database 2012
Adventure Works Database is also used in code examples found in SQL Server documentation and books There are also versions for SQL Azure and SQL Server
2005
2 Description the Database
In this report, my team will perform operations on the tables under the topic of Human Resource Management, including:
> Person (Person)
AdditionalContactInfo xml
ModifiedDate datetime
Trang 5
> Employee (Humanresources)
NationalIDNumber nvarchar(15)
OrganizationNode hierarchyid
OrganizationLevel
SickLeaveHours smallint
> JobCandidate (Humanresources)
ModifiedDate datetime
> SalesPerson (Sales)
ID
Sal
Bonus
CommissionPct
SalesYTD
SalesLast Year
rowegul
ModifiedDate
smallmon
datetime
> EmployeePayHistory (HumanResources)
ID
eDate
int datetime
Trang 6
> Shift (HumanResources)
Trang 7
3
Relationship diagram
JobCandidate (HumanResources)
# ®bLandidsleD Wanesfebtyl2
đan me
Magife47Ate (HumanResources)
© dunresstentyD 5 f4 sêCvhing tsgáoyee EesezZ6f21
logan
Onperezportiode
(roanzeoosd evel sebTitie
BrmOate
Martalstans #19 _0amesgtretyŸ
Gower
EmploveeDepartmentHistory (HumanResources) SalanedFing
Mod SecDete
oe “TP”
Ê\ EngkgesoeppTrertesyv, tết GEN
4 ÉrgleyseQepe“Trevts4zeyr/ (xeowyArxes| Deaw^reee(T \_S9le#fprbon_Engloyet 90949670001
Shift (HumanResources)
† SưMO
Name
Department (HumanResources) StartTime
GupNeee
MoaheuDute
(me Promebor
Trang 8
CHAPTER 2 OPERATIONS ON THE DATABASE
1 Create View
Create 5 View by levels
v 2 simple conditional views (on I table, multiple tables):
1/ Create view to display male employees who under 35 years old and still single
CREATE VIEW VIEWL
AS
SELECT Employee BusinessEntity1D, FirstName, LastName, JobTitle, Gender
FROM HumanResources.Employee, Person.Person
WHERE Employee.BusinessEntityID = Person.BusinessEntityTD
60
2/ Create view to display employees who work in the evening shift
CREATE VIEW VIEW2
AS mờ
SELECT Employee.BusinessEntityID, FirstName, LastName, JobTitle, Gender
FROM HumanResources Employee, Person.Person
WHERE Employee BusinessEntityID = Person BusinessEntityID
AND Employee.BusinessEntityID IN (SELECT BusinessEntityID
FROM HumanResources.EmployeeDepartmentHistory, HumanResources.Shift WHERE EmployeeDepartmentHistory.ShiftID = Shift.ShiftID AND Shift.Name = ‘Evening’ )
60
v2 views with complex conditions
/* 3/ Create view to display employees with organization level is 3,
under 4@ years old and work in the night shift */
CREATE VIEW VIEW3
AS
SELECT Employee.BusinessEntityID, FirstName, LastName, JobTitle, Gender
FROM HumanResources Employee, Person.Person
WHERE Employee.BusinessEntityID = Person.BusinessEntityID
AND Employee BusinessEntityID IN (SELECT BusinessEntityID
FROM HumanResources.EmployeeDepartmentHistory, HumanResources Shift
WHERE EmployeeDepartmentHistory.ShiftID = Shift.ShiftID AND Shift.Name = 'Night')
AND DATEDIFF(YY, Employee.BirthDate, GETDATE()) < 40
AND OrganizationLevel = 3
60
Trang 9/* 4/ Create view to display infomation of male employees over 40
that has salary hourly rate less than 15 */
CREATE VIEW VIEW4
AS
SELECT Employee.BusinessEntityID, FirstName, LastName, JobTitle, Gender
FROM HumanResources.Employee, Person.Person
WHERE Employee.BusinessEntityID = Person.BusinessEntity1D
FROM HumanResources Employee JOIN HumanResources EmployeePayHistory
ON Employee.BusinessEntityID = EmployeePayHistory.BusinessEntityID
WHERE Gender = 'MM' AND DATEDIFF(YY,BirthDate,GETDATE()) > 40 AND RATE < 15)
G0
~ 1 view data update
/* 5/ Create an updated view to increase 10% of hourly salary rate for employees who have worked for more than 19 years */
CREATE VIEW VIEWS
AS
SELECT *
FROM HumanResources EmployeePayHistory
GO
UPDATE VIEWS
SET Rate = Rate + (Rate*10/1@0)
WHERE BusinessEntityID IN ( SELECT BusinessEntityID
FROM HumanResources Employee WHERE DATEDIFF(YY, Employee.HireDate, GETDATE()) > 10)
GO
Trang 102 Create Stored procedures
¥ 1 non-parametric procedure
/* 1/ Non-parametric procedure: Display the number of MALE employees */ CREATE PROC PROC1
AS
BEGIN
DECLARE @T INT
SET @T = (SELECT COUNT(*)
FROM HumanResources Employee
WHERE Gender = 'M')
IF(@T > 1)
PRINT ‘THERE ARE '+RTRIM(CAST(@T AS CHAR) )+' EMPLOYEES'
ELSE
PRINT 'THERE IS '+RTRIM(CAST(@T AS CHAR) )+' EMPLOYEE’
END
G0
~ 1 procedure with default parameters
/* 2/ Procedure with default parameter: Display the information
of the employees when user enter the BusinessEntityID, set default as 1 */
CREATE PROC PROC2 (@T INT = 1)
AS
BEGIN
IF EXISTS (SELECT * FROM HumanResources.Employee WHERE BusinessEntityID = @T)
SELECT Employee.BusinessEntityID, FirstName, LastName, JobTitle, Gender
FROM HumanResources.Employee, Person.Person
WHERE Employee.BusinessEntityID = Person.BusinessEntityID AND Employee.BusinessEntityID = @T ELSE
PRINT 'THE EMPLOYEES IS NOT EXISTS'
END
GO
¥ 1 procedure with output parameter
Trang 11/* 3/ Procedure with output parameter: Display the highest hourly
salary rate of the employees of the organization level that the user entered*/
CREATE PROC PROC3 (@T INT , @A FLOAT OUT)
AS
BEGIN
IF(@T > @) AND (@T <5)
SET @A = (SELECT TOP 1 Rate
FROM HumanResources Employee JOIN HumanResources EmployeePayHistory
ON Employee.BusinessEntityID = EmployeePayHistory.BusinessEntityID WHERE OrganizationLevel = @T
ORDER BY Rate DESC)
ELSE
PRINT ‘INVALID ORGANIZATION LEVEL'
END
GO
~ 2 procedures with input parameters
/* 1-Count the number of employees whose hourly salary rate is below
the number entered by user */
CREATE PROC PROC4_1 (@T FLOAT)
AS
BEGIN
IF(@T > @)
BEGIN
DECLARE @A INT
SET @A = (SELECT COUNT(*)
FROM HumanResources EmployeePayHistory
WHERE Rate < @T)
IF (@A>1)
PRINT ‘THERE ARE '+RTRIM(CAST(@A AS CHAR) )+' EMPLOYEES WITH ORGANIZATION LEVEL IS '+RTRIM(CAST(@T AS CHAR))
ELSE
PRINT "THERE IS '+RTRIM(CAST(@A AS CHAR) )+' EMPLOYEE WITH ORGANIZATION LEVEL IS '+RTRIM(CAST(@T AS CHAR) ) END
ELSE
PRINT ‘INVALID ORGANIZATION LEVEL'
END
G0
Trang 12/* 2-Display the department and the shift of an
employee when user enter a BusinessEntity10 */
CREATE PROC PROC4_2 (@T INT)
AS
BEGIN
IF EXISTS (SELECT * FROM HumanResources.EmployeeDepartmentHistory WHERE BusinessEntityID = @T) SELECT BUSINESSENTITYID, DEPARTMENT.NAME AS DEPARTMENT NAME, SHIFT NAME AS SHIFT_NAME
FROM HumanResources EmployeeDepartmentHistory EDH, HumanResources.Department, HumanResources Shift WHERE EDH.DepartmentID = Department.DepartmentID AND EDH.ShiftID = Shift ShiftID
AND EDH.BusinessEntityID = @T
ELSE
PRINT 'THE EMPLOYEES IS NOT EXISTS"
END
G0
3 Create User defined Function
~ 2 functions that return scalar type
1.1/ Function returns the number of months that the employee has worked when the user enter the BusinessEntityID
CREATE FUNCTION FN1_1 (@T INT)
AAAARA
RETURNS INT
AS
BEGIN
DECLARE @A INT
IF EXISTS ( SELECT * FROM HumanResources.Employee WHERE BusinessEntityID = @T) BEGIN
SET @A = (SELECT DATEDIFF(MM, HireDate, GETDATE())
FROM HumanResources Employee
WHERE BusinessEntityID = @T)
END
ELSE
SET @A = @
RETURN @A
END
G0
Trang 13
1.2/ Function returns the average hourly salary rate of a Department
when the user enter the DepartmentID
RETURNS FLOAT
AS
BEGIN
DECLARE @A FLOAT
IF EXISTS (SELECT * FROM HumanResources.Department WHERE DepartmentID = @T)
BEGIN
SET @A = (SELECT AVG(Rate)
FROM HumanResources EmployeePayHistory
FROM HumanResources EmployeeDepartmentHistory AS EDH
END
ELSE
SET @A = 9
RETURN @A
END
60
~ 2 functions that return a table
2/ Function that return a table
2.1/ Function return the information of employees of a Department when user enter name of Department CREATE FUNCTION FN2 1 (@T VARCHAR)
RETURNS TABLE
AS
RETURN SELECT Employee BusinessEntityID, FirstName, LastName, JobTitle, Gender
FROM HumanResources Employee, Person Person
WHERE Employee BusinessEntityID = Person BusinessEntityID
AND Employee BusinessEntityID IN (SELECT BusinessEntity10
FROM HumanResources.EmployeeDepartmentHistory AS EDH, HumanResources Department
WHERE EDH.DepartmentID = Department DepartmentID
AND Department.Name LIKE @T)
60
Trang 14
/* 2.2/ The function returns the information of employees who are paid monthly and have hourly salary rate above the number entered by the user */
CREATE FUNCTION FN2 2 (@T FLOAT)
RETURNS TABLE
AS
RETURN SELECT Employee.BusinessEntityID, FirstName, LastName, JobTitle, Gender
FROM HumanResources Employee, Person.Person
WHERE Employee BusinessEntityID = Person BusinessEntityID
AND Employee.BusinessEntityID IN (SELECT BusinessEntityID
FROM HumanResources.EmployeePayHistory WHERE Rate > @T AND PayFrequency = 1)
60
~ 1 function that returns a self-defined table
3/ Function that returns a self-defined table
/* Function that allow user enter the name of department,
return the information of employees of that department who have
worked for more than 1@ years, the information include business entity id, job title,
gender and hire date */
CREATE FUNCTION FN3 (@T NVARCHAR(3@) )
RETURNS @TAB TABLE (BusinessEntityID INT, Job_Title VARCHAR(3@), Gender VARCHAR(1), HireDate DATE)
AS
BEGIN
IF EXISTS ( SELECT * FROM HumanResources.Department WHERE Department.Name LIKE @T)
INSERT INTO @TAB
SELECT Employee BusinessEntityID, JobTitle, Gender, HireDate
FROM HumanResources Employee, HumanResources.EmployeeDepartmentHistory AS EDH,
HumanResources Department
WHERE Employee BusinessEntityID = EDH.BusinessEntityID AND EDH.DepartmentID = Department DepartmentID AND Department Name LIKE @T
AND DATEDIFF(YY, HireDate, GETDATE()) > 10
RETURN
END
60
4 Create Triggers and Transactions
Y 1 Trigger Insert
Trang 151/ Trigger Insert : Trigger not allow user enter the new salary lower than the old one
CREATE TRIGGER TRGL
ON HumanResources EmployeePayHistory
FOR INSERT
AS
BEGIN
IF (SELECT Rate FROM inserted) < (SELECT TOP 1 EPH.Rate
FROM HumanResources.EmployeePayHistory AS EPH, inserted
WHERE inserted.BusinessEntityID = EPH.BusinessEntity1D
ORDER BY EPH.Rate DESC) BEGIN
PRINT ‘The new salary cannot be lower than the old one’
ROLLBACK TRAN
END
ELSE
COMMIT TRAN
END
GO
~ 1 Trigger Update
2/ Trigger Update : Trigger increase hourly salary rate by 20% for newly married employees
CREATE TRIGGER TRG2
ON HumanResources Employee
AFTER UPDATE
AS
BEGIN
IF (SELECT MaritalStatus FROM inserted) = 'M'
UPDATE HummanResources EmployeePayHistory
SET Rate = Rate + (Rate*@.2)
WHERE BusinessEntityID IN (SELECT BusinessEntityID FROM inserted)
END
G0
Y 1 Trigger Delete
Trang 163/ Trigger Delete : Trigger for disallowing users to delete applications from
people who have not yet been hired
CREATE TRIGGER TRG3
ON HumanResources JobCandidate
FOR DELETE
AS
BEGIN
IF (SELECT BusinessEntityID FROM deleted) IS NULL
BEGIN
PRINT ‘CAN NOT DELETE THIS JOB CANDIDATE"
ROLLBACK TRAN
END
ELSE
COMMIT TRAN
END
G0
Y 2 Transaction
1/ Not Explicit transaction: Update Chief Executive Officer's middle name to ‘Oliver'
UPDATE Person.Person
SET MiddleName = '011ver'
WHERE BusinessEntityTD = (SELECT BusinessEntityTD
FROM HumanResources Employee
60
/* 2/ Explicit transaction: employees who have not been assigned territories
are assigned a sales quota of 259999, count the number of sales person
with a quota of less than 380009 */
BEGIN TRAN
UPDATE Sales.SalesPerson
SET SalesQuota = 250000
WHERE TerritoryID IS NULL
SELECT COUNT(*) AS Quota_below_300800
FROM Sales.SalesPerson
WHERE SalesQuota < 300000
ROLLBACK TRAN
60
5 Create users
~ Create 2 users and decentralize these 2 different users