Dbms 2 Notes
Dbms 2 Notes
Dbms 2 Notes
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an
attribute.
o 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.
Example:
2. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o 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.
o A table can contain a null value other than the primary key field.
Example:
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o 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 and null value in the relational table.
Example:
SQL Statements
Show databases;
Create database db_name;
Use db_name;
Select database();
Creating Table
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
);
a. SELECT: This is the same as the projection operation of relational algebra. It is used to select
the attribute based on the condition described by WHERE clause
b. Views in SQL
c. Views in SQL are considered as a virtual table. A view also contains rows and columns.
d. To create the view, we can select the fields from one or more tables present in the database.
e. A view can either have specific rows based on certain condition or all the rows of a table.
h. FROM Student_Details
Relational Algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output. It uses
operators to perform queries. An operator can be either
unary or binary. They accept relations as their input and yield relations as
their output. Relational algebra is performed recursively on a relation and
intermediate results are also considered relations.
The fundamental operations of relational algebra are as follows −
Select
Project
Union
Set different
Cartesian product
Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
For example −
σsubject = "database"(Books)
For example −
Selects and projects columns named as subject and author from the
relation Books.
Union Operation (𝖴)
It performs binary union between two given relations and is defined as −
r 𝖴 s = { t | t ∈ r or t ∈ s}
Notation − r U s
Output − Projects the names of the authors who have either written a
book or an article or both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one
relation but are not in the second relation.
Notation − r − s
Output − Provides the name of authors who have written books but not
articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Χ s = { q t | q ∈ r and t ∈ s}
Output − Yields a relation, which shows all the books and articles written
by tutorialspoint.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name.
The rename operation allows us to rename the output relation. 'rename'
operation is denoted with small Greek letter rho ρ.
Notation − ρ x (E)
Set intersection
Assignment
Natural join
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural
query language, that is, it tells what to do but never explains how to do it.
Notation − {T | Condition}
For example −
Output − Returns tuples with 'name' from Author who has written article on
'database'.
For example −
Output − The above query will yield the same result as the previous
one.
Domain Relational Calculus (DRC)
In DRC, the filtering variable uses the domain of attributes instead of
entire tuple values (as done in TRC, mentioned above).
Notation −
Where a1, a2 are attributes and P stands for formulae built by inner
attributes.
For example −
Constraint describes conditions that every legal instance of a relation must satisfy.
Can be used to :
• ensure application
• prevent
has to be a string,
Fundamental:
• Domain constraints,
General:
Sailors ( sid
INTEGER,
sname CHAR(10),
rating INTEGER,
age REAL,
PRIMARY KEY
(sid),
DEFAULT 1
CREATE TABLE
Sailors ( sid
INTEGER,
sname CHAR(10),
rating values-of-ratings,
age REAL,
Reserves ( sname
CHAR(10),
bid INTEGER,
day DATE,
CONSTRAINT noInterlakeRes
( SELECT B.bname
FROM Boats B
WHERE B.bid=
bid)))
Table Constraints
CREATE TABLE
Sailors ( sid
INTEGER,
sname CHAR(10),
rating INTEGER,
age REAL,
PRIMARY KEY
(sid), CHECK
< 100 )
Three parts:
Semantics:
Triggers – Event,Condition,Action
Events could be :
Example Trigger
pName, salary)
Example Trigger
BEGIN
END;
SET Operations in SQL
SQL supports few Set operations which can be performed on the table data.
These are used to get meaningful results from data stored in the table,
under different special conditions.
In this tutorial, we will cover 4 different types of SET operations,
along with example:
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
UNION Operation
UNION is used to combine the results of two or more SELECT
statements. However it will eliminate duplicate rows from its resultset.
In case of union, number of columns and datatype must be same in
both the tables, on which UNION operation is being applied.
Example of UNION
The First table,
ID Name
1 abhi
2 adam
ID Name
2 adam
3 Chester
ID NAME
1 abhi
2 adam
3 Chester
UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.
ID NAME
1 abhi
2 adam
ID NAME
2 adam
3 Chester
ID NAME
1 abhi
2 adam
2 adam
3 Chester
INTERSECT
Intersect operation is used to combine two SELECT statements, but it only
retuns the records which are common from both SELECT statements. In case
of Intersect the number of columns and datatype must be same.
NOTE: MySQL does not support INTERSECT operator.
Example of Intersect
The First table,
ID NAME
1 abhi
2 adam
ID NAME
2 adam
3 Chester
ID NAME
2 adam
MINUS
The Minus operation combines results of two SELECT statements and return
only those in the final result, which belongs to the first set of the result.
Example of Minus
The First table,
ID NAME
1 abhi
2 adam
ID NAME
2 adam
3 Chester
1 abhi
The SQL EXCEPT clause/operator is used to combine two SELECT
statements and returns rows from the first SELECT statement that are
not returned by the second SELECT statement. This means EXCEPT
returns only rows, which are not available in the second SELECT
statement.
Just as with the UNION operator, the same rules apply when using the
EXCEPT operator. MySQL does not support the EXCEPT operator.
Syntax
The basic syntax of EXCEPT is as follows.
EXCEPT
Here, the given condition could be any given expression based on your requirement.
Example
Consider the following two tables.
++++++
++++++
| 6 | Komal | 22 | MP | 4500.0 |
0
| 7 | Muffy | 24 | Indore | 10000.00 |
++++++
+++++
+++++
Now, let us join these two tables in our SELECT statement as shown
below.
FROM CUSTOMERS
LEFT JOIN
ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
EXCEPT