unit 2
unit 2
unit 2
MANAGEMENT SYSTEMS
Example:
Select id , name, age, address, salary from customers where salary is not null;
Integrity Constraints
• Integrity constraints are a set of rules. It is used to maintain the quality
of information.
• Integrity constraints ensure that the data insertion, updating, and other
processes have to be performed in such a way that data integrity is not
affected. Types of integrity constraints:
Types of Integrity Constraints
1. Domain Constraints:
• Domain constraints can be defined as the definition of a valid set of
values for an attribute.
• The data type of domain includes string, character, integer, time, date,
currency, etc. The value of the attribute must be available in the
corresponding domain
2. Entity Integrity Constraints:
• The entity integrity constraint states that primary key value can't be
null.
• This is because the primary key value is used to identify individual
rows in relation and if the primary key has a null value, then we can't
identify those rows.
• A table can contain a null value other than the primary key field
3. Referential Integrity Constraints:
• A referential integrity constraint is specified between two tables.
• In the Referential integrity constraints, if a foreign key in Table 1
refers to the Primary Key of Table 2, then every value of the Foreign
Key in Table 1 must be null or be available in Table 2.
4. Key Constraints:
• Keys are the entity set that is used to identify an entity within its entity
set uniquely.
• An entity set can have multiple keys, but out of which one key will be
the primary key.
• A primary key can contain a unique value in the relational table
Example:
Constraints on table
1. Primary key
Constraints on table
2. NOT NULL
Constraints on table
3. CHECK
Constraints on table
4. DEFAULT
Constraints on table
5. UNIQUE
Constraints on table
6. FOREIGN KEY
Database Schema (Database Design)
• It defines how the data is organized and how the relations among them
are associated. It formulates all the constraints that are to be applied on
the data. A database schema defines its entities and the relationship
among them.
Querying Relational data
1. SQL Data Types:
• Data types are used to represent the nature of the data that can be
stored in the database table.
• For example, in a particular column of a table, if we want to store a
string type of data then we will have to declare a string data type of
this column.
• Data types mainly classified into three categories for every database.
COLLEGE
College maintains database of different branches. When we try to modify the data in
any branch it will automatically reflects in the college data base by using VIEWS.
Syntax for creation of View
create view view_name as
select column1,column2, ………..
from table_name where condition;
Example:
create view cse_stud as
select * from student where branch=‘cse’;
Student table
Updating view:
• Views are updated only if certain conditions are met otherwise if any
one of the conditions are not met views will not be updated.
Inserting a row into a views
• To insert a row in a view just like inserting a row in an ordinary table.
Deleting a row from a view
• A row in a view can be deleted just like simply deleting rows from a
Table using delete statement.
• But remember a row in a view can be deleted only if the row is actually
deleted in the original table from which it is created.
Syntax:
Delete from view_name
where condition;
Relational Algebra
• Relational algebra is a procedural query language. It gives a step by
step process to obtain the result of the query. It uses operators to
perform queries.
• It is a theoretical model and base for SQL.
• In relational mode, the data can retrieved theoretically or practically.
• Theoretical means -> Relational Algebra, Relational Calculus
• Practical means-> Through SQL queries
RELATIONAL ALGEBRA
Examples:
σ ( student) – displays all rows
σ (course =‘cs101’ and grade=‘A’ )
(student) -> display the entire row
σ (perc < 90) (student)
• Projection:
• used to select a subset of columns from a table.
• Projection (Π/ π)
• Vertical subset of a relation (columns)
• Syntax: π (col1,col2….coln) (relation)
Example:
Π ( Name) (student)
Π (Roll, per)(student)
Π (roll,per) (σ per>90) (student)
• Rename operation
• Represented with symbol RHO(ρ)
• Syntax: ρ (new_name) (relation_name)
Student
Example:
ρ (final_students) (student)
SET THEORY OPERATIONS
• Set Theory Operations can be performed on 2 tables or
relations.
UNION( U ) :
• Let R and S be two relations.
• Then-
• R ∪ S is the set of all tuples belonging to either R or S or both.
• In R ∪ S, duplicates are automatically removed.
Intersection (∩):
• Let R and S be two relations.
• Then-
• R ∩ S is the set of all tuples belonging to both R and S.
• In R ∩ S, duplicates are automatically removed.
• Intersection operation is both commutative and associative.
Minus/Difference operator:
• Let R and S be two relations.
• Then-
• R – S is the set of all tuples belonging to R and not to S.
• In R – S, duplicates are automatically removed.
• Difference operation is associative but not commutative.
Cross product operation (X):
• The Cartesian product is used to combine each row in one table
with each row in the other table. It is also known as a cross
product.
• Notation: E X D
• Example: (Employee X DEPARTMENT)
EMPLOYEE DEPARTMENT
EMP_ID EMP_NAME EMP_DEPT DEPT_NUM DEPT_NAME
1 SMITH A A MARKETING
2 HARRY C B SALES
3 JOHN B C LEGAL
JOINS
• Joins (⋈) in DBMS is used to combine tables.
• There are three types of joins: inner joins and outer joins.
• Inner joins are classified into three types: Theta Join(for relational
operators), natural join and Equi Join(for Equality).
• There are three types of outer joins in DBMS: left outer join, right outer
join, and full outer join.
• Natural join is only performed when at least one matching attribute exists in
both tables.
• No matter the Join condition, a left outer join always returns every row from
the left table.
• Regardless of the Join condition, Right Outer Join always returns all rows
from the right table.
• Regardless of the join condition, Complete Outer Join always returns all
Join = cartesian product + selection
RELATION R RELATION S
A B C D
1 2 5 6
3 4 7 8
RELATION R X S
A B C D
1 2 5 6
1 2 7 8
3 4 5 6
3 4 7 8
Inner join
Theta join: A Theta Join uses a condition other than equality to join
Example: R ⋈ S
of columns and that column should be present in both tables.
R⋈S
E
101 RAJU 1 CSE
102 RAVI 2 ECE
103 HARI 1 CSE
104 RAMU 3 EEE
Equi Join: R1 ⋈ condition R2
• R1 ⋈ (R1. DeptID = R2.DeptID) R2
Name Course
System Btech Course
Database Mtech Name
÷ Btech =
Database Btech database
Mtech
Algebra Btech
Example : Retrieve names of employees who work on all the projects
that John Smith works on.
Relational calculus
• Relational calculus in DBMS is a non-procedural language, which only
focuses on what kind of data is required and it does not care about how to
get these data.
Example: Let’s take an example to understand what is relational calculus in DBMS.
SELECT the tuples from EMPLOYEE relation with DEPARTMENT= 'NETWORKING’
Tuple Relational Calculus (TRC)
• TRC in DBMS is based on the concept of selecting tuples (rows) from
a relation (table) that satisfies certain conditions.
• Tuple Relational Calculus uses variables to represent tuples and
logical predicates to specify the conditions that must be met for the
tuples to be selected.
• The resulting expression is a formula that describes a set of tuples that
meet the specified conditions.
• The general syntax for Tuple Relational Calculus is:
• where, < x1, x2, x3, …, xn > represents resulting domains variables and P (x1,
x2, x3, …, xn ) represents the condition or formula equivalent to the Predicate
calculus
Example : Now, we will take the database table Employee and try to apply
the Domain Relational Calculus expression on that table.