0% found this document useful (0 votes)
87 views199 pages

Database Management System UNIT-1

The document discusses database management systems and why they are important for storing and managing large amounts of data across many applications. It covers common data models like hierarchical, network, relational, and object-oriented models. The key benefits of a DBMS are data independence, efficient data access, data integrity, security, and concurrent access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views199 pages

Database Management System UNIT-1

The document discusses database management systems and why they are important for storing and managing large amounts of data across many applications. It covers common data models like hierarchical, network, relational, and object-oriented models. The key benefits of a DBMS are data independence, efficient data access, data integrity, security, and concurrent access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 199

Database Management System

UNIT-1
• Data is something which can be recorded like name, mobile
no, customer-id, stsudent_id etc. ( Data is a real world
entity)
• Information is something which we extract by
manipulating / processing the stored data.

• Now a days, Databases form the back bone of the several


applications that we are using on daily life knowingly and
unknowingly.

Example:

• Google something over net.


• Email systems like Gmail
• Net banking system
• Academic management systems etc.
• So there is a database in small, medium and large scale
that is involved .
• They are use over intranet/internet.
• They are used for scientific, Social, financial and also in
every sections.
• So, Structured DBMS which are also called RDBMS mostly
based on relational or other kind of models are the
requirement for

1. Design.
2. Development
3. Maintenance and
4. User friendly deployment of such systems.
• There are several DBMS , at times we say relational DBMS
like ORACLE, MS-SQL.

• And there are number of public domain free RDBMS like


MYSQL,POSTGRES and soon.

• And most importantly all of them are interfaced through a


moderately standardized language called SQL.

• Therefore in this course it is imperative for us to understand


the design ,development and deployment issues in RDBMS.
In this course, we will cover in details the following major
topics related database.

1. Modeling in the context of RDBMS.


2. Use of Relational algebra.
3. Formation of queries
4. Implementation issues in data representation
5. Data Structure and file system.
6. Actual transaction systems.
7. Ensure concurrency and consistency.
8. Issues relating to recovery systems.
What Is a DBMS?

• Some thing which can be recorded is called data, like


name, phone no etc
• A very large, integrated collection of data.
• Models real-world enterprise.
– Entities (e.g., students, courses)
– Relationships (e.g., Madonna is taking CS564)
• A Database Management System (DBMS) is a software
package designed to store and manage databases.

Slide No:L1-2
Database System Applications
• DBMS contains information about a particular enterprise
– Collection of interrelated data
– Set of programs to access the data
– An environment that is both convenient and efficient to use
• Database Applications:
– Banking: all transactions
– Airlines: reservations, schedules
– Universities: registration, grades
– Sales: customers, products, purchases
– Online retailers: order tracking, customized
recommendations
– Manufacturing: production, inventory, orders, supply chain
– Human resources: employee records, salaries, tax
deductions
• Databases touch all aspects of our lives

Slide No:L1-1
Why Use a DBMS?

• Data independence and efficient access.


• Reduced application development time.
• Data integrity and security.
• Uniform data administration.
• Concurrent access, recovery from crashes.

Slide No:L1-3
Purpose of Database Systems
• In the early days, database applications were built
directly on top of file systems
• Drawbacks of using file systems to store data:
– Data redundancy and inconsistency
• Multiple file formats, duplication of information in
different files
– Difficulty in accessing data
• Need to write a new program to carry out each
new task
– Data isolation — multiple files and formats
– Integrity problems
• Integrity constraints (e.g. account balance > 0)
become “buried” in program code rather than
being stated explicitly
• Hard to add new constraints or change existing
ones
Slide No:L1-6
Purpose of Database Systems (Cont.)

• Drawbacks of using file systems (cont.)


– Atomicity of updates
• Failures may leave database in an inconsistent state
with partial updates carried out
• Example: Transfer of funds from one account to
another should either complete or not happen at all
– Concurrent access by multiple users
• Concurrent accessed needed for performance
• Uncontrolled concurrent accesses can lead to
inconsistencies
– Example: Two people reading a balance and
updating it at the same time, But the scenario is
different in case of DB.
– Security problems
• Hard to provide user access to some, but not all, data
• Database systems offer solutions to all the above problems

Slide No:L1-7
Levels of Abstraction

• Physical level: Describes how a record is stored.

• Logical level: Describes how the data is organised in


database, and the relationships among the data.
type customer = record
customer_id : number;
customer_name : string;
customer_street : string;
customer_city : string;
end;

• View level: Application programs hide details of data


types. Views can also hide information (such as an
employee’s salary) for security purposes.

Slide No:L1-8
View of Data

An architecture for a database system

Slide No:L2-1
Instances and Schemas

• Schema – The logical structure of the database


(A database schema is the skeleton structure that represents the logical
view of the entire database.)
• It defines how the data is organized and what is the association
among them.
• It formulates all the constraints that are to be applied on the data.
• It defines tables, views, and integrity constraints.
– Physical schema: Database design at the physical
level
– Logical schema: Database design at the logical level.

• Instance – The actual content of the database at a


particular point in time

Slide No:L2-2
Data Independence

The ability to modify schema definition in one level without


affecting schema definition in the next higher level is called
data independence.
There are two levels of data independence.

Physical data independence- is the ability to modify the physical


schema without changing the logical view .

(Change in Binary search/linear search/sub list search etc OR change


in data structure)
• Modifications at the physical level are occasionally necessary to
improve performance.
• It means we change the physical storage/level without affecting the
conceptual or external view of the data. 

Slide No:L2-3
Logical data independence- is the ability to modify the logical
schema without causing application program (in view label) to be
rewritten.

Logical Data independence means if we add some new columns or


remove some columns from table then the user view and programs
should not change.

For example: consider two users A & B. Both are selecting the fields
"EmployeeNumber" and "EmployeeName". If user B adds a new
column (e.g. salary) to his table, it will not effect the external view
for user A, though the internal schema of the database has been
changed for both users A & B.
Data Models

A data model shows the logical structure of a database,


including the relationships and constraints that
determine how data can be stored and accessed.

Data models are generally represented by a diagram.


There are many kinds of data models. Some of the most
common ones include:

1.Hierarchical model
2.Netwk model
3.Relational Model
4. Object -oriented model
5.Enity-relationship model

Slide No:L2-4
Hierarical Model
This model organizes data into a tree-like structure, where each record has a
single parent or root. Sibling records are sorted in a particular order. It allows
one-to-many relationship. This model was primarily used by IBM systems ,but
they are rarely seen today due to certain operational inefficiencies.
Network Model
The network model builds on the hierarccal model by allowing many-
to-one and one-to-many relationships between linked records,
implying multiple parent records.
Based on the mathematical set theory, the model is constructed with
sets of related records. Each set consists of one owner or parent record
and one or more child records.
Object-oriented model
Object-oriented Data Model  is based upon real world situations.
These situations are represented as objects, with different attributes.
This model defines a database as a collection of objects or reusable
software elements. There are several kind of object-oriented databases
like.

Multimedia database: Incorporates media such as images, video etc.

Hypertext database: allows any object to link to any other object.


The object-oriented database model is the best known post relational
model, since it incorporates tables, but is not limited to tables. Such
model is also known as hybrid database model.
Relational Data model
Relational data model is the primary data model, which is used
widely around the world for data storage and processing. This
model is simple and it has all the properties and capabilities
required to process data with storage efficiency.

• In relational data model, relations are saved in the format of


Tables. This format stores the relation among entities.
• A table has rows and columns, where rows represents records
and columns represent the attributes.
• Tuple − A single row of a table, which contains a single record for
that relation is called a tuple.
• Attribute domain − Every attribute has some pre-defined value
scope, known as attribute domain.
A Sample Relational Database

Slide No:L3-4
Entity - Relationship model

An ER diagram shows the relationship among entity sets. An entity set is a


group of similar entities and these entities can have attributes.
Relationships Between Entities
A relationship is how the data is shared between entities. There are
three types of relationships between entities:

One-to-one : One instance of an entity (A) is associated with one


other instance of another entity (B)

One-to-many: One instance of an entity (A) is associated with zero, one or


many instances of another entity (B), but for one instance of entity B there is
only one instance of entity A.
Many-to-one :More than one entities from entity set A can be associated
with at most one entity of entity set B, however an entity from entity set
B can be associated with more than one entity from entity set A.

Many-to-Many : One instance of an entity (A) is associated with zero or more


instances of another entity (B), and one instance of entity B is associated
with zer or more instances of entity A. 
The symbols used to design an ER diagram are shown.  
Example: University Database
• Conceptual schema:
– Students(sid: string, name: string, login:
string,
age: integer, gpa:real)
– Courses(cid: string, cname:string,
credits:integer)
– Enrolled(sid:string, cid:string, grade:string)
• Physical schema:
– Relations stored as unordered files.
– Index on first column of Students.
• External Schema (View):
– Course_info(cid:string,enrollment:integer)

Slide No:L2-6
DATA BASE LANGUAGE

• Language for accessing and manipulating the data organized


by the appropriate data model
• Two classes of languages
– Procedural – user specifies what data is required and how
to get those data
– Declarative (nonprocedural) – user specifies what data is
required without specifying how to get those data
• SQL is the most widely used query language

Slide No:L3-1
SQL
Data Definition Language (DDL)
• Specification notation for defining the database schema
Example: create table account (
account_number char(10),
branch_name char(10),
balance integer)
• DDL compiler generates a set of tables stored in a data
dictionary
• Data dictionary contains metadata (i.e., data about data)
– Database schema
– Data storage and definition language
• Specifies the storage structure and access methods
used
– Integrity constraints
• Domain constraints
• Referential integrity (e.g. branch_name must
correspond to a valid branch in the branch table)
– Authorization

Slide No:L3-2
Structured Query Language(SQL)
• SQL: widely used non-procedural language
– Example: Find the name of the customer with
customer-id 192-83-7465
select customer.customer_name
from customer
where customer.customer_id = ‘192-83-7465’
– Example: Find the balances of all accounts held by
the customer with customer-id 192-83-7465
select account.balance
from depositor, account
where depositor.customer_id = ‘192-83-7465’
and
depositor.account_number =
account.account_number

Slide No:L3-5
SQL

• Application programs generally access databases


through one of
– Language extensions to allow embedded SQL
– Application program interface (e.g., ODBC/JDBC)
which allow SQL queries to be sent to a database

Slide No:L3-6
SQL Constraints
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
• INDEX
NOT NULL Constraint

By default, a column can hold NULL values. The NOT


NULL constraint enforces a column to NOT accept NULL
values.
This enforces a field to always contain a value, which means
that you cannot insert a new record, or update a record
without adding a value to this field.
Example:
UNIQUE Constraint
• The UNIQUE constraint ensures that all values in a column are different.
• Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint.
• However, you can have many UNIQUE constraints per table, but only
one PRIMARY KEY constraint per table.
PRIMARY KEY Constraint
• The PRIMARY KEY constraint uniquely identifies each record in a table.
• Primary keys must contain UNIQUE values, and cannot contain NULL
values.
• A table can have only ONE primary key; and in the table, this primary key
can consist of single or multiple columns (fields).
Foreign key
• A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the 
PRIMARY KEY in another table.
• The table with the foreign key is called the child table, and the table with the
primary key is called the referenced or parent table.
• The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
CHECK Constraint:
The CHECK Constraint enables a condition to check the value being entered
into a record. If the condition evaluates to false, the record violates the
constraint and isn’t entered into the table.
INDEX constraint
• The INDEX is used to create and retrieve data from the database
very quickly.
• Index can be created by using single or group of columns in a table.
When index is created, it is assigned a ROWID for each row before it
sorts out the data. Index can be delete using DROP statement.
• Creating index on single column.

• Creating index by combining multiple columns.


