DBMS-Unit2
DBMS-Unit2
me/jntuh
UNIT - 2
Introduction to the Relational Model
Relational Model (RM) represents the database as a collection of relations. A relation is nothing
but a table of values. Every row in the table represents a collection of related data values. These
rows in the table denote a real-world entity or relationship.
The table name and column names are helpful to interpret the meaning of values in each row.
The data are represented as a set of relations. In the relational model, data are stored as tables.
However, the physical storage of the data is independent of the way the data are logically
organized.
10. Attribute domain – Every attribute has some pre-defined value and scope which is known
as attribute domain
Advantages of Realation Model
Structural Independence: Structural independence is an ability that allows us to make
changes in one database structure without affecting other. The relational model have
structural independence. Hence making required changes in the database is convenient in
relational database model.
Conceptual Simplicity: The relational model allows the designer to simply focus on
logical design and not on physical design. Hence relational models are conceptually
simple to understand.
Query Capability: Using simple query language (such as SQL) user can get information
from the database or designer can manipulate the database structure.
Easy design,maintenance and usage: The relational models can be designed logically
hence they are easy to maintain and use.
Keys in DBMS
KEYS in DBMS is an attribute or set of attributes which helps you to identify a row(tuple) in a
relation(table). They allow you to find the relation between two tables. Keys help you uniquely
identify a row in a table by a combination of one or more columns in that table.
Super key:
• A superkey is a group of single or multiple keys which identifies rows in a table. Super
Key is a superset of Candidate key.
• All of the following sets of super key are able to uniquely identify a row of the employee
table.
• {Emp_SSN}
• {Emp_Number}
• {Emp_SSN, Emp_Number}
• {Emp_SSN, Emp_Name}
• {Emp_SSN, Emp_Number, Emp_Name}
• {Emp_Number, Emp_Name}
Candidate Key:
CANDIDATE KEY in SQL is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no repeated attributes. The Primary key should be selected
from the candidate keys. Every table must have at least a single candidate key. A table can have
multiple candidate keys but only a single primary key.
A primary key is selected from the set of candidate keys. That means we can either have Emp_Id
or Emp_Number as primary key. The decision is made by DBA.
Primary Key:
PRIMARY KEY in DBMS is a column or group of columns in a table that uniquely identify
every row in that table. The Primary Key can’t be a duplicate meaning the same value can’t
appear more than once in the table. A table cannot have more than one primary key.
Foreign key:
FOREIGN KEY is a column that creates a relationship between two tables. The purpose of
Foreign keys is to maintain data integrity and allow navigation between two different instances
of an entity. It acts as a cross-reference between two tables as it references the primary key of
another table.
Composite Key:
Sometimes, a table might not have a single column/attribute that uniquely identifies all the
records of a table. To uniquely identify rows of a table, a combination of two or more
columns/attributes can be used. It still can give duplicate values in rare cases. So, we need to
find the optimal set of attributes that can uniquely identify rows in a table.
It acts as a primary key if there is no primary key in a table
Two or more attributes are used together to make a composite key.
Different combinations of attributes may give different accuracy in terms of identifying
the rows uniquely.
Alternate Key:
The candidate key other than the primary key is called an alternate key.
All the keys which are not primary keys are called alternate keys.
It is a secondary key.
It contains two or more fields to identify two or more records.
These values are repeated.
Surrogate key:
SURROGATE KEYS is An artificial key which aims to uniquely identify each record is called a
surrogate key. This kind of partial key in dbms is unique because it is created when you don’t
have any natural primary key.
1. Domain Constraints
1. Every domain must contain atomic values(smallest indivisible units) which means
composite and multi-valued attributes are not allowed.
2. We perform a datatype check here, which means when we assign a data type to a column
we limit the values that it can contain. Eg. If we assign the datatype of attribute age as int,
we can’t give it values other than int datatype.
Example:
EID Name Phone
123456789
01 Bikash Dutta
234456678
Explanation: In the above relation, Name is a composite attribute and Phone is a multi-values
attribute, so it is violating domain constraint.
Entity Integrity Constraint is used to ensure that the primary key cannot be null. A primary key is
used to identify individual records in a table and if the primary key has a null value, then we
can't identify those records. There can be null values anywhere in the table except the primary
key column.
Example:
EID Name Phone
01 Bikash 9000900099
02 Paul 600000009
Explanation: In the above relation, EID is made the primary key, and the primary key can’t
take NULL values but in the third tuple, the primary key is null, so it is violating Entity
Integrity constraints.
In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of
Table 2, then every value of the Foreign Key in Table 1 must be null or be available in Table 2.
4. Key constraints:
Keys are the set of entities that are used to identify an entity within its entity set uniquely. There
could be multiple keys in a single entity set, but out of these multiple keys, only one key will be
the primary key. A primary key can only contain unique and not null values in the relational
database table.
NOT NULL:
NOT NULL constraint makes sure that a column does not hold NULL value. When we
don’t provide value for a particular column while inserting a record into a table, it takes
NULL value by default. By specifying NULL constraint, we can be sure that a particular
column(s) cannot have NULL values.
Example:
CREATE TABLE STUDENT(
ID INT NOT NULL,
NAME VARCHAR (35) NOT NULL,
AGE INT NOT NULL,
PRIMARY KEY (ID)
);
UNIQUE:
UNIQUE Constraint enforces a column or set of columns to have unique values. If a column has
a unique constraint, it means that particular column cannot have duplicate values in a table.
CHECK:
This constraint is used for specifying range of values for a particular column of a table.
When this constraint is being set on a column, it ensures that the specified column must
have the value falling in the specified range.
DEFAULT:
The DEFAULT constraint provides a default value to a column when there is no value
provided while inserting a record into a table.
PRIMARY KEY:
Primary key uniquely identifies each record in a table. It must have unique values and cannot
contain nulls. The primary key has automatically UNIQUE and NOT NULL constraints applied
to it. It is usually used to index the table or uniquely identify each tuple in the table.
We can say that:
PRIMARY KEY CONSTRAINT = UNIQUE CONSTRAINT + NOTNULL CONSTRAINT
We can retrieve rows corresponding to students who are younger than 18 withthe following SQL
query:
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:
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 :
Example:
Example:
Views in DBMS:
• Writing Complex queries and Securing Database access is very challenging for any
Database Developer and Database Administrator.
• Sometimes SQL queries get very complicated by joins, Group By clauses, and other
referential dependencies, So those Types of queries can be simplified to proxy data or
virtual data which simplifies the queries.
• Suppose, the user needs only 2 columns of data, so instead of giving him access to the
whole table in the database, the Database Administrator can easily create a virtual table
of 2 columns that the user needs using the views.
• This will not give full access to the table and the user is only seeing the projection of
only 2 columns and it keeps the database secure.
• View is a virtual table that contains rows and columns, just like a real table.
• It is used to hide complexity of query from user.
• A virtual table does not exist physically, it is created by a SQL statement that joins one or
more tables.
• Views in DBMS can be visualized as virtual tables that are formed by original tables
from the database.
Creating Views:
• Database views are created using the CREATE VIEW statement. Views can be created
from a single table, multiple tables or another view.
• To create a view, a user must have the appropriate system privilege according to the
specific implementation.
Syntax:
• CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE
[condition];
Syntax
DROP VIEW view_name;
Example:
If we want to delete the View MarksView, we can do this as:
DROP VIEW MarksView;
• Inserting a row in the view takes the same syntax as we use to insert a row in a simple
table.
Syntax:
• INSERT INTO ViewName(column1, column2,..) VALUES(value1, value2,..);
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:
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:
The WITH CHECK OPTION clause in SQL is a very useful clause for views. It is applicable
to an updatable view. If the view is not updatable, then there is no meaning of including this
clause in the CREATE VIEW statement.
The WITH CHECK OPTION clause is used to prevent the insertion of rows in the view
where the condition in the WHERE clause in CREATE VIEW statement is not satisfied.
If we have used the WITH CHECK OPTION clause in the CREATE VIEW statement, and
if the UPDATE or INSERT clause does not satisfy the conditions then they will return an
error.
Example: In the below example we are creating a View SampleView from StudentDetails
Table with WITH CHECK OPTION clause.
CREATE VIEW SampleView AS SELECT S_ID, NAME FROM StudentDetails WHERE NAME IS NOT
NULL WITH CHECK OPTION;
In this View if we now try to insert a new row with null value in the NAME column then it
will give an error because the view is created with the condition for NAME column as NOT
NULL.
Select (σ):
• It is used to retrieve tuples(rows) from the table where the given condition is satisfied.
• It is a unary operator means it requires only one operand.
• Where σ is used to represent SELECTION,R is used to represent RELATION
Example:
Query Used :
σ Name and Age>21 (Student_Details)
Ex 2:
Query:
σ Customer_City="Agra" (CUSTOMER)
Project (∏):
Example:
Query Used :
πMarks(Student_Details)
Example 2:
Query:
∏ Customer_Name, Customer_City (CUSTOMER)
Output
Union (∪):
Union operation is done by Union Operator which is represented by "union"(∪).
It is the same as the union operator from set theory, i.e., it selects all tuples from both relations
but with the exception that for the union of two relations/tables both relations must have the
same set of Attributes.
• It is a binary operator as it requires two operands.
• Also, two things need to keep in mind while applying union operation are :
Both the relations compulsory to have same number of attributes.
Both the relations compulsory to have same domain for attributes.
Consider the two tables with relations X1(Name, Age) and X2(Name, Age).
• If we wish to apply the union operation, then it can be done by
Example:
Query:
Example:
Query:
RXS
Query:
JOINS in DBMS
Databases usually have more than one table. JOINs are an SQL construction used to join data
from two or more tables. When you want to use columns from two tables in a result table, the
easiest way to do it is to write a JOIN query. JOIN Keyword is used in SQL queries for joining
two or more tables.
Inner Join:
This is a simple JOIN in which the result is based on matched data as per the equality condition
specified in the SQL query.
Inner Join Syntax is,
Example;
Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and same
datatype present in both the tables to be joined.
The syntax for Natural Join is,
Example:
Output:
OUTER JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
1. Left Outer Join
Query:
SELECT * FROM song LEFT JOIN artist ON song.artist_id=artist.artist_id
The right outer join returns a resultset table with the matched data from the two tables being
joined, then the remaining rows of the right table and null for the remaining left table's columns.
Syntax for Right Outer Join is,
Query:
SELECT * FROM song RIGHT JOIN artist ON song.artist_id=artist.artist_id
Output:
The full outer join returns a resultset table with the matched data of two table then remaining
rows of both left table and then the right table.
Syntax:
Query:
SELECT * FROM song FULL JOIN artist ON song.artist_id=artist.artist_id
Output:
Relational Calculus
Procedural Language - Those Languages which clearly define how to get the required results
from the Database are called Procedural Language.
Relational algebra is a Procedural Language.
Declarative Language - Those Language that only cares about What to get from the database
without getting into how to get the results are called Declarative Language.
Relational Calculus is a Declarative Language.
• Table: Student
Last_Name
---------
Singh
• Query to display all the details of students where Last name is ‘Singh’
First_Name Age
---------- ----
Ajeet 30
Chaitanya 31
Carl 28