Unit_3 Introduction to SQL (Structured Query Language)
Unit_3 Introduction to SQL (Structured Query Language)
INDEX
Unit-III Introduction to SQL: [Weightage=25% approx., Lectures=8, Practical’s= 16]:
Overview, Characteristics of SQL, Advantage of SQL, SQL data types and literals. Basics of
SQL Types of SQL Statements: DDL, DML, DCL, TCL.
Basic SQL Queries: SQL operators, Creating Database, Creating, Modifying and Deleting
Tables, Queries: Insert, Select, Update, Delete, Where Clause, Order By.
X ---------------- X
Page 1 of 33
❖ Overview:
➢ SQL stands for Structured Query Language.
➢ It is used for storing and managing data in relational database
management system (RDMS).
➢ It is a standard language for Relational Database System.
➢ It enables a user to create, read, update, and delete relational databases
and tables.
➢ All the RDBMS like MySQL, Oracle, and SQL Server use SQL as their
standard database language.
➢ SQL allows users to query the database in a number of ways, using
English-like statements.
▪ SQL Process:
Page 2 of 33
❖ Characteristics of SQL:
Page 3 of 33
▪ DBMS can be classified into two types:
▪ Relational Database Management System (RDBMS) and
▪ Non-Relational Database Management System. (NoSQL or Non-
SQL)
➢ RDBMS: Data is organized in the form of tables and each table has a
set of rows and columns. The data are related to each other through
primary and foreign keys.
➢ NoSQL: Data is organized in the form of key-value pairs, documents,
graphs, or column-based. These are designed to handle large-scale,
high-performance scenarios.
❖ Advantage of SQL:
Page 5 of 33
❖ Disadvantage of SQL:
➢ Complex Interface:
▪ SQL has a difficult interface that makes few users uncomfortable
while dealing with the database.
➢ Cost:
▪ Some versions are costly and hence, programmers cannot access it.
➢ Partial Control:
▪ Due to hidden business rules, complete control is not given to the
database.
➢ Limited Flexibility:
▪ SQL databases are less flexible than NoSQL databases when it
comes to handling unstructured or semi-structured data, as they
require data to be structured into tables and columns.
➢ Lack of Real-Time Analytics:
▪ SQL databases are designed for batch processing and do not
support real-time analytics, which can be a disadvantage for
applications that require real-time data processing.
➢ Limited Query Performance:
▪ SQL databases may have limited query performance when
dealing with large datasets, as queries may take longer to process
than in-memory databases.
➢ Complexity:
▪ SQL databases can be complex to set up and manage, requiring
skilled database administrators to ensure optimal performance
and maintain data integrity.
Page 6 of 33
❖ SQL Data Types and Literals:
Page 7 of 33
➢ MySQL String Data Types and Literals.
Page 8 of 33
➢ MySQL Numeric Data Types and Literals.
Page 9 of 33
➢ MySQL Date and Time Data Types and Literals.
Page 10 of 33
❖ Basics of SQL Types of SQL Statements: DDL, DML,
DCL, TCL.
➢ Database Languages:
▪ Data Definition Language (DDL).
▪ Data Manipulation Language (DML).
▪ Data Control Language (DCL).
▪ Transactional Control Language (TCL).
Page 11 of 33
➢ Before learning DDL Commands you need to know basic
Database Commands.
➢ These steps must be followed before writing the database query in
the console screen (Shell) of "XAMPP!" Software.
Page 12 of 33
➢ Data Definition Language (DDL) Commands Syntax:
➢ Command-1:
CREATE :
This command is used to create a new table in SQL. The user has to give information like table
name, column names, and their datatypes.
Syntax –
CREATE TABLE table_name
(
column_1 datatype,
column_2 datatype,
column_3 datatype,
....
);
Example –
We need to create a table for storing Student information of a particular Division. Create syntax
would be as below.
Output:
Query OK, 0 rows affected (1.828 sec)
Page 13 of 33
➢ Command-2:
ALTER :
This command is used to add, delete or change columns in the existing table. The user needs to
know the existing table name and can do add, delete or modify tasks easily.
Syntax –
Syntax to add a column to an existing table.
Example –
In our Section_I table, we want to add a new column for CGPA. The syntax would be as below
as follows.
Output:
Query OK, 0 rows affected (0.568 sec)
Records: 0 Duplicates: 0 Warnings: 0
Page 14 of 33
➢ Command-3:
TRUNCATE :
This command is used to remove all rows from the table, but the structure of the table still exists.
Syntax –
Syntax to remove an existing table.
Example –
The College Authority wants to remove the details of all students for new batches but wants to
keep the table structure. The command they can use is as follows.
➢ Command-4:
RENAME:
It is possible to change name of table with or without data in it using simple RENAME
command.
We can rename any table object at any point of time.
Syntax –
RENAME TABLE <Table Name> To <New_Table_Name>;
Example:
If you want to change the name of the table from Section_I to Division_I we can use rename
command as..
Page 15 of 33
➢ Command-5:
DROP:
This command is used to remove an existing table along with its structure from the Database.
Syntax –
Syntax to drop an existing table.
Page 16 of 33
➢ Data Manipulation Language (DML):
▪ DML is the short name for Data Manipulation Language which
deals with data manipulation and includes most common SQL
statements such SELECT, INSERT, UPDATE, DELETE, etc.,
and it is used to store, modify, retrieve, delete, and update data in
a database.
Page 17 of 33
➢ Data Manipulation Language (DML) Commands Syntax:
➢ Command-1:
SELECT DML Command:
▪ SELECT is the most important data manipulation command in Structured Query
Language.
▪ The SELECT command shows the records of the specified table. It also shows the
particular record of a particular column by using the WHERE clause.
Syntax –
SELECT column_Name_1, column_Name_2, ….., column_Name_N FROM
Name_of_table;
Page 18 of 33
➢ Command-2:
INSERT DML Command:
▪ INSERT is another most important data manipulation command in
Structured Query Language, which allows users to insert data in database
tables.
Syntax –
INSERT INTO TABLE_NAME
( column_Name1, column_Name2, column_Name3, .... column_NameN ) VALUES
(value_1, value_2, value_3, .... value_N) ;
Example –
This example describes how to insert the record in the database table.
Let's take the following Section_I table, which consists of only 3 records of the student.
Page 19 of 33
➢ Command-3:
UPDATE DML Command:
▪ UPDATE is another most important data manipulation command in
Structured Query Language, which allows users to update or modify the
existing data in database tables.
▪ Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and
'Table_name' is the name of the table whose values you want to update.
Syntax –
UPDATE Table_name SET [column_name1= value_1, ….., column_nameN = value_N]
WHERE CONDITION;
Example –
Suppose you want to update the CGPA of the student whose Enrollment_NO is
23BCA04291. To do this, you have to write the following DML UPDATE command:
Example –2
This example describes how to update the value of multiple fields of the database table.
Suppose you want to update the CGPA of the student whose Enrollment_NO is
23BCA04291. To do this, you have to write the following DML UPDATE command:
Page 20 of 33
➢ Command-4:
DELETE DML Command:
▪ DELETE is a DML command which allows SQL users to remove single or
multiple existing records from the database tables.
▪ This command of Data Manipulation Language does not delete the stored data
permanently from the database.
▪ We use the WHERE clause with the DELETE command to select specific rows
from the table.
Syntax –
DELETE FROM Table_Name WHERE condition;
Example –
This example describes how to delete a single record from the table.
Suppose you want to delete that student from the Section_I table
whose Enrollment_NO is 23BCA04317.
To do this, you have to write the following DML DELETE command:
Example – 2
Suppose you want to delete the record of those students whose CGPA is less than 6.00.
To do this, you have to write the following DML Update command:
Page 21 of 33
➢ Data Control Language (DCL):
▪ DCL is short for Data Control Language which acts as an access
specifier to the database (basically to grant and revoke
permissions to users in the database).
Page 22 of 33
➢ Data Control Language (DCL) Commands Syntax:
Command-1:
▪ GRANT, as the name itself suggests, provides. This command
allows the administrator to provide particular privileges or
permissions over a database object, such as a table, view, or
procedure.
▪ It can provide user access to perform certain database or
component operations.
▪ In simple language, the GRANT command allows the user to
implement other SQL commands on the database or its objects.
▪ The primary function of the GRANT command in SQL is to
provide administrators the ability to ensure the security and
integrity of the data is maintained in the database.
▪ To have a better understanding of implementing the GRANT
statement in the database. Let us use an example.
Syntax –
GRANT SELECT ON your_database_name.your_table_name TO 'your_username'@'your_host';
Example – Implementing GRANT Statement Consider a scenario where you are the
database administrator, and a Section_I table is in the database.
Suppose you want a specific user VRAJ to only SELECT (read)/ retrieve the
data from the Section_I table. Then you can use GRANT in the below GRANT
statement.
Page 23 of 33
Command-2:
REVOKE Command:
▪ As the name suggests, revoke is to take away.
▪ The REVOKE command enables the database administrator to remove the
previously provided privileges or permissions from a user over a database or
database object, such as a table, view, or procedure.
▪ The REVOKE commands prevent the user from accessing or performing a specific
operation on an element in the database.
▪ In simple language, the REVOKE command terminates the ability of the user to
perform the mentioned SQL command in the REVOKE query on the database or
its component.
▪ The primary reason for implementing the REVOKE query in the database is to
ensure the data's security and integrity.
▪ Let us use an example to better understand how to implement the REVOKE
command in SQL.
Syntax –
REVOKE SELECT ON your_database_name.table_name FROM 'specific_user'@'localhost';
Example –
Consider a scenario where the user is the database administrator. In the above
implementation of the GRANT command, the user 'PATEL VRAJ BHARATBHAI' was provided
permission to implement a SELECT query on the Section_I table that allowed VRAJ to read or
retrieve the data from the table. Due to certain circumstances, the administrator wants to revoke
the abovementioned permission. To do so, the administrator can implement the below REVOKE
statement:
MariaDB [BPCCS]> REVOKE SELECT ON BPCCS.Section_I FROM 'PATEL VRAJ
BHARATBHAI'@'localhost';
Query OK, 0 rows affected (0.124 sec)
Page 24 of 33
➢ Transaction Control language (TCL):
▪ A single unit of work in a database is formed after the consecutive
execution of commands is known as a transaction.
▪ There are certain commands present in SQL known as TCL
commands that help the user manage the transactions that take place
in a database.
▪ ROLLBACK,COMMIT and SAVEPOINT are the most commonly
used TCL commands in SQL.
▪ Now let us take a deeper dive into the TCL commands of SQL with
the help of examples. All the queries in the examples will be written
using the MySQL database.
Save Point: It is used to save the data on the temporary basis in the database.
Roll Back: Used to cancel or Undo changes made in the database.
Commit: It is used to apply or save changes in the database.
Page 25 of 33
➢ Transaction Control Language (TCL) Commands Syntax:
Command-1:
Save Point Command:
▪ We can divide the database operations into parts.
For example,
▪ we can consider all the insert related queries that we will execute
consecutively as one part of the transaction and the delete command
as the other part of the transaction.
▪ Using the SAVEPOINT command in SQL, we can save these
different parts of the same transaction using different names.
▪ For example, we can save all the insert related queries with the
savepoint named INS.
▪ To save all the insert related queries in one savepoint, we have to
execute the SAVEPOINT query followed by the savepoint name after
finishing the insert command execution.
Syntax –
START TRANSACTION;
SAVEPOINT savepoint_name;
Example –
MariaDB [BPCCS]> START TRANSACTION;
Query OK, 0 rows affected (0.000 sec)
MariaDB [BPCCS]> SAVEPOINT INS;
Query OK, 0 rows affected (0.046 sec)
Page 26 of 33
Command-2:
Roll Back Command:
▪ While carrying a transaction, we must create savepoints to save
different parts of the transaction.
▪ According to the user's changing requirements, he/she can roll back
the transaction to different savepoints.
▪ Consider a scenario: We have initiated a transaction followed by the
table creation and record insertion into the table.
▪ After inserting records, we have created a savepoint INS. Then we
executed a delete query, but later we thought that mistakenly we had
removed the useful record.
▪ Therefore in such situations, we have an option of rolling back our
transaction.
▪ In this case, we have to roll back our transaction using the
ROLLBACK command to the savepoint INS, which we have
created before executing the DELETE query.
Syntax –
ROLLBACK TO SAVEPOINT savepoint_name;
Example –
MariaDB [BPCCS]> ROLLBACK TO SAVEPOINT INS;
Query OK, 0 rows affected (0.000 sec)
Page 27 of 33
Command-3:
Commit Command:
▪ COMMIT command in SQL is used to save all the transaction-related
changes permanently to the disk.
▪ Whenever DDL commands such as INSERT, UPDATE and DELETE
are used, the changes made by these commands are permanent only
after closing the current session.
▪ So before closing the session, one can easily roll back the changes
made by the DDL commands.
▪ Hence, if we want the changes to be saved permanently to the disk
without closing the session, we will use the commit command.
Syntax –
COMMIT;
Example –
MariaDB [BPCCS]> COMMIT;
Query OK, 0 rows affected (0.086 sec)
Page 28 of 33
❖ Basic SQL Queries: SQL operators.
Page 29 of 33
➢ Arithmetic Operators:
➢ Bitwise Operators:
Page 30 of 33
➢ Comparison Operators:
➢ Logical Operators:
Page 31 of 33
➢ Compound Operators:
Page 32 of 33
❖ Basic SQL Queries: Creating Database, Creating, Modifying and
Deleting Tables, Queries: Insert, Select, Update, Delete, Where Clause.
[The answer to this question is shown in page 12 To 28]
➢ ORDER BY:
▪ The ORDER BY keyword is used to sort the result-set in
ascending or descending order.
Syntax –
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example –
MariaDB [BPCCS]> SELECT * FROM Section_I ORDER BY Name_of_the_student;
2 rows in set (0.249 sec)
Example – 2
MariaDB [BPCCS]> SELECT * FROM Section_I ORDER BY Name_of_the_student DESC;
2 rows in set (0.249 sec)
▪ DESC:
▪ The ORDER BY keyword sorts the records in ascending order
by default. To sort the records in descending order, use the DESC
keyword.
Using Both ASC and DESC:
The following SQL statement selects all students from the "Section_I"
table, sorted ascending by the "Enrollment_NO" and descending by the
"Name_of_the_student" column:
Example –
SELECT * FROM Section_I
ORDER BY Enrollment_NO ASC, Name_of_the_student DESC;
X ---------------- X
Page 33 of 33