DEFAULT Constraint
• The DEFAULT constraint is used to provide a default value for a column.
• The default value will be added to all new records If no other value is
specified.
Example:
CREATE TABLE Persons ( ID int NOT NULL, Name varchar2(255)  NOT NULL,
   Age int, City varchar2(255)  DEFAULT  'Sandnes‘ );

• The DEFAULT constraint can also be used to insert system values, by using
functions like GETDATE()
Example:
CREATE TABLE Orders ( ID int  NOT NULL, OrderNumber int  NOT NULL,
    OrderDate date DEFAULT GETDATE() );
Data Types in SQL
• CHARACTER [(length)] or CHAR [(length)]
• VARCHAR (length)
• BOOLEAN
• SMALLINT
• INTEGER or INT
• DECIMAL [(p[,s])] or DEC [(p[,s])]
• NUMERIC [(p[,s])]
• REAL
• FLOAT(p)
• DOUBLE PRECISION
• DATE
• TIME ----------------------(HH:MM:SS)
• TIMESTAMP-----------------(YYYY-MM-DD HH:MM:SS)
Operator in SQL
An operator is a reserved word or a character used primarily in an SQL
statement's WHERE clause to perform operation(s), such as
comparisons and arithmetic operations. These Operators are used to
specify conditions in an SQL statement and to serve as conjunctions for
multiple conditions in a statement.

Arithmetic operators (+,-,*,/,% etc)

Comparison operators (<,>,<=,>=,!= ,== etc)

Logical operators (ALL,ANY,IN,BETWEEN,AND,OR,EXISTS,LIKE, IS


NULL,UNIQUE)
Database Users

Users are differentiated by the way they expect to interact with


the system
• Application programmers – As its name shows, application
programmers are those who writes application programs that uses the
database. These application programs are written in programming
languages like COBOL or PL . 
• Sophisticated users – form requests in a database query
language
• Specialized users – write specialized database applications
that do not fit into the traditional data processing framework.
• Naïve users – End users are those who access the database from the
terminal end. They use the developed applications and they don’t have
any knowledge about the design and working of database.
For example: Clerical staff in any bank is a naïve user. They don’t have any
dbms knowledge but they still use the database and perform their given
task.
Slide No:L4-1
Database Administrator (DBA)

• Coordinates all the activities of the database system


– has a good understanding of the enterprise’s
information resources and needs.

• Roles of DBA:
– Storage structure and access method definition
– Schema and physical organization modification
– Granting users authority to access the database
– Backing up data
– Monitoring performance and responding to
changes

Slide No:L4-2
Data storage and Querying

• Storage management
• Query processing
• Transaction processing

Slide No:L5-1
Storage Management

• Storage manager is a program module that provides


the interface between the low-level data stored in the
database and the application programs and queries
submitted to the system.
• The storage manager is responsible to the following
tasks:
– Interaction with the file manager
– Efficient storing, retrieving and updating of data
• Issues:
– Storage access
– File organization
– Indexing and hashing

Slide No:L5-2
Query Processing

1.Parsing and translation


2. Optimization
3. Evaluation

Slide No:L5-3
Query Processing (Cont.)

• Alternative ways of evaluating a given query


– Equivalent expressions
– Different algorithms for each operation

• Cost difference between a good and a bad way of evaluating a


query can be enormous

• Need to estimate the cost of operations


– Depends critically on statistical information about relations
which the database must maintain
– Need to estimate statistics for intermediate results to
compute cost of complex expressions

Slide No:L5-4
Transaction Management

• A transaction is a collection of operations that


performs a single logical function in a database
application
• Transaction-management component
ensures that the database remains in a
consistent (correct) state despite system
failures (e.g., power failures and operating
system crashes) and transaction failures.
• Concurrency-control manager controls the
interaction among the concurrent transactions,
to ensure the consistency of the database.

Slide No:L5-5
Database Architecture

• The design of a DBMS depends on its architecture.

• It can be centralized or decentralized or hierarchical.

• The architecture of a DBMS can be seen as either single tier


or multi-tier.

• An n-tier architecture divides the whole system into related


but independent n modules, which can be independently
modified, altered, changed, or replaced.

Slide No:L6-1
In 1-tier architecture, the DBMS is the only entity where the user
directly sits on the DBMS and uses it.
• Any changes done here will directly be done on the DBMS itself. It
does not provide handy tools for end-users.
• Database designers and programmers normally prefer to use single-
tier architecture.

• The 2-Tier architecture is same as basic client-server. In the two-tier


architecture, applications on the client end can directly
communicate with the database at the server side. For this
interaction, API's like: ODBC, JDBC are used.
• The user interfaces and application programs are run on the client-
side.
• The server side is responsible to provide the functionalities like: query
processing and transaction management.
• To communicate with the DBMS, client-side application establishes a
connection with the server side.
• The 3-Tier architecture contains another layer between the client
and server. In this architecture, client can't directly communicate
with the server.

• The application on the client-end interacts with an application


server which further communicates with the database system.

• End user has no idea about the existence of the database beyond
the application server.

• The database also has no idea about any other user beyond the
application.

• The 3-Tier architecture is used in case of large web application.


Database Application Architectures

(web
browser)

Old Modern

Slide No:L6-3
Database Design
•Database is absolutely an integral part of software system.
•Database engineering guarantee you to produce high quality database
design to use in database creation, management and maintenance. 
•A Database designer design the conceptual model of the database first
before he/she implement it.

•Entity Relationship Diagram, also known as ERD, ER Diagram or ER model, is


a type of structural diagram /theoretical model for use in database design.

•An ERD contains different symbols and connectors that visualize two
important information: 

1.The major entities within the system scope, and


2.The inter-relationships among these entities.
When to draw ER Diagrams?
Database design - By drawing ER diagrams to visualize database design ideas,
you have a chance to identify the mistakes and design flaws, and to make
correction before executing the changes in database.

Database debugging - To debug database issues can be challenging,


especially when the database contains many tables, which require writing
complex SQL in getting the information you need. By visualizing a database
schema with an ERD, you have a full picture of the entire database
schema. 

Database creation and patching - ERD tool like Visual Para supports database
generation tool that can automate the database creation and patching
process by means of ER diagrams. 

Requirements gathering - Determine the requirements of an information


system by drawing a conceptual ERD that depicts the high-level business
objects of the system.
Entity
A definable thing—such as a person, object, concept or event—that can have
data stored about it.

Entity type/ Set: A group of definable things, such as students or books.

Entity categories: Entities are categorized as strong, weak or associative.

• A strong entity can be defined solely by its own attributes,

• while a weak entity cannot.


An associative entity is an entity that associates two other entities in a many
to many relationship.  

Attribute: A property or characteristic of an entity which defines/identify it.

Key Attribute: Refers to an attribute that uniquely defines an entity in an entity


set. Entity keys can be super key , candidate key etc.
Super key: A set of attributes (one or more) that together
define an entity in an entity set. 

Candidate key: A minimal super key, meaning it has the least


possible number of attributes to still be a super key. An
entity set may have more than one candidate key. 

A candidate key is a column, or set of columns, in a


table that can uniquely identify any database record
without referring to any other data. Each table may
have one or more candidate keys, but onecandidate
key is unique, and it is called the primarykey.
Implementation purpose:
Primary key: A candidate key chosen by the
database designer to uniquely identify each
record in a relation.
 Foreign key: Identifies the relationship between
two relations/tables.
Relationship
• How entities act upon each other or are associated with
each other.
• Relationships are typically shown as diamonds or labels
directly on the connecting lines.
Relationship constraints
• There are two main types of relationship constraints, cardinality ratio,
and participation.

• Cardinality  for Binary Relationship.


• Participation Constraints.

Cardinality  – maximum number of relationship instances that an entity can.


participate in.

possible cardinality ratio: 1:1, 1: N, N:1, N:M.


Participation Constraints.

 Total Participation − Each entity is involved in the


relationship. Total participation is represented by double
lines.

 Partial participation − Not all entities are involved in the


relationship. Partial participation is represented by single
lines.
Partial Key
• Partial Key is a set of attributes that can uniquely identify weak entities
and that are related to same owner entity. It is sometime called as
Discriminator. In ER diagram it is denoted by dotted underline (--------).

• For example, suppose "Course Number" is an attribute of the Course


entity type in our design for a University database. Suppose Course
Number alone cannot uniquely identify courses. Rather, to identify a
course we must include the Department Number attribute as well.
• Here Course Number is a partial key.
Mapping from E-R Model to Relational Model
Rule-01: For Strong Entity Set With Simple Attributes-
• A strong entity set with simple attributes will require only one
relation/table in relational model.
• Attributes of the entity set will be the attributes of the table/relation.
• The key attributes of the entity set will be the primary key of the relation.
Rule-02: For Strong Entity Set With Composite Attributes-
• A strong entity set with any number of composite attributes will
require only one table in relational model.
• While conversion, simple attributes of the composite attributes are
taken into account and not the composite attribute itself.
 
Rule-03: For Strong Entity Set With Multi Valued Attributes-
• A strong entity set with any number of multi valued attributes will
require two tables in relational model.
• One table will contain all the simple attributes with the primary key.
• Other table will contain the primary key and all the multi valued
attributes.
Rule-04: Translating Relationship Set into a Table-
• A relationship set will require one table in the relational model.
• Attributes of the table are-
• Primary key attributes of the participating entity sets
• Its own descriptive attributes if any.
• Set of non-descriptive attributes will be the primary key.
Rule-05: For Binary Relationships With Cardinality Ratios-

The following four cases are possible-

  Case-01: Binary relationship with cardinality ratio m:n

Case-02: Binary relationship with cardinality ratio 1:n

Case-03: Binary relationship with cardinality ratio m:1

Case-04: Binary relationship with cardinality ratio 1:1


 
Conceptual Design

• Conceptual design follows requirements analysis,


– Yields a high-level description of data to be stored
• ER model popular for conceptual design
– Constructs are expressive, close to the way people think about
their applications.
– Note: There are many variations on ER model
• Both graphically and conceptually
• Basic constructs: entities , attributes and relationships.
• Some additional constructs: weak entities, ISA hierarchies and
aggregation.
The Enhanced / Extended ER Model

• As part of the Enhanced ER Model, along with other improvements, three


new concepts were added to the existing ER Model, they were:

▪ Generalization

▪ Specialization

▪ Aggregration
ISA relationship
Generalization. 

Generalization is like a bottom-up approach in which two or more


entities of lower level combine to form a higher level entity if they
have some attributes in common. In generalization, entities are
combined to form a more generalized entity, i.e., subclasses are
combined to make a super class.
Specialization

Specialization is opposite to Generalization. It is a top-down approach in


which one higher level entity can be broken down into two lower level
entity. In specialization, a higher level entity may not have any lower-level
entity sets, it's possible.
Aggregration
• Aggregration is a process when relation between two entities is treated as
a single entity. In the diagram above, the relationship
between Center and Course together, is acting as an Entity, which is in
relationship with another entity Visitor. Now in real world, if a Visitor or a
Student visits a Coaching Center, he/she will never enquire about the
center only or just about the course, rather he/she will ask enquire about
both.
Review of UNIT-1

▪ Database and its applications.


▪ Database vs. file system.
▪ Data Abstraction and Data Independence.
▪ Different levels of schema.
▪ Data models
▪ Database language.
▪ ER Models and mapping from E-R Model to Relational
model.
▪ Extended ER model.
End of uni-1
UNIT-2
• Relational Model

• Integrity Constraints

• Querying Relational Data

• Creating And Destroying Table /Views

• Relational Algebra

• Relational Calculus
Relational data model is the primary data model, which is used
widely around the world for data storage and processing. This
model is simple and it has all the properties and capabilities
required to process data with storage efficiency.

Concepts

• Tables − In relational data model, relations are saved in the format


of Tables. This format stores the relation among entities. A table has
rows and columns, where rows represents records and columns
represent the attributes.

• Tuple − A single row of a table, which contains a single record for


that relation is called a tuple.
• Relation instance − A finite set of tuples in the relational database
system represents relation instance. Relation instances do not have
duplicate tuples.

• Relation schema − A relation schema describes the relation name


(table name), attributes, and their names.

• Relation key − Each row has one or more attributes, known as


relation key, which can identify the row in the relation (table)
uniquely.

• Attribute domain − Every attribute has some pre-defined value


scope, known as attribute domain.
Constraints
Every relation has some conditions that must hold for it to be a
valid relation. These conditions are called Relational Integrity
Constraints.

There are three main integrity constraints

• Key constraints

• Domain constraints

• Referential integrity constraints


Key Constraints
• There must be at least one minimal subset of attributes in the
relation, which can identify a tuple uniquely. This minimal subset of
attributes is called key for that relation. If there are more than one
such minimal subsets, these are called candidate keys.

Key constraints force that −

• in a relation with a key attribute, no two tuples can have identical


values for key attributes.

• a key attribute can not have NULL values.

• Key constraints are also referred to as Entity Constraints.


Domain Constraints
• Attributes have specific values in real-world scenario. For example, age can
only be a positive integer. The same constraints have been tried to employ
on the attributes of a relation.

• Every attribute is bound to have a specific range of values. For example,


age cannot be less than zero and telephone numbers cannot contain a
digit outside 0-9.

Referential integrity Constraints


• Referential integrity constraints work on the concept of Foreign Keys. A
foreign key is a key attribute of a relation that can be referred in other
relation.

• Referential integrity constraint states that if a relation refers to a key


attribute of a relation, then that key element must exist.
Querying a Relational data
A "query" refers to the action of retrieving data from the database.

An user designs a query depending upon the requirement of data.

There are 2 ways to query a database.


1.Programatically ( using the programming syntax, keywords etc)
2.User Interface

In relational data base we can query the data either from a


relation/table or from a view.
VIEW in SQL
• Views in SQL are kind of virtual tables. A view also has rows and
columns as they are in a real table in the database.
• We can create a view by selecting fields from one or more tables
present in the database.
• A View can either have all the rows of a table or specific rows based on
certain condition.
• Thedifference between a view and a table is that views are definitions
built on top of other tables (or views), and do not hold data
themselves. If data is changing in theunderlying table, the same
change is reflected in the view.

• We can create View using CREATE VIEW statement. A View can be


created from a single table or multiple tables.
• Creating View from a single table:
Creating views from multiple tables

CREATE VIEW MarksView AS SELECT StudentDetails.NAME,


StudentDetails.ADDRESS, StudentMarks.MARKS FROM StudentDetails,
StudentMarks WHERE StudentDetails.NAME = StudentMarks.NAME;

SELECT * FROM MarksView;


DELETING VIEWS

SQL allows us to delete an existing View.


We can delete or drop a View using the DROP statement.

Syntax:
DROP VIEW view_name;

Example: DROP VIEW MarksView;


UPDATING VIEWS
There are certain conditions needed to be satisfied to update a
view.

• The SELECT statement should not have the DISTINCT keyword.


• The View should have all NOT NULL values.
• The view should not be created using nested queries or complex
queries.
• The SELECT statement which is used to create the view should
not include GROUP BY clause or ORDER BY clause.
• The view should be created from a single table. If the view is
created using multiple tables then we will not be allowed to
update the view.
CREATE OR REPLACE VIEW statement to add or remove fields
from a view.
Syntax:
CREATE OR REPLACE VIEW view_name AS SELECT
column1,coulmn2,.. FROM table_name WHERE condition;

For example, if we want to update the view MarksView and add the field AGE
to this View from StudentMarks Table, we can do this as:

CREATE OR REPLACE VIEW MarksView AS SELECT StudentDetails.NAME,


StudentDetails.ADDRESS, StudentMarks.MARKS, StudentMarks.AGE
FROM StudentDetails, StudentMarks WHERE StudentDetails.NAME =
StudentMarks.NAME;
SELECT * FROM MarksView;

Inserting a row in a view:


We can insert a row in a View in a same way as we do in a table.
We can use the INSERT INTO statement of SQL to insert a row in
a View.

Syntax:
INSERT view_name(column1, column2 , column3,..)
VALUES(value1, value2, value3..);
INSERT INTO DetailsView ( NAME, ADDRESS) VALUES("Suresh","Gurgaon");

SELECT * FROM DetailsView;

Deleting a row from a View:


DELETE FROM view_name WHERE condition;
DELETE FROM DetailsView WHERE NAME="Suresh";
Relational Query Languages

• Relational query languages uses relational algebra to break


the user requests and instruct the DBMS to execute the
requests.

• It is the language by which user communicates with the


database.

• These relational query languages can be procedural or non-


procedural.
Procedural Query Language

A procedural query language will have set of queries


instructing the DBMS to perform various transactions in the
sequence to meet the user request.

This procedural query language tells the database what is


required from the database and how to get them from the
database.

Relational algebra is a procedural query language.


Non-Procedural Query Language

• Non-procedural queries will have single query on one or more


tables to get result from the database.

• Relational Calculus is a non procedural language which informs


what to do with the tables, but doesn’t inform how to accomplish
this.

• These query languages basically will have queries on tables in the


database. In the relational database, a table is known as relation.
Relational Algebra
•It is used in the design of transactions and form the conceptual basis
for SQL.

•It consists of operators and manipulators which applied to relation


and results a relation.

•Relational algebra is a procedural query language. it is an


intermediate language.
(SQL Query-PARSER-relational algebra exp-OPTIMIZER-optimised RAE-CODE GEN-Execuable code)

• It takes one or more relations / tables and performs the operation


and produce the result.

•This result is also considered as a new table or relation.


• Relational algebra will have operators to indicate the
operations.

• This algebra can be applied on single relation – called unary or


can be applied on two tables – called binary.

• While applying the operations on the relation, the resulting


subset of relation is also known as new relation.

• There can be multiple steps involved in some of the


operations. The subsets of relations at the intermediary level
are also known as relation.
Select (σ)
• Select (σ) - This is a unary relational operation. This operation pulls
the horizontal subset (subset of rows) of the relation that satisfies
the conditions. This can use operators like <, >, <=, >=, = and != to
filter the data from the relation. It can also use logical AND, OR and
NOT operators to combine the various filtering conditions.

• This operation can be represented as below: σ p(R). Where σ is the


symbol for select operation, R represents the relation/table, and
• p is the PREDICATE/COND or the LOGIC FORMULA to get the subset.

• Let us see an example as below: σSTD_NAME = “James” (STUDENT) .


• σdept_id = 20 AND salary>=10000 (EMPLOYEE) - Selects the records from
EMPLOYEE table with department ID = 20 and employees whose
salary is more than 10000.
Project (∏)

• Project (∏) - This is a unary operator and is similar to select operation above. It
creates the subset of relation based on the conditions specified. Here, it selects
only selected columns/attributes from the relation- vertical subset of relation.. It
is denoted as below: ∏a1, a2, a3 (r)

• Where ∏ is the operator for projection, r is the relation and a1, a2, a3 are the
attributes of the relations which will be shown in the resultant subset.

• ∏std_name, address, course (STUDENT) - This will select all the records from STUDENT table
but only selected columns – std_name, address and course.

• ∏STD_ID, address, course (σ STD_NAME = “James”(STUDENT)) - this selects the record for ‘James’
and displays only std_ID, address and his course columns. Here we can see two
unary operators are combined, and it has two operations performing.
Rename (ρ)
• Rename (ρ) - This is a unary operator used to rename the tables and
columns of a relation.

• It is denoted as follows : ρ R-new(R-old)


• Where ρ is the rename operator, R-old is the existing relation
name/column name , and R-new is the new relation name.

• ρ STUDENT (STD_TABLE) – Renames STD_TABLE table to STUDENT

• Let us see another example to rename the columns of the table. If the
STUDENT table has ID, NAME and ADDRESS columns and if they have to
be renamed to STD_ID, STD_NAME, STD_ADDRESS, then we have to write
as follows.
• ρ STD_ID, STD_NAME, STD_ADDRESS(STUDENT) – It will rename the columns in the order
the names appear in the table
Union (U)
• Union (U) - It is a binary operator, which combines the tuples of two
relations. It is denoted by R U S: Where R and S are the relations and U is
the operator.       
            
• It is different from Cartesian product in:

• Union combines the tuples of two relations into one relation. Cartesian
product combines the attributes of two relations into one relation
whereas
• In Union, both relations should have same number of columns. Cartesian
product does not concentrate on number of attribute or rows. It blindly
combines the attributes.
• In Union, both relations should have same types of attributes in same
order. It need not have same number of tuples in both the relation. If
there is a duplicate tuples as a result of union, then it keeps only one
tuple. If a tuple is present in any one relation, then it keeps that tuple in
the new relation.
Cartesian product (X):
• Cartesian product (X): - This is a binary operator. It combines the tuples of
two relations into one relation.
• RXS: Where R and S are two relations and X is the operator. If relation R
has m tuples and relation S has n tuples, then the resultant relation will
have mn tuples.
• Example, if we perform Cartesian product on EMPLOYEE (5 tuples) and
DEPT relations (3 tuples), then we will have new tuple with 15 tuples.
• This set intersection
can also be written
as a combination of
set difference
operations.
R ∩ S   R-(R-S)
Assignment

• Assignment - As the name indicates, the assignment operator ‘←’ is used
to assign the result of a relational operation to temporary relational
variable.
 T ←S – denotes relation S is assigned to temporary relation T
A relational operation ∏a1, a2 (σ p (E)) with selection and projection can be
divided as below.

   T  ←  σ p (E) S ←  ∏a1, a2 (T)


Our example above in projection for getting STD_ID, ADDRESS and
COURSE for the Student ‘James’ can be re-written as below.

∏STD_ID, address, course (σ STD_NAME = “James”(STUDENT))



T  σ STD_NAME = “James”(STUDENT) and S  ∏STD_ID, address, course (T)
Division
Division Operation – Example
Relations r, s:
A B B
α 1
1
α 2
α 3 2
β 1 s
γ 1
δ 1
δ 3
δ 4
∈ 6 e.g.
∈ 1
A is customer name
β 2
B is branch-name
r ÷ s: A r 1and 2 here show two specific branch-names
(Find customers who have an account in all
α branches of the bank)
β
Another Division Example
Relations r, s:
A B C D E D E

α a α a 1 a 1
α a γ a 1 b 1
α a γ b 1 s
β a γ a 1
β a γ b 3
γ a γ a 1
γ a γ b 1
γ a β b 1
r
e.g.
r ÷ s: Students who have taken both "a”
A B C and “b” courses, with instructor
“1”
α a γ
(Find students who have taken all
γ a γ courses given by instructor 1)
Join
• The join operation is one of the most useful operations in relational
algebra and the most commonly used way to combine information from
two or more relations.
• A join can be defined as a cross product followed by selection and
projections. Joins arise much more frequently in practice than plain cross
product.
• Join is of two types: INNER JOIN , OUTERJOIN.
• Theta Join, Equijoin, and Natural Join are called inner joins.

CONDITIONAL JOINS (THETA JOIN): The most general version of join


operation accepts a join condition c and a pair of relation instances as an
argument s and returns a relation instance. If R and S are two relational
objects , then their join can be denoted as follows:

• The join is also defined as a cross-product followed by a selection.


S2: Instance of Sailors
S1: Instance of Sailors

R1: Instance of Reserves


Example:

1.

EQUI-JOIN:
A common special case of join operation is when the join
condition is consists of equalities of the form R.x=S.x .That is
equalities between two fields in R and S.
2.

Natural Join:- Equijoin on all common fields.


Without specified condition.
means the natural join of R and S.

(Natural join is a type of equi join which occurs implicitly by comparing all


the same names columns in both tables. The join result have only one
column for each pair of equally named columns.)
Example of Natural join
Outer join
More examples
Relational Calculus
• It is not like differential and integral calculus in mathematics,
but takes its names from a branch of symbolic logic termed as
predicate calculus.
• It includes variables, constants ,quantifiers ,operators and
logical connectives.
• It is declarative, It states what data has to retrieve, but not
how to retrieve.

• It has two variations


✔Tuple relational calculus
✔Domain relational calculus.
Example
Table-1: Customer
CUSTOMER NAME STREET CITY Table-2: Branch
Saurabh A7 Patiala BRANCH NAME BRANCH CITY

Mehak B6 Jalandhar ABC Patiala


Sumiti D9 Ludhiana DEF Ludhiana
Ria A5 Patiala GHI Jalandhar

Table-3: Account Table-4: Loan

ACCOUNT NUMBER BRANCH NAME BALANCE   LOAN NUMBER BRANCH NAME AMOUNT

1111 ABC 50000   L33 ABC 10000


1112 DEF 10000   L35 DEF 15000
1113 GHI 9000   L49 GHI 9000
1114 ABC 7000   L98 DEF 65000

Table-5: Borrower Table-6: Depositor

CUSTOMER NAME LOAN NUMBER CUSTOMER NAME ACCOUNT NUMBER

Saurabh L33 Saurabh 1111


Mehak L49 Mehak 1113
Ria L98 Sumiti 1114
Queries-1: Find the loan number, branch, amount of loans of
greater than or equal to 10000 amount.
ANS: {t| t ∈ loan ∧ t[amount]>=10000}
OUTPUT
LOAN NUMBER BRANCH NAME AMOUNT

L33 ABC 10000


L35 DEF 15000
L98 DEF 65000

Queries-2: Find the loan number for each loan of an amount greater or equal
to 10000.
ANS: {t| ∃ s ∈ loan(t[loan number] = s[loan number] ∧
s[amount]>=10000)}
LOAN NUMBER

L33
L35
L98
Queries-3: Find the names of all customers who have a loan and an account
at the bank.

{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name]) ∧ ∃ u ∈


