-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassignment_2.sql
278 lines (244 loc) · 8.64 KB
/
assignment_2.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*ASSIGMENT 1 + VIEW PART OF ASSIGNMENT 2
*/
CREATE DATABASE group_2;
SHOW DATABASES;
/*
+--------------------+
| Database |
+--------------------+
| group_2 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
*/
USE group_2;
CREATE TABLE student_info (roll_no INT ,name VARCHAR(30),submission_status VARCHAR(30));
INSERT INTO student_info VALUES(26,'PRANJAL','NA');
INSERT INTO student_info VALUES(27,'PRASHANT','NA');
INSERT INTO student_info VALUES(28,'AADARSH','NA');
INSERT INTO student_info VALUES(29,'TEJAS','NA');
INSERT INTO student_info VALUES(30,'OM','NA');
INSERT INTO student_info VALUES(31,'HEMANT','NA');
INSERT INTO student_info VALUES(32,'DHANASHREE','NA');
INSERT INTO student_info VALUES(33,'UTKARSH','NA');
SELECT * FROM student_info;
/*
+---------+------------+-------------------+
| roll_no | name | submission_status |
+---------+------------+-------------------+
| 26 | PRANJAL | NA |
| 27 | PRASHANT | NA |
| 28 | AADARSH | NA |
| 29 | TEJAS | NA |
| 30 | OM | NA |
| 31 | HEMANT | NA |
| 32 | DHANASHREE | NA |
| 33 | UTKARSH | NA |
+---------+------------+-------------------+
*/
ALTER TABLE student_info ADD PRIMARY KEY(roll_no);
desc student_info;
/*
+-------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+-------+
| roll_no | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| submission_status | varchar(30) | YES | | NULL | |
+-------------------+-------------+------+-----+---------+-------+
*/
ALTER TABLE student_info ADD COLUMN teacher_name VARCHAR(30) DEFAULT 'DEEPALI';
/*
+---------+------------+-------------------+--------------+
| roll_no | name | submission_status | teacher_name |
+---------+------------+-------------------+--------------+
| 26 | PRANJAL | NA | DEEPALI |
| 27 | PRASHANT | NA | DEEPALI |
| 28 | AADARSH | NA | DEEPALI |
| 29 | TEJAS | NA | DEEPALI |
| 30 | OM | NA | DEEPALI |
| 31 | HEMANT | NA | DEEPALI |
| 32 | DHANASHREE | NA | DEEPALI |
| 33 | UTKARSH | NA | DEEPALI |
+---------+------------+-------------------+--------------+
*/
UPDATE student_info SET submission_status = 'COMPLETED' WHERE roll_no = 31;
/*
+---------+------------+-------------------+--------------+
| roll_no | name | submission_status | teacher_name |
+---------+------------+-------------------+--------------+
| 26 | PRANJAL | NA | DEEPALI |
| 27 | PRASHANT | NA | DEEPALI |
| 28 | AADARSH | NA | DEEPALI |
| 29 | TEJAS | NA | DEEPALI |
| 30 | OM | NA | DEEPALI |
| 31 | HEMANT | COMPLETED | DEEPALI |
| 32 | DHANASHREE | NA | DEEPALI |
| 33 | UTKARSH | NA | DEEPALI |
+---------+------------+-------------------+--------------+
*/
UPDATE student_info SET submission_status = 'COMPLETED' WHERE name = 'DHANASHREE';
/*
+---------+------------+-------------------+--------------+
| roll_no | name | submission_status | teacher_name |
+---------+------------+-------------------+--------------+
| 26 | PRANJAL | NA | DEEPALI |
| 27 | PRASHANT | NA | DEEPALI |
| 28 | AADARSH | NA | DEEPALI |
| 29 | TEJAS | NA | DEEPALI |
| 30 | OM | NA | DEEPALI |
| 31 | HEMANT | COMPLETED | DEEPALI |
| 32 | DHANASHREE | COMPLETED | DEEPALI |
| 33 | UTKARSH | NA | DEEPALI |
+---------+------------+-------------------+--------------+
*/
INSERT INTO student_info (roll_no,name,submission_status) VALUES (34,'XYZ','NA');
/*
+---------+------------+-------------------+--------------+
| roll_no | name | submission_status | teacher_name |
+---------+------------+-------------------+--------------+
| 26 | PRANJAL | NA | DEEPALI |
| 27 | PRASHANT | NA | DEEPALI |
| 28 | AADARSH | NA | DEEPALI |
| 29 | TEJAS | NA | DEEPALI |
| 30 | OM | NA | DEEPALI |
| 31 | HEMANT | COMPLETED | DEEPALI |
| 32 | DHANASHREE | COMPLETED | DEEPALI |
| 33 | UTKARSH | NA | DEEPALI |
| 34 | XYZ | NA | DEEPALI |
+---------+------------+-------------------+--------------+
*/
DELETE FROM student_info WHERE name = 'XYZ';
/*
+---------+------------+-------------------+--------------+
| roll_no | name | submission_status | teacher_name |
+---------+------------+-------------------+--------------+
| 26 | PRANJAL | NA | DEEPALI |
| 27 | PRASHANT | NA | DEEPALI |
| 28 | AADARSH | NA | DEEPALI |
| 29 | TEJAS | NA | DEEPALI |
| 30 | OM | NA | DEEPALI |
| 31 | HEMANT | COMPLETED | DEEPALI |
| 32 | DHANASHREE | COMPLETED | DEEPALI |
| 33 | UTKARSH | NA | DEEPALI |
+---------+------------+-------------------+--------------+
*/
CREATE VIEW a_view AS SELECT roll_no , submission_status FROM student_info;
SELECT * FROM a_view;
/*
+---------+-------------------+
| roll_no | submission_status |
+---------+-------------------+
| 26 | NA |
| 27 | NA |
| 28 | NA |
| 29 | NA |
| 30 | NA |
| 31 | COMPLETED |
| 32 | COMPLETED |
| 33 | NA |
+---------+-------------------+
*/
ALTER VIEW a_view AS SELECT name FROM student_info;
SELECT * FROM a_view;
/*
+------------+
| name |
+------------+
| PRANJAL |
| PRASHANT |
| AADARSH |
| TEJAS |
| OM |
| HEMANT |
| DHANASHREE |
| UTKARSH |
+------------+
*/
UPDATE a_view SET name = 'OM GAWANDE' WHERE name = 'OM';
/*
+------------+
| name |
+------------+
| PRANJAL |
| PRASHANT |
| AADARSH |
| TEJAS |
| OM GAWANDE |
| HEMANT |
| DHANASHREE |
| UTKARSH |
+------------+
*/
DROP VIEW a_view;
CREATE TABLE set_1 (emp_id INT,name VARCHAR(30),city VARCHAR(30));
CREATE TABLE set_2 (emp_id INT,name VARCHAR(30),country VARCHAR(30));
INSERT INTO set_1 VALUES (001,'ASHUTOSH','PUNE');
INSERT INTO set_1 VALUES (002,'ARYAN','MUMBAI');
INSERT INTO set_1 VALUES (003,'SARTHAK','SURAT');
INSERT INTO set_2 VALUES (001,'ASHUTOSH','INDIA');
INSERT INTO set_2 VALUES (002,'ARYAN','UK');
INSERT INTO set_2 VALUES (003,'SARTHAK','USA');
INSERT INTO set_2 VALUES (004,'YASH','AUSTRALIA');
SELECT * FROM set_1;
/*
+--------+----------+--------+
| emp_id | name | city |
+--------+----------+--------+
| 1 | ASHUTOSH | PUNE |
| 2 | ARYAN | MUMBAI |
| 3 | SARTHAK | SURAT |
+--------+----------+--------+
*/
SELECT * FROM set_2;
/*
+--------+----------+-----------+
| emp_id | name | country |
+--------+----------+-----------+
| 1 | ASHUTOSH | INDIA |
| 2 | ARYAN | UK |
| 3 | SARTHAK | USA |
| 4 | YASH | AUSTRALIA |
+--------+----------+-----------+
*/
SELECT * FROM set_1 UNION SELECT * FROM set_2;
/*
+--------+----------+-----------+
| emp_id | name | city |
+--------+----------+-----------+
| 1 | ASHUTOSH | PUNE |
| 2 | ARYAN | MUMBAI |
| 3 | SARTHAK | SURAT |
| 1 | ASHUTOSH | INDIA |
| 2 | ARYAN | UK |
| 3 | SARTHAK | USA |
| 4 | YASH | AUSTRALIA |
+--------+----------+-----------+
*/
SELECT name FROM set_1 UNION SELECT name from set_2;
/*
+----------+
| name |
+----------+
| ASHUTOSH |
| ARYAN |
| SARTHAK |
| YASH |
+----------+
*/
SELECT name FROM set_1 UNION ALL SELECT name from set_2;
/*
+----------+
| name |
+----------+
| ASHUTOSH |
| ARYAN |
| SARTHAK |
| ASHUTOSH |
| ARYAN |
| SARTHAK |
| YASH |
+----------+
/*