0% found this document useful (0 votes)
46 views9 pages

m2 and m3 Ans Dbms Imp 2ia

The document defines key terms related to the relational data model like relational database schemas, degree, and cardinality. It also explains relational algebra operations like union, intersection, and set difference with examples. Additionally, it discusses entity integrity constraints, referential integrity constraints, and how referential integrity is implemented in SQL. The document provides examples to illustrate a referential integrity constraint between tables. It also explains unary and binary relation operations with examples and describes the process of mapping an entity-relationship diagram to relational tables through various steps.

Uploaded by

Mahesh A.V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views9 pages

m2 and m3 Ans Dbms Imp 2ia

The document defines key terms related to the relational data model like relational database schemas, degree, and cardinality. It also explains relational algebra operations like union, intersection, and set difference with examples. Additionally, it discusses entity integrity constraints, referential integrity constraints, and how referential integrity is implemented in SQL. The document provides examples to illustrate a referential integrity constraint between tables. It also explains unary and binary relation operations with examples and describes the process of mapping an entity-relationship diagram to relational tables through various steps.

Uploaded by

Mahesh A.V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Module 2 (dbms)

1. Define the following term:-

Ans:-
i) Relational Data Model: The relational data model is a conceptual model used in database
management systems (DBMS) that organizes data into tables (relations) consisting of rows
and columns.

ii) Relational Database Schemas: Relational database schemas define the structure and
organization of tables (relations) within a relational database. It specifies the attributes
(columns) that comprise each table and defines the relationships (foreign key constraints)
between tables.

iii) Degree: In the context of relational databases, the degree refers to the number of attributes
(columns) within a relation (table). It indicates the arity or the number of components in a
tuple (row) of the relation.

iv) Cardinality: Cardinality in the context of databases refers to the uniqueness of data values
within a column or relationship. It describes the number of instances or occurrences of one
entity (or record) that are associated with a single instance of another entity (or record)
through a relationship.

2. Explain Relational Algebra Operations from Set Theory with an example.


Ans:-
i) UNION: The result of this operation, denoted by R S, is a relation that includes all tuples that are
either in R or in S or in both R and S. Duplicate tuples are eliminated.
ii) INTERSECTION: The result of this operation, denoted by R S, is a relation that includes all
tuples that are in both R and S.
iii) SET DIFFERENCE (or MINUS): The result of this operation, denoted by R S, is a relation that
includes all tuples that are in R but not in S.
3. What are the different constants used in the relational data model? Explain entity integrity constraints
of referential integrity with an example. how referential integrity is implemented in SQL.
Ans:-
In the relational data model, there are several constants (constraints) used to maintain data integrity
and enforce rules within the database. These constraints ensure that data remains consistent and
accurate. The commonly used constraints include:

1. Entity Integrity Constraints:


Entity integrity constraints ensure that each row (tuple) in a table is uniquely identifiable. The
primary key constraint is a type of entity integrity constraint that ensures that the values in one or
more columns (attributes) of a table are unique and not null, thereby uniquely identifying each row in
the table.

2. Referential Integrity Constraints:


Referential integrity constraints maintain the consistency of relationships between tables by
ensuring that references (foreign keys) between tables remain valid. This constraint ensures that any
foreign key value in a child table must match a primary key value in the parent table or be null.

Now, let's explain referential integrity with an example:

Consider two tables:


Employees (with columns: EmployeeID - primary key, Name, DepartmentID - foreign key)
Departments(with columns: DepartmentID - primary key, DepartmentName)

The referential integrity constraint ensures that every value in the DepartmentID column of the
Employees table exists in the DepartmentID column of the Departments table.

Example:

i) Employees table:

| EmployeeID | Name | DepartmentID |


|------------|-----------|--------------|
|1 | John Doe | 101 |
|2 | Jane Smith| 102 |
|3 | Bob Brown | 103 |

ii) Departments table:

| DepartmentID | DepartmentName |
|--------------|----------------|
| 101 | Sales |
| 102 | Marketing |
| 103 | Finance |

In this example, if a record with DepartmentID 104 were to be added to the Employees table, it
would violate referential integrity because there is no corresponding DepartmentID of 104 in the
Departments table.

Referential integrity is implemented in SQL using foreign key constraints. In the example above, the
DepartmentID column in the Employees table would have a foreign key constraint referencing the
DepartmentID column in the Departments table. This constraint ensures that every DepartmentID
value in the Employees table must exist in the Departments table or be null. Here's how it would be
implemented in SQL:

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
DepartmentID INT,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