depositor( t[customer-name] = u[customer-name])}
CUSTOMER NAME

Saurabh
Mehak

• Queries-4: Find the names of all customers having a loan at the “ABC”


branch.
{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name] ∧ ∃ u ∈
loan(u[branch-name] = “ABC” ∧ u[loan-number] = s[loan-number]))}

CUSTOMER NAME

Saurabh
More examples
Syntax of TRC Query: (atomic formula)
Let ‘op’ is an operator in the set {<,>,=,<=,>=,≠} and let X and Y be tuple
variables , a and b are the attributes of X and Y respectively.. Then an
Atomic formula in TRC is one of the following.

• (a1, a2, a3, … an) ϵ R, where R is a relation with n attributes; each ‘ai ’
1<=i<=n is either a constant or a variable.
• X.a op Y.b
• X op constant, or constant op X

A formula is recursively defined to be one of the following , where p and q are


themselves formulas and p(X) denotes a formula in which the variable X
appears:
• Any atomic formula
• ¬p, pɅq, pvq, or p→q
• Ǝx (p(x)), where x is a tuple variable.
• ∀x(p(x)), where x is a tuple variable.
Free and Bounded variables
• In the predicate calculus, if a variable is associated with an Existential
(Ǝ) or For All(∀) (Universal quantifier) quantifiers Is called as a
bounded variable, Otherwise, it is a Free Variable.

Example:
Domain Relational Calculus
• In contrast to tuple relational calculus, domain relational calculus used to
select a list of attributes from the relation based on the condition. It is
same as TRC, but differs by selecting the attributes rather than selecting
whole tuples.
• It uses domain variable. A domain variable is a variable that ranges over
the values in the domain of some attributes.

• A DRC query is of the form {< a1, a2, a3, … an > | P(a1, a2, a3, … an)}

• Where each ‘ai ‘ is either a domain variable or a constant and


P(a1, a2, … an) denotes DRC formula .

• The results of the query is set of all tuples (a1, a2, a3, … an) for which the
formula evaluates to true.
Syntax of DRC Query: (atomic formula)
Let ‘op’ is an operator in the set {<,>,=,<=,>=,≠} and let X and Y be domain
variables. Then an Atomic formula in DRC is one of the following.

• (a1, a2, a3, … an) ϵ R, where R is a relation with n attributes; each ‘ai ’
1<=i<=n is either a constant or a variable.
• X op Y
• X op constant, or constant op X

A formula is recursively defined to be one of the following , where p and q are


themselves formulas and p(X) denotes a formula in which the variable X
appears:

• Any atomic formula


• ¬p, pɅq, pvq, or p→q
• Ǝx (p(x)), where x is a domain variable.
• ∀x(p(x)), where x is a domain variable.
DRC query formation :: Example
• Find all sailors with a rating above 7

• Find the names of sailors who reserved boat


103.
Note:

• In terms of Expressive power both the RA and


RC are equivalent.
• That means the query which can be express by
RA can also be express by RC and vice-versa.
UNIT-3

• Basic SQL queries and examples.


• Comparison operators (<,<=,>,>=,!=).
• Logical connectivity's( AND,OR,NOT).
• Aggregate operators ( min,max,avg,count,sum).
• Null values Integrity constraints
• Nested queries and Correlated queries
• Integrity constraints in SQL Triggers
• Embedded Sql
SQL

Select:
• it is used to query the database.
• Comes alone or sometimes with where clause.
• where clause is used when the retrieval is specific.
Select tuples/columns from table name where expression;

• The expression of where clause may include one or more comparison


operators like <,>,<=,>=,..etc.

• It may include one or more logical connectivity like AND,OR,NOT etc.

• It may includes some other operator like IN,ANY,ALL,EXIST , LIKE etc.


Comparison operator
• A comparison (or relational) operator is a mathematical symbol which is
used to compare two values.
• Comparison operators are used in conditions that compares one
expression with another. The result of a comparison can be TRUE, FALSE,
or UNKNOWN (an operator that has one or two NULL expressions returns
UNKNOWN).

The following table describes different types of comparison operators -


Operator Description Operates on

Equal to. Any compatible data types


=
Greater than. Any compatible data types
>
Less than. Any compatible data types
<
Any compatible data types
>= Greater than equal to.
Any compatible data types
<= Less than equal to.
Not equal to. Any compatible data types
<>

Example: SELECT * FROM agents WHERE commission> 0.14;


The SQL EXISTS Operator
• The EXISTS operator is used to test for the existence of any record in
a subquery.
• The EXISTS operator returns true if the subquery returns one or
more records.

EXISTS Syntax

SELECT column_name(s) FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition)
;
SELECT SupplierName FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.Suppli
erID = Suppliers.supplierID AND Price < 20);
The SQL ANY and ALL Operators
• The ANY and ALL operators are used with a WHERE or HAVING
clause.
• The ANY operator returns true if any of the subquery values meet
the condition.
• The ALL operator returns true if all of the subquery values meet the
condition.

ANY SYNTAX:
SELECT column_name(s) FROM table_name WHERE column_name   ANY
(SELECT column_name FROM table_name WHERE condition);
ALL SYNTAX :
SELECT column_name(s) FROM table_name WHERE column_name ALL
(SELECT column_name FROM table_name WHERE condition);
SELECT ProductName FROM Products WHERE ProductID
= ANY (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);

SELECT ProductName FROM Products WHERE ProductID


= ALL (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);
SQL ORDER BY Clause

• SELECT returns records in no particular order.


• To ensure a specific order use the ORDER BY clause.
• ORDER BY allows sorting by one or more columns.
• Records can be returned in ascending or descending order.

SQL ORDER BY syntax

SELECT column-names FROM table-name WHERE condition


ORDER BY column-names;
Example:
SELECT CompanyName, ContactName, City, Country FROM Supplier ORDER
BY CompanyName;

The default sort order is ascending, that is, low-high or a-z. 


----------------------------------------------------------------------------------------------

SELECT CompanyName, ContactName, City, Country


FROM Supplier ORDER BY CompanyName DESC

The keyword DESC denotes descending, i.e., reverse order. 


------------------------------------------------------------------------------------------------
SELECT FirstName, LastName, City, Country FROM Customer
ORDER BY Country, City
Q: List all suppliers in the india, brazil, canara ordered by COUNTRY, 
then by CITY in reverse order;

SELECT Id, CompanyName, City, Country FROM Supplier WHERE Country


IN(‘brazil', ‘canara', 'india') ORDER BY Country ASC, city DESC;

Id CompanyName City Country

101 y Ut brazil

102 g TU brazil

103 t Da canara

104 y Bb canara

105 u Um india

106 z Mu india
Q: Show all orders, sorted by total amount, the largest first, within each year 

SELECT Id, OrderDate, CustomerId, TotalAmount


FROM [Order] ORDER BY YEAR(OrderDate) ASC, TotalAmount DESC;
GROUP BY clause
• The GROUP BY clause groups records into summary rows.
• GROUP BY returns one records for each group.
• GROUP BY typically also involves aggregates: COUNT, MAX, SUM, AVG, etc.
• GROUP BY can group by one or more columns.
SYNTAX:

SELECT column-names FROM table-name WHERE condition


GROUP BY column-names;

The general syntax with ORDER BY is:

SELECT column-names FROM table-name WHERE condition


GROUP BY column-names ORDER BY column-names;
Q: List the number of customers in each country ?

SELECT COUNT(Id), Country FROM Customer GROUP BY Country;


Count Country
3 Argentina
2 Austria
2 Belgium
9 Brazil
3 Canada

Q:  List the number of customers in each country sorted high to low ?

SELECT COUNT(Id), Country FROM Customer GROUP BY Country ORDER BY


COUNT(id) DESC;

Count Country
13 USA
11 France
11 Germany
9 Brazil
7 UK
Q: List fname,lname and the total amount ordered for each customer ?

SELECT SUM( O.TotalPrice), C.FirstName, C.LastName


FROM Order O JOIN Customer C ON O.c_cid = C.cid
GROUP BY C.FirstName, C.LastName ORDER BY SUM(O.TotalPrice) DESC;

Sum FirstName LastName

117483.39 HARSIT CHAWLA

115673.39 MANOJ PRABHAKAR

113236.68 ANJU MARTIN

57317.39 BINAY PRADHAN

52245.9 RAJU RATHORE

34101.15 BIJAY TAGORE

32555.55 HARSIT PANDIYA


Aggregate functions
• In database management an aggregate function is a function where the
values of multiple rows are grouped together as input on certain criteria
to form a single value of more significant meaning.
• The ISO standard defines five (5) aggregate functions namely;

1) COUNT()
2) SUM()
3) AVG()
4) MIN()
5) MAX ()
• COUNT():The COUNT function returns the total number of values in the
specified field. It works on both numeric and non-numeric data types. All
aggregate functions by default exclude nulls values before working on the
data.
• COUNT (*) is a special implementation of the COUNT function that returns the
count of all the rows in a specified table. COUNT (*) also considers Nulls and
duplicates.

• MIN ():The MIN function returns the smallest value in the specified table field.

• MAX (): It returns the largest value from the specified table field.

• SUM ():It  returns the sum of all the values in the specified column. SUM
works on numeric fields only. Null values are excluded from the result
returned.
SELECT SUM(column_name) FROM table_name WHERE condition;
• AVG(): It returns the average of the values in a specified column. it works only
on numeric data types.
Nested Queries in SQL
In nested queries, a query is written inside a query. The result of inner query
is used in execution of outer query. We will use STUDENT, COURSE,
STUDENT_COURSEtables for understanding nested queries.

STUDENT

S_ID S_NAME S_ADDRESS S_PHONE S_AGE


S1 RAM DELHI 9455123451 18
S2 RAMESH GURGAON 9652431543 18
S3 SUJIT ROHTAK 9156253131 20
S4 SURESH DELHI 9156768971 18

COURSE STUDENT_COURSE
C_ID C_NAME S_ID C_ID
C1 DSA S1 C1
C2 Programming S1 C3
C3 DBMS S2 C1
S3 C2
S4 C2
S4 C3
There are mainly two types of nested queries:

• Independent Nested Queries: In independent nested queries, query


execution starts from innermost query to outermost queries. The
execution of inner query is independent of outer query, but the result of
inner query is used in execution of outer query. Various operators like IN,
NOT IN, ANY, ALL etc are used in writing independent nested queries. 

• IN: If we want to find out S_ID who are enrolled in C_NAME ‘DSA’ or


‘DBMS’, we can write it with the help of independent nested query and IN
operator. From COURSE table, we can find out C_ID for C_NAME ‘DSA’ or
DBMS’ and we can use these C_IDs for finding S_IDs
from STUDENT_COURSE TABLE.
STEP 1: Finding C_ID for C_NAME =’DSA’ or ‘DBMS’
Select C_ID from COURSE where C_NAME = ‘DSA’ or C_NAME = ‘DBMS’
 
STEP 2: Using C_ID of step 1 for finding S_ID

Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME = ‘DSA’ or C_NAME=’DBMS’);

The inner query will return a set with members C1 and C3 and outer query
will return those S_IDs for which C_ID is equal to any member of set (C1
and C3 in this case). So, it will return S1, S2 and S4.
Note: If we want to find out names of STUDENTs who have either enrolled in
‘DSA’ or ‘DBMS’, it can be done as:
Select S_NAME from STUDENT where S_ID IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’ or C_NAME=’DBMS’));
 
NOT IN: If we want to find out S_IDs of STUDENTs who have neither
enrolled in ‘DSA’ nor in ‘DBMS’, it can be done as:
Select S_ID from STUDENT where S_ID NOT IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’ or C_NAME=’DBMS’));

• The innermost query will return a set with members C1 and C3. Second
inner query will return those S_IDs for which C_ID is equal to any member
of set (C1 and C3 in this case) which are S1, S2 and S4. The outermost
query will return those S_IDs where S_ID is not a member of set (S1, S2
and S4). So it will return S3.
Co-related Nested Queries:

• In co-related nested queries, the output of inner query depends on the


row which is being currently executed in outer query. e.g.; If we want to
find out S_NAME of STUDENTs who are enrolled in C_ID ‘C1’, it can be
done with the help of co-related nested query as:

Select S_NAME from STUDENT S where EXISTs ( select *


from STUDENT_COURSE SC where S.S_ID=SC.S_ID and SC.C_ID=’C1’);

• For each row of STUDENT S, it will find the rows


from STUDENT_COURSEwhere S.S_ID = SC.S_ID and SC.C_ID=’C1’. If for
a S_ID from STUDENT S, atleast a row exists in STUDENT_COURSE SC
with C_ID=’C1’, then inner query will return true and
corresponding S_ID will be returned as output.
JOIN
• An SQL JOIN clause combines rows from two or more tables. It creates a
set of rows in a temporary table.
• A JOIN works on two or more tables if they have at least one common
field and have a relationship between them.
• JOIN keeps the base tables (structure and data) unchanged.

SQL JOIN
• EQUI JOIN
• NON-EQUIJOIN
• INNER JOIN
• NATURAL JOIN
• CROSS JOIN
• OUTER JOIN
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
EQUI JOIN

SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the
associated tables. An equal sign (=) is used as comparison operator in the where
clause to refer equality.
SYNTAX:
SELECT column_list FROM table1, table2 WHERE table1.column_name = table2.column_name;
OR
SELECT * FROM table1 JOIN table2 ON (join_condition);
Non Equi join
• The SQL NON EQUI JOIN uses comparison operator instead of the equal
sign like >, <, >=, <= along with conditions.

OR THRU ALTERNATE SYNTAX:


Inner Join
The INNER JOIN selects all rows from both participating tables as long as there is
a match between the columns.
An SQL INNER JOIN is same as JOIN clause, combining rows from two or more
tables.
Syntax:
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name =
table2.column_name;
NATURAL JOIN
• The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a
way that, columns with the same name of associated tables will appear
once only.
Natural Join: Guidelines
• - The associated tables have one or more pairs of identically named
columns. The columns must be the same data type.
- Don’t use ON clause in a natural join.

Syntax: SELECT * FROM table1 NATURAL JOIN table2;


