100% found this document useful (1 vote)
13 views

SQL (Structured Query Language)

SQL

Uploaded by

Srinibash Patra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
13 views

SQL (Structured Query Language)

SQL

Uploaded by

Srinibash Patra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL – Structured Query Language

It is a computer language for storing, manipulating and retrieving data stored in a relational
database. SQL is the standard language for all Relational Database Systems like MySql, Oracle,
Sql Server etc.
SQL was first introduced at IBM by Donald D. Chamberlin and Raymond F. Boyce in early 1970s.
This version, initially called as SEQUEL (Structured English Query Language). In the late of 1970s,
Oracle Corporation made R&D on SEQUEL and released first commercial version of SQL.
SQL includes 4 intermediate languages such as –
DDL (Data Definition Language), DML (Data Manipulation Language), TCL (Transaction Control
Language) and DCL (Data Control Language).
Under each category there are certain pre-defined commands. By using these commands user
can create database and manipulate data on the database.

DDL DML TCL DCL DQL


i. Create i. Insert i. Start Transaction i. Grand i.Select
ii. Alter ii. Update ii. Commit ii. Revoke
iii. Rename iii. Delete iii. Rollback
iv. Truncate iv. Use iv. Savepoint
v. Drop v. Show
vi. Select

NB

• SQL commands are not case-sensitive


• Select command is not considered as a complete DML command (it cannot manipulate data
in the database) rather it is a DQL (Data Query Language) command. Because, select is used
for retrieving (querying) data from the database.

DDL – Data Definition Language


DDL is used to create and modify the structure of database objects in a database. These database
objects include tables, views, schemas, indexes, etc.
It is also known as data description language in some contexts, as it describes the fields and
records in a database table.
DML – Data Manipulation Language
DML includes commands permitting users to manipulate data in a database. This manipulation
involves inserting data into database tables, modifying existing data, retrieving existing data and
deleting data from existing tables.
TCL – Transaction Control Language
TCL includes commands which are used for managing/controlling of transactions made by the
DML commands in a database and for maintaining consistency of the database. TCL commands
are only used with DML commands such as INSERT, UPDATE and DELETE. These commands
cannot be used with DDL commands – CREATE, ALTER, TRUNCATE etc.

3
DCL – Data Control Language
DCL includes commands which mainly deal with the permissions, rights and other controls of
the database system.
DQL – Data Query Language
DQL is used to fetch the data from the database. It uses only one command: SELECT.
Commonly used SQL data types (MySql)
MySql uses different data types for working on different kinds of data. These data types are
divided into 3 categories –
▪ Numeric (for number data)
▪ Date and Time (for date, time, year etc. type data)
▪ String (for character data)
Each category further includes various types. Some commonly used types are given below.

INT – Used to store an integer that can be signed or unsigned. If signed, the allowable range
is from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to 4294967295.
You can specify a width of up to 11 digits.
BIGINT – Used to store large integers that can be signed or unsigned. If signed, the allowable
range is from -9223372036854775808 to 922337203685477807. If unsigned, the allowable
range is from 0 to 18446744073709551615. You can specify a width of up to 20 digits.
FLOAT (M, D) – Used to store a floating-point number. You can define the display length (M)
and the number of decimals (D). This is not required and will default to 10,2, where 2 is the
number of decimals and 10 is the total number of digits (including decimals). Decimal
precision can go to 24 places for a FLOAT.
DOUBLE (M, D) – Used to store a double precision floating-point number. You can define the
display length (M) and the number of decimals (D). This is not required and will default to
16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for a DOUBLE.
DATE – Stores a date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For
example, December 30th, 1973 would be stored as 1973-12-30.
DATETIME – Stores a date and time combination in YYYY-MM-DD HH:MM:SS format, between
1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the afternoon on December
30th, 1973 would be stored as 1973-12-30 15:30:00.
TIME − Stores the time in a HH:MM:SS format.
CHAR(M) − It is used for the fixed length of character data and are padded with space
characters to match the specified length. It can hold a maximum of 255 characters. Char
uses static memory allocation.
VARCHAR(M) – It is used for the variable length of character data. and are not padded with
any characters. It can hold a maximum of 65,535 characters. Varchar uses dynamic memory
allocation.

4
BLOB – It stands for “Binary Large Object”. This data type is used to store large amount of
binary data such as images and similar type files. It can hold a maximum of 65535 characters.

WORKING WITH MYSQL


