DBMS Unit-2
DBMS Unit-2
• Domain Constraints
• Key Constraint
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, Real, Time, Date, Currency,
Etc. The value of that attribute must be
available in the corresponding domain.
• A domain of possible values must be
associated with every attribute
• They are tested easily by the system whenever a
new data item is entered into the database.
• Each table has certain set of columns and each
column allows a same type of data, based on its
data type. The column does not accept values of
any other data type.
• Domain constraints can be violated if an attribute
value is not appearing in the corresponding
domain or it is not of the appropriate data type.
• Example : If there is a column(attribute) of integer
values then we can’t put an alphabet in it. Since
the attribute has only integer values as its domain
Age
17
22
23
24
25
A
Not allowed Because Age is an
Integer Attribute
Entity Integrity Constraints
• Entity Integrity Constraints states that primary
key value can’t be NULL.
123 A
132 B
135 C
136 D
137 E
1 A 20 10 10 Hyderabad
2 B 30 14 14 Delhi
Relationships
3 C 40 17 13 Noida
4 D 50 13
Table 2
Table 1
D.No.17 is not Allowed Because it is not defined As a primary key of Table 2 and In Table 1
D.No. is a foreign key Defined.
Key Constraints
• Keys are the entity set that is used to identify
an entity within its entity set uniquely.
• Default constraint:
Provides a default value for a column when
none is specified.
• NOT NULL constraint − Ensures that a column cannot
have NULL value.
Employee
• CREATE TABLE Employee (Emp_No. INTEGER,
Emp_Name CHAR(20), Emp_Salary MONEY, PRIMARY
KEY(Emp_No.))
Relationship Sets (without Constraints) to Tables:
A relationship set, like an entity set, is mapped to a
relation in the relational model. To represent a
relationship, we must be able to identify each
participating entity and give values to the descriptive
attributes of the relationship.
Thus, the attributes of the relation include:
• The primary key attributes of each participating entity
set, as foreign key fields.
• ER Diagram:
Did D.Name
E.No. E.Name Salary Since
Did D.Name
E.No. E.Name Salary Since
S_ID Names
1 Harsh
2 Ashish
3 Pratik
4 Dhanraj
• Creating View from multiple tables:
In this example we will create a View named MarksView from
two tables StudentDetails and StudentMarks. To create a View
from multiple tables we can simply include multiple tables in the
SELECT statement.
Query:
CREATE VIEW MarksView AS SELECT
StudentDetails.NAME, StudentDetails.ADDRESS,
StudentMarks.MARKS FROM StudentDetails,
StudentMarks WHERE StudentDetails.NAME =
StudentMarks.NAME;
To display data of View MarksView:
SELECT * FROM MarksView;
Output:
• DELETING VIEWS
We have learned about creating a View, but what if a created
View is not needed any more? Obviously we will delete it. SQL
allows us to delete an existing View. We can delete or drop a
View using the DROP statement.
Syntax:
• DROP VIEW view_name;
view_name: Name of the View which we want to
delete.
• For example, if we want to delete the
View MarksView, we can do this as:
• DROP VIEW MarksView;
UPDATING VIEWS
• There are certain conditions needed to be satisfied to update a view. If
any one of these conditions is not met, then we will not be allowed to
update the view.
1. The SELECT statement which is used to create the view should
not include GROUP BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex
queries.
5.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.
• We can use the CREATE OR REPLACE VIEW statement to add or
remove fields from a view.
Syntax:
• CREATE OR REPLACE VIEW view_name AS SELECT column1,coulmn2,..
>update StudentNames set Names=‘Shiva' where S_ID=2;
1 Row updated.
SQL>select * from StudentNames;
S_ID Names
1 Harsh
2 Shiva
3 Pratik
4 Dhanraj
• 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:
Output:
Deleting a row from a View:
Deleting rows from a view is also as simple as
deleting rows from a table. We can use the DELETE
statement of SQL to delete rows from a view. Also
deleting a row from a view first delete the row from
the actual table and the change is then reflected in
the view.
Syntax:
DELETE FROM view_name WHERE condition;
view_name: Name of view from where we want to
delete rows
condition: Condition to select rows
Example:
Where
A1, A2, A3 is used as an attribute name of relation r.
• Example: CUSTOMER RELATION
Smith Rye
Curry Rye
• Union operation (U) :
• UNION is symbolized by ∪ symbol. It includes all tuples that are in
tables A or in B. It also eliminates duplicate tuples.
• So, set A UNION set B would be expressed as:
– The Notation is A ∪ B For a union operation to be valid, the
following conditions must hold
• R and S must be the same number of attributes.
• Attribute domains need to be compatible.
• Duplicate tuples should be automatically removed.
• Relation S
ID Name Subject
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
• BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
• Input:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
• Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
• Intersection Operator (∩) :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.
CUSTOMER_NAME
Smith
Jones
• 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.
ID Name Subject
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
• Cartesian Product: 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.
• It is denoted by X.
• Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
EMPLOYEE X DEPARTMENT
Output:
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
• Rename (ρ):
• Rename is a unary operation used for renaming
attributes of a relation and rename the output
relation. It is denoted by rho (ρ).
• Example : ρ a/b(R) will rename the attribute 'b' of
relation by 'a'.
• Example: We can use the rename operator to
rename STUDENT relation to STUDENT1.
• ρ(STUDENT1, STUDENT) or ρSTUDENT1 (STUDENT)
• Example Query: σ Condition(User X ρUser1(User))
Relational Calculus
• Relational calculus is a non-procedural query
language. In the non-procedural query language,
the user is concerned with the details of how to
obtain the end results.
• The relational calculus tells what to do but never
explains how to do.
• Relational Calculus exists in two forms:
– Tuple Relational Calculus (TRC)
– Domain Relational Calculus (DRC)
Tuple Relational Calculus (TRC)
• The tuple relational calculus is specified to select
the tuples in a relation. In TRC, filtering variable
uses the tuples of a relation.
• The result of the relation can have one or more
tuples.
• {t | P (t)} or {t | condition (t)} — this is also known
as expression of relational calculus Where t is the
resulting tuples, P(t) is the condition used to fetch
t.
• {t | EMPLOYEE (t) and t.SALARY>10000} – implies
that it selects the tuples from EMPLOYEE relation
such that resulting employee tuples will have
salary greater than 10000. It is example of
selecting a range of values.
• Here the t.SALARY >10000 refers for all the
SALARY>10000, display the employees. Here the
SALARY is called as bound variable. Any tuple
variable with ‘For All’ (∀ ) or ‘there exists’ (∃)
condition is called bound variable
• {t | EMPLOYEE (t) AND t.DEPT_ID = 10}
– this select all the tuples of employee
name who work for Department 10.
The variable which is used in the condition
is called tuple variable. In above example
t.SALARY and t.DEPT_ID are tuple variables
Domain Relational Calculus