Cross join
• The SQL CROSS JOIN produces a result set which is the number of rows in
the first table multiplied by the number of rows in the second table .
• If no WHERE clause is used along with CROSS JOIN. This kind of result is
called as Cartesian Product.
• If WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN.
Syntax: SELECT * FROM table1 CROSS JOIN table2;
OUTER JOIN
The SQL OUTER JOIN returns all rows from both the participating
tables which satisfy the join condition along with rows which do not
satisfy the join condition.
The SQL OUTER JOIN operator (+) is used only on one side of the
join condition only.
Syntax: Select * FROM table1, table2 WHERE conditions [+];
Left Outer join: Fetches all records from Left side table irrespective of the match
in Right table.
Syntax : SELECT * FROM table1 LEFT [ OUTER ] JOIN table2 ON table1.c_name = table2.c_name;
Right Outer join: Fetches all records from Right side table irrespective of the
match in Left table.
Syntax : SELECT * FROM table1 RIGHT [ OUTER ] JOIN table2 ON table1.c_name = table2.c_name;
Full outer Join
• It combines the results of both left It combines the results of both left and 
right outer joins and returns all (matched or unmatched) rows from both
the tables.

Syntax:
SELECT * FROM table1 FULL [OUTER] JOIN table2 ON table1.cname=table2.cname;
PL/SQL
• The PL/SQL programming language was developed by Oracle Corporation
in the late 1980s as procedural extension language for SQL and the Oracle
relational database.

Following are certain notable facts about PL/SQL −

• PL/SQL is a completely portable, high-performance transaction-processing


language.
• PL/SQL provides a built-in, interpreted and OS independent programming
environment.
• PL/SQL can also directly be called from the command-line SQL*Plus
interface.
• Direct call can also be made from external programming language calls to
database.
• PL/SQL's general syntax is based on that of ADA and Pascal programming
language.
Features of PL/SQL

• PL/SQL is tightly integrated with SQL.


• It offers extensive error checking.
• It offers numerous data types.
• It offers a variety of programming structures.
• It supports structured programming through functions and
procedures.
• It supports object-oriented programming.
• It supports the development of web applications and server pages.
• Basic Syntax of PL/SQL which is a block-structured language;
• This means that the PL/SQL programs are divided and written in logical
blocks of code.
• Each block consists of three sub-parts 
• Every PL/SQL statement ends with a semicolon (;).
• PL/SQL blocks can be nested within other PL/SQL blocks using BEGIN and END.

Following is the basic structure of a PL/SQL block −

DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
PL/SQL -BLOCKS/SUB BLOCKS

PL/SQL-ARRAYS

PL/SQL-PROCEDURES

PL/SQL-FUNCTIONS

PL/SQL-CURSORS

PL/SQL-RECORDS

PL/SQL-TRIGGERS

PL/SQL- PACKAGES

PL/SQL-COLLECTIONS

PL/SQL-TRANSACTIONS

PL/SQL-OBJECT ORIENTED
Trigger
Triggers are stored programs, which are automatically executed or fired
when some events occur.
Triggers are, in fact, written to be executed in response to any of the
following events −

• A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).

• A database definition (DDL) statement (CREATE, ALTER, or DROP).

• A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or


SHUTDOWN).

• Triggers can be defined on the table, view, schema, or database with


which the event is associated.
Benefits of Triggers
Triggers can be written for the following purposes

• Generating some derived column values automatically.

• Enforcing referential integrity.

• Event logging and storing information on table access.

• Auditing.

• Synchronous replication of tables.

• Imposing security authorizations.

• Preventing invalid transactions.


Syntax to create Trigger

CREATE [OR REPLACE ] TRIGGER trigger_name


{BEFORE | AFTER|insead of }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name] ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW] WHEN (condition)
DECLARE
Declaration-statements
BEGIN Executable-statements
EXCEPTION Exception-handling-statements (if any)
END;
Where,
• CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an
existing trigger with the trigger_name.
• {BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be
executed. The INSTEAD OF clause is used for creating trigger on a view.
• {INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML operation.
• [OF col_name] − This specifies the column name that will be updated.
• [ON table_name] − This specifies the name of the table associated with
the trigger.
• [REFERENCING OLD AS o NEW AS n] − This allows you to refer new and
old values for various DML statements, such as INSERT, UPDATE, and
DELETE.
• [FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will
be executed for each row being affected. Otherwise the trigger will
execute just once when the SQL statement is executed, which is called a
table level trigger.
• WHEN (condition) − This provides a condition for rows for which the
trigger would fire. This clause is valid only for row-level triggers.
• The following program creates a row-level trigger for the customers table.
• And that would fire for INSERT or UPDATE or DELETE operations
performed on the CUSTOMERS table.
• This trigger will display the salary difference between the old values and
new values .
CREATE OR REPLACE TRIGGER display_salary_changes BEFORE DELETE
OR INSERT OR UPDATE ON customers FOR EACH ROW WHEN
(NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW. salary - :OLD. salary;
dbms_output.put_line('Old salary: ' || :OLD. salary);
dbms_output.put_line('New salary: ' || :NEW. salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;

• When the above code is executed at the SQL prompt, the output
will be −
O/P: Trigger created.
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (7, 'Kriti', 22,
'HP', 7500.00 );
When a record is created in the CUSTOMERS table, the above create
trigger, display_salary_changes will be fired and it will display the following result .

Old salary:
New salary: 7500
Salary difference:
Because this is a new record, old salary is not available and the above result comes as
null.
Let us now perform one more DML operation on the CUSTOMERS table.
The UPDATE statement will update an existing record in the table −
UPDATE customers SET salary = salary + 500 WHERE id = 2;
• When a record is updated in the CUSTOMERS table, the above create
trigger, display_salary_changes will be fired and it will display the following result .
• Old salary: 1500
• New salary: 2000
• Salary difference: 500
Embedded SQL
Embedded SQL is a method of inserting inline SQL statements or queries into
the code of a programming language, which is known as a host language.

Because the host language cannot parse SQL, the inserted SQL is parsed by an
embedded SQL preprocessor.

Embedded SQL is a robust and convenient method of combining the


computing power of a programming language with SQL's specialized data
management and manipulation capabilities.
The C programming language is commonly used for embedded SQL
implementation.
For example, a commercial bank's information system (IS) has a front-end
user interface created in the C language, and the IS interfaces with a back-
end Oracle DB database. 

Example: SELECT 0.2*SALE_AMOUNT FROM TOTAL_SALES WHERE


SALE_DATE='MM/DD'YYYY' AND AGENT_NO=xx
Example:
int emp_number;
char temp[20];
VARCHAR emp_name[20];

/* get values for input host variables */


printf("Employee number? ");
gets(temp);
emp_number = atoi(temp);

/*atoi(const char *str) converts the string argument str to an integer (type int)*/
printf("Employee name? ");
gets(emp_name.arr);
emp_name.len = strlen(emp_name.arr);
EXEC SQL INSERT INTO EMP (EMPNO, ENAME) VALUES (:emp_number, :emp_name);
Null values in SQL
A field with a NULL value is a field with no value. If a new record is inserted /
updated with out any value to a particular field, then its value automatically
considered as NULL. A NULL value is different from a zero value or a field that
contains spaces.

How to Test for NULL Values?


• Through IS NULL and IS NOT NULL operators
The IS NULL operator is used to test for empty values (NULL values).
IS NULL -SYNTAX: SELECT column_names FROM table_name
WHERE column_name IS NULL;
Ex: SELECT Name, Address FROM Customers WHERE Address IS NULL;

The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
IS NOT NULL-SYNTAX: SELECT column_names FROM table_name
WHERE column_name IS NOT NULL;
Ex: SELECT Name, Address FROM Customers
WHERE Address IS NOT NULL;
End of 3rd unit.

You are also suggested to read the text book


given below.

You might also like