dbms unit2
dbms unit2
2.2.2. Entity Integrity Constraint- This rule states that in any database relation value of
attribute of a primary key can't be null.
EXAMPLE- Consider a relation "STUDENT" Where "Stu_id" is a primary key and it must not
contain any null value whereas other attributes may contain null value e.g "Branch" in the
following relation contains one null value.
Stu_id Name Branch
113421 Ajay IT
113564 Sandy CSE
112453 Sujith
114532 Rose CIVIL
113422 Jack IT
Table 2.3
2.2.3. Referential Integrity Constraint-It states that if a foreign key exists in a relation
then either the foreign key value must match a primary key value of some tuple in its home
relation or the foreign key value must be null.
The rules are:
1. You can't delete a record from a primary table if matching records exist in a related table.
2. You can't change a primary key value in the primary table if that record has related
records.
3. You can't enter a value in the foreign key field of the related table that doesn't exist in
the primary key of the primary table.
4. However, you can enter a Null value in the foreign key, specifying that the records are
unrelated.
EXAMPLE-Consider 2 relations "stu" and "stu_1" Where "Stu_id " is the primary key in the
"stu" relation and foreign key in the "stu_1" relation.
Relation "stu”
Relation "stu_1"
Stu_id Course Duration
113421 B.Tech 4 years
113564 B.Tech 4 years
112453 B.Tech 4 years
114532 B.Tech 4 years
113422 B.Tech 4 years
Table 2.5
Examples:
Rule 1. You can't delete any of the rows in the ”stu” relation that are visible since all the
”stu” are in use in the “stu_1” relation.
Rule 2. You can't change any of the ”Stu_id” in the “stu” relation since all the “Stu_id” are
in use in the ”stu_1” relation.
Rule 3. The values that you can enter in the” Stu_id” field in the “stu_1” relation must be in
the” Stu_id” field in the “stu” relation.
Rule 4 You can enter a null value in the "stu_1" relation if the records are unrelated.
2.2.4. Key Constraints- A Key Constraint is a statement that a certain minimal subset of
the fields of a relation is a unique identifier for a tuple.
SQL Constraints are rules used to limit the type of data that can go into a table, to maintain
the accuracy and integrity of the data inside table.
Constraints can be divided into following two types,
Column level constraints: limits only column data. The style is called Inline
specification.
Table level constraints: limits whole table data. The style is called out of line
specification
Constraints are used to make sure that the integrity of data is maintained in the database.
Following are the most used constraints.
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
UNIQUE
2.2.4.1 NOT NULL Constraint:
NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL
constraint is applied to a column, you cannot pass a null value to that column. It enforces a
column to contain a proper value. One important point to note about NOT NULL constraint is
that it cannot be defined at table level.
Example using NOT NULL constraint:
CREATE table Student (s_id int NOT NULL, Name varchar(60), Age int);
The above query will declare that the s_id field of Student table will not take NULL value.
2.2.4.2UNIQUE Constraint:
UNIQUE constraint ensures that a field or column will only have unique values. A UNIQUE
constraint field will not have duplicate data. UNIQUE constraint can be applied at column
level or table level.
Example using UNIQUE constraint when creating a Table (Table Level)
CREATE table Student(s_id int NOT NULL UNIQUE, Name varchar(60), Age int);
The above query will declare that the s_id field of Student table will only have unique
values and won’t take NULL value.
Example using UNIQUE constraint after Table is created (Column Level)
ALTER table Student add UNIQUE(s_id);
The above query specifies that s_id field of Student table will only have unique value.
On Delete Cascade : This will remove the record from child table, if that value of
foriegn key is deleted from the main table.
On Delete Null : This will set all the values in that record of child table as NULL, for
which the value of foriegn key is deleted from the main table.
If we don't use any of the above, then we cannot delete data from the main table for
which data in child table exists. We will get an error if we try to do so.
ERROR : Record in child table exist
2.2.4.5 CHECK Constraint:
CHECK constraint is used to restrict the value of a column between a range. It performs
check on the values, before storing them into the database. Its like condition checking
before saving data into a column.
Example using CHECK constraint at Table Level:
create table Student(s_id int NOT NULL CHECK(s_id > 0),
Name varchar(60) NOT NULL, Age int);
The above query will restrict the s_id value to be greater than zero.
Example using CHECK constraint at Column Level:
ALTER table Student add CHECK(s_id > 0);
name Login
Rony Rony@cs
Guldu Guldu@cs
You can include multiple tables in your SELECT statement in a similar way as you use them in
a normal SQL SELECT query.
Example: Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example to create a view from the CUSTOMERS table. This view would be used
to have customer name and age from the CUSTOMERS table.
SQL > CREATE VIEW CUSTOMERS_VIEW AS SELECT name, age FROM CUSTOMERS;
Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual table.
Following is an example for the same.
+----------+-----+
| name | age |
+----------+-----+
| Ramesh | 32 |
| Khilan | 25 |
| kaushik | 23 |
| Chaitali | 25 |
| Hardik | 27 |
| Komal | 22 |
| Muffy | 24 |
+----------+-----+
This would ultimately update the base table CUSTOMERS and the same would reflect in the
view itself. Now, try to query the base table and the SELECT statement would produce the
following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
This would ultimately delete a row from the base table CUSTOMERS and the same would
reflect in the view itself. Now, try to query the base table and the SELECT statement would
produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+-------------+------+------------------+--------------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Dropping Views
Obviously, where you have a view, you need a way to drop the view if it is no longer needed.
The syntax is very simple and is given below −
DROP VIEW view_name;
Types of View
There are two types of view,
Simple View
Complex View
Simple View Complex View
Created from one table Created from one or more table
Does not contain functions Contain functions
Does not contain groups of data Contains groups of data
2.6 DESTROYING/ALTERING TABLES AND VIEWS:
If we decide that we no longer need a base table and want to destroy it, we can use
the DROP TABLE command. For example, DROP TABLE Students RESTRICT destroys the
Students table unless some view or integrity constraint refers to Students; if so, the
command fails. If the keyword RESTRICT is replaced by CASCADE, Students is dropped and
any referencing views or integrity constraints are (recursively) dropped as well; one of
these two keywords must always be specified. A view can be dropped using the DROP VIEW
command, which is just like DROP TABLE.ALTER TABLE modifies the structure of an existing
table. To add a column called maiden-name to Students, for example, we would use the
following command:
ALTER TABLE Students ADD COLUMN maiden-name char(10)
The definition of Students is modified to add this column, and all existing rows are padded
with null values in this column. ALTER TABLE can also be used to delete columns and to add
or drop integrity constraints on a table; we will not discuss these aspects of the command
beyond remarking that dropping columns is treated very similarly to dropping tables or
views.
Relational database systems are expected to be equipped with a query language that can
assist its users to query the database instances. There are two kinds of query languages −
relational algebra and relational calculus proposed by E. F. Codd in 1976.
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or
those books published after 2010.
Project Operation (∏):
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r) Where A1, A2, An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
Selects and projects columns named as subject and author from the relation Books.
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)
Where the result of expression E is saved with name of x.
Binary operations: Operates on pairs of relation. These include:
Union
Set Difference
Cartesian product
Division
Joins
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
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
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
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
Output − Yields a relation, which shows all the books and articles written by tutorial.
Division:
The division operator divides a dividend relation A of degree (means number of columns in a
relation) m + n by a divisor relation B of degree n, and produces a\ resultant relation of
degree m. It is denoted by ÷.
In this example dividend relation A has two attributes of Sno, Pno (of degree 2) and division
relation B has only one attribute Pno (of degree 1). Then, A divide by B gives a resultant
relation of degree 1. It means it has only one attribute of Sno.
A = SNO* SNO = SNO. B = PNO
The resultant relation has those tuples that are common values of those attributes, which
appears in the resultant attribute sno. For example, in CASE II,
P2 has Snos. -- Sl, S2, S3,S4 P4 has Snos. -- SI, S4
S1, S4 are the common supplier who supply both P2 and P4. So the resultant relation has
tuples S1 and S4.
In CASE III : There is only one supplier S1 who supply all the parts from PI to P6.
Joins:
We understand the benefits of taking a Cartesian product of two relations, which gives
us all the possible tuples that are paired together. But it might not be feasible for us in certain
cases to take a Cartesian product where we encounter huge relations with thousands of tuples
having a considerable large number of attributes.
Join is a combination of a Cartesian product followed by a selection process. A Join operation
pairs two tuples from different relations, if and only if a given join condition is satisfied.
We will briefly describe various join types in the following sections.
Theta (θ) Join:
Theta join combines tuples from different relations provided they satisfy the theta condition.
The join condition is denoted by the symbol θ.
Notation
R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that the
attributes don’t have anything in common, that is R1 ∩ R2 = Φ.
Theta join can use all kinds of comparison operators.
Student
SID Name Std
101 Alex 10
102 Maria 11
Subjects
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail −
HoD
Dept Head
CS Alex
ME Maya
EE Mira
Courses ⋈ HoD
Dept CID Course Head
CS CS01 Database Alex
ME ME01 Mechanics Maya
EE EE01 Electronics Mira
Outer Joins: Theta Join, Equijoin, and Natural Join are called inner joins. An inner join
includes only those tuples with matching attributes and the rest are discarded in the resulting
relation. Therefore, we need to use outer joins to include all the tuples from the participating
relations in the resulting relation. There are three kinds of outer joins − left outer join, right
outer join, and full outer join.
Right
A B
100 Alex
102 Maya
104 Mira
Courses HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya
TEXT BOOK:
1. Raghurama Krishnan, Johannes Gehrke, Database Management Systems, 3rd Edition,
TATA McGraw hill.
2. Web pages