0% found this document useful (0 votes)
4 views22 pages

Chapter 7

This document provides an introduction to MySQL and SQL, detailing their definitions, history, and characteristics. It explains various SQL commands categorized into DDL, DML, DQL, DCL, and TCL, along with their functions and syntax. The document also includes examples of commands such as CREATE, INSERT, UPDATE, DELETE, and transaction control commands like COMMIT and ROLLBACK.

Uploaded by

asf7008777037
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Chapter 7

This document provides an introduction to MySQL and SQL, detailing their definitions, history, and characteristics. It explains various SQL commands categorized into DDL, DML, DQL, DCL, and TCL, along with their functions and syntax. The document also includes examples of commands such as CREATE, INSERT, UPDATE, DELETE, and transaction control commands like COMMIT and ROLLBACK.

Uploaded by

asf7008777037
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1

INTRODUCTION TO MYSQL
SQL:
• SQL stands for Structured Query Language.
• It is a database language used for storing, manipulating and accessing data stored in RDBMS.
• It is a standard query language for RDBMS like MySQL, SQL server, etc.
• SQL was developed by Donald D. Chamberlin, Donald C. Messerly, Raymond F. Boyce at IBM in 1970.
• It was initially called as SEQUEL (Structured English Query Language).
• In 1986, ANSI (American National Standard Institute) adopted SQL as a standard language for RDBMS.
• SQL is a very simple, reliable and easy to learn language for RDBMS.
Characteristics of SQL:
• SQL is a very simple, reliable and easy to learn language for RDBMS.
• It is specially used to perform various operations in relational database.
• It can perform various operations like create, insert, update, delete and access data from a database.
• It provides high speed data access from a database.
• It provides easy and efficient facility for the manipulation of database.
MySQL:
• MySQL is a most popular RDBMS based on SQL.
• It is a freely downloaded software.
• MySQL was introduced by a software company named, MySQL AB in 1994.
• It was developed by David Axmark, Allam Larsson, Michael Widenius.
• It was first released on 23rd may 1995 for personal users based on ISAM (Indexed Sequential Access
Method).
• It was acquired by Sun Micro System in 2008.
• It uses SQL statements for creating, manipulating, updating and accessing database.
• In MySQL data can be stored in the form of tables and runs virtually on cross platform like LINUX, UNIX,
WINDOWS, etc.
Features of MySQL:
• It is a free open source software available freely for download.
• It provides fast, easy and reliable method of use.
• It can run on a server.
• It is used for both small and large applications.
• MySQL uses SQL commands for operation.
• It runs on cross platform.
• It works with different programming languages like C, C++, Java, Python, etc...
SQL COMMANDS:
• SQL commands can be used not only for searching the data in the database but also to perform various
other functions like create tables, insert data to tables, modify data, drop the table, etc.
• SQL commands are classified into following types:
1. DDL(Data Definition Language)commands
2. DML(Data Manipulation Language)commands
3. DQL(Data Query Language)/DRL(Data Retrieval Language)commands
4. DCL(Data Control Language)commands
5. TCL(Transaction Control Language)commands
CLASSIFICATION OF SQL COMMANDS:
DDL
• DDL stands for Data Definition Language.
• It is a sub-language of SQL.
• It is used to define the structure of the tables and other objects in a database (index, view, etc.).
• It allows a user to create, modify, remove database objects like table.
• The structure of database objects can be modified by using following DDL commands:
2

1. CREATE
2. ALTER
3. DROP
4. TRUNCATE
5. RENAME
DML
• DML stands for Data Manipulation Language.
• DML is a sub-language of SQL.
• The DML commands are used to access and manipulate data stored in an existing database.
• It is used to insert data into database table, update data of a database table and delete data from a
database table.
• Various types of DML commands are:
1. INSERT
2. UPDATE
3. DELETE
4. LOCK TABLE
DQL it use to access specific data from the data base table
• DQL stands for Data Query Language.
• It is also known as DRL (Data Retrieval Language).
• DQL is the sub-language of SQL.
• It is used to access data stored in a database table.
• It uses several clauses (WHERE, LIKE, IN, BETWEEN, DISTINCT, GROUP BY, ORDER BY) to access
records with certain condition.
• It has only one command:
1. SELECT
DCL
• DCL stands for Data Control Language.
• DCL is a sub-language of SQL.
• It is used to provide permission to the database user to control database access and manipulation of
data stored in that database.
• There are mainly two types of DCL commands:
1. GRANT: This command is used to give user’s access privileges to database.
2. REVOKE: This command is used to withdraw access privileges given with the GRANT command.
TCL
• TCL stands for Transaction Control Language.
• TCL is a sub-language of SQL.
• It is used to manage and control all the database transactions.
• TCL commands are used to control the changes made by DML commands.
• If the transactions are successful, then these transactions save permanently by using COMMIT
command.
• If the transactions are unsuccessful due to some error, then these transactions can be cancelled by
using ROLLBACK command.
• Various types of TCL commands are:
1. START TRANSACTION
2. COMMIT
3. ROLLBACK
4. SAVEPOINT
5. SET TRANSACTION
3

CREATE COMMAND:
• This command is used to create database as well as database objects (table) with the specified name.
• It is a DDL command.
• Syntax for creating database:
mysql>CREATE DATABASE <database name>;
• Example for creating database:
mysql>CREATE DATABASE COLLEGE;
• Syntax for creating table:
mysql>CREATE TABLE <table name>
(<column name1> <data type1> (<size>),
<column name2> <data type2> (<size>),
--------------------------------
<column name N> <data type N> (<size>));
• Example for creating table:
mysql>CREATE TABLE STUDENT
(ROLL INT, int , varchar, decimal are the data type
NAME VARCHAR(30),
MARK DECIMAL);
USE COMMAND:
• After creating a database, we need to open it for use.
• USE command is used to open a database and allowed to perform SQL operations into that
database.
• It is a DML command.
• Syntax:
mysql>USE <database_name>;
• Example:
mysql>USE COLLEGE;
DESC COMMAND:
• DESC command can also be termed as DESCRIBE command.
• This command is used to show the structure of a table which include name of the column, data-type
of column and nullability which means, that column can contain null values or not.
• It is a DML command.
• Syntax:
mysql>DESC <table_name>;
OR,
mysql>DESCRIBE <table_name>;
• Example:
mysql>CREATE DATABASE COLLEGE;
mysql>USE COLLEGE;
mysql>CREATE TABLE STUDENT(ROLL INT, NAME VARCHAR(36), MARK DECIMAL);
mysql>DESC STUDENT;
Field Type Null Key Default Extra
ROLL INT Yes Null
NAME VARCHAR(36) Yes Null
MARK DECIMAL(10,0) Yes Null

ALTER TABLE COMMAND:


• ALTER TABLE command is used to modify or alter the structure of an existing table.
• It allows a user to add/drop a column, modify the data type of a column, rename a column or a table.
• It is a DDL command.
4

• Syntax:
1. mysql> ALTER TABLE <table_name> ADD <new col_name> <datatype>;
2. mysql> ALTER TABLE <table_name> DROP <existing col_name>;
3. mysql> ALTER TABLE <table_name> MODIFY <existing col_name> <new datatype>;
4. mysql> ALTER TABLE <table_name> RENAME COLUMN <existing col_name> TO <new
col_name>;
• Example:
mysql>DESC STUDENT;
Field Type Null Key Default Extra
ROLL INT Yes Null
NAME VARCHAR(36) Yes Null
MARK DECIMAL(10,0) Yes Null

• mysql>ALTER TABLE STUDENT DROP MARK;


• mysql>DESC STUDENT;

Field Type Null Key Default Extra


ROLL INT Yes Null
NAME VARCHAR(36) Yes Null

• We can also change the name of the table by using RENAME TO clause in an ALTER TABLE statement.
• Syntax:
mysql>ALTER TABLE <existing table_name> RENAME TO <new table_name>;
• Example:
mysql>ALTER TABLE STUDENT RENAME TO STUDENT1;

INSERT COMMAND:
• This command is used to insert records (rows) in a table.
• It is a DML command.
• Syntax:
mysql>INSERT INTO <table_name>
VALUES(value1, value2, value3,…),
(value1, value2, value3,…),
-----------------------
(value1, value2, value3,…);
OR,
mysql>INSERT INTO <table_name>
(column1, column2,…)
VALUES(value1, value2, value3,…),
(value1, value2, value3,…),
-----------------------
(value1, value2, value3,…);
• Example:
mysql>CREATE DATABASE COLLEGE;
mysql>USE COLLEGE;
mysql>CREATE TABLE STUDENT
(ROLL INT, ROLL NAME MARK
NAME VARCHAR(30), 1 AA 50
MARK DECIMAL);
2 BB 60
mysql>INSERT INTO STUDENT
3 CC 70
VALUES(1,‘AA’,50),
(2,‘BB’,60),
(3,‘CC’,70);
mysql> SELECT * FROM STUDENT;
5

UPDATE COMMAND:
• This command is used to update an existing value of a field or more than one field of a table.
• It uses WHERE clause to put condition.
• It is a DML command.
• Syntax:
mysql>UPDATE <table_name>
SET <col_name> = value
WHERE <condition>;
• Example:
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 60
3 CC 70
• After update the new result:
mysql>UPDATE STUDENT
SET MARK=100, NAME='PP'
WHERE ROLL=3;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 60
3 PP 100

DELETE COMMAND:
• This command is used to delete records from a table.
• This command is used to delete one record or multiple record at a time.
• It uses WHERE clause to put condition.
• It is a DML command.
• Syntax:
mysql>DELETE FROM <table_name>
WHERE <condition>;
• Example:
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 60
3 PP 100
• Ater delete the data:
mysql>DELETE FROM STUDENT
WHERE ROLL=1;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
2 BB 60
3 PP 100
TRUNCATE COMMAND:
• This command is used to remove all the records from a table.
• It is a DDL command.
• Syntax:
mysql>TRUNCATE TABLE <table_name>;
• Example:
mysql>SELECT * FROM STUDENT; ROLL NAME MARK
mysql>TRUNCATE TABLE STUDENT; 2 BB 60
mysql>SELECT * FROM STUDENT; 3 PP 100
Empty set
6

DROP COMMAND:
• This command is used to remove database as well as database objects(table).
• It is a DDL command.
• Syntax to remove a table from a database:
mysql>DROP TABLE <table_name>;
• Example:
mysql>DROP TABLE STUDENT;
• Syntax to remove a database:
mysql>DROP DATABASE <database_name>;
• Example:
mysql>DROP DATABASE COLLEGE;
SELECT COMMAND:
• This command is used to access data from the database table.
• It uses several clauses (WHERE, LIKE, IN, BETWEEN, DISTINCT, GROUP BY, ORDER BY) to access
records with certain condition.
• It is a DQL command.
• Syntax:
mysql>SELECT <col_name> FROM <table_name>;
• Example:
mysql>CREATE DATABASE COLLEGE;
mysql>USE COLLEGE; ROLL NAME
mysql>CREATE TABLE STUDENT 1 AA
(ROLL INT, NAME VARCHAR(30), MARK DECIMAL); 2 BB
mysql>INSERT INTO STUDENT 3 CC
VALUES (1,'AA',50), (2,'BB',60), (3,'CC',70);
mysql>SELECT ROLL,NAME FROM STUDENT;
• If we want to select all the columns and records from a table, then the syntax is:-
mysql>SELECT * FROM STUDENT;
here * means all
ROLL NAME MARK
1 AA 50
2 BB 60
3 CC 70
• If we want to select some of the selected columns and some of the selected records, then the syntax
is:
mysql>SELECT <col_name> FROM <table_name>
WHERE <condition>;
• Example:
mysql>SELECT ROLL,NAME FROM STUDENT
WHERE MARK=60;
ROLL NAME
2 BB

SELECT DISTINCT command


• This command is used to display the records of a table with distinct values.
• This command will display only one instance of the duplicate values.
• Syntax: in distinct command if we apply in a table for a
mysql>SELECT DISTINCT <col_name> FROM <table_name>; particular column to find out duplicate data it will
• Example: show.
mysql>SELECT DISTINCT ROLL FROM STUDENT; or in this command if any data present more than one
ROLL times than it will show only once
1
2
3
7

RENAME COMMAND:
• This command is used to rename database objects (table).
• It is a DDL command.
• Syntax:
mysql>RENAME TABLE <table_name> TO <new table_name>;
• Example:
mysql>RENAME TABLE STUDENT TO STUDENT1;

START TRANSACTION COMMAND:


• By default, MySQL runs with AUTOCOMMIT mode enabled. This means that as soon as we execute a
statement, the changes made by the statement save permanently in the disk.
• So, the changes made by the statement cannot be rolled back.
• To disable AUTOCOMMIT mode, we use the START TRANSACTION statement.
• START TRANSACTION command is used to commit the current transaction and start a new
transaction.
• It is a TCL command.
• Syntax:
mysql>START TRANSACTION;
• Example:
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 60
3 CC 70
mysql>START TRANSACTION;
mysql>UPDATE STUDENT
SET MARK=100
WHERE ROLL=2;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 100
3 CC 70
mysql>COMMIT;

COMMIT COMMAND:
• This command is used to save the changes made by a transaction permanently.
• When a transaction is completed successfully, then a COMMIT command is issued.
• A COMMIT command save the changes made by a transaction permanently and end the current
transaction.
• A committed transaction cannot be rolled back.
• It is a TCL command.
• Syntax:
mysql>COMMIT;
OR,
mysql>COMMIT WORK;
• Example:
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 60
3 CC 70
mysql>START TRANSACTION;
mysql>UPDATE STUDENT
SET MARK=100
8

WHERE ROLL=2;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 100
3 CC 70
mysql>COMMIT;

ROLLBACK COMMAND:
• When a transaction is being executed, it may be executed successfully or unsuccessfully.
• If the transaction is being executed unsuccessfully, then the entire transaction is reverted (cancelled)
using the ROLLBACK statement.
• The ROLLBACK statement cancels the entire transaction and back to the beginning after the last
commit.
• A committed transaction cannot be rolled back.
• It is a TCL command.
• Syntax:
mysql>ROLLBACK;
OR,
mysql>ROLLBACK WORK;
• Example:
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 100
3 CC 70
mysql>START TRANSACTION;
mysql>UPDATE STUDENT
SET MARK=130
WHERE ROLL=2;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 130
3 CC 70
mysql>ROLLBACK;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 100
3 CC 70

SAVEPOINT COMMAND:
• This command is used as a marker point in a transaction.
• It is used for semi committing a transaction.
• Since, the ROLLBACK statement cancels the entire transaction and back to the beginning after last
commit.
• So that, to rollback a transaction up to a certain point from bottom to top SAVEPOINT statement is
used.
• It is a TCL command.
• Syntax:
mysql>SAVEPOINT VALUE;
• example:
mysql>SAVEPOINT A;
• To remove SAVEPOINT from a transaction, RELEASE SAVEPOINT command is executed.
9

• Syntax:
mysql>RELEASE SAVEPOINT VALUE;
• example:
mysql>RELEASE SAVEPOINT A;
• Example:
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 100
3 CC 70
mysql>START TRANSACTION;
mysql>UPDATE STUDENT
SET MARK=130
WHERE ROLL=2;
mysql>SAVEPOINT A;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 130
3 CC 70
mysql>UPDATE STUDENT
SET NAME='PP'
WHERE ROLL=3;
mysql>SAVEPOINT B;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 130
3 PP 70
mysql>ROLLBACK TO A;
mysql>SELECT * FROM STUDENT;
ROLL NAME MARK
1 AA 50
2 BB 130
3 CC 70
mysql>COMMIT;

SET TRANSACTION COMMAND:


• This command is used to specify properties for the current transaction.
• For example, we can specify a transaction to be read only or read write.
• This command is used before starting a transaction.
• It is a TCL command. read transaction:select,show, desc.
write transaction: delete, insert, update
• Syntax:
mysql>SET TRANSACTION [ READ ONLY | READ WRITE];
• Example:
mysql>SET TRANSACTION READ ONLY;

SHOW COMMAND:
• SHOW command is used to display a list of existing databases on the server.
• It can also be used to display a list of existing tables in a database.
• It is a DML command.
• Syntax:
mysql>SHOW <expression>;
• Example of showing all the existing databases on the server-
mysql>SHOW databases;
10

Databases
COLLEGE
DHE
BIODATA
• Example of showing all the existing tables in a database-
mysql>SHOW tables;
COLLEGE_tables
STUDENT
STUDENT_DETAILS

SELECT DATABASE COMMAND:


• This command is used to show the name of the current database.
• It is a DML command.
• Syntax-
mysql>SELECT DATABASE();
Database
COLLEGE

DATA TYPES IN MySQL:


• A data type is a format that specifies the nature of the data to be stored in a field of the table.
• It represents what kind of value or data a field can store.
• For example: int, float, decimal, char, etc.
• Types of data type:
1. Numeric data type
2. String data type
3. Date and time data type
Numeric data type
• The data type which is used to store integer value as well as fractional value is called as numeric data
type.
• Different types of numeric data type are:
1. TINYINT: -128 to 127
• It is used to store integer value ranges from -27 to 27-1 or 0 to 255.
• It reserves 1 byte of memory space. 1 byte= 8bit
2. SMALLINT:
• It is used to store integer value ranges from -215 to 215-1.
• It reserves 2 bytes of memory space. 2 byte= 2*8=16 bit
3. MEDIUMINT:
• It is used to store integer value ranges from -223 to 223-1.
• It reserves 3 bytes of memory space.
4. INT:
• It is used to store integer value ranges from -231 to 231-1.
• It reserves 4 bytes of memory space.
5. BIGINT:
• It is used to store integer value ranges from -263 to 263-1.
• It reserves 8 bytes of memory space.
6. FLOAT:
• It is used to store fractional value. 1.342 here after point means 342 is called precision
• It is used to store single precision floating point number up to 24 decimal places.
• It reserves 4 bytes of memory space.
7. DOUBLE:
• It is used to store fractional value.
• It is used to store double precision floating point number up to 53 decimal places.
• It reserves 8 bytes of memory space.
8. DECIMAL:
• It is used to store unpacked floating point number.
11

• It reserves 5 to 17 bytes of memory space.


String data type
• The data type which is used to store string value is called as string data type.
• A string is a set of characters written within single quote.
• Types of string data type:
1. CHAR(size):
• It is used to store string value of fixed length.
• It can hold maximum 255(28-1) no. of characters. each character store 1byte memory space
• It uses static memory allocation.
• It is 50% faster than VARCHAR.
2. VARCHAR(size):
• It is used to store string value of variable length.
• It can hold maximum 65,535(216-1) no. of characters. each character store 2 byte memory space
• It uses dynamic memory allocation.
• It is slower than CHAR.
3. TINYTEXT:
• It can hold a maximum of 28-1 characters in a string.
4. TEXT:
• It can hold a maximum of 216-1 characters in a string.
5. MEDIUMTEXT:
• It can hold a maximum of 224-1 characters in a string.
6. LONGTEXT:
• It can hold a maximum of 232-1 characters in a string.
Date and time data type
• The data type which is used to store date and time value in YYYY-MM-DD HH:MM:SS format is called
as date and time data type.
• Types of date and time data type:
1. DATE():
• It is used to store date values into a field in YYYY-MM-DD format.
2. TIME():
• It is used to store time values into a field in HH:MM:SS format.
3. DATETIME():
• It is used to store both date and time values in YYYY-MM-DD HH:MM:SS format.
4. TIMESTAMP():
• It is used to store both date and time values in YYYYMMDDHHMMSS format.
5. YEAR():
• It is used to store year values in 2-digit format(YY) or in 4-digit format(YYYY).