Working with DDL commands
CREATE
This command is used to create the database and its objects like table, index, view, procedure,
trigger, function etc.
CREATE DATABASE: It creates a database with the specified name
Syntax: CREATE DATABASE database_name;
Example: CREATE DATABASE Library;
CREATE TABLE: It creates a table under a selected database.
Syntax: CREATE TABLE table_name (column_name datatype (size), column_name datatype (size),
column_name datatype (size) ……………);
Example: CREATE TABLE Book_Master (ISBN varchar (20), Title varchar (50), Price int (8),
Authors varchar (100), Publisher varchar (50));
NB
Before executing CREATE TABLE command a database must be used i.e.,
USE Database_Name;
Example: USE Library;
ALTER
This command is used to modify the structure of a database table. Modification of table structure
includes 3 things i.e., ADDITION of new columns, MODIFICATION of data type and DELETION of
columns.
Syntax (Adding new columns):
ALTER TABLE table_name ADD (column_name datatype (size), column_name datatype (size),
column_name datatype (size) ……………);
Example: ALTER TABLE Book_Master ADD (Publication_No int (8), First_Edition int (8),
Year_of_Publication varchar (10));
Syntax (Modifying data type):
ALTER TABLE table_name MODIFY (column_name new_datatype (size), column_name
new_datatype (size) ……………);
Example: ALTER TABLE Book_Master MODIFY (Publication_No varchar (15), First_Edition varchar
(10), Year_of_Publication int (8));
Syntax (Deleting Columns):
ALTER TABLE table_name DROP (column_name, column_name ……………);
Example: ALTER TABLE Book_Master DROP Year_of_Publication;
RENAME
This command is used to rename a database table as well as a database. It is used with ALTER
command.

5
Syntax: ALTER TABLE old_table_name RENAME to new_table_name.
Example: ALTER TABLE Book_Master to Book_Info;
TRUNCATE
This command truncates a database table, i.e., deletes all records on one run without asking any
condition.
Syntax: TRUNCATE table_name;
Example: TRUNCATE Book_Info;
DROP
This command removes a table from a database. And also used to remove a database.
Syntax: DROP TABLE table_name;
Example: DROP TABLE Book_Info;
Similarly, we can remove a database as –
DROP DATABASE database_name;

Working with DML commands


USE
This command opens a specified database for use.
Syntax: USE database_name;
Example: USE Library;
SHOW
This command is used to display the list of tables present in the current database.
Syntax: SHOW tables;
DESCRIBE
This command displays the structure of a table.
Syntax: DESCRIBE table_name;
Example: DESCRIBE Book_Master;
SELECT DATABASE ()
This command displays the name of current database you are working on.
Syntax: DISPLAY DATABASE ();
INSERT
This command is used to insert data (records) to a table. We can insert data for specific columns
as well as for all columns of the table.
for specific columns
Syntax: INSERT INTO table_name (col_name, col_name, ……) VALUES (value1, value2, …);
Example: INSERT INTO Book_Master (ISBN, Title, Price) VALUES (‘369PX12’, ‘Computer
Fundamentals’, 490);
for all columns
Syntax: INSERT INTO table_name VALUES (value1, value2, …);

6
Example: INSERT INTO Book_Master VALUES (‘369PX12’, ‘Computer Fundamentals’, 490, ‘P.K.
Sinha’, ‘BPB Publication’, ‘1548/2019’, ‘2007’);
INSERT INTO Book_Master VALUES (‘479DS212’, ‘Data Structure using C’, 440, ‘S.D. Sharma’, ‘BPB
Publication’, ‘36748/2018’, ‘2008’);
INSERT INTO Book_Master VALUES (‘25CP312’, ‘Programming in C’, 390, ‘T. Srivastav’, ‘TMH India’,
‘83248/2019’, ‘2006’);
NB
➢ While inserting data for all columns observe the order of columns in the table. And
accordingly supply the values.
➢ While inserting data for a column with VARCHAR data type, write the value within single
quotes (‘ ‘).
UPDATE
This command is used to modify the existing data in a table.
Syntax: UPDATE table_name SET col_name=value WHERE condition;
Example: UPDATE Book_Master SET Price=520 WHERE ISBN= ‘369PX12’;
DELETE
This command is used to delete any specific data/record or all records from a table.
delete specific record
Syntax: DELETE FROM table_name WHERE condition;
Example: DELETE FROM Book_Master WHERE ISBN= ‘369PX12’;
delete all records (with specific condition)
Syntax: DELETE FROM table_name;
Example: DELETE FROM Book_Master;
Use of SELECT command
SELECT command is used to retrieve records/data from a database table. We can retrieve all
records, specific record, specific column data using this command.
Let’s consider the following table
STUDENT_MARK
ROLL_NO SNAME BRANCH TOTAL_MARKS GRADE
EE204 Amit Sinha Electrical 742 A+
CS123 Dipan Sharma Computer Science 702 A+
ME254 Anupam Pandey Mechanical 687 A
CS253 Sikha Dey Computer Science 590 B
ET110 Simran Kaur Electronics 712 A+
CS302 Jatin Sapru Computer Science 680 A
ME312 Alan David Mechanical 620 B
EE242 Ivan Bayross Electrical 690 A

