Dbms Solved Questions
Dbms Solved Questions
define Keys. List the various types of Keys. Explain any one
A key is a specific column or set of columns in a table that is used to uniquely identify each row in the table. Keys play an
important role in maintaining data integrity and establishing relationships between tables in a relational database.
There are several types of keys:
1. Primary key: A primary key is a column or set of columns that uniquely identifies each row in a table. A table can
have only one primary key and it cannot be null.
2. Foreign key: A foreign key is a column or set of columns that references the primary key of another table. It is used to
establish a link between data in two tables and to maintain referential integrity.
3. Alternate key: An alternate key is a unique key that is not the primary key of a table. A table can have multiple
alternate keys.
4. Composite key: A composite key is a key that is made up of two or more columns.
5. Candidate key: A candidate key is a column or set of columns that can be used as a primary key for a table. A table
can have multiple candidate keys.
6. Super key: A super key is a column or set of columns that can be used to uniquely identify a row in a table. A super
key can be a primary key, a foreign key, or a composite key.
One example of a key is the primary key. It is used to uniquely identify each row in a table. For example, in a table called
"Customers", the primary key could be a column called "CustomerID" which is a unique identifier for each customer in
the table. This means that no two customers in the table can have the same CustomerID, and each customer must have a
unique CustomerID. This allows for easy identification and retrieval of specific customer information and also helps to
enforce data integrity by ensuring that there are no duplicate customer records.
Explain single value and multivalve attribute of ER model.
In an Entity-Relationship (ER) model, attributes describe the properties of entities. There are two types of attributes,
single-valued and multi-valued.
Single-valued attribute: It is an attribute that has only one value for a given entity. For example, a "customer" entity
might have a single-valued attribute called "name" which would contain a single value such as "John Smith".
Multi-valued attribute: It is an attribute that can have multiple values for a given entity. For example, a "customer" entity
might have a multi-valued attribute called "phone numbers" which would contain multiple values such as "555-555-1212"
and "555-555-1313".
Multi-valued attributes can be represented in an ER diagram using an oval shape connected to the entity with a double
line. It is also represented as a separate relationship with the entity.
Single-valued attributes are represented in an ER diagram using an oval shape connected to the entity with a single line.
It's worth noting that multi-valued attribute can be converted into a separate table with a one-to-many relationship with
the entity. This is done to maintain the normalization and to avoid data redundancy.
Explain the following terms: i) Entity ii) Attribute iii) Domain iv) Instance v) Tuple
i) Entity: An entity is a real-world object or concept that is represented in a database. It can be a physical object such as a
person, place, or thing, or it can be a logical object such as an event, process, or relationship. In an Entity-Relationship
(ER) model, entities are represented by rectangles.
ii) Attribute: An attribute is a property of an entity that describes it. It represents a characteristic of the entity, such as its
name, age, or address. In an ER model, attributes are represented by oval shapes and they are connected to the entity
that they describe.
iii) Domain: A domain is the set of possible values that an attribute can have. It specifies the type and format of data that
can be stored in the attribute. For example, if an attribute is "age", the domain would specify that it can only contain
integers.
iv) Instance: An instance is a specific occurrence of an entity or attribute. It is a concrete example of the entity or
attribute in the real world. For example, "John Smith" would be an instance of the entity "customer" with the attribute
"name".
v) Tuple: A tuple is a single row of a table in a relational database. It represents an instance of an entity and contains the
values for each attribute. For example, in a table called "Customers" a tuple might contain the values "John Smith" for the
attribute "name", "35" for the attribute "age", and "123 Main St" for the attribute "address". Each tuple in a table
represents a unique instance of the entity represented by the table.
What are different types of users that play different roles in a database environment?
In a database environment, there are several types of users who play different roles and have different levels of access
and privileges. Some of the most common types of users are:
1. Database Administrator (DBA): The DBA is responsible for managing the database and ensuring its performance,
security, and availability. They are responsible for creating and maintaining the database schema, managing user
accounts and permissions, and monitoring and tuning the database.
2. Database Developer: Database Developers are responsible for creating, maintaining and optimizing the database
schema and the stored procedures, triggers and functions that are used to access the data in the database.
3. End User: End users are the individuals or applications that use the data stored in the database. They may include
employees, customers, or other software applications that need to access the data. End users typically have limited
access and privileges, and can only perform a specific set of actions on the data.
4. Data Analyst: Data Analyst are responsible for analysing the data stored in the database, creating reports, and
making data-driven decisions. They have read-only access to the database, and use tools like SQL to extract data,
analyse it and create the reports.
5. System Administrator: System Administrators are responsible for managing the hardware and software that the
database runs on, including the operating system, storage, and network. They ensure that the system is running
optimally and troubleshoot any issues that arise.
6. Security Administrator: Security Administrators are responsible for ensuring that the data stored in the database is
secure. They manage user access and permissions, implement security policies, and monitor the database for any
security breaches.
In summary, a database environment has different types of users with different roles and responsibilities, from managing
the database to analysing the data, from ensuring security to maintaining the hardware and software. Each of them plays
a critical role in the proper functioning of a database.
What is attribute explaining to type of attribute?
An attribute is a characteristic or property of an entity in a database. It is a named column in a table that stores data for a
specific aspect of the entity.
There are two types of attributes:
1. Simple Attribute: Simple attributes are atomic, meaning they cannot be divided into smaller parts. They are also
called scalar attributes. For example, "name", "age", "address" are simple attributes.
2. Composite Attribute: Composite attributes are composed of two or more simple attributes. They are also called
structured attributes. For example, "Full name" which is composed of "first name" and "last name" is a composite
attribute.
In summary, an attribute is a property of an entity in a database, which is represented as a named column in a table.
There are two types of attributes: simple and composite. Simple attributes are atomic and cannot be divided into smaller
parts, while composite attributes are composed of two or more simple attributes.
Explain two phase locking protocol
The two-phase locking protocol (2PL) is a method used to ensure consistency and isolation in a database management
system. It is used to coordinate concurrent access to shared resources by multiple transactions.
The two phases in 2PL are:
1. Growing Phase: In this phase, a transaction acquires locks on the data items it needs to access, but does not release
any locks. This ensures that the transaction has exclusive access to the data items it needs to perform its operations.
2. Shrinking Phase: In this phase, a transaction releases all the locks it acquired during the growing phase. This ensures
that other transactions can now access the data items that were locked by the first transaction.
The two-phase locking protocol ensures that a transaction releases all the locks it acquired in the growing phase, before it
completes. This ensures that a transaction cannot hold on to locks for an extended period of time, and prevents
deadlocks from occurring.
Two-phase locking protocol is widely used in systems that need high consistency and isolation. It is a simple, yet effective
approach to handle concurrent access to shared resources by multiple transactions, and it provides a good balance
between concurrency and consistency.
In summary, the two-phase locking protocol (2PL) is a method used to ensure consistency and isolation in a database
management system. It uses two phases: growing phase, in which a transaction acquires locks on the data items it needs,
and shrinking phase, in which it releases all the locks it acquired. This protocol is widely used in systems that need high
consistency and isolation, and it provides a good balance between concurrency and consistency.
What is De-normalization?
De-normalization is the process of intentionally introducing redundancy in a normalized database. This is done to
improve the performance of certain types of queries, by reducing the number of joins that are required to retrieve the
data.
De-normalization is a trade-off between performance and data integrity. While it can improve query performance, it can
also lead to data inconsistencies and make it more difficult to update the data.
There are different ways to de-normalize a database, but some common methods include:
1. De-normalizing by adding redundant data: This is the most common method of de-normalization. It involves adding
a copy of data from one table to another table, so that the data can be retrieved with fewer joins.
2. De-normalizing by creating a denormalized view: This method involves creating a virtual table that combines data
from multiple tables, so that it can be queried as if it were a single table.
3. De-normalizing by creating a summary table: This method involves creating a new table that contains pre-
aggregated data, so that it can be queried more quickly.
De-normalization is usually done in situations when the performance is critical and the high read-write ratio is present
and the number of joins required to retrieve the data is high. However, it's important to consider the trade-off between
performance and data integrity, also it's important to monitor the database performance after de-normalization, as it can
lead to data inconsistencies and update difficulties.
What is SQL
SQL (Structured Query Language) is a programming language designed for managing and manipulating relational
databases. It is used to insert, update, retrieve, and delete data in a database. SQL is based on the relational model,
which organizes data into a series of tables, where each table represents an entity or a relationship. The tables are linked
to one another through a system of keys, which allows data to be related and accessed in a logical and efficient manner.
SQL commands are used to interact with the data stored in a relational database.
Some common SQL commands include:
1. SELECT: Used to retrieve data from one or more tables in the database.
2. INSERT: Used to add new data to a table in the database.
3. UPDATE: Used to modify existing data in a table in the database.
4. DELETE: Used to remove data from a table in the database.
5. CREATE: Used to create a new table, view, or index in the database.
6. ALTER: Used to modify the structure of an existing table, view, or index in the database.
7. DROP: Used to delete an existing table, view, or index in the database.
SQL is widely used for managing data in relational databases, and it's supported by most relational databases like MySQL,
Oracle, PostgreSQL, SQL Server, and many others. SQL is not only used for managing data, but also for managing the
database structure and users.
What is stored procedure in SQL write the syntax for stored procedure?
A stored procedure is a pre-compiled, reusable piece of code that is used to perform a specific task or set of tasks in a
database management system. The syntax for creating a stored procedure in SQL (Structured Query Language) varies
depending on the type of database you are using, but the basic format is similar across most relational database
management systems.
The basic syntax for creating a stored procedure in SQL is as follows:
CREATE PROCEDURE procedure_name
( [parameter1 data_type,] [parameter2 data_type,]
... )
BEGIN --SQL statements
END;
For example, to create a stored procedure named "usp_InsertProduct" that takes two parameters (product_name and
product_price) and insert the product into the "Products" table, the following SQL statement can be used:
CREATE PROCEDURE usp_InsertProduct
(@product_name varchar(255), @product_price float)
BEGIN
INSERT INTO Products (product_name, product_price)
VALUES (@product_name, @product_price);
END;
After creating the stored procedure, it can be executed by calling the procedure name with passing the parameter values,
for example:
EXEC usp_InsertProduct 'product name', 12.5
In summary, A stored procedure is a pre-compiled, reusable piece of code that is used to perform a specific task or set of
tasks in a database management system. The basic syntax for creating a stored procedure in SQL is as follows: "CREATE
PROCEDURE procedure_name ( [parameter1 data_type,] [parameter2 data_type,] ... ) BEGIN --SQL statements END;",
where procedure_name is the name of the stored procedure, parameter1, parameter2, etc. are the names and data
types of the parameters that the stored procedure takes as input, and the SQL statements are the code that the stored
procedure will execute when it is called.
write a short note on primary key with syntax and example.
A primary key is a column or set of columns in a table that is used to uniquely identify each row in the table. It is a special
type of unique key and it cannot contain null values. The main characteristics of a primary key are that it must be unique
and not null. Primary keys are used to enforce referential integrity, which means that they are used to prevent duplicate
values and ensure that each row in the table can be uniquely identified.
The syntax for creating a primary key in SQL is as follows:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ...);
For example, consider a table named "EMPLOYEES" with columns "ID", "NAME", and "AGE". To set the "ID" column as the
primary key for the "EMPLOYEES" table, the following SQL statement can be used:
ALTER TABLE EMPLOYEES
ADD CONSTRAINT pk_Employees PRIMARY KEY (ID);
In summary, A primary key is a column or set of columns in a table that is used to uniquely identify each row in the table,
it's unique and not null. It's used to enforce referential integrity and prevent duplicate values.
Any five data type in SQL.
INT or INTEGER: This data type is used to store whole numbers. It can typically store values between -2147483648 and
2147483647.
1. VARCHAR or CHAR: These data types are used to store character strings of varying lengths. The VARCHAR data type
stores strings of variable length, while the CHAR data type stores strings of fixed length.
2. DATE: This data type is used to store date values. It stores the date in the format YYYY-MM-DD.
3. FLOAT: This data type is used to store floating-point numbers. It can store values with decimal points, such as 3.14.
4. BOOLEAN: This data type is used to store Boolean values, which can be either true or false.
5. DECIMAL: This data type is used to store decimal numbers; it can store values with decimal points and precision can
be set.
These are some commonly used data types in SQL. Depending on the database management system, there may be
additional data types available.
In this example, the CEO is the parent of the two department managers, and the department managers are parents of the
employees. The employees are the children of the department managers. The hierarchical data model is useful for
representing data that has a natural hierarchical structure, but it can be less flexible than other data models when it
comes to querying and manipulating the data.
In summary, the hierarchical data model is a data model that organizes data in a tree-like structure, where each record is
represented as a node in the tree. It's useful for representing data that has a natural hierarchical structure, but it can be
less flexible than other data models when it comes to querying and manipulating the data. An example of a hierarchical
data model is a company organizational chart, where the CEO is at the top of the hierarchy, with the department
managers and their respective employees below
Explain the procedure for Creating Views. With Suitable Example
A view is a virtual table that is based on the result of an SQL statement. It is a way to present a subset of the data in a
table or multiple tables in a specific way. Creating views can simplify the process of querying data, as it allows you to
access the data in a specific format without having to rewrite the underlying SQL statement every time.
The procedure for creating views in SQL is as follows:
1. Create a SELECT statement that retrieves the desired data from one or more tables.
2. Use the CREATE VIEW statement to create a view and give it a name.
3. Specify the SELECT statement as the SELECT statement for the view.
For example, consider a table named "EMPLOYEES" that contains columns for "ID", "NAME", "SALARY", and
"DEPARTMENT". To create a view that displays the name and salary of employees in the "SALES" department, the
following SQL statement can be used:
CREATE VIEW Sales_Employees AS
SELECT NAME, SALARY
FROM EMPLOYEES
WHERE DEPARTMENT = 'SALES';
The above statement creates a view named "Sales_Employees" that contains the name and salary of all employees in the
"SALES" department. Now, you can query the view just like a table, for example:
SELECT * FROM Sales_Employees;