Repository navigation
Expand file tree
/
Copy path02_sql_command_categories.sql
More file actions
311 lines (199 loc) · 5.15 KB
/
Copy path02_sql_command_categories.sql
File metadata and controls
311 lines (199 loc) · 5.15 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
===============================================================================
SQL COMMAND CATEGORIES
===============================================================================
SQL commands are commonly divided into five major categories:
+------+-----------------------------+----------------------------------+
| | Full Form | Purpose |
+------+-----------------------------+----------------------------------+
| DDL | Data Definition Language | Define database objects |
| DML | Data Manipulation Language | Modify data |
| DQL | Data Query Language | Retrieve data |
| DCL | Data Control Language | Control permissions |
| TCL | Transaction Control Language| Manage transactions |
+------+-----------------------------+----------------------------------+
===============================================================================
1. DDL - DATA DEFINITION LANGUAGE
===============================================================================
Purpose:
Define and modify the structure of database objects.
Common DDL commands:
CREATE
ALTER
DROP
TRUNCATE
Examples:
CREATE TABLE
ALTER TABLE
DROP TABLE
TRUNCATE TABLE
CREATE
------
Creates a new database object.
Example:
CREATE TABLE Categories
(
category_id INT PRIMARY KEY,
category_name VARCHAR(50)
);
ALTER
-----
Modifies the structure of an existing object.
Example:
ALTER TABLE Products
ADD brand VARCHAR(50);
DROP
----
Deletes a database object and its structure.
Example:
DROP TABLE Categories;
TRUNCATE
--------
Removes all rows from a table while keeping the table structure.
Example:
TRUNCATE TABLE Categories;
===============================================================================
2. DML - DATA MANIPULATION LANGUAGE
===============================================================================
Purpose:
Insert, modify, and delete data stored in tables.
Common DML commands:
INSERT
UPDATE
DELETE
INSERT
------
Adds new rows to a table.
Example:
INSERT INTO Products
(product_id, product_name, category, price, stock_quantity, supplier)
VALUES
(211, 'Webcam', 'Electronics', 3500, 20, 'Logitech');
UPDATE
------
Modifies existing rows.
Example:
UPDATE Products
SET price = 3700
WHERE product_id = 211;
DELETE
------
Removes rows from a table.
Example:
DELETE FROM Products
WHERE product_id = 211;
===============================================================================
3. DQL - DATA QUERY LANGUAGE
===============================================================================
Purpose:
Retrieve data from database tables.
Main command:
SELECT
Example:
SELECT *
FROM Products;
Example:
SELECT
product_name,
price
FROM Products
WHERE price > 10000;
NOTE:
Some SQL references classify SELECT separately as DQL,
while others group it under DML. For learning purposes,
we will treat SELECT as DQL.
===============================================================================
4. DCL - DATA CONTROL LANGUAGE
===============================================================================
Purpose:
Control access and permissions on database objects.
Common commands:
GRANT
REVOKE
GRANT
-----
Gives permissions to a user or role.
Example:
GRANT SELECT
ON Products
TO SomeUser;
REVOKE
------
Removes previously granted permissions.
Example:
REVOKE SELECT
ON Products
FROM SomeUser;
NOTE:
These commands require appropriate SQL Server permissions.
Do not execute them unless you have created the required user/role.
===============================================================================
5. TCL - TRANSACTION CONTROL LANGUAGE
===============================================================================
Purpose:
Manage transactions.
Common commands:
BEGIN TRANSACTION
COMMIT
ROLLBACK
SAVE TRANSACTION
BEGIN TRANSACTION
-----------------
Starts a transaction.
COMMIT
------
Permanently saves the changes.
ROLLBACK
--------
Undoes changes made during the transaction.
Example:
BEGIN TRANSACTION;
UPDATE Products
SET price = price + 500
WHERE category = 'Electronics';
ROLLBACK;
After ROLLBACK, the price changes are undone.
Example using COMMIT:
BEGIN TRANSACTION;
UPDATE Products
SET price = price + 500
WHERE category = 'Electronics';
COMMIT;
After COMMIT, the changes are permanently saved.
===============================================================================
QUICK REVISION
===============================================================================
DDL
-> Defines database structure.
CREATE
ALTER
DROP
TRUNCATE
DML
-> Modifies data.
INSERT
UPDATE
DELETE
DQL
-> Retrieves data.
SELECT
DCL
-> Controls permissions.
GRANT
REVOKE
TCL
-> Manages transactions.
BEGIN TRANSACTION
COMMIT
ROLLBACK
SAVE TRANSACTION
===============================================================================
IMPORTANT
===============================================================================
DDL -> Structure
DML -> Data modification
DQL -> Data retrieval
DCL -> Permissions
TCL -> Transactions
===============================================================================
*/