PPT
PPT
Query examples
that define R:
values in t= to be ordered .
The student schema assert that a student entity has Name, SSN,
HomePhone, Address, OfficePhone, Age, and CGPA.
Both t[Au, Aw,…., Az] and t.( Au, Aw,….,Az),where Au, Aw,….,Az is
a list of attributes from R, refer to the sub tuple of values from t
corresponding to the attributes specified in the list.
This is because the same name may be used for two attributes in
different relations.
A database state that does not obey all the integrity constraints is
called an invalid state, and
A state that satisfies all the constraints in IC is called a valid state.
Key constraints
Constraints are conditions/ rules that must hold on all valid relation
instances.
1. Domain Constraints
2. Key constraints
3. Entity integrity constraints
4. Referential Integrity constraints
Domain Constraints
Specify that within each tuple, the value of each attribute A
1. Foreign key
2. Alternate Key
3. Unique Key
4. Composite/ Compound Key
Entity Integrity Constraints (Primary key
integrity constraints)
Entity Integrity is the mechanism the system provides to maintain
primary keys.
Each table in the database can have at most one PRIMARY KEY constraint.
columns.
The primary key columns do not allow nulls.
i.e ., The primary key attributes PK of each relation schema R in S
cannot have null values in any tuple of r(R).
This is because primary key values are used to identify the individual tuples.
t[PK] ≠ null for any tuple t in r(R) for any R
Referential Integrity constraints
There is other constrain which is related to the relationships
between the relations referential constrain
The foreign key and relationship attributes, if any, are not included
yet; they will be added during subsequent steps.
For each weak entity type W in the ER schema with owner entity
type E, create a relation R and include all simple attributes of W as
attributes of R.
EG: We map the 1:1 relationship type MANAGES from by choosing the
participating entity type DEPARTMENT to serve in the role of S because its
participation in the MANAGES relationship type is total (every department has
a manager)
We include the primary key of the EMPLOYEE relation as foreign key in the
DEPARTMENT relation and rename it Mgr_ssn.
The primary key of R will be one of the two foreign keys, and the
other foreign key will be a unique key of R.
For each regular binary 1:N relationship type R, identify the relation S
that represents the participating entity type at the N-side of the
relationship type.
1(P) N (F)
Also include any simple attributes of the M:N relationship type (or
simple components of composite attributes) as attributes of S.
The result of an operation is a new relation, which may have been formed
from one or more input relations
2. Binary Operation:
There are operations which operate on two operands known as
binary operations. For example,
JOIN, DIVISION
One can consider the SELECT operation to be a filter that keeps only
those tuples that satisfy a qualifying condition.
That is
The result of the PROJECT operation has only the attributes specified in
<attribute list> in the same order as they appear in the list.
Hence, its degree is equal to the number of attributes in <attribute list>
If the projection list is a super key of R-that is, it includes some key of R-
the resulting relation has the same number of tuples as R.
Sequences of Operations and the
RENAME Operation
We can write the operations as a single relational algebra
expression by nesting the operations.
A formal RENAME operation which can rename either the relation name or
the attribute names, or both.
Symbol (rho) denote the RENAME operator, S the new relation name
& (Bl, B2,..., Bn) the new attribute names.
Second renames the relation only, and the third renames the attributes
only.
Relational Algebra operations from set
theory
Binary operations that is applying on two sets.
When this operation is applied in between two relations the type of
the tuples in both relation must be same.
This condition is called compatible.
Two relations R (A1, A2,..., An) and S (B1, B2,..., Bn) are said to be
union compatible
if they have the same degree n and
if dom(A) = dom(B)
This means that the two relations have the same number of
attributes, and each corresponding pair of attributes has the same
domain.
INTERSECTION:
The result of this operation, denoted by R Ω S, is a relation that
includes all tuples that are in both R and S.
Suppose a relation R (A1, A2, …., An) X S (B1, B2, …., Bm) will
result to a relation Q (A1, A2, …., An, B1, B2, …., Bm ) which has
an n+m attributes.
For getting the female employee with her dependent those tuples
where the SSN is equal to the ESSN have to filter.
The join condition is specified on attributes from the two relations R and
S and is evaluated for each combination of tuples.
Each tuple combination for which the join condition evaluates to TRUE
is included in the resulting relation Q as a single combined tuple.
A general join condition is of the form
The EQUIJOIN and NATURAL JOIN
The most common use of JOIN involves join conditions with
equality comparisons only.
Such a JOIN, where the only comparison operator used is =, is
called an EQUIJOIN.
The example in the above join operation is EQUIJOIN.
Example :
Here combining the PROJECT tuple with each DEPARTMENT tuple.
If the attributes have same name, then the rename operation is not
necessary.
All those in S, or
There are two types of Outer Join operations LEFT OUTER JOIN and
RIGHT OUTER JOIN.
The LEFT OUTER JOIN
Keeps every tuple in the first or left, relation R; if no
This operation will take the UNION of tuples in two relations R(X,
Y) and S(X, Z) that are partially compatible, meaning that only
some of their attributes, say X, are union compatible.
Query 2.
For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s
last name, address, and birth date.
Query 3.
Find the names of employees who work on all the projects
controlled by department number 5.
Query 4.
Make a list of project numbers for projects that involve an
employee whose last name is ‘Smith’, either as a worker or as a
manager of the department that controls the project
Query 5.
List the names of all employees with two or more dependents
Query 6.
Retrieve the names of employees who have no dependents.
Query 7.
List the names of managers who have at least one dependent.
Database Languages
A DBMS is a software package that carries out many different
tasks including the provision of facilities to enable the user to
access and modify information in the database.
Data Manipulation Language (DML):
set.
The vast majority of the SQL commands used in applications are SELECT
Structured Query Language
SQL Commands
The standard SQL commands to interact with relational databases
are CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETE.
These commands can be classified into the following groups
based on their nature
Example:
The basic syntax of ALTER TABLE to DROP PRIMARY KEY constraint from
a table is as follows:
ALTER TABLE table_name DROP PRIMARY KEY;
SQL - DROP Table
The SQL DROP TABLE statement is used to remove a table
definition and all data, constraints, and permission specifications
for that table.
2 type reprasentation
Specify the column(s) name in the SQL query if you are adding values
for all the columns of the table.
But make sure the order of the values is in the same order as the
columns in the table.
Syntax:
INSERT INTO TABLE_NAME VALUES
(value1,value2,value3,...valueN);
Syntax:
Eg:
UPDATE CUSTOMERS SET ADDRESS = “Kannur” where ID = 1;
SQL - DELETE Query
The SQL DELETE Query is used to delete the existing records from a
table.
Use WHERE clause with DELETE query to delete selected rows,
otherwise all the records would be deleted.
Syntax
Syntax:
SELECT column1, column2, columnN FROM table_name;