SQL (Structured Query Language)
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.
NB
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.
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;
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;
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.
Here, STUDENT_MARK is the database table name and User-1 is one user of the Database.