Unit 2
Unit 2
Unit 2
RELATIONAL MODEL
The relational model was founded by E.F.Codd of the IBM in 1972.The basic concept in
the relational model is that of a relation.
Properties:
The column of a table is assigned distinct names and the ordering of these columns in immaterial.
Domain, attributes tuples and relational:
Tuple:
A relation consist of
o Relational schema
o Relation instance
Relational schema:
PERSON(PERSON_IDinteger,NAME:
STRING,AGE:INTEGER,ADDRESS:string)
Total number of attributes in a relation denotes the degree of a
relation.since the PERSON relation schemea contains four
attributes ,so this relation is of degree 4.
Relation Instance:
STUDENT(rollno:strinhg,name:string,city:string,age:integer
Relation instance:
Student:
Rollno Name City Age
101 Sujit Bam 23
102 kunal bbsr 22
Keys:
Super key:
A super key is an attribute or a set of attributes used to identify the records uniquely in a relation.
Eg: (cname,telno)
Primary key:
The primary key is the candidate key that is chosen by the database designer as
the principal means of identifying entities with in an entity set. The remaining
candidate keys if any are called alternate key.
RELATIONAL CONSTRAINTS:
There are three types of constraints on relational database that include
o DOMAIN CONSTRAINTS
o KEY CONSTRAINTS
o INTEGRITY CONSTRAINTS
DOMAIN CONSTRAINTS:
It specifies that each attribute in a relation an atomic value from the
corresponding domains. The data types associated with commercial RDBMS
domains include:
Standard numeric data types for integer
o Real numbers
o Characters
o Fixed length strings and variable length strings
Thus, domain constraints specifies the condition that we to put on each instance of the relation. So the values that
appear in each column must be drawn from the domain associated with that column.
Rollno Name City Age
101 Sujit Bam 23
102 kunal bbsr 22
Key constraints:
This constraints states that the key attribute value in each tuple msut be unique .i.e, no two tuples
contain the same value for the key attribute.(null values can allowed)
Integrity constraints:
Integrity constraints are rules that specify the conditions that must be met for the data in a database to be considered
valid. These constraints help to ensure the accuracy and consistency of the data by limiting the values that can be entered for
a particular attribute and specifying the relationships between entities in the database.
There are several types of integrity constraints that can be enforced in a DBMS:
Domain constraints: These constraints specify the values that can be assigned to an attribute in a database. For
example, a domain constraint might specify that the values for an "age" attribute must be integers between 0 and
120.
Participation constraints: These constraints specify the relationship between entities in a database. For example, a
participation constraint might specify that every employee must be assigned to a department.
Entity integrity constraints: These constraints specify rules for the primary key of an entity. For example, an entity
integrity constraint might specify that the primary key cannot be null.
Referential integrity constraints: These constraints specify rules for foreign keys in a database. For example, a
referential integrity constraint might specify that a foreign key value must match the value of the primary key in
another table.
User-defined constraints: These constraints are defined by the database administrator and can be used to specify
custom rules for the data in a database.
Declarative referential integrity: This method involves specifying the integrity constraints at the time of database
design and allowing the DBMS to enforce them automatically.
Triggers: A trigger is a special type of stored procedure that is executed automatically by the DBMS when certain
events occur (such as inserting, updating, or deleting data). Triggers can be used to enforce integrity constraints by
checking for and rejecting invalid data.
Stored procedures: A stored procedure is a pre-defined set of SQL statements that can be executed as a single unit.
Stored procedures can be used to enforce integrity constraints by performing checks on the data before it is inserted,
updated, or deleted.
Application-level code: Integrity constraints can also be enforced at the application level by writing code to check for
and reject invalid data before it is entered into the database.
It is important to carefully consider the appropriate method for enforcing integrity constraints in a DBMS in order to ensure
the accuracy and consistency of the data.
Querying relational data:
A relational database query is a question about the data, and the
answer consists of a new relation containing the result. For example,
we might want to find all students AGE less than 18 or all students
enrolled in perticular course.
The SELECT statement is used to fetch the data from a database
table which returns this data in the form of a result table. These
result tables are called result-sets.
Syntax in Mysql
SELECT column1, column2, ...
FROM table_name;
If you want to select all the fields available in the table, use the
following syntax:
Syntax in Mysql
SELECT * FROM table_name;
The symbol ´*´ means that we retain all fields of selected tuples in
the result.
We can retrieve rows corresponding to students who are younger
than 18 withthe following SQL query:
Example:
SELECT * FROM Students WHERE age < 18;
The condition age < 18 in the WHERE clause specifies that we want
to select only tuples in which the age field has a value less than 18.
In addition to selecting a subset of tuples, a query can extract a
subset of the fields of each selected tuple. we can compute the
student_id and First_name of students who are younger than 18
with the following query:
Example:
SELECT ID,FirstName FROM Students WHERE age < 18;
SQL Aliases
Aliases are the temporary names given to tables or columns. An alias
is created with the AS keyword.
Alias Column Syntax in Mysql
SELECT column_name AS alias_name
FROM table_name;
Example:
SELECT studentID AS ID,
FROM students AS S;
Aliases can be useful when:
There are more than one table involved in a query
Functions are used in the query
Column names are big or not very readable
Two or more columns are combined togeth
SELECT data from Multiple Tables
We can also combine information from multiple tables.
Syntax in Mysql
SELECT table1.column1, table2.column2
FROM table1, table2
WHERE table1.column1 = table2.column1;
Example:
SELECT S.name, E.cid
FROM Students AS S, Enrolled AS E
WHERE S.sid = E.sid;
With the help of the Logical database, we will read the same data
from multiple programs.
READ
PROCESS
DISPLAY
Points To Remember:
Structure of Database
Database Program
Example:
Suppose in a University or College, a HOD wants to get information
about a specific student. So for that, he firstly retrieves the data about
its batch and Branch from a large amount of Data, and he will easily get
information about the required Student but didn’t alter the information
about it.
Advantages Of Logical Database:
Let us look at some advantages of the logical database:
In this Coding, the part is less required to retrieve data from the
database as compared to Other Databases.
Access performance of reading data from the hierarchical
structure of the Database is good.
Logical Database takes more time when the required data is at the
last because if that table which is required at the lowest level then
firstly all upper-level tables should be read which takes more time
and this slows down the performance.
Views:
Views in SQL
o To create the view, we can select the fields from one or more
tables present in the database.
Advantages of View:
1. Complexity: Views help to reduce the complexity. Different views
can be created on the same base table for different users.
6. Storage Capacity: Views take very little space to store the data.
Disadvantages of View:
1. You cannot INSERT if the base table has any not null column that
do not appear in view.
Sample table:
Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
Syntax:
3. FROM table_name
4. WHERE condition;
Query:
3. FROM Student_Details
4. WHERE STU_ID < 4;
Just like table query, we can query the view to view the data.
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
Query:
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
Syntax
Example:
Significance of Views:
Types of Views:
1. Join View: A join view is a view that has more than one table or
view in its from clause and it does not use any Group by Clause,
Rownum, Distinct and set operation.
Any user can also change the name of the table using this
statement.
The above syntax only allows you to add a single column to the
existing table. If you want to add more than one column to the
table in a single SQL statement, then use the following syntax:
3. column_Name2 column-definition,
----------------------------------------------
4. column_NameN column-definition);
Table: Cars
This statement will add the Car_Model column to the Cars table.
Table: Employee
3. column_Name2 column-definition,
4. .....
5. column_NameN column-definition);
Table: Cars
Table: Employee
Table: Cars
o Suppose, you want to change the name of the Car_Color column
of the above Cars table. For this, you have to type the following
query in the SQL:
This statement will change the name of a column of the Cars table.
To see the changes, you have to type the following query:
Table: Cars
Table: Employee
Table: Employee
Destroying tables:
Drop
Truncate
In many situations, you may require to delete the columns from the
existing table. Instead of deleting the whole table or database you
can use DROP keyword for deleting the columns.
Table: Cars
Table: Cars
This is very important to know that once a table is deleted all the
information available in the table is lost forever, so we have to be
very careful when using this command.
Let's see the syntax to drop the table from the database.
First we verify STUDENTS table and then we would delete it from the
database.
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
Let's see the command to drop a table from the MySQL database.
But if you do not specify the WHERE condition it will remove all the
rows from the table.
The TRUNCATE statement: it is used to delete all the rows from the
table and free the containing space.
Let's see an "employee" table.
When you use the drop statement it deletes the table's row together
with the table's definition so all the relationships of that table with
other tables will no longer be valid.
Truncate table is faster and uses lesser resources than DELETE TABLE
command.
Drop table command can also be used to delete complete table but
it deletes table structure too. TRUNCATE TABLE doesn't delete the
structure of the table.
Let's see the syntax to truncate the table from the database.
For example, you can write following command to truncate the data
of employee table
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.
Types of Relational operation
1. Select Operation:
1. Notation: σ p(r)
Where:
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
o This operation shows the list of those attributes that we wish to
appear in the result. Rest of the attributes are eliminated from
the table.
o It is denoted by ∏.
Where
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
1. Notation: R ∪ S
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
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:
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
1. Notation: R - S
1. ∏ CUSTOMER_NAME (BORROW) -
∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
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
7. Rename Operation:
1. ρ(STUDENT1, STUDENT)
Relational Calculus:
There is an alternate way of formulating queries known as 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. Most commercial
relational languages are based on aspects of relational calculus
including SQL-QBE and QUEL.
Why it is called Relational Calculus?
Free and bound variables may be compared with global and local
variable of programming languages.
Notation:
Where
Output: This query selects the tuples from the AUTHOR relation. It
returns a tuple with 'name' from Author who has written an article
on 'database'.
For example:
Output: This query will yield the same result as the previous one.
Notation:
1. { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where
For example: