0% found this document useful (0 votes)
24 views

Module II DBMS

The document discusses logical database design and querying relational data in a database management system (DBMS). Logical databases organize data in a hierarchical tree structure and use Open SQL statements to read data. Relational database queries retrieve data from tables using the SELECT statement and WHERE clause to filter rows.

Uploaded by

diwewe9515
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Module II DBMS

The document discusses logical database design and querying relational data in a database management system (DBMS). Logical databases organize data in a hierarchical tree structure and use Open SQL statements to read data. Relational database queries retrieve data from tables using the SELECT statement and WHERE clause to filter rows.

Uploaded by

diwewe9515
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Integrity Constraints

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.

Types of Integrity Constraint

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:

3. Referential Integrity Constraints


o A referential integrity constraint is specified between two tables.
o 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.

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.

CREATE TABLE Customers (


CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50) NOT NULL,
ContactName VARCHAR(50),
Country VARCHAR(50) DEFAULT 'USA',
CONSTRAINT chk_country CHECK (Country IN ('USA', 'Canada', 'Mexico')),
CONSTRAINT uq_contact UNIQUE (ContactName)
);

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
Amount DECIMAL(10,2),
CONSTRAINT fk_customer FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

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.

CREATE TRIGGER trg_check_amount


BEFORE INSERT ON Orders
FOR EACH ROW
BEGIN
IF NEW.Amount < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Amount must be greater than or equal to zero';
END IF;
END;

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.

INSERT INTO Customers (CustomerID, CustomerName, ContactName, Country)


VALUES (1, 'ABC Company', 'John Doe', 'USA');

INSERT INTO Customers (CustomerID, CustomerName, ContactName, Country)


VALUES (2, 'XYZ Company', 'Jane Smith', 'Australia');

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.

GRANT SELECT, INSERT, UPDATE, DELETE ON Customers TO User1;

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.

Querying Relational Data in DBMS


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;

Alias Table Syntax in Mysql

SELECT column_name(s)
FROM table_name AS alias_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;
Logical Database Design

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.

Structure of Logical database

Features of Logical Database:


In this section, let us look at some features of a logical database:
 We can select only that type of Data that we need.
 Data Authentication is done in order to maintain security.
 Logical Database uses hierarchical Structure due to this data integrity is maintained.
Goal Of Logical Database:
The goal of Logical Database is to create well-structured tables that reflect the need of the user. The tables of
the Logical database store data in a non-redundant manner and foreign keys will be used in tables so that
relationships among tables and entities will be supported.
Top VS Code Extensions for Web Developer in 2024!! GeeksforGeeks

Tasks Of Logical Database:


Below is some important task of Logical Database:
 With the help of the Logical database, we will read the same data from multiple programs.
 A logical database defines the same user interface for multiple programs.
 Logical Database ensures the Authorization checks for the centralized sensitive database.
 With the help of a Logical Database, Performance is improved. Like in Logical Database we will use joins
instead of multiple SELECT statements, which will improve response time and this will increase the
Performance of Logical Database.
Data View Of Logical Database:
Logical Database provides a particular view of Logical Database tables. A logical database is appropriately
used when the structure of the Database is Large. It is convenient to use flow i.e
 SELECT
 READ
 PROCESS
 DISPLAY
In order to work with databases efficiently. The data of the Logical Database is hierarchical in nature. The
tables are linked to each other in a Foreign Key relationship.
Diagrammatically, the Data View of Logical Database is shown as:

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.

Advantages Of Logical Database:


Let us look at some advantages of the logical database:
 In a Logical database, we can select meaningful data from a large amount of data.
 Logical Database consists of Central Authorization which checks for Database Accesses is Authenticated
or not.
 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.
 Easy to understand user interfaces.
 Logical Database firstly check functions which further check that user input is complete, correct, and
plausible.
Disadvantages Of Logical Database:
This section shows the disadvantages of the logical database:
 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.
 In Logical Database ENDGET command doesn’t exist due to this the code block associated with an event
ends with the next event statement.

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

STU_ID NAME ADDRESS

1 Stephan Delhi

2 Kathrin Noida

3 David Ghaziabad

4 Alina Gurugram

Student_Marks

STU_ID NAME MARKS AGE

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:

1. CREATE VIEW view_name AS


2. SELECT column1, column2.....
3. FROM table_name
4. WHERE condition;

2. Creating View from a single table

In this example, we create a View named DetailsView from the table Student_Detail.

Query:

1. CREATE VIEW DetailsView AS


2. SELECT NAME, ADDRESS
3. FROM Student_Details
4. WHERE STU_ID < 4;

Just like table query, we can query the view to view the data.

1. SELECT * FROM DetailsView;

Output:

NAME ADDRESS

Stephan Delhi

Kathrin Noida

David Ghaziabad

ADVERTISEMENT

3. Creating View from multiple tables

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:

1. CREATE VIEW MarksView AS


2. SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student_Marks.MARKS
3. FROM Student_Detail, Student_Mark
4. WHERE Student_Detail.NAME = Student_Marks.NAME;

To display data of View MarksView:

1. SELECT * FROM MarksView;

NAME ADDRESS MARKS

Stephan Delhi 97

Kathrin Noida 86

David Ghaziabad 74

Alina Gurugram 90

4. Deleting View

A view can be deleted using the Drop View statement.

Syntax

1. DROP VIEW view_name;

Example:

If we want to delete the View MarksView, we can do this as:

1. DROP VIEW MarksView;

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.

SQL ALTER TABLE


The ALTER TABLE statement in Structured Query Language allows you to add, modify, and delete columns
of an existing table. This statement also allows database users to add and remove various SQL constraints
on the existing tables.

Any user can also change the name of the table using this statement.

ALTER TABLE ADD Column statement in SQL


In many situations, you may require to add the columns in the existing table. Instead of creating a whole
table or database again you can easily add single and multiple columns using the ADD keyword.

Syntax of ALTER TABLE ADD Column statement in SQL


1. ALTER TABLE table_name ADD column_name column-definition;

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);

Examples of ALTER TABLE ADD Column statement in SQL


Here, we have taken the following two different SQL examples, which will help you how to add the single
and multiple columns in the existing table using ALTER TABLE statement:

Example 1: Let's take an example of a table named Cars:

Car Name Car Color Car Cost

Hyundai Creta White 10,85,000

Hyundai Venue White 9,50,000

Hyundai i20 Red 9,00,000

Kia Sonet White 10,00,000

Kia Seltos Black 8,00,000

Swift Dezire Red 7,95,000

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:

1. ALTER TABLE Cars ADD Car_Model Varchar(20);

This statement will add the Car_Model column to the Cars table.

Example 2: Let's take an example of a table named Employee:


Emp_Id Emp_Name Emp_Salary Emp_City

201 Abhay 25000 Goa

202 Ankit 45000 Delhi

203 Bheem 30000 Goa

204 Ram 29000 Goa

205 Sumit 40000 Delhi

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:

