Unit II
Unit II
Unit II
Database
Management
System
Mr. Madale L S (M.
Computer Tech
Engg. CSE)
(Third Semester)
Lecturer in Computer Engg. Department
ByPolytechnic Vishnupuri, Nanded
Gramin
Mr. Madale L. S.
Lecturer in Computer Engg. Department
Introduction to SQL
• Objectives
• Definition of SQL
• Pronouncing SQL: S-Q-L or sequel?
• Types of SQL statements.
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
• Data Control Language (DCL)
• Data Query Language (DQL)
• Transaction Control Language (TCL)
Characteristics of SQL
• SQL is easy to learn.
• Binary:
It has a maximum length of 8000 bytes. It contains fixed-length
binary data.
• Varbinary:
It has a maximum length of 8000 bytes. It contains variable-
length binary data.
• Image:
It has a maximum length of 2,147,483,647 bytes. It contains
variable-length binary data.
Approximate Numeric Data Type
FLOAT :
-1.79E + 308 to 1.79E + 308
It is used to specify a floating-point value e.g. 6.2, 2.9
etc.
REAL :
-3.40e + 38 to 3.40E + 38
It specifies a single precision floating point number.
Exact Numeric Data Type
The subtypes are given below:
INT :
It is used to specify an integer value.
SMALLINT :
It is used to specify small integer value.
BIT :
It has the number of bits to store.
DECIMAL :
It specifies a numeric value that can have a decimal number.
NUMERIC :
It is used to specify a numeric value.
Character String Data type:
The subtypes are given below:
char
It has a maximum length of 8000 characters. It
contains Fixed-length non-unicode characters.
varchar
It has a maximum length of 8000 characters. It
contains variable-length non-unicode characters.
text
It has a maximum length of 2,147,483,647
characters. It contains variable-length non-unicode
characters.
Date and time Data types :
The subtypes are given below:
Date:
It is used to store the year, month, and days value.
Time:
It is used to store the hour, minute, and second values.
Timestamp:
It stores the year, month, day, hour, minute, and the second
value.
Types of SQL Commands
• There are five types of SQL commands: DDL, DML, DCL, TCL,
and DQL.
Data Definition Language
Syntax :
CREATE TABLE TABLE_NAME (COLUMN_NA
ME1 DATATYPES[SIZE],
COLUMN_NAME2 DATATYPES[SIZE]…….
COLUMN_NAME n DATATYPES[SIZE]);
Example :
Syntax :
Example :
Syntax :
Example :
Syntax :
Example :
Syntax :
Example :
• Syntax :
• Create table table_name(column_name1 datatype(size) Null
or Not Null constraint, column_name2 datatype(size)…..);
• Example :
• Create table student(roll number(5) Not Null, name
varchar(20),…);
Domain Relational Constraints
• Check Constraints / User defined Constraints
• The Check Constraints is used to ensure that attribute
value satisfies specific condition as specified by data
requirements.
• The DBMS can prevent user from entering incorrect or
other data in database table.
• Syntax :
• Create table table_name(column_name1 datatype(size)
Check Constraint, column_name2 datatype(size)…..);
• Example :
• Syntax :
• Create table table_name(column_name1 datatype(size)
Default ‘unknown’, column_name2 datatype(size)…..);
• Example :
DCL :
Data control language is used to control various user actions
like inserting data, updating data, deleting data or viewing
data.
To perform any operation in the database user needs
privileges like creating table, indexes or views.
DCL is set of commands used to,
GRANT
REVOKE
Data Control Language
GRANT:
A system privilege is the right to perform a particular action or
to perform an action on any schema objects or a particular type.
An authorized user may pass on this authorization to other
users. This process is called as Granting of privileges.
Syntax :
Example :
CASCADE : Will revoke all privileges along with all dependent grant
privilege.
RESTRICT : Will not revoke all related grants only removes that
GRANT only.
Data Control Language
Example :
Syntax:
COMMIT;
Example :
DELETE FROM CUSTOMERS
WHERE AGE = 25;
COMMIT;
Transaction Control Language
ROLLBACK COMMAND:
Rollback command is used to undo transactions that
have not already been saved to the database.
Syntax:
ROLLBACK;
Example :
Syntax:
SAVEPOINT SAVEPOINT_NAME;
Example :
SAVEPOINT S1;
Data Query Language
DQL is used to fetch the data from the database.
SELECT expressions
FROM TABLES
WHERE conditions;
Example:
SELECT emp_name
FROM employee
WHERE age > 20;
Practice Queries
Consider the table
Student (name, marks, dept, age, place, phone, birthdate)
Write SQL query for following :
(i) To list students having place as ‘Pune’ or ‘Jalgaon’.
• Objectives
SQL Operators:
The SQL operators are used to perform operations
on data.
Types of Operators
Arithmetic
ArithmeticOperators
Operators
Comparison Operators
Set Operators
Range Searching Operators
Pattern Marching Operators
SQL Operator : Arithmetic Operators
Arithmetic Operators:
To perform various arithmetic operations we use these
operators.
Result of arithmetic operators is numeric value.
Comparison Operators:
The comparison operator is used to compare
attribute value of tuple with arithmetic comparisons.
A comparison operator is a mathematical symbol
used to compare two values in mathematical
expression.
Comparison operators in conditions will be used to
evaluate expression.
Types of Comparison Operators
Sr No. Symbol Operation
1 = Equals to
Logical Operators:
Syntax :
Select * from Table_name
where expression1 AND expression2;
Example:
Select * from emp
where address=‘Pune’ AND salary >50000;
Types of Logical Operators
Syntax :
Select * from Table_name
where expression1 OR expression2;
Example:
Select * from emp
where address=‘Pune’ OR salary >50000;
Types of Logical Operators
Syntax :
Select * from Table_name
where NOT expression;
Example:
Select * from emp
where NOT salary >50000;
SQL Operators : SET Operators
SET Operators:
The results of two queries can be combined using
the set operation
Types of Set Operators
Union operation
Intersect operation
Except operation
Nesting SET operation
Types of SET Operators
Example:
Select * from emp
where eid between 1 and 4;
Pattern Matching Operators
Example:
Select * from emp
Where EmpName LIKE ‘S%’;