This SQL statement creates the Employees table with a foreign key constraint on the DepartmentID
column, referencing the DepartmentID column in the Departments table.

4. Write diagram the er and schema diagram


i) movie database
a. ER diagram

b. schema diagram
ii) order database :-
a. ER diagram :-

b. schema diagram:-

5. Explain Unary and binary relation operations with an example in detail


Ans:- Unary and binary relation operations are fundamental concepts in relational algebra, which is a
mathematical system used for manipulating and querying relational databases. These operations are
used to perform various transformations on relations (tables) within a database.

a. Unary Relation Operation:

A unary relation operation operates on a single relation (table). It takes one relation as input
and produces another relation as output.

There are several unary relation operations, but some of the common ones include:

i. Selection (σ): Selects a subset of rows from a relation that satisfy a specified
condition.
Eg:- σ(DepartmentID = 101)(Employees)
ii. Projection (π): Selects a subset of columns (attributes) from a relation.
Eg:- π(Name, DepartmentID)(Employees)

iii. Union (∪): Combines two relations to create a new relation containing all the tuples
from both input relations, removing duplicates.
Eg:- Employees ∪ Employees

iv. Difference (−): Takes two relations as input and outputs tuples that are in the first
relation but not in the second relation.
Eg:- Employees - σ(DepartmentID = 101)(Employees)

v. Rename (ρ): Changes the name of attributes in a relation.


Eg:- ρ(EmployeeName/Name)(Employees)

b. Binary Relation Operation:

A binary relation operation operates on two relations (tables). It takes two relations as input
and produces another relation as output.
Some common binary relation operations include:

i. Cartesian Product (×): Generates a new relation that contains all possible
combinations of tuples from two input relations.
Eg: - Employees × Departments

ii. Join (⨝): Combines tuples from two relations based on a related attribute.
Eg: - Employees ⨝(DepartmentID = DepartmentID) Departments

iii. Intersection (∩): Outputs tuples that are common to both input relations.
Eg: - σ(DepartmentID = 101)(Employees) ∩ σ(DepartmentID = 102)(Employees)

iv. Division (÷): Outputs tuples from the first relation that are related to all tuples in the
second relation.
Eg: - Employees ÷ Departments

6. Explain the ER to relation mapping algorithm with a suitable example for each step
Ans:- The process of mapping an Entity-Relationship (ER) model to relational tables involves
converting the conceptual model represented by entities, attributes, and relationships into a physical
model represented by tables, columns, and foreign key constraints in a relational database.

i) Identify Entities and Attributes: Identify entities and their attributes from the ER diagram.
Example: "Student" entity with attributes like "StudentID" and "Name."\
ii) Create Tables for Entities: Create a table for each entity with its attributes.
Example: CREATE TABLE Student ( StudentID INT PRIMARY KEY, Name
VARCHAR(50) );
iii) Identify Relationships: Identify relationships between entities.
Example: "Registration" relationship between "Student" and "Course."
iv) Map Relationships to Tables: Represent relationships using foreign keys
Example: CREATE TABLE Registration ( StudentID INT, CourseID INT, FOREIGN KEY
(StudentID) REFERENCES Student(StudentID), FOREIGN KEY (CourseID)
REFERENCES Course(CourseID) );
v) Resolve Many-to-Many Relationships: Create separate tables for many-to-many
relationships.
Example: "Registration" table for the many-to-many relationship between "Student" and
"Course."
vi) Add Integrity Constraints: Define primary keys and foreign keys for data consistency.
Example: Adding primary keys and foreign keys as necessary.
vii) Normalize the Database: Apply normalization techniques if needed to eliminate redundancy.

