-
Notifications
You must be signed in to change notification settings - Fork 0
/
PracticeSQL7.sql
170 lines (133 loc) · 4.39 KB
/
PracticeSQL7.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
CREATE TABLE Clients(
ClientID INT,
ClientName VARCHAR(50) NOT NULL,
Address VARCHAR(100),
Email VARCHAR(30) UNIQUE,
Phone INT,
Business VARCHAR(30) NOT NULL, --Business type of client like Manufacturer, Reseller ...
PRIMARY KEY (ClientID)
)
CREATE TABLE Projects(
ProjectID INT,
Description VARCHAR(100), --Description of project like Accounting, Payroll, ...
StartDate DATE, --Start date of project
PlannedEndDate DATE, --Planned end date of project
ActualEndDate DATE ,
Budget INT CHECK(Budget > 0),
ClientID INT,
CHECK(ActualEndDate > PlannedEndDate),
FOREIGN KEY (ClientID) REFERENCES Clients(ClientID)
)
CREATE TABLE Departments(
DepartmentNo INT,
DepartmentName VARCHAR(100) NOT NULL,
PRIMARY KEY (DepartmentNo)
)
CREATE TABLE Employees(
EmployeeNo INT,
EmployeeName VARCHAR(20) NOT NULL,
Job VARCHAR(20),
Salary INT CHECK(Salary > 1700),
DepartmentNo INT, --DepartmentNo as per departments table
PRIMARY KEY (EmployeeNo),
FOREIGN KEY (DepartmentNo) REFERENCES Departments(DepartmentNo)
)
CREATE TABLE EmployeeProjectTasks(
ProjectID INT,
EmployeeNo INT,
StartDate DATE, --Start date when employee begins task on this project
EndDate DATE, --End date when employee finishes task on this project
Task VARCHAR(30), --Task performed by employee like designing, coding ..
Status VARCHAR(30), -- Status of task like ‘in progress’, ‘complete’,‘cancelled’
PRIMARY KEY (ProjectID, EmployeeNo),
FOREIGN KEY (ProjectID) REFERENCES Projects(ProjectID),
FOREIGN KEY (EmployeeNo) REFERENCES Employees(EmployeeNo)
)
INSERT INTO Clients(ClientID, ClientName, Address, Email, Phone, Business)
VALUES
(1, 'Nayem', 'Portugal', 'shnartho@yahoo.com', 920485163, 'MoslinMeta'),
(2, 'Belmahi', 'Morrocco', 'belmahi@yahoo.com', 920485162, 'BelmahiMeta');
INSERT INTO Projects(ProjectID, Description, StartDate, PlannedEndDate, ActualEndDate, Budget, ClientID)
VALUES
(1, 'Accounting', GETDATE(), GETDATE()+1, GETDATE()+2, 2500, 1),
(2, 'Payroll', GETDATE(), 2022-05-05, 2021-06-06, 2502, 2);
INSERT INTO Departments(DepartmentNo, DepartmentName)
VALUES
(1, 'Main Department'),
(2, 'Secondary Department');
INSERT INTO Employees(EmployeeNo, EmplouyeeName, Job, Salary, DepartmentNo)
VALUES
(1, 'Nayem Pagla', 'Caretaker', 2500, 1),
(2, 'Maheera', 'Doctor', 4000, 2);
SELECT GETDATE()
INSERT INTO EmployeeProjectTasks
(ProjectID, EmployeeNo, StartDate, EndDate, Task, Status)
VALUES
(1, 1, '2022-02-02', '2022-03-05', 'Data Entry', 'in progress'),
(2, 2, '2022-02-02', '2022-03-05', 'Sergery', 'Complete');
select * FROM Employees where EmployeeName like 'M%';
select EmployeeNo, EmployeeName
from table Employees
order by len(EmployeeName) desc;
select Departments.DepartmentName, Employees.EmployeeName, Employees.Salary
from LAB5
order by Salary desc;
--task 1
CREATE PROCEDURE Employees_GetAll
AS
SELECT EmployeeNo, EmplouyeeName, Job, Salary FROM Employees;
EXEC Employees_GetAll;
-- task 2
CREATE PROCEDURE Employees_Insert
@employeeNo int,
@emplouyeeName VARCHAR(20),
@job VARCHAR(20),
@salary int,
@departmentNo int
AS
BEGIN
INSERT INTO employees(employeeNo,emplouyeeName,job,salary,departmentNo) VALUES(@employeeNo,@emplouyeeName,@job,@salary,@departmentNo)
END;
EXEC Employees_Insert @employeeNO = 100,@emplouyeeName = 'NayemKhan', @job = 'Engineer', @salary = 5200, @departmentNo = 2;
-- task 3
alter PROCEDURE Clients_Update
@clientName varchar(50),
@clientID int
AS
BEGIN
UPDATE Clients SET clientName = @clientName
WHERE clientID = @clientID
END;
EXEC Clients_Update @clientID = 1 , @clientName ='Nayem Khan';
-- task 4
CREATE PROCEDURE Employees_Delete
@employeeNo int
AS
BEGIN
DELETE FROM Employees
WHERE employeeNo = @employeeNo
END;
EXEC Employees_Delete @employeeNo = 100;
--task 5
SELECT * FROM Departments;
ALTER TABLE Departments ADD NumberOfEmployees int;
ALTER TABLE Departments DROP COLUMN NumberOfEmployees;
SELECT * FROM Departments;
SELECT * FROM Employees;
UPDATE Departments
SET NumberOfEmployees = 2;
WHERE DepartmentNo = 2;
CREATE TRIGGER trigger1
ON employees
AFTER INSERT
AS
BEGIN
--INSERT INTO Departments(NumberOfEmployees) VALUES (NumberOfEmployees + 1)
--WHERE Employees.DepartmentNo = Depapart
update Departments
set NumberOfEmployees = NumberOfEmployees +1
where DepartmentNo = (
select DepartmentNo from inserted
)
END;
SELECT * FROM Employees;