1. ALTER TABLE Employee ADD ( Emp_ContactNo. Number(13), Emp_EmailID varchar(50) ;

This statement will add Emp_ContactNo. and Emp_EmailID columns to the Employee table.

ALTER TABLE MODIFY Column statement in SQL


The MODIFY keyword is used for changing the column definition of the existing table.

ADVERTISEMENT

Syntax of ALTER TABLE MODIFY Column statement in SQL


1. ALTER TABLE table_name MODIFY column_name column-definition;

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:

1. ALTER TABLE table_name


2. MODIFY (column_Name1 column-definition,
3. column_Name2 column-definition,
4. .....
5. column_NameN column-definition);
Examples of ALTER TABLE MODIFY Column statement in SQL
Here, we have taken the following two different SQL examples, which will help you how to modify single
and multiple columns of the existing table using ALTER TABLE statement:

Example 1: Let's take an example of a table named Cars:

Car Name Car Color Car Cost

Hyundai Creta White 10,85,000

Hyundai Venue White 9,50,000

Hyundai i20 Red 9,00,000

Kia Sonet White 10,00,000

Kia Seltos Black 8,00,000

Swift Dezire Red 7,95,000

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:

1. ALTER TABLE Cars ADD Car_Color Varchar(50);

Example 2: Let's take an example of a table named Employee:

Emp_Id Emp_Name Emp_Salary Emp_City

201 Abhay 25000 Goa

202 Ankit 45000 Delhi

203 Bheem 30000 Goa


204 Ram 29000 Goa

205 Sumit 40000 Delhi

Table: Employee

o Suppose, you want to modify the datatypes of two


columns Emp_ContactNo. and Emp_EmailID of the above Employee table. For this, you have to
type the following query in the SQL:

1. ALTER TABLE Employee ADD ( Emp_ContactNo. Int, Emp_EmailID varchar(80) ;


ADVERTISEMENT

ALTER TABLE DROP Column statement in SQL


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.

ADVERTISEMENT

Syntax of ALTER TABLE DROP Column statement in SQL


1. ALTER TABLE table_name DROP Column column_name ;

Examples of ALTER TABLE DROP Column statement in SQL


Here, we have taken the following two different SQL examples, which will help you how to delete a
column from the existing table using ALTER TABLE statement:

ADVERTISEMENT

Example 1: Let's take an example of a table named Cars:

Car Name Car Color Car Cost

Hyundai Creta White 10,85,000

Hyundai Venue White 9,50,000

Hyundai i20 Red 9,00,000


Kia Sonet White 10,00,000

Kia Seltos Black 8,00,000

Swift Dezire Red 7,95,000

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:

1. ALTER TABLE Cars DROP COLUMN Car_Color ;


o Let's check using the following statement that the Car_Color column is deleted from the table or
not:

1. SELECT * FROM Cars;

Car Name Car Cost

Hyundai Creta 10,85,000

Hyundai Venue 9,50,000

Hyundai i20 9,00,000

Kia Sonet 10,00,000

Kia Seltos 8,00,000

Swift Dezire 7,95,000

ADVERTISEMENT

ADVERTISEMENT

Table: Cars

Example 2: Let's take an example of a table named Employee:


Emp_Id Emp_Name Emp_Salary Emp_City

201 Abhay 25000 Goa

202 Ankit 45000 Delhi

203 Bheem 30000 Goa

204 Ram 29000 Goa

205 Sumit 40000 Delhi

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:

1. ALTER TABLE Cars DROP COLUMN Emp_Salary ;


2. ALTER TABLE Cars DROP COLUMN Emp_City ;

ALTER TABLE RENAME Column statement in SQL


The RENAME keyword is used for changing the name of columns or fields of the existing table.

Syntax of ALTER TABLE RENAME Column statement in SQL


1. ALTER TABLE table_name RENAME COLUMN old_name to new_name;

Examples of ALTER TABLE RENAME Column statement in SQL


Here, we have taken the following two different SQL examples, which will help you how to change the
name of a column of the existing table using ALTER TABLE statement:

Example 1: Let's take an example of a table named Cars:

Car Name Car Color Car Cost

Hyundai Creta White 10,85,000


Hyundai Venue White 9,50,000

Hyundai i20 Red 9,00,000

Kia Sonet White 10,00,000

Kia Seltos Black 8,00,000

Swift Dezire Red 7,95,000

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:

1. ALTER TABLE Cars RENAME COLUMN Car_Color to Colors;

This statement will change the name of a column of the Cars table. To see the changes, you have to type
the following query:

1. SELECT * FROM Cars;

Car Name Car Color Car Cost

Hyundai Creta White 10,85,000

Hyundai Venue White 9,50,000

Hyundai i20 Red 9,00,000

Kia Sonet White 10,00,000

Kia Seltos Black 8,00,000

Swift Dezire Red 7,95,000

Table: Cars
Example 2: Let's take an example of a table named Employee:

Emp_Id Emp_Name Emp_Salary Emp_City

201 Abhay 25000 Goa

202 Ankit 45000 Delhi

203 Bheem 30000 Goa

204 Ram 29000 Goa

205 Sumit 40000 Delhi

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:

1. ALTER TABLE Employee RENAME COLUMN Emp_City to Emp_Address;

This statement will change the name of a column of the Employee table. To see the changes, you have to
type the following query:

1. SELECT * FROM Employee;

Emp_Id Emp_Name Emp_Salary Emp_Address

201 Abhay 25000 Goa

202 Ankit 45000 Delhi

203 Bheem 30000 Goa

204 Ram 29000 Goa

205 Sumit 40000 Delhi


Table: Employee

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:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).

1. Notation: σ p(r)

Where:

σ is used for selection prediction


r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR and NOT. These
relational can use as relational operators like =, ≠, ≥, <, >, ≤.

For example: LOAN Relation

BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000


Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300

Input:

1. σ BRANCH_NAME="perryride" (LOAN)

Output:

BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500

Perryride L-16 1300

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 ∏.

1. Notation: ∏ A1, A2, An (r)

Where

A1, A2, A3 is used as an attribute name of relation r.

Example: CUSTOMER RELATION


NAME STREET CITY

Jones Main Harrison

Smith North Rye

Hays Main Harrison

Curry North Rye

Johnson Alma Brooklyn

Brooks Senator Brooklyn

Input:

1. ∏ NAME, CITY (CUSTOMER)

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

A union operation must hold the following condition:

o R and S must have the attribute of the same number.


o Duplicate tuples are eliminated automatically.

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:

1. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)

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

Example: Using the above DEPOSITOR table and BORROW table

Input:

1. ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)

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

Example: Using the above DEPOSITOR table and BORROW table

Input:

1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)


Output:

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

EMP_ID EMP_NAME EMP_DEPT

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:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAM

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)

Tuple Relational Calculus (TRC)

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:

A Query in the tuple relational calculus is expressed as following notation

1. {T | P (T)} or {T | Condition (T)}

Where
ADVERTISEMENT

T is the resulting tuples

P(T) is the condition used to fetch T.

For example:

1. { T.name | Author(T) AND T.article = 'database' }

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:

1. { R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}

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:

1. { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}

Where

a1, a2 are attributes


P stands for formula built by inner attributes

For example:

1. {< article, page, subject > | ∈ javatpoint ∧ subject = 'database'}

You might also like