OPERATORS IN MySQL:
• Operators are special symbols or keywords used to perform different operations on columns of a
table to produce some result.
• Different types of operators used in MySQL are:
Arithmetic operators
• These operators are used to perform arithmetic operations like addition, subtraction, multiplication,
division and remainder.
• Arithmetic operators can be used through SELECT command.
OPERATOR DESCRIPTION
+ Addition
- Subtraction
* Multiplication
/ or DIV Division
% or mod Modulo
• Syntax:
mysql>SELECT <expression1> <arithmetic operator> <expression>
[FROM <table_name>]
12

[WHERE <condition>];
• Example:
mysql>SELECT * FROM STUDENT;
NAME ROLL MARK
AA 1 50
BB 2 60
CC 3 70
mysql>SELECT NAME, ROLL, MARK+20 FROM STUDENT;
NAME ROLL MARK+20
AA 1 70
BB 2 80
CC 3 90
Comparison operators / Relational operators
• These operators are used to compare two or more values of a table.
• If the comparison is false, then the result is zero(0) and if the comparison is true, then the result is
non-zero.
• These operators are also called as relational operators.
OPERATOR DESCRIPTION
= Equal to
> Greater than
< Less than
>= Greater than equal to
<= Less than equal to
< > or != Not equal to
• Syntax:
mysql>SELECT <column name> FROM <table name>
WHERE <expression> <comparison operator> <expression>;
• Example:
mysql>SELECT * FROM STUDENT;
NAME ROLL MARK
AA 1 50
BB 2 60
CC 3 70
mysql>SELECT NAME, ROLL FROM STUDENT WHERE MARK>=60;
NAME ROLL
BB 2
CC 3
Logical operators
• These operators are used to perform logical operations on a table.
OPERATOR DESCRIPTION
AND Logical AND
OR Logical OR
NOT Logical NOT
• Syntax:
mysql>SELECT <column_name> FROM <table_name>
WHERE <expression> <logical operator> <expression>;
• Example:-
mysql>SELECT * FROM STUDENT;
NAME ROLL MARK
AA 1 50
BB 2 60
CC 3 70
mysql>SELECT NAME, ROLL FROM STUDENT
WHERE MARK>=60 AND MARK<70;
13

NAME ROLL
BB 2
BETWEEN operator
• The BETWEEN operator/clause is used to show a range of values of the specified column, including
the lower and the upper values.
• Syntax:
mysql>SELECT <column name> FROM <table name>
WHERE <column name> BETWEEN <value1> AND <value2>;
• Example:
mysql>SELECT * FROM STUDENT;
NAME ROLL MARK
AA 1 50
BB 2 60
CC 3 70
mysql>SELECT NAME,ROLL FROM STUDENT
WHERE MARK BETWEEN 61 AND 80;
NAME ROLL
CC 3
IN operator
• The IN operator/clause is used to checks a value within a List(set of values separated by commas).
• If the value is matched, then it will return the matching rows.
• Syntax:
mysql>SELECT <column name> FROM <table name>
WHERE <column name> IN <(value1,value2,…)>;
• Example:
mysql>SELECT * FROM STUDENT;
NAME ROLL MARK
AA 1 50
BB 2 60
CC 3 70
mysql>SELECT NAME,ROLL FROM STUDENT WHERE MARK IN(61,70,82);
NAME ROLL
CC 3
LIKE operator
• The LIKE operator/clause is used to search data, by matching some pattern.
• It is used for pattern matching of string data using wildcard characters % and _.
• Percent sign(%) used to match any sequence of characters.
• Underscore(_) used to match a single character.
• Syntax:
mysql>SELECT <column name> FROM <table name>
WHERE <column name> LIKE <pattern>;
• Example:
mysql>SELECT * FROM STUDENT;
NAME ROLL MARK
AA 1 50
BB 2 60
CC 3 70
• mysql>SELECT ROLL,NAME FROM STUDENT WHERE MARK LIKE ‘_0’;
NAME ROLL MARK
AA 1 50
BB 2 60
CC 3 70
14

DATABASE TRANSACTION:
• A small unit of logical work performed on database is known as transaction.
• A database transaction is any operation made on a database.
• A transaction is said to be done when it performs operations like INSERT, UPDATE, DELETE, SELECT,
etc.
• When a transaction is successful, then any changes made by the transaction in database is
permanent otherwise the database remain unchanged.
• Example:
mysql>SELECT * FROM STUDENT;

STATES OF A TRANSACTION:

• Every transaction in a database goes through a series of logical steps, known as states of transaction.
• The following are the various states of a transaction:
1. Begin
2. Active
3. Partially committed
4. Failed
5. Committed
6. Aborted
7. End
Begin-
• When a transaction is initialized, it enters to begin state.
• e.g. inserting an ATM card into ATM machine.
Active-
• After starting a transaction it enters to active state, to get ready for the operation.
• e.g. entering pin in ATM machine to connect to the bank.
Partially committed-
• When a transaction executes its final operation but it is not completed yet, then it enters to partially
committed state.
• When a partially committed transaction completed successfully, then it enters to committed state
and then the transaction ends.
• When a partially committed transaction is unsuccessful, then it enters to failed state.
Failed-
• When a transaction is unsuccessful in partially committed state, then it enters to failed state.
• A transaction in active state may also enter to failed state when it is unable to proceed further.
• For a failed state transaction a recovery mechanism is required to recover the database.
• Example of some recovery mechanism are:
i. Forward recovery
ii. Backward recovery, etc.
15

Committed-
• When a transaction is successful in partially committed state, then it enters to committed state.
• A committed transaction changes database permanently.
• Once a transaction is committed it cannot be rolled back.
Aborted-
• A transaction enters to aborted state when it is failed.
• If any abnormal condition arises during transaction, then it enters to aborted state.
• An unsuccessful transaction goes through aborted state to end state.
• To recover a database from aborted state we must kill the transaction or restart the transaction.
End state-
• A transaction enters to end state after the transaction is completed successfully or unsuccessfully.
• An end state transaction disconnects the client from the server.

ACID PROPERTY:
• A transaction is a small unit of logical work performed on a database.
• A transaction in a database system must maintain accuracy, completeness and data integrity.
• Every transaction in database must satisfy the following four properties:
A-Atomicity
C-Consistency
I-Isolation
D-Durability
Atomicity-
• Atomicity property of a transaction ensures that all operations within a work unit are successfully
completed and the changes made by the transaction must be permanent.
• If the transaction is failed, then the database must be rolled back to previous state.
• Each transaction must have atomic value.
Consistency-
• The consistency property of the transaction ensures that a database must be consistent before and
after transaction.
• For example, suppose you have 10000 rupees in your bank account before transaction, after doing
transaction of 2000 rupees successfully, now you have 8000 rupees in your bank account. That
means your transaction is in consistent state.
Isolation-
• The isolation property of transaction ensures that, transaction must be operated independently and
transparent to each other.
• In database, multiple transactions can be performed without interfering with each other.
Durability-
• This property of transaction ensures that the effect of a successfully committed transaction persist
even if in system failure.

FUNCTIONS IN MySQL:
• MySQL uses several types of functions to perform some operation(e.g. manipulation, calculation,
etc) on data stored in database.
• There are two types of functions in MySQL:
1. Single Row functions
2. Multiple Row functions
Single Row functions-
• Single row functions are used to perform operation on single value of a table to return a single value
as result.
• They can accept one or more arguments but return only one result per row.
• These functions are used with SELECT command to return the result.
• Various types of single row functions are:
i. String functions
ii. Mathematical functions/Numeric functions
iii. Date & time functions
16

Multiple Row functions-


• Multiple row functions are used to perform operation on multiple rows of a table to return a single
value as result.
• These functions are also called as aggregate functions or group functions.
• By using these functions we can perform operation on multiple rows at a time.
• Various types of aggregate functions are:-
a. MAX()
b. MIN()
c. SUM()
d. AVG()
e. COUNT(*)
f. COUNT(DISTINCT)

STRING FUNCTIONS IN MySQL:


• String functions are used to perform operation on characters or string values.
• These functions are used to manipulate string value stored in a database table.
• They return either a character, a string or a numeric value as a result.
• Various types of string functions are-
a. ASCII()
b. CHAR()
c. CONCAT()
d. CONCAT_WS()
e. INSTR()
f. LCASE()/LOWER()
g. UCASE()/UPPER()
h. LENGTH()
i. LTRIM()
j. RTRIM()
k. TRIM()
l. SUBSTR()
m. MID()
n. REVERSE()
o. LEFT()
p. RIGHT()
ASCII()-
• ASCII stands for American Standard Code for Information Interchange.
A to Z – 65 to 90
a to z – 97 to 122
• This function is used to return the ASCII value of the left most character of a string.
• Syntax-
mysql>SELECT ASCII(string);
• Example-
mysql>SELECT ASCII(‘SUNIL’);
83
If it takes an empty string,then it returns zero.
mysql>SELECT ASCII(‘ ‘);
0
If it takes an NULL argument, then it returns NULL.
mysql>SELECT ASCII(NULL);
NULL
CHAR()-
• This function is used to return a string of characters by taking one or more ASCII values as argument.
• Syntax-
mysql>SELECT CHAR(N1,N2,N3,…);
17

• Example-
mysql>SELECT CHAR(65,66,67);
ABC
CONCAT()-
• This function is used to concatenate strings.
• Syntax-
mysql>SELECT CONCAT(str1,str2,…);
• Example-
mysql>SELECT CONCAT(‘SUNIL’,‘SAHU’);
SUNILSAHU
CONCAT_WS()-
• This function is used to concatenate strings with a specified separator.
• Syntax-
mysql>SELECT CONCAT_WS(separator,str1,str2,…);
• Example-
mysql>SELECT CONCAT_WS(‘_’,‘SUNIL’,‘SAHU’);
SUNIL_SAHU
INSTR()-
• This function is used to return the first occurrence of a substring on a string.
• Syntax-
mysql>SELECT INSTR(string, substring);
• Example-
mysql>SELECT INSTR(‘SUNIL’,‘NI’);
3
LOCATE()-
• This function is similar to INSTR() but, the order of arguments are reversed.
• This function is also used to return the first occurrence of a substring on a string.
• Syntax-
mysql>SELECT LOCATE(substring, string);
• Example-
mysql>SELECT LOCATE(‘NI’,‘SUNIL’);
3
LCASE()/LOWER()-
• This function is used to convert all the characters of a string into lowercase letter.
• Syntax-
mysql>SELECT LCASE(string);
or,
mysql>SELECT LOWER(string);
• Example-
mysql>SELECT LCASE(‘SUNIL’);
sunil
Or,
mysql>SELECT LOWER(‘SUNIL’);
sunil
UCASE()/UPPER()-
• This function is used to convert all the characters of a string into uppercase letter.
• Syntax-
mysql>SELECT UCASE(string);
Or,
mysql>SELECT UPPER(string);
• Example-
mysql>SELECT UCASE(‘sunil’);
SUNIL
Or,
mysql>SELECT UPPER(‘sunil’);
SUNIL
18

LENGTH()-
• This function is used to return the length of a string, where space is also count as one character.
• Syntax-
mysql>SELECT LENGTH(string);
• Example-
mysql>SELECT LENGTH(‘hello world’);
11
LTRIM()-
• This function is used to remove blank spaces from the left side of a string.
• Syntax-
mysql>SELECT LTRIM(string);
• Example-
mysql>SELECT LENGTH(‘ SUNIL’);
6
mysql> SELECT LENGTH (LTRIM (‘ SUNIL’));
5
RTRIM()-
• This function is used to remove blank spaces from the right side of a string.
• Syntax-
mysql>SELECT RTRIM(string);
• Example-
mysql>SELECT LENGTH(‘SUNIL ’);
6
mysql>SELECT LENGTH(RTRIM(‘SUNIL ’));
5
TRIM()-
• This function is used to remove blank spaces from both side of a string.
• Syntax-
mysql>SELECT TRIM(string);
• Example-
mysql>SELECT LENGTH(‘ SUNIL ’);
7
mysql>SELECT LENGTH(TRIM(‘ SUNIL ’));
5
SUBSTR()-
• This function is used to return a substring from the current string, specified by start index and length
of the substring.
• Syntax-
mysql>SELECT SUBSTR(string,start index,length);
• Example-
mysql>SELECT SUBSTR(‘SUNIL’, 3, 2);
NI
MID()-
• This function is same as SUBSTR().
• This function is also used to return a substring from the current string, specified by start index and
length of the substring.
• Syntax-
mysql>SELECT MID(string,start index, length);
• Example-
mysql>SELECT MID(‘SUNIL’, 3, 2);
NI
REVERSE()-
• This function is used to return the reverse of a string.
• Syntax-
mysql>SELECT REVERSE(string);
19

• Example-
mysql>SELECT REVERSE(‘SUNIL’);
LINUS
LEFT()-
• This function is used to return ‘n’ number of characters from the left side of a string.
• Syntax-
mysql>SELECT LEFT(string, n);
• Example-
mysql>SELECT LEFT(‘SUNIL’, 3);
SUN
RIGHT()-
• This function is used to return ‘n’ number of characters from the right side of a string.
• Syntax-
mysql>SELECT RIGHT(string, n);
• Example-
mysql>SELECT RIGHT(‘SUNIL’, 3);
NIL

MATH FUNCTIONS IN MySQL


• These functions are used to perform mathematical operation on numeric values and return numeric
values as result.
• Various types of mathematical functions are-
a. POW()/POWER()
b. ROUND(n,d)
c. ROUND(n)
d. TRUNCATE()
e. ABS()
POW()/POWER()-
• This function is used to return the power of a number raised to an another number.
• Syntax-
mysql>SELECT POW(x,y);
Or,
mysql>SELECT POWER(x,y);
• Example-
• mysql>SELECT POW(2,3);
8
Or,
mysql>SELECT POWER(2,3);
8
ROUND(n,d)-
• This function is used to return the round up value of a number up to ‘d’ decimal places.
• Syntax-
mysql>SELECT ROUND(n,d);
• Example-
mysql>SELECT ROUND(4.726,1);
4.7
mysql>SELECT ROUND (4.752, 1);
4.8
ROUND(n)-
• This function is used to return a nearest integer value of a floating point number.
• Syntax-
mysql>SELECT ROUND(n);
• Example-
mysql>SELECT ROUND(4.726);
5
• mysql>SELECT ROUND (4.345);
20

