-
Notifications
You must be signed in to change notification settings - Fork 2
/
Session(4&5).sql
145 lines (72 loc) · 3.43 KB
/
Session(4&5).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
SELECT * FROM Company.Emp_info;
SET SQL_SAFE_UPDATES = 0;
DELETE FROM Company.Emp_info WHERE Emp_Id = 2;
DELETE FROM Company.Emp_info WHERE Emp_Name = "JOHN";
-- ******* Add new column ************
ALTER TABLE Company.Emp_info ADD Emp_Email VARCHAR(30);
-- ******** UPDATE AFTER ALTER **********
UPDATE Company.Emp_info SET Emp_Email = "[email protected]" WHERE Emp_id = 3;
UPDATE Company.Emp_info SET Emp_Email = "[email protected]" WHERE Emp_id = 4;
UPDATE Company.Emp_info SET Emp_Email = "[email protected]" WHERE Emp_id = 5;
UPDATE Company.Emp_info SET Emp_Email = "[email protected]" WHERE Emp_id = 6;
-- ******** UPDATE Multiple Data *******
UPDATE Company.Emp_info SET Emp_Profile = "Senior HR" , Emp_Salary = 55000 WHERE Emp_id = 5;
-- **** Modify Column position *****
ALTER TABLE Company.Emp_info MODIFY Emp_Email VARCHAR(30) AFTER Emp_Name;
-- ******* change Column name ******* Emp_id - id
ALTER TABLE Company.Emp_info CHANGE Emp_Id ID INT(3);
-- ********* Drop the column *******
ALTER TABLE Company.Emp_info DROP COLUMN Emp_Email;
-- ***** In & NOT IN *******
SELECT * FROM Company.Emp_info WHERE ID IN(4);
SELECT * FROM Company.Emp_info WHERE ID NOT IN(4);
SELECT * FROM Company.Emp_info WHERE ID IN(3,6);
SELECT * FROM Company.Emp_info WHERE ID NOT IN(3,6);
SELECT * FROM Company.Emp_info;
ALTER TABLE Company.Emp_info ADD CITY INT(3);
ALTER TABLE Company.Emp_info ADD foreign key(CITY) references Company.City(City_ID);
UPDATE Company.Emp_info SET City = 1 WHERE ID = 3;
UPDATE Company.Emp_info SET City = 2 WHERE ID = 4;
UPDATE Company.Emp_info SET City = 1 WHERE ID = 5;
UPDATE Company.Emp_info SET City = 3 WHERE ID = 6;
CREATE TABLE Company.City(
City_ID INT(3) NOT NULL AUTO_INCREMENT,
CIty_Name VARCHAR(20),
PRIMARY KEY(City_ID)
);
INSERT INTO Company.City(City_ID,City_Name)
VALUES(1,"AGRA"),(2,"Bhopal"),(3,"Dehli"),(4,"Noida");
SELECT * FROM Company.Emp_info;
SELECT * FROM Company.City;
-- ********* JOIN *****************
SELECT * FROM Company.Emp_info INNER JOIN Company.City ON Emp_info.City = City.City_ID;
-- Add new emp info ****
INSERT INTO Company.Emp_Info(ID,Emp_Name,Emp_Profile,Emp_Salary,CITY)
VALUES(7,"Tom","Software tester",65000,4);
INSERT INTO Company.Emp_Info(ID,Emp_Name,Emp_Profile,Emp_Salary,CITY)
VALUES(8,"John","HR",30000,6);
INSERT INTO Company.Emp_Info(ID,Emp_Name,Emp_Profile,Emp_Salary,CITY)
VALUES(9,"Lilly","Junior HR",20000,11);
-- ******** LEFT JOIN *******
SELECT * FROM Company.Emp_info LEFT JOIN Company.City ON Emp_info.City = City.City_ID;
INSERT INTO Company.City(City_ID,City_Name)
VALUES(6,"MUMBAI");
INSERT INTO Company.City(City_ID,City_Name)
VALUES(7,"Pune"),(8,"NASIK");
-- ******** RIGHT JOIN *******
SELECT * FROM Company.Emp_info RIGHT JOIN Company.City ON Emp_info.City = City.City_ID;
-- ******** OUTER JOIN *******
SELECT * FROM Company.Emp_info LEFT JOIN Company.City ON Emp_info.City = City.City_ID
UNION
SELECT * FROM Company.Emp_info RIGHT JOIN Company.City ON Emp_info.City = City.City_ID;
-- ******** VIEW **********
CREATE VIEW Company.Emp_City_Join as SELECT * FROM Company.Emp_info LEFT JOIN Company.City ON Emp_info.City = City.City_ID
UNION
SELECT * FROM Company.Emp_info RIGHT JOIN Company.City ON Emp_info.City = City.City_ID;
CREATE VIEW Company.Profile_data as SELECT Emp_Name, Emp_Profile FROM Company.Emp_info;
delimiter $$
CREATE procedure Company.GetEmp(x_id INT)
begin
SELECT * FROM Company.Emp_info WHERE ID = x_id;
end $$
call Company.GetEmp(9)