You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SQL allows us to interact with the databases and bring out/manipulate data within them. Using SQL, we can create our own databases and then add data into these databases in the form of tables.
2
+
3
+
The following functionalities can be performed on a database using SQL:
4
+
• Create or Delete a Database.
5
+
• Create or Alter or Delete some tables in a Database.
6
+
• SELECT data from tables.
7
+
• INSERT data into tables.
8
+
• UPDATE data in tables.
9
+
• DELETE data from tables.
10
+
• Create Views in the database.
11
+
• Execute various aggregate functions.
12
+
13
+
14
+
SQL-Create Table:
15
+
We use the CREATE command to create a table. The table can be created with the following code:
16
+
CREATE TABLE student(
17
+
ID INT NOT NULL,
18
+
Name varchar(25),
19
+
Phone varchar(12),
20
+
Class INT
21
+
);
22
+
23
+
SQL-Delete Table:
24
+
To delete a table from a database, we use the DROP command.
25
+
DROP TABLE student;
26
+
27
+
SQL Commands
28
+
SQL Commands are instructions that are used by the user to communicate with the database, to perform specific tasks, functions and queries of data.
29
+
30
+
31
+
Types of SQL Commands:
32
+
33
+
The above image broadly shows the different types of SQL commands available in SQL in the form of a chart.
34
+
35
+
1. Data Definition Language(DDL): It changes a table’s structure by adding, deleting and altering its contents. Its changes are auto-committed(all changes are automatically permanently saved in the database). Some commands that are a part of DDL are:
36
+
37
+
• CREATE: Used to create a new table in the database.
38
+
39
+
Example:
40
+
CREATE TABLE STUDENT(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);
41
+
42
+
• ALTER: Used to alter contents of a table by adding some new column or attribute, or changing some existing attribute.
43
+
Example:
44
+
ALTER TABLE STUDENT ADD(ADDRESS VARCHAR2(20));
45
+
ALTER TABLE STUDENT MODIFY (ADDRESS VARCHAR2(20));
46
+
47
+
• DROP: Used to delete the structure and record stored in the table.
48
+
Example:
49
+
DROP TABLE STUDENT;
50
+
51
+
• TRUNCATE: Used to delete all the rows from the table, and free up the space in the table.
52
+
Example:
53
+
TRUNCATE TABLE STUDENT;
54
+
55
+
56
+
2. Data Manipulation Language(DML): It is used for modifying a database, and is responsible for any form of change in a database. These commands are not auto-committed, i.e all changes are not automatically saved in the database. Some commands that are a part of DML are:
57
+
58
+
• INSERT: Used to insert data in the row of a table.
59
+
Example:
60
+
INSERT INTO STUDENT (Name, Subject) VALUES ("Shreya", "DSA");
61
+
In the above example, we insert the values “Shreya” and “DSA” in the columns Name and Subject in the STUDENT table.
62
+
63
+
• UPDATE: Used to update value of a table’s column.
64
+
Example:
65
+
UPDATE STUDENT
66
+
SET User_Name = 'Devansh'
67
+
WHERE Student_Id = '2'
68
+
In the above example, we update the name of the student, whose Student_ID is 2, to the User_Name = “Devansh”.
69
+
70
+
• DELETE: Used to delete one or more rows in a table.
71
+
Example:
72
+
DELETE FROM STUDENT
73
+
WHERE Name = "Sneha";
74
+
In the above example, the query deletes the row where the Name of the student is “Sneha” from the STUDENT table.
75
+
76
+
3. Data Control Language(DCL): These commands are used to grant and take back access/authority (revoke) from any database user. Some commands that are a part of DCL are:
77
+
78
+
• Grant: Used to grant a user access privileges to a database.
79
+
Example:
80
+
GRANT SELECT, UPDATE ON TABLE_1 TO USER_1, USER_2;
81
+
In the above example, we grant the rights to SELECT and UPDATE data from the table TABLE_1 to users - USER_1 and USER_2.
82
+
83
+
• Revoke: Used to revoke the permissions from an user.
84
+
Example:
85
+
REVOKE SELECT, UPDATE ON TABLE_1 FROM USER_1, USER_2;
86
+
In the above example we revoke the rights to SELECT and UPDATE data from the table TABLE_1 from the users- USER_1 and USER_2.
87
+
88
+
4. Transaction Control Language: These commands can be used only with DML commands in conjunction and belong to the category of auto-committed commands. Some commands that are a part of TCL are:
89
+
90
+
• COMMIT: Saves all the transactions made on a database.
91
+
Example:
92
+
DELETE FROM STUDENTS
93
+
WHERE AGE = 16;
94
+
COMMIT;
95
+
In the above database, we delete the row where AGE of the students is 16, and then save this change to the database using COMMIT.
96
+
97
+
• ROLLBACK: It is used to undo transactions which are not yet been saved.
98
+
Example:
99
+
DELETE FROM STUDENTS
100
+
WHERE AGE = 16;
101
+
ROLLBACK;
102
+
By using ROLLBACK in the above example, we can undo the deletion we performed in the previous line of code, because the changes are not committed yet.
103
+
104
+
• SAVEPOINT: Used to roll transaction back to a certain point without having to roll back the entirity of the transaction.
105
+
Example:
106
+
SAVEPOINT SAVED;
107
+
DELETE FROM STUDENTS
108
+
WHERE AGE = 16;
109
+
ROLLBACK TO SAVED;
110
+
In the above example, we have created a savepoint just before performing the delete operation in the table, and then we can return to that savepoint using the ROLLBACK TO command.
111
+
112
+
5. Data Query Language: It is used to fetch some data from a database. The command belonging to this category is:
113
+
114
+
• SELECT: It is used to retrieve selected data based on some conditions which are described using the WHERE clause. It is to be noted that the WHERE clause is also optional to be used here and can be used depending on the user’s needs.
115
+
Example:
116
+
SELECT Name
117
+
FROM Student
118
+
WHERE age >= 18;
119
+
120
+
121
+
Keys in SQL
122
+
A database consists of multiple tables and these tables and their contents are related to each other by some relations/conditions. To identify each row of these tables uniquely, we make use of SQL keys. A SQL key can be a single column or a group of columns used to uniquely identify the rows of a table. SQL keys are a means to ensure that no row will have duplicate values. They are also a means to establish relations between multiple tables in a database.
123
+
124
+
Types of Keys:
125
+
1. Primary Key: They uniquely identify a row in a table.
126
+
Properties:
127
+
• Only a single primary key for a table. (A special case is a composite key, which can be formed by the composition of 2 or more columns, and act as a single candidate key.)
128
+
• The primary key column cannot have any NULL values.
129
+
• The primary key must be unique for each row.
130
+
131
+
Example:
132
+
CREATE TABLE Student (
133
+
ID int NOT NULL,
134
+
LastName varchar(255) NOT NULL,
135
+
FirstName varchar(255),
136
+
Class int,
137
+
PRIMARY KEY (ID)
138
+
);
139
+
140
+
2. Foreign Key: Foreign keys are keys that reference the primary keys of some other table. They establish a relationship between 2 tables and link them up.
141
+
Example: In the below example, a table called Orders is created with some given attributes and its Primary Key is declared to be OrderID and Foreign Key is declared to be PersonId referenced from the Person's table. A person's table is assumed to be created beforehand.
4. LEFT JOIN: Returns all of the records from the first table, along with any matching records from the second table.
187
+
188
+
We will apply Left Join on the above tables, as follows,
189
+
SELECT Customers.CustomerName, Orders.OrderID
190
+
FROM Customers
191
+
LEFT JOIN Orders
192
+
ON Customers.CustomerID=Orders.CustomerID
193
+
ORDER BY Customers.CustomerName;
194
+
195
+
196
+
5. FULL JOIN: Returns all records from both tables when there is a match.
197
+
198
+
Applying Outer Join on the above 2 tables, using the code:
199
+
SELECT ID, NAME, AMOUNT, DATE
200
+
FROM CUSTOMERS
201
+
FULL JOIN ORDERS
202
+
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
203
+
204
+
205
+
Triggers in SQL
206
+
SQL codes automatically executed in response to a certain event occurring in a table of a database are called triggers. There cannot be more than 1 trigger with a similar action time and event for one table.
207
+
208
+
Syntax:
209
+
Create Trigger Trigger_Name
210
+
(Before | After) [ Insert | Update | Delete]
211
+
on [Table_Name]
212
+
[ for each row | for each column ]
213
+
[ trigger_body ]
214
+
215
+
Example:
216
+
CREATE TRIGGER trigger1
217
+
before INSERT
218
+
ON Student
219
+
FOR EACH ROW
220
+
SET new.total = (new.marks/ 10) * 100;
221
+
222
+
Here, we create a new Trigger called trigger1, just before we perform an INSERT operation on the Student table, we calculate the percentage of the marks for each row.
223
+
Some common operations that can be performed on triggers are:
224
+
225
+
• DROP: This operation will drop an already existing trigger from the table.
226
+
o Syntax:
227
+
DROP TRIGGER trigger name;
228
+
229
+
• SHOW: This will display all the triggers that are currently present in the table.
230
+
o Syntax:
231
+
SHOW TRIGGERS IN database_name;
232
+
233
+
Aggregate functions:
234
+
1) count:
235
+
syntax select count(*) from table_name where some_condition;
236
+
237
+
238
+
View in SQL
239
+
View is a database object created from a pre-existing table/s . View is basically a Virtual Table created by a query by joining one or more tables .
240
+
241
+
Syntax:
242
+
CREATE [OR REPLACE] VIEW [VIEW_Name] AS
243
+
SELECT [Table_columns]
244
+
FROM [Table_Name]
245
+
[WHERE conditions];
246
+
247
+
Example:
248
+
CREATE VIEW trainer AS
249
+
SELECT course_name, trainer
250
+
FROM courses;
251
+
252
+
Here a View is created to see course_name and trainer from table courses .
253
+
254
+
SQL Cursor
255
+
256
+
When an MYSQL Statement is processed, a memory area is created known as context area.A cursor is
257
+
a pointer to that context area
258
+
259
+
There are two types of cursor
260
+
261
+
1. Implict cursor
262
+
2. Explict cursor
263
+
264
+
Explict Cursor
265
+
266
+
Explict cursors are used when you are exceuting a SELECT statement query that will return more than one row.
0 commit comments