7. Explain different types of update operations and show an example of a violation of referential
integrity constraint each with an update operation.
Ans:-
i) The INSERT Command
INSERT is used to add a single tuple (row) to a relation (table). Must specify the relation name and a
list of values for the tuple. The values should be listed in the same order in which the corresponding
attributes were specified in the CREATE TABLE command.
Example:-
INSERT INTO EMPLOYEE VALUES ( ‘Richard’, ‘K’, ‘Marini’, ‘653298653’, ‘1962-12-30’, ‘98
Oak Forest, Katy, TX’, ‘M’, 37000, ‘653298653’, 4 );
ii) The DELETE Command
The DELETE command removes tuples from a relation. It includes a WHERE clause, similar to that
used in an SQL query, to select the tuples to be deleted. Tuples are explicitly deleted from only one
table at a time. Depending on the number of tuples selected by the condition in the WHERE clause,
zero, one, or several tuples can be deleted by a single DELETE command.
Example:-
DELETE FROM EMPLOYEE WHERE Ssn = ‘123456789’;
iii) The UPDATE Command
The UPDATE command is used to modify attribute values of one or more selected tuples. As in the
DELETE command, a WHERE clause in the UPDATE command selects the tuples to be modified
from a single relation.
Example:-
UPDATE PROJECT, SET Plocation = ‘Bellaire’, Dnum = 5 WHERE Pnumber = 10;

Module 3(dbms)

1) Explain in detail the insert, delete and update statements in sql with examples
Ans:-
a. INSERT Statement:
The INSERT statement is used to add new records (rows) into a table.
Example:

INSERT INTO Students (StudentID, Name)


VALUES (1, 'John Doe');

b. DELETE Statement:
The DELETE statement is used to remove existing records (rows) from a table.
Example:

DELETE FROM Students


WHERE StudentID = 1;
c. UPDATE Statement:
The UPDATE statement is used to modify existing records (rows) in a table.
Example:

UPDATE Students
SET Name = 'Jane Smith'
WHERE StudentID = 1;

2) Explain ambiguity attribute names, aliasing and tuple variables in sql

1. **Ambiguity with Attribute Names**:


- Consider a query involving two tables, "Employees" and "Departments," both of
which have a column named "ID." If the query attempts to select the "ID" column
without specifying the table name or alias, SQL will not know which "ID" column to
retrieve, leading to ambiguity.

2. **Aliasing**:
- Aliasing involves giving a temporary name (alias) to a table or column in a query
to resolve ambiguity and simplify the query.
- Table Aliasing: Assigning a short, temporary name to a table within the query.
SELECT e.ID, e.Name, d.Name AS Department
FROM Employees e
JOIN Departments d ON e.DepartmentID = d.ID;
- Column Aliasing: Giving a temporary name to a column in the result set.
SELECT FirstName AS First_Name, LastName AS Last_Name
FROM Employees;

3. **Tuple Variables**:Tuple variables are used in nested queries to reference the


result set of an outer query within the inner query.

SELECT e.ID, e.Name


FROM Employees e
WHERE e.DepartmentID IN (
SELECT DepartmentID
FROM Departments d
WHERE d.Location = e.Location
);
```
3)Explain the following terms with example
i. Specifying primary key in sql
Ans:- In SQL, a primary key constraint is used to uniquely identify each record in a
table. It ensures that the values in the specified column(s) are unique and not null.
Eg:- CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50)
);

ii. Specifying referential integrity constarints in sql


Ans;- Referential integrity constraints in SQL ensure the consistency of relationships
between tables by enforcing rules regarding the referential integrity of foreign key
values.
Eg:-
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(50),
StudentID INT,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
);

iii. Distinct clause in sql


ans:-
The DISTINCT clause is used in SQL queries to eliminate duplicate rows from the
result set.
Eg:-
SELECT DISTINCT DepartmentID
FROM Employees;

v. Select-from-where clause
Ans:- The SELECT-FROM-WHERE clause is used in SQL queries to specify the
columns to retrieve (SELECT), the table(s) from which to retrieve data (FROM),
and optional conditions to filter the data (WHERE).
Eg:-
SELECT Name
FROM Students
WHERE StudentID < 100;
vi. Ordering of query results in sql
Ans:- The ORDER BY clause is used in SQL queries to sort the result set based on
one or more columns.
Eg:-
SELECT Name
FROM Students
ORDER BY StudentID ASC;

3) Explain in detail substring pattern matching with example


Ans:-
a. LIKE Operator: The LIKE operator is used to search for a specified pattern within
a string column.
Eg:-
SELECT Name
FROM Employees
WHERE Name LIKE 'J%';

b. SUBSTRING Function: The SUBSTRING function is used to extract a substring


from a string column.
Eg:-
SELECT SUBSTRING(ProductCode FROM 2 FOR 3)
FROM Products;

c. REGEXP_LIKE Function:- The REGEXP_LIKE function is used for pattern


matching using regular expressions.
Eg:-
SELECT Email
FROM Employees
WHERE REGEXP_LIKE(Email, 'gmail\.com');

You might also like