Module II DBMS
Module II DBMS
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
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:
Enforcing integrity constraints in SQL
Enforcing integrity constraints is essential for ensuring the accuracy and consistency of data in a database. In SQL,
there are several ways to enforce integrity constraints, including:
1. Defining Constraints: One way to enforce integrity constraints is to define constraints when creating tables. For
example, you can specify a primary key or a unique constraint on a column to ensure that the values in the column
are unique. Similarly, you can define a foreign key constraint to enforce referential integrity between tables.
In this example, we are creating two tables, Customers and Orders. We are defining several constraints:
Customers table: We are defining a primary key constraint on the CustomerID column, a NOT NULL
constraint on the CustomerName column, and a default value constraint on the Country column. We are also defining
a check constraint to ensure that the Country column only contains values of 'USA', 'Canada', or 'Mexico', and a
unique constraint on the ContactName column to ensure that each contact name is unique.
Orders table: We are defining a primary key constraint on the OrderID column and a foreign key constraint
on the CustomerID column to enforce referential integrity with the Customers table.
2. Adding Constraints to Existing Tables: If you need to add constraints to an existing table, you can use the
ALTER TABLE statement to add constraints. For example, you can add a CHECK constraint to ensure that a column
only contains values within a specified range.
ALTER TABLE Employees ADD CONSTRAINT chk_age CHECK (Age >= 18 AND Age <= 65);
In this example, we are adding a check constraint to an existing Employees table. The constraint checks that the Age
column contains values between 18 and 65.
3. Using Triggers: Triggers can be used to enforce more complex constraints that cannot be expressed using simple
constraints. For example, you can create a trigger that checks for the existence of certain data in related tables
before allowing a record to be inserted or updated.
In this example, we are creating a trigger called trg_check_amount that is executed before an insert operation on the
Orders table. The trigger checks if the amount being inserted is greater than or equal to zero. If it is not, an error
message will be generated and the insert operation will be cancelled.
4. Data Validation: Data validation is the process of verifying that data entered into a database meets certain
requirements. This can be done manually, using data validation software, or by writing custom scripts to validate data.
In this example, we are manually validating data being inserted into the Customers table. We are inserting a record
with a valid Country value of 'USA', and a record with an invalid Country value of 'Australia'. The second insert
operation will fail due to the check constraint defined on the Country column.
5. Access Control: Access control refers to the process of controlling who has access to a database and what
actions they are allowed to perform. By controlling access to a database, you can prevent unauthorized changes to
the data and ensure that the integrity of the data is maintained.
In this example, we are granting User1 permission to select, insert, update, and delete records from the Customers
table. By controlling access to the database, we can prevent unauthorized changes to the data and ensure that the
integrity of the data is maintained.
If you want to select all the fields available in the table, use the following syntax:
Syntax in Mysql
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:
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:
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(s)
FROM table_name AS alias_name;
Example:
SELECT studentID AS ID,
FROM students AS S;
Example:
A Logical Database is a special type of ABAP (Advance Business Application and Programming) that is used
to retrieve data from various tables and the data is interrelated to each other. Also, a logical database provides a
read-only view of Data.
Structure Of Logical Database:
A Logical database uses only a hierarchical structure of tables i.e. Data is organized in a Tree-like Structure
and the data is stored as records that are connected to each other through edges (Links). Logical Database
contains Open SQL statements which are used to read data from the database. The logical database reads the
program, stores them in the program if required, and passes them line by line to the application program.
Points To Remember:
Tables must have Foreign Key Relationship.
A logical Database consists of logically related tables that are arranged in a hierarchical manner used for
reading or retrieving Data.
Logical Database consist of three main elements:
Structure of Database
Selections of Data from Database
Database Program
If we want to improve the access time on data, then we use VIEWS in Logical Database.
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.
Views in SQL
o Views in SQL are considered as a virtual table. A view also contains rows and columns.
o To create the view, we can select the fields from one or more tables present in the database.
o A view can either have specific rows based on certain condition or all the rows of a table.
Advantages of View:
1. Complexity: Views help to reduce the complexity. Different views can be created on the same
base table for different users.
2. Security: It increases the security by excluding the sensitive information from the view.
3. Query Simplicity: It helps to simplify commands from the user. A view can draw data from
several different tables and present it as a single table.
4. Consistency: A view can present a consistent, unchanged image of the structure of the database.
Views can be used to rename the columns without affecting the base table.
5. Data Integrity: If data is accessed and entered through a view, the DBMS can automatically check
the data to ensure that it meets the specified integrity constraints.
6. Storage Capacity: Views take very little space to store the data.
7. Logical Data Independence: View can make the application and database tables to a certain
extent independent.
Disadvantages of View:
The DML statements which can be performed on a view created using single base table have certain
restrictions are:
1. You cannot INSERT if the base table has any not null column that do not appear in view.
2. You cannot INSERT or UPDATE if any of the column referenced in the INSERT or UPDATE contains
group functions or columns defined by expression.
3. You can't execute INSERT, UPDATE, DELETE statements on a view if with read only option is
enabled.
4. You can't be created view on temporary tables.
5. You cannot INSERT, UPDATE, DELETE if the view contains group functions GROUP BY, DISTINCT or
a reference to a psuedocolumn rownum.
6. You can't pass parameters to the SQL server views.
7. You can't associate rules and defaults with views.
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
A view can be created using the CREATE VIEW statement. We can create a view from a single table or
multiple tables.
Syntax:
In this example, we create a View named DetailsView from the table Student_Detail.
Query:
Just like table query, we can query the view to view the data.
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
ADVERTISEMENT
View from multiple tables can be created by simply include multiple tables in the SELECT statement.
ADVERTISEMENT
In the given example, a view is created named MarksView from two tables Student_Detail and
Student_Marks.
Query:
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
Syntax
Example:
Significance of Views:
Views are highly significant, as they can provide advantages over tasks. Views can represent a subset of
data contained in a table. Consequently they can limit the degree of exposure of the underlying base
table to the outer world. They are used for security purpose in database and act as an intermediate
between real table schemas and programmability. They act as aggregate tables.
Types of Views:
There are two 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.
2. Inline View: An inline view is a view which is created by replacing a subquery in the from clause
which defines the data source that can be referenced in the main query. The sub query must be
given an alias for efficient working.
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:
ADVERTISEMENT
1. ALTER TABLE table_name
2. ADD (column_Name1 column-definition,
3. column_Name2 column-definition,
4. .....
5. column_NameN column-definition);
Table: Cars
ADVERTISEMENT
ADVERTISEMENT
o Suppose, you want to add the new column Car_Model in the above table. For this, you have to
type the following query in the SQL:
This statement will add the Car_Model column to the Cars table.
Table: Employee
o Suppose, you want to add two columns, Emp_ContactNo. and Emp_EmailID, in the above
Employee table. For this, you have to type the following query in the SQL:
This statement will add Emp_ContactNo. and Emp_EmailID columns to the Employee table.
ADVERTISEMENT
This syntax only allows you to modify a single column of the existing table. If you want to modify more
than one column of the table in a single SQL statement, then use the following syntax:
Table: Cars
o Suppose, you want to modify the datatype of the Car_Color column of the above table. For this,
you have to type the following query in the SQL:
Table: Employee
ADVERTISEMENT
ADVERTISEMENT
Table: Cars
o Suppose, you want to delete the Car_Color column from the above table. For this, you have to
type the following query in the SQL:
ADVERTISEMENT
ADVERTISEMENT
Table: Cars
Table: Employee
o Suppose, you want to delete the Emp_Salary and Emp_City column from the above Employee
table. For this, you have to type the following two different queries in the SQL:
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
Example 2: Let's take an example of a table named Employee:
Table: Employee
o Suppose, you want to change the name of the Emp_City column of the above Employee table.
For this, you have to type the following query in the SQL:
This statement will change the name of a column of the Employee table. To see the changes, you have to
type the following query:
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.
1. Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).
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:
o Suppose there are two tuples R and S. The union operation contains all the tuples that are either
in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
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 Suppose there are two tuples R and S. The set intersection operation contains all tuples that are in
both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples that are in
R but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
Input:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the other table.
It is also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
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:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
1. ρ(STUDENT1, STUDENT)
It is a non-procedural query language which is based on finding a number of tuple variables also
known as range variable for which predicate holds true. It describes the desired information
without giving a specific procedure for obtaining that information. The tuple relational calculus
is specified to select the tuples in a relation. In TRC, filtering variable uses the tuples of a
relation. The result of the relation can have one or more tuples.
Notation:
Where
ADVERTISEMENT
For example:
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'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃) and
Universal Quantifiers (∀).
For example:
Output: This query will yield the same result as the previous one.
ADVERTISEMENT
ADVERTISEMENT
Domain Relational Calculus (DRC)
The second form of relation is known as Domain relational calculus. In domain relational
calculus, filtering variable uses the domain of attributes. Domain relational calculus uses the
same operators as tuple calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓ (not). It uses
Existential (∃) and Universal Quantifiers (∀) to bind the variable. The QBE or Query by example
is a query language related to domain relational calculus.
Notation:
Where
For example: