DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
Anna University Regulation: 2021
AD3391-DATABASE DESIGN AND MANAGEMENT
II Year/III Semester
UNIT II - RELATIONAL MODEL AND SQL
QUESTION BANK
Prepared By,
Dr.B.Mahalakshmi, AP/AI&DS
AD3391_DDM
UNIT II RELATIONAL MODEL AND SQL
Relational model concepts -- Integrity constraints -- SQL Data manipulation –
SQL Data definition – Views -- SQL programming.
1. What is a view in SQL?
A view in SQL is essentially a virtual table that provides a way to simplify
complex queries. Unlike a regular table, a view does not store data physically;
instead, it stores the SQL query that generates thedata dynamically whenever the
view is queried. Views can join multiple tables, aggregate data, or provide a
specific subset of data based on certain conditions. Views are commonly used to
encapsulate complex queries, provide data abstraction, and simplify access to data
without exposing the underlying table structures.
Example: If you have a table for Employees and Departments, you could create a
view that combines both tables, showing employees along with their department
names, simplifying the query for users.
2. How are views created in SQL?
● Views are created using the CREATE VIEW statement, followed by a
SELECT query. The SELECT query specifies the data that the view will
represent, and this data is displayed each time the view is queried. Views
are stored in the database metadata, and they can be used like a regular
table in SELECT statements.
Example:
CREATE VIEW
employee_details AS SELECT
e.name, e.salary,
d.department_name FROM
employees e
JOIN departments d ON e.department_id = d.department_id;
This view will allow you to retrieve employee details with their department
names by simply querying SELECT * FROM employee_details.
AD3391_DDM
3. What are the advantages of using views in SQL?
● Simplicity: Views can simplify complex queries. By encapsulating
frequently used queries into a view, you can write simpler SELECT
statements for end-users or developers.Data Security: Views can be used to
provide limited access to data. For instance, you can create a view that only
exposes certain columns of a table, preventing access to sensitive data.
● Abstraction: Views abstract the underlying database structure, making
it easier for users to work with data without worrying about the
complexities of the schema.Reusability: Once a view is defined, it
can be used multiple times across different queries, improving code
reusability and reducing redundancy.
4.Can views be updated in SQL?
● Yes, views can be updated, but only under certain conditions. If the view
directly represents data from one table without complex joins or
aggregates, it can be updated. However, views that combine multiple tables
or use functions (like SUM or COUNT) cannot be updated because there’s
no straightforward way to reflect changes in the underlying tables.
○ Updatable Views: A view that contains a straightforward SELECT
statement with no aggregations or joins is updatable. For example,
if you create a view based on a single table, such as:
CREATE VIEW employee_view AS
SELECT employee_id, name, department
FROM employees; You can then use an
UPDATE statement on this UPDATE
employee_view
SET department =
'Sales' WHERE
employee_id = 101;
5. What is SQL injection and how can it be prevented?
SQL injection is a security vulnerability that occurs when an attacker
inserts or "injects" malicious SQL code into a query. This can allow the
attacker to bypass authentication, access or modify sensitive data, or even
delete data. SQL injection typically occurs when user input is directly
AD3391_DDM
included in SQL queries without proper validation or sanitization.
6. What are stored procedures in SQL?
A stored procedure is a set of precompiled SQL statements that are stored
in the database and can be executed as a unit. Stored procedures can
accept input parameters, perform operations, and return results. They
provide benefits like improved performance (due to precompilation) and
better encapsulation (the logic is stored in the database). Stored
procedures are commonly used for routine operations, like inserting data
into multiple tables or enforcing business rules.
CREATE PROCEDURE InsertEmployee(IN emp_name VARCHAR(100),
IN emp_salary DECIMAL(10, 2))
BEGIN
INSERT INTO employees(name, salary) VALUES
(emp_name, emp_salary); END;
This stored procedure can be called to insert an employee's data into the table.
7. What is a trigger in SQL?
A trigger is a special type of stored procedure that automatically executes
(or "fires") when certain events occur on a specified table or view.
Triggers are used to enforce business rules, track changes, or
automatically perform actions in response to data manipulation events
like INSERT, UPDATE, or DELETE.
○ Example: A trigger can be created to automatically log changes
made to the employees table whenever an employee’s salary is
updated:
CREATE TRIGGER salary_change
AFTER UPDATE ON
employees FOR
EACH ROW
BEGIN
INSERT INTO salary_audit(employee_id,
old_salary, new_salary) VALUES
(OLD.employee_id, OLD.salary, NEW.salary);
END;
AD3391_DDM
This trigger ensures that any salary changes are logged into the salary_audit table.
8. What is the difference between a function and a stored
procedure in SQL?
Functions and stored procedures are both reusable blocks of SQL code,
but there are key differences:
○ Return Value: A function must return a value and can be used in
SQL expressions (e.g., SELECT or WHERE clause). In contrast,
a stored procedure does not necessarily return a value (though it
can use OUT parameters for returning data).
○ Usage: Functions are typically used for calculations or
retrieving values, while stored procedures are used for tasks like
data manipulation or enforcing business logic.
○ Side Effects: Functions are intended to be side-effect free and
should not modify the database state. Stored procedures,
however, can modify data.
9. What are the different types of joins in SQL?
● INNER JOIN: Returns rows when there is a match in both tables.
Rows with no match are excluded from the result.
SELECT * FROM employees e INNER JOIN departments d ON e.department_id
=d.department_id;
LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table,
and matching rows from the right table. If no match exists, NULL values
are returned for columns from the right table.
SELECT * FROM employees e LEFT JOIN departments d ON e.department_id =
d.department_id;RIGHT JOIN (or RIGHT OUTER JOIN): Similar to LEFT JOIN
but returns all rows from the right table, and matching rows from the left
table.FULL JOIN (or FULL OUTER JOIN): Returns all rows when there is a
match in either table. If there’s no match, NULL values are returned for the non-
matching table's columns.
10.What is the UNION operator in SQL?
The UNION operator combines the result sets of two or more SELECT
queries into a single result set, eliminating duplicate rows. Both SELECT
statements must have the same number of columns, and the columns must
have compatible data types.
AD3391_DDM
○ UNION ALL can be used to include
duplicate rows. Example:
SELECT name FROM
employees WHERE
department = 'HR'
UNION
SELECT name FROM employees WHERE department = 'IT';
This query returns a list of unique employee names from the HR and IT departments.
11.What is the Relational Model in databases?
○ The Relational Model organizes data into tables (also known as
relations), where each table consists of rows (tuples) and columns
(attributes). The model uses keys (primary, foreign) to establish
relationships between tables and ensures data integrity and
structure.
12.What are the basic components of a relation in the relational model?
○ A relation consists of tuples (rows) and attributes (columns). Each
attribute contains a domain, which defines possible values for the
attribute, and each tuple contains data corresponding to these
attributes.
13.What is the difference between a table and a relation in a relational database?
○ In the context of relational databases, table and relation are often
used interchangeably. However, a table typically refers to a physical
representation, while a relation emphasizes the abstract mathematical
structure of the data.
14.What is a tuple in the relational model?
○ A tuple is a single row in a table. It represents a specific
instance of the data being described by the table, with each
attribute holding a value corresponding to that instance.
15.What is a primary key in the relational model?
○ A primary key is a set of one or more attributes that uniquely
identifies each tuple in a relation. It ensures that no two tuples in
the same relation have identical values for these attributes.
16.What are integrity constraints in a relational database?
○ Integrity constraints are rules applied to data in a relational
database to ensure its accuracy and consistency. These include
AD3391_DDM
entity integrity, referential integrity, and domain integrity.
17.What is entity integrity?
○ Entity integrity ensures that every relation has a primary key and that
no attribute in the primary key can be NULL. This guarantees that
each tuple in a relation is uniquely identifiable.
18. What is referential integrity?
○ Referential integrity ensures that foreign keys in a relation refer to
valid primary keys in another relation. It ensures consistency
between related tables, preventing orphaned records.
19.What is domain integrity in a relational database?
○ Domain integrity ensures that each attribute in a table holds data
that is of a specified data type and within an acceptable range. It
prevents the insertion of incorrect or inappropriate data.
20. What is a foreign key in the relational model?
○ A foreign key is an attribute in a table that links to the primary
key of another table. It establishes a relationship between two
tables and enforces referential integrity.
Part-B
Relational Model Concepts
1. Explain the concept of the relational model in databases and its key components.
2. What are the main differences between a table and a relation in a relational database
model?
3. Describe the role and significance of a primary key in a relational database.
4. How does a foreign key ensure referential integrity in a relational database system?
5. What is the purpose of a tuple in the relational model? How does it differ from an
attribute?
Integrity Constraints
6. What are integrity constraints in relational databases? Discuss
the different types of integrity constraints.
7. Explain the concept of entity integrity and how it is enforced in a relational database.
8. What is referential integrity, and how is it maintained between two related tables in
AD3391_DDM
SQL?
9. Define domain integrity and its significance in ensuring the correctness of data within
a table.
10. How would you define and enforce a foreign key constraint in SQL?
AD3391_DDM