0% found this document useful (0 votes)
26 views

CREATE DATABASE BaiDieuKien02

The document defines tables and relationships for an employee database. It creates tables for regions, positions, jobs, employees, salary details, countries, locations, departments, and job histories. It also inserts sample data into the tables and creates views on the tables.

Uploaded by

hunggiant2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CREATE DATABASE BaiDieuKien02

The document defines tables and relationships for an employee database. It creates tables for regions, positions, jobs, employees, salary details, countries, locations, departments, and job histories. It also inserts sample data into the tables and creates views on the tables.

Uploaded by

hunggiant2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CREATE DATABASE BaiDieuKien02

GO

USE BaiDieuKien02
GO

CREATE TABLE Regions(


RegionId int PRIMARY KEY,
RegionName nvarchar(64) NOT NULL,
Deleted bit
)
GO

CREATE TABLE Positions(


PositionId int PRIMARY KEY,
PositionName nvarchar(128),
PositionAllowance float,
LunchAllowance float,
PhoneAllowance float,
GasolineAllowance float,
Deleted bit
)
GO

CREATE TABLE Jobs(


JobId int PRIMARY KEY,
JobTitle nvarchar(128) NOT NULL,
MinSalary float,
MaxSalary float,
Deleted bit
)
GO

CREATE TABLE Employees (


EmployeeId varchar(16) PRIMARY KEY,
FirstName nvarchar(32),
LastName nvarchar(16),
Email nvarchar(64),
Birthday date,
Sex bit,
HomePhone varchar(16),
Mobile varchar(16),
Address nvarchar(128),
City nvarchar(32),
Country nvarchar(32),
HireDate date,
Description nvarchar(MAX),
Deleted bit
)
GO

CREATE TABLE SalaryDetails(


SalaryId int PRIMARY KEY,
EmployeeId varchar(16) FOREIGN KEY REFERENCES Employees(EmployeeId),
PayDate date,
Salary float,
TotalAllowance float,
Other float,
TotalSalary float,
Note nvarchar(512)
)
GO

CREATE TABLE Countries(


CountryId varchar(16) PRIMARY KEY,
CountryName nvarchar(64) NOT NULL,
RegionId int FOREIGN KEY REFERENCES Regions(RegionId),
Deleted int
)
GO

CREATE TABLE Locations(


LocationId int PRIMARY KEY,
StreetAddress nvarchar(64),
PostalCode varchar(8),
City nvarchar(64),
StateProvince varchar(32),
CountryId varchar(16) FOREIGN KEY REFERENCES Countries(CountryId),
Deleted bit
)
GO

CREATE TABLE Departments(


DepartmentId varchar(8) PRIMARY KEY,
DepartmentName nvarchar(64),
LocationId int FOREIGN KEY REFERENCES Locations(LocationId),
Deleted bit
)
GO

CREATE TABLE JobHistories(


EmployeeId varchar(16) FOREIGN KEY REFERENCES Employees(EmployeeId),
StartDate date,
EndDate date,
JobId int FOREIGN KEY REFERENCES Jobs(JobId),
BasicSalary float,
PositionId int FOREIGN KEY REFERENCES Positions(PositionId),
DepartmentId varchar(8) FOREIGN KEY REFERENCES
Departments(DepartmentId),
Deleted bit,
PRIMARY KEY(EmployeeId, StartDate)
)

GO

INSERT INTO Regions


VALUES(1, N'Châu Á',''),
(2, N'Châu Phi',''),
(3, N'Châu Âu',''),
(4, N'Châu Bắc Mĩ',''),
(5, N'Châu Nam Mĩ',''),
(6, N'Châu Úc',''),
(7, N'Châu Nam Cực','')
INSERT INTO Positions
VALUES (1, N'Giám đốc', 0.6, 730000, 100000, 500000, ''),
(2, N'Phó Giám đốc', 0.4, 700000, 100000, 500000, ''),
(3, N'Trưởng phòng', 0.3, 70000, 100000, 500000, ''),
(4, N'Phó Trưởng phòng', 0.2, 430000, 100000, 500000, ''),
(5, N'Nhân viên', 0.1, 730000, 100000, 500000, '')

INSERT INTO Jobs


VALUES (1, N'Giám đốc điều hành', 50000000, 600000000, ''),
(2, N'Kinh tế và tài chính', 7800000, 20000000, ''),
(3, N'Quản lý', 5500000, 20000000, ''),
(4, N'Trưởng nhóm', 5000000, 50000000, ''),
(5, N'Lập trình viên', 8500000, 30000000, ''),
(6, N'Kế toán', 5000000, 30000000, ''),
(7, N'Thiết kế đồ họa', 8000000, 20000000, ''),
(8, N'Marketing', 5000000, 30000000, '')

INSERT INTO Employees


VALUES ('G001', N'Đạt', N'Nguyễn', '[email protected]', '1998-12-12', 1,
'0123324254', '095734255', N'Ba Đình - Hà Nội', N'Hà Nội', N'Việt Nam', '2011-
2-2', '',''),
('G002', N'Đức', N'Nguyễn', '[email protected]', '2004-2-12', 1,
'0123324254', '095734255', N'Phú Xuyên - Hà Nội', N'Hà Nội', N'Việt Nam',
'2023-10-2', '',''),
('G003', N'Đông', N'Nguyễn', '[email protected]', '2004-1-1', 1,
'0123324254', '095734255', N'Long Biên - Hà Nội', N'Hà Nội', N'Việt Nam',
'2012-2-2', '',''),
('G004', N'Đăng', N'Nguyễn', '[email protected]', '2003-12-12', 1,
'0123324254', '095734255', N'Hoàn Kiếm - Hà Nội', N'Hà Nội', N'Việt Nam',
'2016-2-2', '',''),
('G005', N'Đô', N'Nguyễn', '[email protected]', '2006-12-12', 1,
'0123324254', '095734255', N'Cầu Giấy - Hà Nội', N'Hà Nội', N'Việt Nam',
'2015-2-2', '',''),
('G006', N'James', N'Nguyễn', '[email protected]', '1997-12-12', 1,
'0123324254', '095734255', N'76 Pulaski St.Chino Hills', N'California', N'Mỹ',
'2015-2-2', '',''),
('G007', N'Robert', N'Nguyễn', '[email protected]', '2001-12-12', 1,
'0123324254', '095734255', N'98 Park End St', N'London', N'Anh', '2009-2-2',
'',''),
('G008', N'John', N'Nguyễn', '[email protected]', '1989-12-12', 1, '0123324254',
'095734255', N'2 Black Range Road', N'Wandella', N'Australia', '2007-2-2',
'',''),
('G009', N'Micheal', N'Nguyễn', '[email protected]', '1996-12-12', 1,
'0123324254', '095734255', N'175 El Hegaz St., HELIOPOLIS', N'Cairo', N'Ả
rập', '2012-2-2', '',''),
('G010', N'David', N'Nguyễn', '[email protected]', '1990-12-12', 1,
'0123324254', '095734255', N'Bolivia 640', N'Santa Rosa', N'Argentina', '2013-
2-2', '','')

INSERT INTO SalaryDetails


VALUES (1, 'G001', '2017-2-2', 100000000, 10000000, 30000000, 113000000, ''),
(2, 'G002', '2023-2-2', 10000000, 1000000, 0, 11000000, ''),
(3, 'G003', '2017-2-2', 20000000, 2000000, 0, 22000000, ''),
(4, 'G004', '2017-2-2', 30000000, 3000000, 0, 33000000, ''),
(5, 'G005', '2017-2-2', 25000000, 2500000, 0, 27500000, ''),
(6, 'G006', '2017-2-2', 10000000, 1000000, 0, 11000000, ''),
(7, 'G007', '2017-2-2', 10000000, 1000000, 200000, 1120000, ''),
(8, 'G008', '2017-2-2', 15000000, 1500000, 0, 16500000, ''),
(9, 'G009', '2017-2-2', 30000000, 3000000, 200000, 33200000, ''),
(10, 'G010', '2017-2-2', 10000000, 1000000, 3000000, 11300000, '')

INSERT INTO Countries


VALUES ('VN001', N'Việt Nam', 1, ''),
('A00L1', N'Anh', 3, ''),
('MY00L1', N'Mỹ', 4, ''),
('AR00L1', N'Ả rập', 2, ''),
('UC00L1', N'Úc', 6, ''),
('BZ00L1', N'Brazil', 5, '')

INSERT INTO Locations


VALUES (1, N'244 Phố Vọng', '630000', N'Hà Nội', 'Ha Noi', 'VN001', ''),
(2, N'1 Đại Cồ Việt', '160000', N'Chính Nghĩa', 'Hung Yen', 'VN001', ''),
(3, N'1 Đại Cồ Việt', 'B12 8XE', N'Birmingham', 'West Midlands', 'A00L1', ''),
(4, N'1 Đại Cồ Việt', '160000', N'Chính Nghĩa', 'Hung Yen', 'MY00L1', ''),
(5, N'1 Đại Cồ Việt', '160000', N'Chính Nghĩa', 'Hung Yen', 'AR00L1', ''),
(6, N'1 Đại Cồ Việt', '160000', N'Chính Nghĩa', 'Hung Yen', 'UC00L1', ''),
(7, N'1 Đại Cồ Việt', '160000', N'Chính Nghĩa', 'Hung Yen', 'BZ00L1', '')

INSERT INTO Departments


VALUES ('DP001', N'Ban giám đốc', 1, ''),
('DP002', N'Ban quản lý', 2, ''),
('DP003', N'Phòng kế toán', 3, ''),
('DP004', N'Phòng Marketing', 4, ''),
('DP005', N'Phòng công nghệ thông tin', 5, '')

INSERT INTO JobHistories


VALUES ('G001', '2011-2-2', '2023-2-2', 1, 23452, 1, 'DP001', ''),
('G002', '2022-2-2', '2023-2-2', 2, 23452, 2, 'DP003', ''),
('G003', '2012-2-2', '2023-2-2', 3, 23452, 3, 'DP002', ''),
('G004', '2016-2-2', '2023-2-2', 4, 23452, 4, 'DP004', ''),
('G005', '2015-2-2', '2023-2-2', 5, 23452, 5, 'DP005', ''),
('G006', '2012-2-2', '2023-2-2', 6, 23452, 5, 'DP003', ''),
('G007', '2009-2-2', '2023-2-2', 7, 23452, 5, 'DP004', ''),
('G008', '2007-2-2', '2023-2-2', 8, 23452, 5, 'DP004', ''),
('G009', '2012-2-2', '2023-2-2', 5, 23452, 5, 'DP005', ''),
('G010', '2013-2-2', '2023-2-2', 7, 23452, 5, 'DP004', '')
GO

-- 3
-- a
CREATE VIEW vwNhanVienOHaNoi AS
SELECT * FROM Employees
WHERE City = N'Hà Nội'
GO

SELECT * FROM vwNhanVienOHaNoi


GO

-- b
CREATE VIEW vwNhanVien5Nam AS
SELECT * FROM Employees
WHERE (YEAR(GETDATE()) - YEAR(HireDate)) >= 5
GO

SELECT * FROM vwNhanVien5Nam


GO

-- c
CREATE VIEW vwNhanVienChauA AS
SELECT e.EmployeeId, e.FirstName, e.LastName, r.RegionName FROM Employees e
INNER JOIN JobHistories j
ON e.EmployeeId = j.EmployeeId INNER JOIN Departments d
ON j.DepartmentId = d.DepartmentId INNER JOIN Locations l
ON d.LocationId = l.LocationId INNER JOIN Countries c
ON l.CountryId = c.CountryId INNER JOIN Regions r
ON c.RegionId = r.RegionId
WHERE r.RegionName LIKE '%Châu Á%'
GO

SELECT * FROM vwNhanVienChauA


GO

-- d
CREATE VIEW vwNhanVienOVietNamLamOMy AS
SELECT e.EmployeeId, e.FirstName, e.LastName, e.Country, c.CountryName AS
'Đang làm việc tại'
FROM Employees e INNER JOIN JobHistories j
ON e.EmployeeId = j.EmployeeId INNER JOIN Departments d
ON j.DepartmentId = d.DepartmentId INNER JOIN Locations l
ON d.LocationId = l.LocationId INNER JOIN Countries c
ON l.CountryId = c.CountryId
WHERE e.Country = N'Việt Nam' AND c.CountryName = N'Mỹ'
GO

SELECT * FROM vwNhanVienOVietNamLamOMy


GO

-- e
CREATE VIEW vwNhanVienTruongPhong AS
SELECT e.EmployeeId, e.FirstName, e.LastName, p.PositionName
FROM Employees e INNER JOIN JobHistories j
ON e.EmployeeId = j.EmployeeId INNER JOIN Positions p
ON j.PositionId = p.PositionId
WHERE p.PositionName = N'Trưởng phòng'
GO

SELECT * FROM vwNhanVienTruongPhong


GO

-- f
CREATE VIEW vwDanhSachPhongBan AS
SELECT d.DepartmentName, l.StreetAddress, c.CountryName FROM Departments d
INNER JOIN Locations l
ON d.LocationId = l.LocationId INNER JOIN Countries c
ON l.CountryId = c.CountryId
GO
SELECT * FROM vwDanhSachPhongBan
GO

-- g
CREATE VIEW vwNhanVienMoiLamThangNay AS
SELECT * FROM Employees
WHERE YEAR(HireDate) = YEAR(GETDATE()) AND MONTH(HireDate) = MONTH(GETDATE())
GO

SELECT * FROM vwNhanVienMoiLamThangNay


GO

-- h
CREATE VIEW vwNhanVienLamPhongKeToan AS
SELECT e.EmployeeId, e.FirstName, e.LastName
FROM Employees e INNER JOIN JobHistories j
ON e.EmployeeId = j.EmployeeId INNER JOIN Departments d
ON j.DepartmentId = d.DepartmentId
WHERE d.DepartmentName = N'Phòng kế toán'
GO

SELECT * FROM vwNhanVienLamPhongKeToan


GO

-- i
CREATE VIEW vwThongKeTongLuongPhong2017 AS
SELECT d.DepartmentId, d.DepartmentName, SUM(s.TotalSalary) AS 'Tổng lương'
FROM SalaryDetails s INNER JOIN Employees e
ON s.EmployeeId = e.EmployeeId INNER JOIN JobHistories j
ON e.EmployeeId = j.EmployeeId INNER JOIN Departments d
ON j.DepartmentId = d.DepartmentId
WHERE s.PayDate LIKE '%2017%'
GROUP BY d.DepartmentId,d.DepartmentName
GO

SELECT * FROM vwThongKeTongLuongPhong2017


GO

-- j
CREATE VIEW vwTongLuong2017 AS
SELECT e.EmployeeId, e.FirstName, e.LastName, SUM(s.TotalSalary) AS
'Tổng...more

You might also like