-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Commands.sql
More file actions
176 lines (120 loc) · 7.98 KB
/
SQL_Commands.sql
File metadata and controls
176 lines (120 loc) · 7.98 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
-- SQL Commands
/*
SQL commands are categorized into different types based on their functionality.
Below are the key categories of SQL commands, along with examples for each:
*/
-- 1. Data Query Language (DQL): DQL is used to query data from the database.
-- • SELECT: Retrieves data from one or more tables.
-- select all columns from a table
SELECT * FROM Employee
--- select specific columns from a table
SELECT Emp_ID, Emp_Name FROM Employee
-- ♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦
-- 2. Data Definition language (DDL): DDL is used to define and manage structure of a database, such as creating, altering and deleting.
/*
1. CREATE
2. ALTER
3. DROP
4. TRUNCATE
*/
-- • CREATE: Create a new table, view, stored procedure and other objects.
-- create a new table Employee
CREATE TABLE Employee (
Emp_ID INT,
Emp_Name VARCHAR(100),
Emp_Age INT UNIQUE,
Emp_Email VARCHAR(100),
Emp_Joining_Date DATE,
Emp_Salary DECIMAL(10, 2)
)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- • ALTER: Modifies an existing database object (like table, view, stored procedure).
-- alter a table Employee by adding new column Department
ALTER TABLE Employee ADD Department VARCHAR(100)
-- alter a column constraint in a table
ALTER TABLE Employee ALTER COLUMN Emp_ID INT NOT NULL
ALTER TABLE Employee ADD CONSTRAINT PK_Emp_ID PRIMARY KEY (Emp_ID)
-- NOTE: you cannot directly alter an existing column to add a PRIMARY KEY constraint if that column is nullable. First, modify the column to make it non-nullable. Then, add the PRIMARY KEY constraint.
-- alter column by droping constraint
ALTER TABLE Employee DROP CONSTRAINT UNIQUE
-- NOTE: As we move ahead we will see all types of constraints (PRIMARY KEY, UNIQUE and more)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- • DROP: Deletes a table, view, stored procedure and other objects.
-- drop a table
DROP TABLE Employee
-- NOTE: Dropping table will delete its a structure as well
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- • TRUNCATE: Removes all rows from a table, but the table structure remain the same.
-- remove all rows from the table
TRUNCATE TABLE Employee
-- ♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦
-- 3. Data Manipulation Language (DML): DML is used for manipulating the data stored in the database.
/*
1. INSERT
2. UPDATE
3. DELETE
*/
-- • INSERT: Add new rows of data into a table.
-- insert single row of data
INSERT INTO Employee VALUES (1, 'John Doe', 30, 'john.doe@gmail.com', '2020-01-15', 50000.00)
INSERT INTO Employee (Emp_ID, Emp_Name, Emp_Age, Emp_Email, Emp_Joining_Date, Emp_Salary)
VALUES (2, 'Jane Smith', 28, 'jane.smith@gmail.com', '2019-03-22', 55000.00)
-- insert single row data into a specific columns
INSERT INTO Employee (Emp_ID, Emp_Name, Emp_Email, Emp_Joining_Date)
VALUES (3, 'Emily Johnson', 'emily.johnson@gmail.com', '2017-09-15')
-- insert multiple row of data
INSERT INTO Employee (Emp_ID, Emp_Name, Emp_Age, Emp_Email, Emp_Joining_Date, Emp_Salary)
VALUES
(4, 'Robert Brown', 35, 'robert.brown@example.com', '2018-07-01', 60000.00),
(5, 'David Williams', 45, 'david.williams@example.com', '2016-11-01', 70000.00)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- • UPDATE: Modifies existing data in a table.
-- update a single record
UPDATE Employee
SET Emp_Age = 40
WHERE Empl_ID = 3
-- update multiple records
UPDATE Employee
SET Emp_Salary = 100000
WHERE Emp_Age = 50
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- • DELETE: Removes one or more rows from a table.
-- delete a specific record
DELETE FROM Employee
WHERE Emp_ID = 10
-- delete multiple records
DELETE FROM Employee
WHERE Emp_Salary > 100000
-- delete all records from a table
-- ♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦
-- 4. Data Control Language (DCL): DCL is used to control access to data in the database.
/*
1. GRANT
2. REVOKE
*/
-- • GRANT: Gives specific privilege to a user or role.
-- grant SELECT privilege to a user
GRANT SELECT ON Employee TO user1
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- • REVOKE: Removes specific privilage from a user or role.
-- revoke SELECT privilege from a user
REVOKE SELECT ON Employee FROM user1
-- ♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦
-- 5. Transaction Control Language (TCL): TCL is used to manage transactions in the database.
/*
1. COMMIT
2. ROLLBACK
3. SAVEPOINT
*/
-- • COMMIT: Save all changes made during the current transaction.
-- commit the trasaction (save changes)
COMMIT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- • ROLLBACK: Undoes chnages made during the current transaction.
-- rollback the transaction (undo changes)
ROLLBACK
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- • SAVEPOINT: Save a point within a transaction to which you can later roll back.
-- set a savepoint with a transaction
SAVEPOINT savepoint1
-- ♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