Database Management System: Dr. Pawan Singh Mehra, Department of CSE
Database Management System: Dr. Pawan Singh Mehra, Department of CSE
Database Management System: Dr. Pawan Singh Mehra, Department of CSE
1
Contents of Module-2
Integrity constraints: Keys constraint, domain
By Dr. Pawan Singh Mehra
2
Integrity Constraints
Constraints: It is the restriction imposed on the relation to
By Dr. Pawan Singh Mehra
3
Types of Integrity Constraints
4
Entity Integrity Constraints
It ensures that the Primary Key value should not be NULL.
By Dr. Pawan Singh Mehra
A table can contain a null value other than the primary key field.
5
Domain Integrity Constraints
It states that the domain or type for an attribute must be within
By Dr. Pawan Singh Mehra
its domain.
for example, integer types, character types, date/time types
Declaring an attribute to be of a particular domain acts as a
constraint on the values that it can take.
Roll_No(PK) Name Aadhar_No Email_ID Roll_No Name Aadhar_No Email_ID
2K21CO01 Mohan 88213211 m@gmail.com 2K21CO01 Mohan 88213211 m@gmail.com
2K21EC02 12345 28341212 r@gmail.com 2K21EC02 Rohan 28341212 r@gmail.com
Abraham ABC a@gmail.com 2K21IT01 Abraham 65762231 a@gmail.com
2K21SW04 Vernika 98322212 v@gmail.com 2K21SW04 Vernika 98322212 v@gmail.com
NOT ALLOWED FOLLOWS DOMAIN INTEGRITY CONSTRAINT
6
Key Constraints
It ensures that every tuple in the relation should be unique
By Dr. Pawan Singh Mehra
7
Referential Integrity Constraints
It is pertinent to Foreign Key.
By Dr. Pawan Singh Mehra
9
Query Language
It is language in which user requests information from the database.
For example: SQL
By Dr. Pawan Singh Mehra
Categories of languages
Procedural
User tells what data to be retrieved from DB and how to
retrieve it
Non-procedural, or declarative
User tells what data to be retrieved from DB and but does not
tell how to retrieve it.
“Pure” languages: form underlying basis of query languages
Relational Algebra
Relational Calculus (Tuple relational calculus, Domain relational
calculus 10
Relational Algebra
Procedural language
Six basic operators
By Dr. Pawan Singh Mehra
select:
project:
union:
set difference: –
Cartesian product: x
rename:
The operators take one or two relations as inputs and produce a new
relation as a result.
11
Select Operation()
It selects tuples(rows) that satisfy the given condition from a relation
Notation: (CONDITION)(name of relation).
By Dr. Pawan Singh Mehra
The condition may use logic connectives like AND(˄), OR(˅) , NOT(!)
and relational operators like =, >, <, ≥, ≤, ≠ etc.
Intersection (∩)
Set Difference (-)
Set operations are performed on two relations which are
compatible
Two relations should be compatible if
Both the relations should have same number of
attributes(columns)
The attributes should have same domain(type)
Duplicate tuples are removed automatically
Union Operation()
Notation: r s
Defined as: r s = {t | t r or t s}
By Dr. Pawan Singh Mehra
For r s to be valid.
r, s must have the same arity (same number of attributes)
The attribute domains must be compatible (example: 2nd column
of r deals with the same type of values as does the 2nd column of s)
A B
A B A B
For r – s to be valid.
r, s must have the same arity (same number of attributes)
The attribute domains must be compatible (example: 2nd column
of r deals with the same type of values as does the 2nd column of s)
A B A B
A B
Relation r Relation s 16
Intersection Operation(∩)
Notation: r ∩ s = r – (r – s)
Defined as: r ∩ s = {t | t r AND t s}
By Dr. Pawan Singh Mehra
For r ∩ s to be valid.
r, s must have the same arity (same number of attributes)
The attribute domains must be compatible (example: 2nd column
of r deals with the same type of values as does the 2nd column of s)
A B A B A B
Expression: A,C (r) ∩ A,C (s) Output:
1 2 2
2 3
1
Relation r Relation s 17
Cartesian Product Operation(×)
Notation: r x s
By Dr. Pawan Singh Mehra
A B C D E
1 10 a
A B C D E 1 10 a
Expression: A,B,C,D,E (r x s) 1 20 b
1 10 a 1 10 b
2
10 a Output: 2 10 a
20 b 2 10 a
10 b 2 20 b
18
Relation r Relation s 2 10 b
Rename Operation()
Allows us to rename
By Dr. Pawan Singh Mehra
Relation
Attributes
Relation and attributes both
Notation: x (E) returns the expression E under the name X
Example 1: rename the relation Student as Male Student and the
attributes of Student – RollNo, SName as (Sno, Name)
ρMaleStudent(Sno,Name)πRollNo,SName(σCondition(Student))
19
Additional Operations
Inner Join
Theta Join
By Dr. Pawan Singh Mehra
Equi-Join
Natural Join
Outer Join
Left Outer Join
Right Outer
Full Outer Join
Aggregate Functions
Division
20
Join
Notation:
Join is an additional/derived operator which simplify the
By Dr. Pawan Singh Mehra
queries.
It is a combination of a Cartesian Product followed by a
selection process
Join= Cartesian Product + Selection
A join operation pairs two tuples from different relations, if
and only if the given condition is satisfied.
A Condition B = condition(A x B)
21
Inner Join
An inner join includes only those tuples that satisfy the
By Dr. Pawan Singh Mehra
22
Theta Join(Ɵ)
It combines tuples from different relations provided they
satisfy the theta(Ɵ) condition.
By Dr. Pawan Singh Mehra
23
Theta Join(Ɵ)
A B C C D E
By Dr. Pawan Singh Mehra
A 4 6 a
B 3
r R.C>S.C s
2 b
C 5 4 Ω c
Ω D 7 8 d
r s
A B r.C s.C D E
A 4 2 b
B 3 2 b
C 5 2 b
C 5 4 Ω c
Ω D 7 6 a
Ω D 7 2 b
Ω D 7 4 Ω c
Resultant table 24
Equi Join
When a theta join uses only equivalence(=) condition, it is called Equi-
Join
By Dr. Pawan Singh Mehra
25
Equi Join
A B C C D E
By Dr. Pawan Singh Mehra
A 4 4 a
B 3
r R.C=S.C s
2 b
C 5 5 Ω c
Ω D 7 7 d
7 a
r
s
A B r.C s.C D E
A 4 4 a
C 5 5 Ω c
Ω D 7 7 d
Ω D 7 7 a
Resultant relation
26
Natural Join
Notation: r s
Let r and s be relations on schemas R and S respectively.
By Dr. Pawan Singh Mehra
A B C D B D E A B C D E
1 a 1 a 1 a
2 a 3 a r s 1 a
4 b 1 a 1 a
1 a 2 b 1 a
2 b 3 b 2 b
r s Resultant relation
28
Outer Join
An extension of the join operation that avoids loss of information.
By Dr. Pawan Singh Mehra
Computes the join and then adds tuples from one relation that does not
match tuples in the other relation to the result of the join.
Uses null values:
null signifies that the value is unknown or does not exist
29
Left Outer Join
Relation loan Relation borrower
loan_number branch_name amount customer_name loan_number
By Dr. Pawan Singh Mehra
G1 ,G 2 ,,G n
F ( A ),F
1 1 2 ( A2 ,,Fn ( An )
(E )
E is any relational-algebra expression
G1, G2 …, Gn is a list of attributes on which to group (can be empty)
Each Fi is an aggregate function
Each Ai is an attribute name 33
Aggregate Functions/Operation
Relation r
By Dr. Pawan Singh Mehra
A B C
7 sum(c )
Expression: g sum(c) (r)
7
27
3
10
34
Aggregate Functions/Operation
Relation account grouped by branch-name:
By Dr. Pawan Singh Mehra
branch_name sum(balance)
Perryridge 1300
Expression: branch_name g sum(balance) (account) Brighton 1500
Redwood 700
Relation account grouped by branch-name
35
Aggregate Functions/Operation
Result of aggregation does not have a name
Can use rename operation to give it a name
By Dr. Pawan Singh Mehra
36
Division Operation
Notation: r s
By Dr. Pawan Singh Mehra
Suited to queries that include the phrase “for all”, “for every”, at every, at all etc.
Division operator can be applied if and only if “Attributes of B is proper
subset of Attributes of A”
Let r and s be relations on schemas R and S respectively where
R = (A1, …, Am , B1, …, Bn )
S = (B1, …, Bn)
The result of r s is a relation on schema
R – S = (A1, …, Am)
r s = { t | t R-S (r) u s ( tu r ) }
Where tu means the concatenation of tuples t and u to produce a single tuple
37
Division Operation Example
A B
By Dr. Pawan Singh Mehra
1
2 B
3 A
1
rs
1 1
1 2
3
4 s
6 Resultant relation
1
2
r
Division Operation Example
A B C D E D E
By Dr. Pawan Singh Mehra
a a 1 a 1
a a 1 b 1
a b 1
a a 1
s A B C
a b 3
rs a
a
a a 1
a b 1
a b 1
Resultant relation
39
Query Examples
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
By Dr. Pawan Singh Mehra
Query: Find the loan number for each loan of an amount greater than $1200
Expression: loan_number (amount > 1200 (loan))
Query: Find the names of all customers who have a loan, an account, or both, from the
bank
Expression: customer_name (borrower) customer_name (depositor)
40
Query Examples
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
By Dr. Pawan Singh Mehra
Query: Find the names of all customers who have a loan at the Perryridge branch but do not have
an account at any branch of the bank.
Expression: customer_name (branch_name = “Perryridge”
(borrower.loan_number = loan.loan_number(borrower x loan))) – customer_name(depositor)
41
Relational Calculus
It is a non procedural language/declarative language
It tells what to do but no description about how to do
By Dr. Pawan Singh Mehra
42
Tuple Relational Calculus (TRC)
It is used for selecting the tuples in a relation that satisfy the given
condition(or predicate).
By Dr. Pawan Singh Mehra
43
Tuple Relational Calculus (TRC) Predicates
Set of attributes and constants
Set of comparison operators: =, >, <, ≥, ≤, ≠
By Dr. Pawan Singh Mehra
44
Tuple Relational Calculus (TRC)
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
By Dr. Pawan Singh Mehra
Query: Find the loan-number, branch-name and amount for all loans of over >
$2000.
Sol: {t | t ϵ loan t[amount]>2000 }
Query: Find the loan number for each loan of an amount greater than 2000
Sol: {t | Ǝs ϵ loan (t[loan-number]=s[loan-number] s[amount]>2000) }
45
Tuple Relational Calculus (TRC)
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
By Dr. Pawan Singh Mehra
47
TRC-Safety of Expression
It is possible to write tuple calculus expressions that generate
infinite relations.
By Dr. Pawan Singh Mehra
48
Domain Relational Calculus (DRC)
It is nonprocedural query language
By Dr. Pawan Singh Mehra
Query: Find the loan-number, branch-name and amount for all loans of over >
$2000.
Sol: { l, b, a | l, b, a loan a > 1200}
Query: Find the names of all customers who have a loan of over $1200
Sol: { c | l, b, a ( c, l borrower l, b, a loan a > 1200)}
50
Domain Relational Calculus (DRC)
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
By Dr. Pawan Singh Mehra
51
Domain Relational Calculus (DRC)
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
By Dr. Pawan Singh Mehra
52
DRC-Safety of Expression
It is possible to write domain relational calculus expressions that generate
infinite relations.
By Dr. Pawan Singh Mehra
56
Commonly used Domain Types in SQL
char(n) : Fixed length character string, with user-specified length
n.
By Dr. Pawan Singh Mehra
Example:
CREATE DATABASE Test;
58
DDL- CREATE COMMAND
Syntax for creating table:
CREATE TABLE <TABLE_NAME>
By Dr. Pawan Singh Mehra
(
column_name1 datatype1,
column_name2 datatype2,
column_name3 datatype3,
column_name4 datatype4
);
Example:
CREATE TABLE branch
(
branch_name char(15),
branch_city char(30),
assets integer
); 59
DDL- CREATE COMMAND
Example
CREATE TABLE customer
By Dr. Pawan Singh Mehra
61
DDL- ALTER COMMAND
Syntax to Alter a Table (Adding Columns):
ALTER TABLE <table name>
By Dr. Pawan Singh Mehra
ADD
(
{<column name> <data type> [(<size>)]
[DEFAULT <value>]
{[[CONSTRAINT <constraint name>] <constraint definition>]} [ … ]
}
[ , …]
[ , <table level constraints> ] [ … ]
);
62
DDL- ALTER COMMAND
Example to Alter a Table (Adding Columns):
ALTER TABLE customer
By Dr. Pawan Singh Mehra
ADD
( customer_phone VARCHAR2(30) NOT NULL,
customer_email VARCHAR2(30) UNIQUE
);
63
DDL- ALTER COMMAND
Syntax to Alter a Table (Modifying Columns):
ALTER TABLE <table name>
By Dr. Pawan Singh Mehra
MODIFY
(
{<column name> <data type> [(<size>)]
[DEFAULT <value>]
{[[CONSTRAINT <constraint name>] <constraint definition>]} [ … ]
}
[ , …]
);
64
DDL- ALTER COMMAND
Example to Alter a Table (Modifying Columns):
ALTER TABLE customer
By Dr. Pawan Singh Mehra
MODIFY
( customer_name VARCHAR2(40) NOT NULL,
customer_email VARCHAR2(50)
);
65
DDL- ALTER COMMAND
Syntax to Alter a Table (Dropping Columns):
ALTER TABLE <table name>
By Dr. Pawan Singh Mehra
Example:
ALTER TABLE customer
DROP COLUMN customer_email;
66
DDL- ALTER COMMAND
Syntax to Alter a Table (Renaming table):
ALTER TABLE <table name>
By Dr. Pawan Singh Mehra
Example:
ALTER TABLE customer
RENAME TO customer_info;
67
DDL- ALTER COMMAND
Syntax to Alter a Table (Renaming Columns):
ALTER TABLE <table name>
By Dr. Pawan Singh Mehra
RENAME COLUMN
<old_column_name> TO < new_column_name >;
Example:
ALTER TABLE customer
RENAME COLUMN
customer_income TO cust_income;
68
DDL- DROP COMMAND
Description: DROP command completely removes a table from
the database. This command will also destroy the table structure
By Dr. Pawan Singh Mehra
Syntax:
DROP TABLE <table name>;
Example:
DROP TABLE customer;
69
DDL- TRUNCATE COMMAND
Description: It removes all the records from a table. But this
command will not destroy the table's structure. When we use
By Dr. Pawan Singh Mehra
Syntax :
TRUNCATE TABLE <table name> ;
Example:
TRUNCATE TABLE customer;
70
DML-INSERT COMMAND
Description: It used to insert data into a table.
By Dr. Pawan Singh Mehra
Example:
INSERT INTO customer VALUES (1, ‘Mr. Ashok’, ‘01-Jan-1970’, 10000, ‘P’);
Note: The columns which are not mentioned in Insert command will get
NULL values by default.
71
DML-SELECT COMMAND
Description: It is used to fetch the data from a database table which
returns this data in the form of a result table. These result tables are called
By Dr. Pawan Singh Mehra
result-sets.
Syntax :
SELECT column1, column2, columnN
FROM table_name;
Example:
SELECT * FROM customer;
Syntax :
SELECT column1, column2, columnN
FROM table_name
WHERE [condition];
Example:
SELECT customer_id, customer_name FROM customer WHERE
customer_id=1;
Note: The WHERE clause is not only used in SELECT statements, it is also
used in UPDATE, DELETE, etc.! 73
DML-UPDATE COMMAND
Description: it is used to update any record of data in a table.
Syntax to Update Record:
By Dr. Pawan Singh Mehra
74
DML-UPDATE COMMAND
Example:
UPDATE customer SET customer_income = 15000;
By Dr. Pawan Singh Mehra
UPDATE customer
SET customer_income = customer_income * 1.1, customer_type = ‘P’
WHERE customer_id = 1;
UPDATE customer
SET customer_income = (SELECT customer_income * 1.1
FROM customer
WHERE customer_id = 1)
WHERE customer_id = 1;
75
DML-DELETE COMMAND
Description: It deletes rows in a table without deleting the table. This
means that the table structure, attributes, and indexes will be intact.
By Dr. Pawan Singh Mehra
Example:
DELETE FROM customer;
DELETE customer;
Syntax :
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
Example:
GRANT SELECT ON employee TO user1;
77
DCL-REVOKE COMMAND
Description: It removes user access rights or privileges to the database
objects
By Dr. Pawan Singh Mehra
Syntax :
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name};
Example:
REVOKE SELECT ON employee FROM user1;
78
TCL-COMMIT COMMAND
Description: It is used to save the changes permanently into the database
By Dr. Pawan Singh Mehra
Syntax :
COMMIT;
Example:
DELETE FROM EMPLOYEES
WHERE AGE = 30;
COMMIT;
79
TCL-SAVEPOINT COMMAND
Description: It is is a point in a transaction when you can roll the
transaction back to a certain point without rolling back the entire
By Dr. Pawan Singh Mehra
transaction.
Syntax :
SAVEPOINT Savepoint_name;
Example:
INSERT INTO Teachers VALUES (4, 'CYPRESS', 'MICHEAL');
SAVEPOINT s;
80
TCL-ROLLBACK COMMAND
Description: It is used to undo transactions that have not already been saved to the
database. This command can only be used to undo transactions since the last COMMIT
By Dr. Pawan Singh Mehra
Syntax :
ROLLBACK ;
or
ROLLBACK TO <savepoint_name>
Example:
INSERT INTO teachers VALUES (4, 'CYPRESS', 'MICHEAL');
SAVEPOINT s;
INSERT INTO teachers VALUES (5, 'PYTHON', 'STEVE');
INSERT INTO teachers VALUES (6, 'PYTEST', 'ARNOLD');
ROLLBACK TO s; 81
SQL- Distinct
Description: It is used with SELECT Command. Inside a table, a
column often contains many duplicate values; and sometimes
By Dr. Pawan Singh Mehra
Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;
Example:
SELECT DISTINCT Country FROM Customers;
82
SQL- Group By
Description: is used in collaboration with the SELECT statement to arrange
identical data into groups. This GROUP BY clause follows the WHERE clause
By Dr. Pawan Singh Mehra
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example:
SELECT * FROM Customers ORDER BY Country;
SELECT * FROM Customers ORDER BY Country DESC;
SELECT * FROM Customers ORDER BY Country ASC, CustomerName DESC;
84
SQL- Having
Description: It enables you to specify conditions that filter which group results
appear in the results .
By Dr. Pawan Singh Mehra
The WHERE clause places conditions on the selected columns, whereas the
HAVING clause places conditions on groups created by the GROUP BY clause.
The HAVING clause must follow the GROUP BY clause in a query and must
also precede the ORDER BY clause if used.
Syntax:
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
85
SQL- Having
Example:
SELECT COUNT(CustomerID), Country
By Dr. Pawan Singh Mehra
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
87
Aggregate Functions- avg()
Description: It returns the average value of a numeric column
By Dr. Pawan Singh Mehra
Syntax:
SELECT AVG(column_name)
FROM table_name
WHERE condition;
Example:
SELECT AVG(Price)
FROM Products;
88
Aggregate Functions- count()
Description: It returns the number of rows in a column or columns
By Dr. Pawan Singh Mehra
Syntax:
SELECT COUNT (column_name) FROM table_name
WHERE condition;
Example:
SELECT COUNT(*)FROM Products;
SELECT COUNT(ProductID) FROM Products;
89
Aggregate Functions- sum ()
Description: It returns the total sum of a numeric column
By Dr. Pawan Singh Mehra
Syntax:
SELECT SUM (column_name)
FROM table_name
WHERE condition;
Example:
SELECT SUM(Quantity)
FROM OrderDetails;
90
Aggregate Functions- min ()
Description: It returns the smallest value of a numeric column
By Dr. Pawan Singh Mehra
Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;;
Example:
SELECT MIN(Price) FROM Products;
SELECT MIN(Price) AS SmallestPrice FROM Products
91
Aggregate Functions- max ()
Description: It returns the maximum value of a numeric column
By Dr. Pawan Singh Mehra
Syntax:
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Example:
SELECT MAX(Price) FROM Products;
SELECT MAX(Price) AS LargestPrice FROM Products;
92
Set Operations in SQL
Set operators are used to join the results of two (or more)
By Dr. Pawan Singh Mehra
93
Set Operations – UNION ALL
Description: It gives the result set without removing duplication and
By Dr. Pawan Singh Mehra
Syntax:
SELECT expression_1, expression_2, ... , expression_n FROM table_1
UNION ALL
SELECT expression_1, expression_2, ... , expression_n FROM table_2
Example:
SELECT * FROM comp1_employees
UNION ALL
SELECT * FROM comp2_employees;
94
Set Operations – UNION
Description: It combines the results of two or more SELECT statements.
The number of columns and the data type must be the same in both tables.
By Dr. Pawan Singh Mehra
Example:
SELECT * FROM comp1_employees
UNION
SELECT * FROM comp2_employees;
95
Set Operations – INTERSECT
Description: It allows us to find the results that exist in both queries.
By Dr. Pawan Singh Mehra
Syntax:
SELECT expression_1, expression_2, ... , expression_n FROM table_1
INTERSECT
SELECT expression_1, expression_2, ... , expression_n FROM table_2
Example:
SELECT * FROM comp1_employees
INTERSECT
SELECT * FROM comp2_employees;
96
Set Operations – MINUS/EXCEPT
Description: It returns all the rows of the first SELECT statement which
By Dr. Pawan Singh Mehra
Example:
SELECT * FROM comp1_employees
MINUS/EXCEPT
SELECT * FROM comp2_employees;
97
LIKE Operator
Description: It is used in a WHERE clause to search for a specified
pattern in a column.
By Dr. Pawan Singh Mehra
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
100
IN Operation
Example :
By Dr. Pawan Singh Mehra
101
ANY Operation
Description: It returns a boolean value as a result. It returns TRUE if ANY
By Dr. Pawan Singh Mehra
102
ANY Operation
Example:
By Dr. Pawan Singh Mehra
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
103
ALL Operation
Description: It returns a boolean value as a result. It returns TRUE if ALL
By Dr. Pawan Singh Mehra
104
ALL Operation
Example:
By Dr. Pawan Singh Mehra
SELECT ProductName
FROM Products
WHERE ProductID = ALL
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
105
EXISTS Operation
Description: It is used to test for the existence of any record in a
By Dr. Pawan Singh Mehra
106
EXISTS Operation
Example:
By Dr. Pawan Singh Mehra
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE
Products.SupplierID = Suppliers.supplierID AND Price < 20);
107
BETWEEN Operation
Description: It selects values within a given range. The values can be
By Dr. Pawan Singh Mehra
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
108
BETWEEN Operation
Example:
By Dr. Pawan Singh Mehra
SELECT CustomerName, (Address || ', ' || PostalCode || ' ' || City || ', ' ||
Country) AS Address
FROM Customers;
is met.
Syntax:
SELECT columns
FROM table1 INNER JOIN table2
ON table1.column COMPARISON OPERATOR table2.column;
Example:
SELECT suppliers.supplier_id, suppliers.supplier_name, order.order_number
FROM suppliers INNER JOIN order
ON suppliers.supplier_id = order.supplier_id;
112
SQL JOIN-Left Outer Join
Description: It returns all rows from the left (first) table specified in the ON
condition and only those rows from the right (second) table where the join
By Dr. Pawan Singh Mehra
condition is met
Syntax:
SELECT columns
FROM table1 LEFT OUTER JOIN table2
ON table1.column = table2.column;
Example:
SELECT suppliers.supplier_id, suppliers.supplier_name, order.order_number
FROM suppliers LEFT OUTER JOIN order
ON suppliers.supplier_id = order.supplier_id;
113
SQL JOIN-Right Outer Join
Description: It returns all rows from the right-hand table specified in the ON
condition and only those rows from the other table where the join condition
By Dr. Pawan Singh Mehra
is met.
Syntax:
SELECT columns
FROM table1 RIGHT OUTER JOIN table2
ON table1.column = table2.column;
Example:
SELECT suppliers.supplier_id, suppliers.supplier_name, order.order_number
FROM suppliers RIGHT OUTER JOIN order
ON suppliers.supplier_id = order.supplier_id;
114
SQL JOIN-Full Outer Join
Description: It returns all rows from the left hand table and right hand table. It
places NULL where the join condition is not met.
By Dr. Pawan Singh Mehra
Syntax:
SELECT columns
FROM table1 FULL OUTER JOIN table2
ON table1.column = table2.column;
Example:
SELECT suppliers.supplier_id, suppliers.supplier_name, order.order_number
FROM suppliers FULL OUTER JOIN order
ON suppliers.supplier_id = order.supplier_id;
115
SQL JOIN-Equi Join
Description: It returns the matching column values of the associated tables. It
uses equal(=) comparison operator in the WHERE clause to refer equality.
By Dr. Pawan Singh Mehra
Syntax:
SELECT column_list
FROM table1, table2....
WHERE table1.column_name = table2.column_name;
Example:
SELECT agents.agent_city,customer.last_name,customer.first_name
FROM agents, customer
WHERE agents.agent_id=customer.customer_id;
116
SQL JOIN-Self Join
Description: In Self Join, a table is joined with itself (Unary relationship). A
self join simply specifies that each rows of a table is combined with itself
By Dr. Pawan Singh Mehra
Syntax:
SELECT a.column_name, b.column_name
FROM table1 as a, table1 as b
WHERE a.common_filed = b.common_field;
Example:
SELECT a.name, b.age, a.SALARY
FROM CUSTOMERS as a, CUSTOMERS as b
WHERE a.SALARY < b.SALARY;
117
SQL JOIN-Cross Join
Description: It specifies that all rows from first table join with all of the
rows of second table. If there are "x" rows in table1 and "y" rows in table2
By Dr. Pawan Singh Mehra
then the cross join result set have x*y rows. It normally happens when no
matching join columns are specified.
Syntax:
SELECT * FROM table1 CROSS JOIN table2;
SELECT * FROM table1, table2
Example:
SELECT * FROM customer, supplier
118
NULL Values
It is possible for tuples to have a null value, denoted by null, for
By Dr. Pawan Singh Mehra
unknown
Three-valued logic using the truth value unknown:
OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
NOT: (not unknown) = unknown
Result of select predicate is treated as false if it evaluates to
unknown 120
Trigger
Description: It is a database object which fires when an event occurs in a
database.
By Dr. Pawan Singh Mehra
AFTER
INSERT
ON
student
FOR EACH ROW
BEGIN
UPDATE student, set full_name = first_name || ' ' || last_name;
END;
constraints, assertions differ in the way that they are defined separately
from table definitions. It is a named statement that defines a condition
that must be true for the data in the database. If the condition is not met,
the assertion will fail and the database will generate an error.