7
retrieve all records
Syntax: SELECT * FROM table_name;
Example: SELECT * FROM STUDENT_MARK;
retrieve specific records (with specific condition)
Syntax: SELECT * FROM table_name WHERE condition;
Example: Retrieve details of only Computer Science students
SELECT * FROM STUDENT_MARK WHERE BRANCH=’Computer Science’;
retrieve specific column data
Syntax: SELECT col_name, col_name, … FROM table_name;
Example: Retrieve ROLL_NO, TOTAL_MARKS and GRADE of all students
SELECT ROLL_NO, TOTAL_MARKS, GRADE FROM STUDENT_MARK;
retrieve specific column data with specific condition
Syntax: SELECT col_name, col_name, … FROM table_name WHERE condition;
Example: Retrieve ROLL_NO, BRANCH and GRADE of the students securing A+ grade.
SELECT ROLL_NO, BRANCH, GRADE FROM STUDENT_MARK WHERE GRADE=’A+’;
retrieve distinct data
SELECT DISTINCT
It retrieves and displays data where one instance of the duplicate values appears.
Syntax: SELECT DISTINCT col_name FROM table_name;
Example: Retrieve distinct grades from the given table.
SELECT DISTINCT GRADE FROM STUDENT_MARK;

Working with TCL commands


START TRANSACTION
It marks the beginning of a transaction.
COMMIT
A COMMIT command is issued at the time when the transaction is completed i.e., all the changes
have been successful and the changes should be saved to the database. It ensures the end of
current transaction and starts a new transaction.
Syntax: COMMIT; or COMMIT WORK;
Consider the table STUDENT_MARK. There are already 8 records present in the table. Let’s add
2 more records (using INSERT command). After insertion if COMMIT command is executed then
data will be saved permanently to the table otherwise not (it can be explained clearly when we
use ROLLBACK).
ROLLBACK
When a transaction is being executed, some types of error checking is usually performed to
check whether it is executing successfully or not, whether wrong data have been inserted,
whether wrong updation has been performed etc. In such situations, the entire transaction can

8
be reverted using ROLLBACK command. The ROLLBACK command cancels the entire
transaction and the state of the database is returned to the original state (as it was before
transaction). And does not save any of the changes made during transaction.
Syntax: COMMIT; or COMMIT WORK;
There are already 8 records present in STUDENT_MARK table. Let’s add 2 more records (using
INSERT command). Execute ROLLBACK command and then retrieve all records (using SELECT
command). We will observe last two records are not inserted.
SAVEPOINT
It defines/ creates a marking point in a transaction. This marking point is useful in rolling back a
transaction as per user requirements. We can add SAVEPOINT anywhere in a transaction. So
that we can rollback a transaction to a particular Savepoint mark. Any changes made to the
database after the SAVEPOINT are discarded and changes made prior to the SAVEPOINT are
saved.
Syntax: SAVEPOINT savepoint_name; (savepoint_name is any user-defined name)
SAVEPOINT can be removed anytime by executing the following statement.
RELEASE SAVEPOINT savepoint_name;
NB
➢ Before executing COMMIT command in MySql, execute the following command.
SET AUTOCOMMIT=0;
This is because, in MySQL all transactions (execution of DML statements) are committed
by default.

Working with DCL commands


It includes commands which are used to authorise database users to access the database tables,
other objects and to control the access to the database, database tables and other objects of the
database. Commonly used commands under DCL are –
GRANT
This command is used to provide access privileges on database tables to the users.
Example: GRAND SELECT ON STUDENT_MARK TO User-1;
REVOKE
This command removes user access rights from database tables from the users.
Example: REVOKE SELECT ON STUDENT_MARK User-1;

Here, STUDENT_MARK is the database table name and User-1 is one user of the Database.

You might also like