4
TRUNCATE(n,d)-
• It is a mathematical function that returns a number truncated to ‘d’ decimal places.
• If d=0, then no fractional part.
• If d= -ve, then ‘d’ digits left of the decimal point of the value ‘n’ becomes 0.
• If d= +ve, then it will display the result up to ‘d’ decimal by truncating the rest part.
• Syntax-
mysql>SELECT TRUNCATE(n,d);
• Example-
mysql>SELECT TRUNCATE(426.393,-2);
400
mysql>SELECT TRUNCATE(2.656, 0);
2
mysql>SELECT TRUNCATE(426.393,1);
426.3
ABS()-
• This function is used to return the absolute value or positive value of a number.
• Syntax-
mysql>SELECT ABS(n);
• Example-
mysql>SELECT ABS(-10);
10

DATE & TIME FUNCTIONS IN MySQL


• The functions that are used to perform operations on date and time values are called as date and
time functions.
• In MySQL default date and time format is:
YYYY-MM-DD and HH:MM:SS
• Various date and time functions used in MySQL are-
a. CURDATE()
b. DATE()
c. MONTH()
d. DAYOFMONTH()
e. DAYOFWEEK()
f. DAYOFYEAR()
g. NOW()
h. SYSDATE()
i. DAYNAME()
CURDATE()-
• This function is used to return the current date of a system in YYYY-MM-DD format.
• Syntax-
mysql>SELECT CURDATE();
• Example-
mysql>SELECT CURDATE();
2020-04-23
mysql>SELECT CURDATE()+100;
20200523
DATE()-
• This function is used to return the date from date or date & time expression.
• Syntax-
mysql>SELECT DATE(expression);
• Example-
mysql>SELECT DATE(‘2020-04-23 10:10:07’);
2020-04-23
21

MONTH()-
• This function is used to return the number of month of the date argument.
• The range of month is from 0 to 12.
• Syntax-
mysql>SELECT MONTH(date);
• Example-
mysql>SELECT MONTH(‘2020-04-23’);
04
DAYOFMONTH()-
• This function is used to return the day of the month of the date argument.
• Range of the day of the month is from 0 to 31.
• Syntax-
mysql>SELECT DAYOFMONTH(date);
• Example-
mysql>SELECT DAYOFMONTH(‘2020-04-23’);
23
DAYOFWEEK()-
• This function is used to return the day of the week of the date argument.
• Range of the days of a week is from 1 to 7. As 1 for Sunday, 2 for Monday and so on.
• Syntax-
mysql>SELECT DAYOFWEEK(date);
• Example-
mysql>SELECT DAYOFWEEK(‘2020-04-23’);
5
DAYOFYEAR()-
• This function is used to return the day of a year of the date argument.
• Its ranges is from 1 to 366.
• Syntax-
mysql>SELECT DAYOFYEAR(date);
• Example-
mysql>SELECT DAYOFYEAR(‘2020-04-23’);
114
DAYNAME()-
• This function is used to return the day name of the date argument.
• Syntax-
mysql>SELECT DAYNAME(date);
• Example-
mysql>SELECT DAYNAME(‘2020-04-23’);
THURSDAY
NOW()-
• This function is used to return the current date and time in YYYY-MM-DD HH:MM:SS format, when
used as a string.
• This function is used to return the current date and time in YYYYMMDDHHMMSS format, when it is
used as a decimal number.
• Syntax-
mysql>SELECT NOW();
• Example-
mysql>SELECT NOW();
NOW()
2020-04-23 11:12:14
mysql>SELECT NOW()+100;
NOW()
20200423111314
22

SYSDATE()-
• This function is used to return the same value as NOW().
• This function returns current date and time when the function executes.
• Where as, NOW() returns a constant time.
• Syntax-
mysql>SELECT SYSDATE();
• Example-
mysql>SELECT SYSDATE();
SYSDATE()
2020-04-23 11:12:14
• SYSDATE() does not get pause but the NOW() get pause by using SLEEP(n) function.
mysql>SELECT SYSDATE(),SLEEP(20),SYSDATE();
SYSDATE() SLEEP(20) SYSDATE()
2020-04-23 06:55:30 0 2020-04-23 06:55:50
mysql>SELECT NOW(),SLEEP(20),NOW();
NOW() SLEEP(20) NOW()
2020-04-23 06:55:30 0 2020-04-23 06:55:30

You might also like