LM 6 - SQL Fundamentals Advanced SQL Features
LM 6 - SQL Fundamentals Advanced SQL Features
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Course Outcome
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Syllabus
UNIT I - RELATIONAL DATABASES
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
TOPIC
SQL Fundamentals
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Introduction
• SQL: Structured Query Language is a simple and powerful language used to
create, access, and manipulate data and structure in the database. SQL is like
plain English: easy to understand and to write.
• Various parts of SQL are:
– Data Definition Language (DDL) – defining relation schemas
– Data Manipulation Language (DML) – manipulating relation schemas
– Transaction Control Language(TCL) – beginning & ending of the transactions
– Data Control Language(DCL)- to grant and take back authority from any database user.
– Data Query Language(DQL)- fetch data from tables based on conditions that we can easily
apply.
– Integrity – specifying integrity constraints
– View Definition – defining views for database
– Embedded SQL and Dynamic SQL – SQL commands in C, C++, COBOL or Java
– Authorization – access rights
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Data Abstraction
Basic data types used in SQL are:
1. char(n)🡪 representing fixed length character string. Eg: to represent name, designation,
course_name
2. varchar(n)🡪 character varying. Denoting variable length character strings
3. int🡪 representing numeric values without precision
4. numeric🡪 representing a fixed point number with user specified precision. Eg:
numeric(3,2) allows 333.11 but it does not allow 3333.11
5. smallint🡪 used to store small integer value
6. real🡪 allows floating point, double precision number
7. float(n)🡪 representing floating point number with precision of atleast n digits
CS8791/CC/IVCSE/VIISEM/KG-KiTE
Basic Schema Definition
• Classification of SQL Commands
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
• Data Control Language (DCL)
DDL
• SQL commands that can be used to define the database schema.
• It deals with descriptions of the database schema and is used to create and modify the
structure of database objects in database.
Examples of DDL commands are:
CREATE: used to create the database or its object
DROP: used to delete objects from the database
ALTER: used to alter the structure of the database
TRUNCATE: used to remove all records from table including all spaces allocated for
records
COMMENT: used to add comments to the data dictionary
RENAME: used to rename an object existing in the database
DML
• It deals with the rights, permissions and other controls of the database
system
Examples of DCL commands are:
GRANT: gives user’s access privileges to database
REVOKE: withdraw user’s access privileges given by using the GRANT
command
TCL
• To keep a check on other commands and their affect on the
database. These commands can annul changes made by other
commands by rolling the data back to its original state. It can also
make any temporary change permanent.
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
DQL
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Commonly used SQL Commands
1. Create Table
Syntax: create table table_name;
Eg: create table Student(RollNo int, Name varchar(10), Marks numeric(3,2), Primary key(RollNo));
2. Insert
Syntax:
i) Insert into table_name(column1, column2,column3…..) values(value1, value2, value3,….);
ii) Insert into table_name values(value1, value2, value3,……);
Eg:
i) Insert into Student(RollNo, Name, Marks) values(1, ‘Sharan’, 95.50);
ii) Insert into Student values(1, ‘Sharan’, 95.50);
Commonly used SQL Commands
3. Delete
Syntax: delete from table_name where condition;
Eg: delete from Student where RollNo = 10;
4. Alter
Syntax:
i) alter table table_name add column_name datatype;
ii) alter table table_name drop column column_name;
Eg:
iii) alter table Student add address varchar(20);
iv) alter table Student drop column address;
SELECT , FROM, WHERE
• SELECT: similar to projection which selects the attributes based on the condition described
by WHERE clause
• SELECT col1,col2,…,coln FROM table_name;
• FROM: takes an relation name as an argument from which attributes are to be
selected/projected
• SELECT * FROM table_name;
• WHERE: defines condition which must match in order to qualify the attributes to be
projected
• SELECT col1,col2,…,coln FROM table_name WHERE condition;
Eg:
SELECT SName FROM Student WHERE Age > 18;
Update
• Modifying the existing record of a table
• Syntax:
UPDATE table_name SET col1 = value1, col2 = value2,… WHERE
condition;
RollNo Name City
Eg: UPDATE Student SET city =
111 AAA Mumbai
‘chennai’ WHERE RollNo = 111
222 BBB Bangalore
RollNo Name City
Student
111 AAA Chennai
222 BBB Bangalore
Delete
• Delete one or more records based on some condition
• Syntax:
DELETE FROM table_name WHERE condition; (or)
DELETE FROM table_name ;
Eg: DELETE FROM student ‘chennai’
RollNo Name City WHERE RollNo = 111
111 AAA Mumbai
222 BBB Bangalore RollNo Name City
222 BBB Bangalore
Student
Logical Operators
• Using WHERE clause we can use the operators such as AND,OR and NOT
• Syntax:
i) SELECT col1,col2,… FROM table_name WHERE condition1 AND
condition2 AND Condition3…;
ii) SELECT col1,col2,… FROM table_name WHERE condition1 OR
condition2 OR Condition3…;
iii) SELECT col1,col2,… FROM table_name WHERE NOT
condition;
Example
1. SELECT col1,col2,… FROM table_name WHERE condition1
AND condition2 AND Condition3…;
Eg: SELECT Rollno, Name, City FROM Student WHERE City = NOT
City=‘Bangalore’;
• Foreign Key for one table is actually a primary key of another table.
CS8791/CC/IVCSE/VIISEM/KG-KiTE
Large Object Types
• Large objects (photo, video, CAD files, etc…) are stored as large object
• When a query returns a large object, a pointer is returned rather than the large
object itself
• 2 large object are:
– Blob (Binary Large Object): object is a collection of uninterrupted binary data
(whose interpretation is left to an application outside of the database system)
– Clob (Character Large Object): object is a collection of character data
Domain and Key Constraints
• Test values inserted in the database, and test queries to ensure that the comparisons
make sense
• New domains can be created from existing datatypes
– Eg: create domain Dollars numeric(12,2)
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Integrity Constraints
• Integrity constraints guard against accidental damage to the database, by
ensuring that authorized changes to the database do not result in a loss of data
consistency
• Eg: A customer must have a (non-null) phone number
Constraints on a Single Relation
• Null
• Primary key
• Unique
• Check(p)
– Where ‘p’ is a predicate
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Contd..
1. Not null constraint
Eg: Declare branch_name for branch is not null
branch_name char(15)not null
3. Check clause
Eg: Declare branch_name as the primary key for branch and ensure that the values of
assets are non-negative
create table branch(branch_name char(15), branch_city char(30), assets
integer, primary key (branch_name), check(assets >= 0))
String Operations
• String Comparisons: = , < , > ,<=, >=, <>, ||,etc.,
• Pattern Matching can be performed on strings using two types of
special characters:
• Percent (%) : It matches zero, one or multiple characters.
• Underscore ( _ ) : The _ Character matches any single character.
• Patterns are Case Sensitive
• Eg: ‘Data%’ matches any string beginning with data.
• ‘_ _ _’ matches any string of exactly three characters
• ‘_ _ _%’ matches any string of atleast length 3 characters
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example : Employee Database
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
SQL Statement
• SELECT * FROM Employee WHERE EmpName LIKE ‘A%’;
EmpID EmpName Department Date_of_Join
1 AAA Marketing 1 – Jan
2 ABC Manager 2 – Jan
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
SQL Statement
• SELECT EmpName FROM Employee WHERE EmpName LIKE
‘F_ _ _ _’;
EmpID EmpName
5 FEEEE
6 FFFFF
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
SET Operations : UNION
• To use this UNION clause, each SELECT statement must have
• Same Number of columns selected
• Same Number of column expressions
• Same Data Type
• Have them in the same order
• Syntax:
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example : UNION
Finds the names of the students who have reserved the “DBMS” book or
“OS” Book
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
SET Operations : INTERSECT
• Common entries between two tables can be represented with the help of
INTERSECT operator
• Syntax:
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example : INTERSECT
Finds the names of the students who have reserved both the “DBMS”
book and“OS” Book
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
SET Operations : EXCEPT
• Syntax:
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example : EXCEPT
Finds the students who have reserved both the “DBMS” book but not
reserved “OS” Book
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Aggregate Functions
• It is used to perform a calculation on a set of values to return a single
scalar value.
• Built-in Aggregate Functions:
• Average (avg)
• Minimum (min)
• Maximum (max)
• Total (sum)
• Count
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
SQL Statement : Aggregate Functions
• Eg: SELECT AVG(marks) FROM Students; Test
(To Compute average marks of the students) Id Value
• Eg: SELECT COUNT(*) FROM Test; 11 100
4 22 200
• Eg: SELECT COUNT(ALL id) FROM Test; 33 300
3 NULL 400
• Eg: SELECT Min (Value) FROM Test;
Eg: SELECT Sum (Value)
100
FROM Test;
• Eg: SELECT Max (Value) FROM Test; 1000
400
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Group By
• Syntax:
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example : Student Table
Sid Sname Marks city Student
1 AAA 60 Pune
2 BBB 70 Mumbai
3 CCC 90 Pune
4 DDD 55 Mumbai
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Having
• Only the groups that meet the HAVING criteria will be returned.
• Syntax:
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example : Student Table
Sid Sname Marks city Student
1 AAA 60 Pune
2 BBB 70 Mumbai
3 CCC 90 Pune
4 DDD 55 Mumbai
5 EEE 84 Chennai
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example : Nested Queries
Sid Sname Phone
1 AAA 1111
2 BBB 2222
3 CCC 3333
Student_City
4 DDD 4444 Sid cid
1 101
Student
1 103
cid cname
2 101
101 Pune
3 102
102 Mumbai
4 102
103 Chennai
4 103
City
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Example: Independent Query
1. Find the cid for cname = ‘Pune’ or ‘Chennai’
• Eg: SELECT cid FROM city WHERE cname = ‘Pune’ or ‘Chennai’;
• Eg: If we want to find out sname of Student who live in city with cid as 101,
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Modification of Databases
• To make changes in the existing databases
• Insertion
• Insert INTO table_name (col1,col2,..,) values (val1,val2,..,)
• Eg: Insert INTO Student (Rollno,Name,Marks) values (111,’AAA’,90);
• Deletion
• DELETE FROM table_name WHERE condition;
• Eg: DELETE FROM Student WHERE Rollno = 10;
• Updation
• Update table_name set col1 = val1, col2 = val2,.., where condition;
• Eg: Delete Student Set Name = ‘www’ where Rollno = 101;
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
DCL Commands
• It is used to control privileges in database.
• GRANT
• It is used to provide access or privilege on the database.
• Syntax: GRANT privilege_name ON object_name TO { user_name | PUBLIC |
role_name} [WITH GRANT OPTION];
• Eg: GRANT SELECT ON student_details TO user1;
• REVOKE
• It removes user access rights or privileges to the database.
• Syntax: REVOKE privilege_name ON object_name FROM {user_name | PUBLIC |
role_name}
• Eg: REVOKE SELECT ON student_details FROM user1;
CS3492/DBMS/IICSE/IVSEM/KG-KiTE