5.unit 5
5.unit 5
UNIT – V
Form of Basic SQL Query, Examples of Basic SQL Queries, Introduction to Nested Queries, Correlated
Nested Queries Set, Comparison Operators, Aggregative Operators, NULL values,
Comparison using Null values, Logical connectivity‘s – AND, OR and NOT, Impact on SQL Constructs,
Outer Joins, Disallowing NULL values, Complex Integrity Constraints in SQL Triggers and Active Data
bases.
Advanced Topics: Overview: Parallel Database, Multimedia Database, Mobile Database, Web
Database, Multi dimensional Database. Data Warehouse, OLTP Vs OLAP, NoSQL Database.
COURSE OBJECTIVES:
To master the basics of SQL and construct queries using SQL
COURSE OUTCOMES:
✓ Ability to design entity relationship model and convert entity relationship diagrams into RDBMS
and formulate SQL queries on the data
COMPARISON OPERATORS:
Assume 'variable a' holds 10 and 'variable b' holds 20, then
Logical connectivity‘s:
• AND: The AND operator allows the existence of multiple conditions in an SQL statement's
WHERE clause.
• OR: The OR operator is used to combine multiple conditions in an SQL statement's WHERE
clause
• NOT: The NOT operator reverses the meaning of the logical operator with which it is used.
Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
DATA TYPES
1. CHAR (Size): It is used to store character strings values of fixed length. Size in brackets determines
number of characters cell can hold. CHAR data type can hold a maximum of 255 characters.
2. VARCHAR (Size) / VERCHAR2 (Size): This data type is used to store variable length alphanumeric
data. The maximum character can hold is 2000 character.
3. NUMBER (P, S): The NUMBER data type is used to store number (fixed or floating point). Number
of virtually any magnitude may be stored up to 38 digits of precision. Number as large as 9.99 *
2
Lecture Notes for DBMS
10124. The precision (p) determines the number of places to the right of the decimal. If scale is
omitted then the default is zero. If precision is omitted, values are stored with their original precision
up to the maximum of 38 digits.
4. DATE: This data type is used to represent date and time. The standard format is DD-MM-YY as in
24-MAR-2022. To enter dates other than the standard format, use the appropriate functions. Date time
stores date in the 24-Hours format. By default the time in a date field is 12:00:00 am, if no time
portion is specified. The default date for a date field is the first day the current month.
5. LONG: This data type is used to store variable length character strings containing up to 2GB. Long
data can be used to store arrays of binary data in ASCII format. LONG values cannot be indexed, and
the normal character functions such as SUBSTR cannot be applied.
6. RAW: The RAW data type is used to store binary data, such as digitized picture or image. Data
loaded into columns of these data types are stored without any further conversion. RAW data type
can have a maximum length of 255 bytes. LONG RAW data type can contain up to 2GB.
DATA DEFINITION LANGUAGE (DDL): The Data Definition Language (DDL) is used to
create and destroy databases and database objects. These commands will primarily be used by database
administrators during the setup and removal phases of a database project. Let's take a look at the structure
and usage of four basic DDL commands:
3
Lecture Notes for DBMS
1. CREATE:
CREATE TABLE: This is used to create a new relation and the corresponding
Syntax: SQL > CREATE TABLE tablename (field_1 data_type (Size), field_2 data_type (Size), .. . );
Example: SQL > CREATE TABLE Student (sno NUMBER (3), sname VARCHAR (30), class
VARCHAR (15));
2. ALTER:
(a) ALTER TABLE ...ADD: This is used to add some extra fields into existing relation.
Syntax: SQL > ALTER TABLE relation_name ADD (new field_1 data_type(size), new field_2
data_type(size),..);
Example: SQL > ALTER TABLE std ADD (Address CHAR (10));
(b) ALTER TABLE...MODIFY: This is used to change the width as well as data type of fields of
existing relations.
Syntax: SQL > ALTER TABLE relation_name MODIFY (field_1 new data_type (Size), field_2 new
data_type (Size),….field_n new data_type (Size));
Example: SQL>ALTER TABLE student MODIFY (sname VARCHAR (10), class VARCHAR (5));
3. DROP: This is used to delete the structure of a relation. It permanently deletes the records in the
table.
Syntax: SQL > DROP TABLE table_name;
Example: SQL > DROP TABLE student;
4
Lecture Notes for DBMS
5. TRUNCATE: This command will remove the data permanently. But structure will not be removed.
Syntax: SQL> TRUNCATE TABLE <Table name>
Example: SQL> TRUNCATE TABLE student;
Example: SQL > INSERT INTO student (sno, sname, class, address) VALUES
(1,’Ravi’,’B.E’,’Hyderabad’);
Example: SQL > INSERT INTO student (sno, sname, class, address) VALUES (&sno, ’&sname’,
’&class’, ’&address’);
Enter value for sno: 101
Enter value for name: Ravi
5
Lecture Notes for DBMS
3) DELETE: This is used to delete all the records of a relation but it will retain the structure of that
relation.
a) DELETE-FROM: This is used to delete all the records of relation. Syntax:
SQL>DELETE FROM relation_name;
DEPTNO DNAME
10 ACCOUNTING
20 RESEARCH
30 SALES
3. SELECT - FROM - WHERE: This query is used to display a selected set of fields for a selected set
of records of a relation.
Syntax: SQL> SELECT a set of fields FROM relation_name WHERE condition; Example:
SQL> select * FROM dept WHERE deptno<=20;
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
4. SELECT - FROM - ORDER BY: This query is used to display a selected set of fields from a relation
in an ordered manner base on some field.
Syntax: SQL> SELECT a set of fields FROM relation_name ORDER BY field_name;
Example: SQL> SELECT empno,ename,job FROM emp ORDER BY job;
EMPNO ENAME JOB
3 RAVI MANAGER
2 ARAVIND MANAGER
1 SAGAR CLERK
5. SELECT - FROM - GROUP BY: This query is used to group to all the records in a relation together
for each and every value of a specific key(s) and then display them for a selected set of fields the
relation.
Syntax: SQL> SELECT a set of fields FROM relation_name GROUP BY field_name;
Example: SQL> SELECT EMPNO, SUM (SALARY) FROM EMP GROUP BY EMPNO;
EMPNO SUM (SALARY)
1 3000
2 4000
3 5000
7
Lecture Notes for DBMS
DATA CONTROL LANGUAGE (DCL): DCL provides users with privilege commands. The
owner of database objects (tables) has the soul authority on them. The owner (database administrators)
can allow other database users to access the objects as per their requirement.
1. GRANT: The GRANT command allows granting various privileges to other users and allowing
them to perform operations within their privileges.
For Example, if user is granted ‘SELECT’ privilege then user can only view data but cannot perform
any other DML operation on database object. Granted privileges can also be withdrawn by DBA at any
time.
Syntax: SQL > GRANT PRIVILEGES on object_name To user_name;
2. REVOKE: To withdraw the privileges that has been granted to a user, we use the REVOKE
command
Syntax: SQL > REVOKE PRIVILEGES ON object-name FROM user_name;
1. COMMIT: This command is used to end a transaction only with the help of the commit
command transaction changes can be made permanent to the database. Syntax: SQL > COMMIT;
2. SAVEPOINT: SAVEPOINTs are like marks to divide a very lengthy transaction to smaller
once. They are used to identify a point in a transaction to which we can latter rollback. Thus,
SAVEPOINT is used in conjunction with rollback.
Syntax: SQL > SAVEPOINT ID;
8
Lecture Notes for DBMS
3. ROLLBACK: A rollback command is used to undo the current transactions. We can rollback
the entire transaction so that all changes made by SQL statements are undo (or) rollback a transaction
to a SAVEPOINT so that the SQL statements after the SAVEPOINT are rollback.
Syntax: SQL > ROLLBACK; (current transaction can be rollback)
(Or)
SQL > ROLLBACK to SAVEPOINT ID;
Aggregative Functions/Operators: Aggregate functions in DBMS take multiple rows from the
table and return a value according to the query. All the aggregate functions are used in Select statement.
Generally used Aggregate functions in SQL are:
1. Max (): Max () is used to find maximum value in the column. It can be used on any type of data.
Ex: Select Max (salary) from Employee;
2. Min (): Min () is used to find maximum value in the column. It can be used on any type of data.
Ex: Select Min (salary) from Employee;
3. Sum (): Sum () function sums up the values in the column supplied as a parameter. Ex: Select Sum
(salary) from Employee;
4. Count (): Count () returns the number of rows in the result. It does not count the null values. Ex:
Select COUNT(*) from Employee where Salary > 20000;
Types:
• COUNT(*): Counts all the number of rows of the table including null.
• COUNT( COLUMN_NAME): count number of non-null values in column.
• COUNT( DISTINCT COLUMN_NAME): count number of distinct values in a column.
5. Avg (): Avg () returns the average value of the numeric column that is supplied as a parameter.
Ex: Select Avg (salary) from Employee
INTRODUCTION TO NESTED QUERIES
A Nested Query is a query within another SQL query and embedded within the WHERE clause. It is
also known as Subquery or Complex query.
o A subquery is a query within another query. The outer query is known as the main query, and
the inner query is known as a subquery.
o A subquery is enclosed in parentheses. o A subquery can be placed in a number of SQL
clauses like WHERE clause, FROM clause.
o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.
o In the Subquery, ORDER BY command cannot be used.
Syntax:
SELECT column_name
FROM table_name
WHERE column_name expression operator
(SELECT column_name from table_name WHERE ... );
Ex: Select EName, Sal From Employee Where sal = (SELECT Max (Sal) From Employee);
In Nested Query, Inner query executes first, and only once. Outer query is executed with result from
Inner query. Hence, Inner query is used in execution of Outer query.
Correlated Nested Queries Set: In Correlated Query, Outer query executes first and for every
Outer query row Inner query is executed. Hence, Inner query uses values from Outer query.
Ex: Orders (OrderID, CustomerID, OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);
NULL values: The SQL NULL is the term used to represent a missing value. A NULL value in a
table is a value in a field that appears to be blank.
A field with a NULL value is a field with no value. It is very important to understand that a NULL value
is different than a zero value or a field that contains spaces.
Comparison using Null values: It is not possible to test for NULL values with comparison operators,
such as =, <, or <>. We will have to use the IS NULL and IS NOT NULL operators instead.
10
Lecture Notes for DBMS
IS NULL Syntax:
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Disallowing NULL values: The integrity constraint NOT NULL while creating the table signifies that
column should always accept an explicit value of the given data type. It does not allow NULL values
into that certain column.
Ex: SQL> CREATE TABLE CUSTOMERS ( ID Number(6) NOT NULL, Name Varchar (20) NOT
NULL, Age Number(2) NOT NULL, Address Varchar (25) , PRIMARY KEY (ID));
Impact on SQL Constructs: The NULL value can cause problems when selecting data. However,
because when comparing an unknown value to any other value, the result is always unknown and not
included in the results.
Outer Joins: In the SQL outer JOIN is used when both tables are integrated together either they are
matched or not. Types of Outer join are:
1. Left Outer Join: This join returns all the rows from left table combine with the matching rows
of the right table. If you get no matching in the right table it returns NULL values. It is also known as
left join Syntax:
SELECT table1.column1, table2.column2....
FROM table1
LEFT JOIN table2
ON table1.column_field = table2.column_field; Ex:
SQL SELECT ID, NAME, AMOUNT,DATE
FROM CUSTOMER
LEFT JOIN ORDER
11
Lecture Notes for DBMS
ON CUSTOMER.ID = ORDER.CUSTOMER_ID;
2. Right Outer Join: This join returns all the rows from right table are combined with the matching
rows of left table .If you get no column matching in the left table .it returns null value. It is also known
as right join Syntax:
SELECT table1.column1, table2.column2....
FROM table1
RIGHT JOIN table2
ON table1.column_field = table2.column_field; Ex:
SQL SELECT ID, NAME, AMOUNT,DATE
FROM CUSTOMER
RIGHT JOIN ORDER
ON CUSTOMER.ID = ORDER.CUSTOMER_ID;
1) Domain constraints: Domain constraints can be defined as the definition of a valid set of values
for an attribute. The value of the attribute must be available in the corresponding domain.
Example:
SNo SName Age MobileNo
101 Ravi 20 9966012345
102 Rani 21 9848012345
103 Raju 19 9849012345
104 Ramu A 9963012345
105 Rajesh 19 9866012345
Not Allowed, as Age is an integer attribute (For SNo 104, Age is not an integer).
12
Lecture Notes for DBMS
2) Entity integrity constraints: The entity integrity constraint states that primary key value can't be
null. This is because the primary key value is used to identify individual rows in relation and if the
primary key has a null value, then we can't identify those rows. A table can contain a null value other
than the primary key field.
Example:
SNo SName Age MobileNo
101 Ravi 20 9966012345
102 Rani 21 9848012345
Raju 19 9849012345
104 Ramu A 9963012345
105 Rajesh 19 9866012345
Not Allowed, as Primary key (SNo) can not contain a NULL value.
3) Key constraints: Keys are used to uniquely identify records of a relation. A relation can have
multiple keys, but out of which one key will be the primary key.
Example:
SNo SName Age MobileNo
101 Ravi 20 9966012345
102 Rani 21 9848012345
103 Raju 19 9849012345
102 Ramu A 9963012345
105 Rajesh 19 9866012345
Not Allowed, as Primary key (SNo) does not contain UNIQUE values.
13
Lecture Notes for DBMS
3. Primary key: Both unique and not null Ex: dept_name vachar(20) primary key;
or
primary key (dept_name);
4. Check clause: The check clause permits domain values to be restricted with user specific
constraints/conditions.
14
Lecture Notes for DBMS
5. Default: SQL allows a default value to be specified for an attribute as illustrated by the following
create table statement:
create table student (ID number (5), name varchar (20) not null, deptname varchar (20), totcred number
(3) default 0, primary key (ID));
The default value of the totcred attribute is declared to be 0. As a result, when a tuple is inserted into the
student relation, if no value is provided for the totcred attribute, its value is set to 0. The following insert
statement illustrates how an insertion can omit the value for the tot cred attribute. insert into student(ID,
name, deptname) values (’12789’, ’Newman’, ’Comp. Sci.’);
6. Referential Integrity: Referential Integrity rule in DBMS is based on Primary and Foreign Key.
The Rule defines that a foreign key have a matching primary key. Reference from a table to another
table should be valid. For example, the dept_name in the Course table has a matching valid
dept_name in the Department table.
Ex: Create table Course (course_id varchar (8), course_name varchar (25), dept_name varchar (20),
credits number (3) check (credits > 0), primary key (course_id), foreign key (dept_name) references
department)
Create Database Trigger: To create a database trigger, we use the CREATE TRIGGER command.
Syntax:
Create [Or Replace] TRIGGER Trigger_name
15
Lecture Notes for DBMS
{Before|After}
{Delete|Insert|Update[Of Columns]} On Table
[For Each Row {When Condition]]
[Reference [Old As Old] [New As New]]
Begin
Pl/Sql Block
End.
Use of Trigger:
• Trigger is used to perform automatic action when another concerned action takes place.
• To implement any complex business rule, that cannot be implemented using integrity constraints.
• Triggers will be used to audit the process i.e., to keep track of changes made to a table.
Active Databases: Database with a set of associated triggers is generally called an active database.
The databases which are regularly accessed and queried are usually referred to as active databases.
ADVANCED TOPICS
Many companies, such as online retailers, want their database to be accessible as fast as possible. This
is where a parallel database stands good.
17
Lecture Notes for DBMS
MULTIMEDIA DATABASE: The multimedia databases are used to store multimedia data
such as images, animation, audio, video along with text. This data is stored in the form of multiple file
types like .txt (text), .jpg (images), .swf (videos), .mp3 (audio) etc.
18
Lecture Notes for DBMS
MOBILE DATABASE: The data in a database can be accessed from anywhere using a mobile
database. It provides wireless database access.. Mobile databases are separate from the main database
and can easily be transported to various places. Even though they are not connected to the main database,
they can still communicate with the database to share and exchange data.
Typing in keywords such as “Pink Dress” enables all the Pink Dresses stored on the website to appear
right on the very browser you are looking on, because the information “Pink” and “Dress” are stored in
their database entries.
Some advantages of using a web database include:
1. The information is accessible from almost any device.
2. Web database programs usually come with their own technical support
19
Lecture Notes for DBMS
3. It’s convenient: web databases allow users to update information so all you have to do is to create
simple web forms.
A multidimensional database is created from multiple relational databases. While relational databases
allow users to access data in the form of queries, the multidimensional databases allow users to ask
analytical questions related to business or market trends.
The multidimensional databases use MOLAP (multidimensional online analytical processing) to access
its data. They allow the users to quickly get answers to their requests by generating and analysing the
data rather quickly.
The data in multidimensional databases is stored in a data cube format. This means that data can be seen
and understood from many dimensions and perspectives.
Example:
DATA WAREHOUSE: A Data Warehouse (DW) is a relational database that is designed for query
and analysis rather than transaction processing. A Data Warehouse provides integrated, enterprise-wide,
historical data and focuses on providing support for decision-makers for data modeling and analysis.
✓ Data Warehouse can be described as a centralized data repository which can be queried for business
benefits..
✓ Data Warehouse is not used for daily operations and transaction processing but used for making
decisions.
Data Warehouse environment contains an extraction, transportation, and loading (ETL) solution, an
online analytical processing (OLAP) engine, customer analysis tools, and other applications that handle
the process of gathering information and delivering it to business users.
21
Lecture Notes for DBMS
Architecture of Data Warehouse: Data Warehousing involves data cleaning, data integration,
and data consolidations. A Data Warehouse has a 3-layer architecture:
1. Data Source Layer: It defines how the data comes to a Data Warehouse. It involves various data
sources and operational transaction systems, flat files, applications, etc.
2. Integration Layer: It consists of Operational Data Store and Staging area. Staging area is used to
perform data cleaning, data transformation and loading data from different sources to a data
warehouse. As multiple data sources are available for extraction at different time zones, staging area
is used to store the data and later to apply transformations on data.
3. Presentation Layer: This is used to perform BI reporting by end users. The data in a DW system is
accessed by BI users and used for reporting and analysis.
Data mart focuses on a single functional area and represents the simplest form of a Data Warehouse.
Consider a Data Warehouse that contains data for Sales, Marketing, HR, and Finance. A Data mart
focuses on a single functional area like Sales or Marketing.
OLTP Vs OLAP
OLTP: OLTP stands for On-Line Transactional Processing. It is featured by a large number of short
on-line transactions (INSERT, UPDATE, and DELETE). It is used for maintaining the online
22
Lecture Notes for DBMS
transaction and record integrity in multiple access environments. OLTP is a system that manages very
large number of short online transactions for example, ATM.
OLAP: OLAP stands for On-Line Analytical Processing. It is used for analysis of database information
from multiple database systems at one time such as sales analysis and forecasting, market research,
budgeting and etc. It is represented by a relatively low volume of transactions. Queries are very difficult
and involve aggregations. Data Warehouse is the example of OLAP system.
The biggest difference between an OLTP and OLAP is the amount of data analyzed in a single
transaction. OLTP handles many concurrent customers and queries touching limited collection of
records at a time, whereas an OLAP system must have the efficiency to operate on millions of data to
answer a single query.
23
Lecture Notes for DBMS
NoSQL DATABASE: NoSQL Database is used to refer a non-SQL or non relational database. It
provides a mechanism for storage and retrieval of data other than tabular relations used in relational
databases. It is generally used to store big data and real-time web applications. Es: MongoDB.
Advantages of NoSQL
1. It can store and retrieve large amounts of data.
2. It supports query language.
3. It provides fast performance.
24