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

Commonly Asked DBMS Interview Questions

The document provides a comprehensive overview of commonly asked interview questions related to Database Management Systems (DBMS), covering topics such as advantages of DBMS, keys, normalization, SQL commands, and data integrity. It explains essential concepts like primary and foreign keys, transactions, triggers, views, and different types of indexes. Additionally, it highlights differences between various SQL commands and discusses data abstraction levels and integrity rules in DBMS.

Uploaded by

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

Commonly Asked DBMS Interview Questions

The document provides a comprehensive overview of commonly asked interview questions related to Database Management Systems (DBMS), covering topics such as advantages of DBMS, keys, normalization, SQL commands, and data integrity. It explains essential concepts like primary and foreign keys, transactions, triggers, views, and different types of indexes. Additionally, it highlights differences between various SQL commands and discusses data abstraction levels and integrity rules in DBMS.

Uploaded by

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

Commonly asked DBMS interview questions

Last Updated : 31 May, 2023


•••
1. What are the advantages of DBMS over traditional file-based systems?
Database management systems were developed to handle the following
difficulties of typical File-processing systems supported by conventional
operating systems.
1. Data redundancy and inconsistency
2. Difficulty in accessing data
3. Data isolation – multiple files and formats
4. Integrity problems
5. Atomicity of updates
6. Concurrent access by multiple users
7. Security problems

2. What are super, primary, candidate, and foreign keys?


A super key is a set of attributes of a relation schema upon which all attributes
of the schema are functionally dependent. No two rows can have the same value
of super key attributes.
A Candidate key is a minimal superkey, i.e., no proper subset of Candidate key
attributes can be a superkey.
A Primary Key is one of the candidate keys. One of the candidate keys is selected
as most important and becomes the primary key. There cannot be more than
one primary key in a table..
A Foreign key is a field (or collection of fields) in one table that uniquely
identifies a row of another table.
3. What is the difference between primary key and unique constraints?
The primary key cannot have NULL value, the unique constraints can have
NULL values. There is only one primary key in a table, but there can be
multiple unique constrains.
4.What is database normalization?
It is a process of analyzing the given relation schemas based on their
functional dependencies and primary keys to achieve the following desirable
properties:
1. Minimizing Redundancy
2. Minimizing the Insertion, Deletion, And Update Anomalies Relation schemas
that do not meet the properties are decomposed into smaller relation schemas
that could meet desirable properties.
5. Why is the use of DBMS recommended? Explain by listing some of its
major advantages?
Some of the major advantages of DBMS are as follows:
• Controlled Redundancy: DBMS supports a mechanism to control
the redundancy of data inside the database by integrating all the data
into a single database and as data is stored in only one place, the
duplicity of data does not happen.
• Data Sharing: Sharing of data among multiple users simultaneously
can also be done in DBMS as the same database will be shared among
all the users and by different application programs.
• Backup and Recovery Facility: DBMS minimizes the pain of
creating the backup of data again and again by providing a feature of
‘backup and recovery’ which automatically creates the data backup
and restores the data whenever required.
• Enforcement of Integrity Constraints: Integrity Constraints are
very important to be enforced on the data so that the refined data
after putting some constraints are stored in the database and this is
followed by DBMS.
• Independence of Data: It simply means that you can change the
structure of the data without affecting the structure of any of the
application programs.

6. What are the differences between DDL, DML, and DCL in SQL?
Following are some details of three :
DDL stands for Data Definition Language. SQL queries like CREATE, ALTER,
DROP, TRUNCATE and RENAME come under this.
DML stands for Data Manipulation Language. SQL queries like SELECT, INSERT,
DELETE and UPDATE come under this.
DCL stands for Data Control Language. SQL queries like GRANT and REVOKE
come under this.
7. What is the difference between having and where clause?
HAVING is used to specify a condition for a group or an aggregate function
used in a select statement. The WHERE clause selects before grouping. The
HAVING clause selects rows after grouping. Unlike the HAVING clause, the
WHERE clause cannot contain aggregate functions. (See this for
examples). See Having vs Where Clause? for more details
8.How to print duplicate rows in a table?
See https://www.geeksforgeeks.org/how-to-print-duplicate-rows-in-a-table/
9. What is Join?
An SQL Join is used to combine data from two or more tables, based on a
common field between them. For example, consider the following two tables.
Table – Student Table
EnrollNo StudentName Address

1000 geek1 geeksquiz1

1001 geek2 geeksquiz2

1002 geek3 geeksquiz3


Table – StudentCourse Table
CourseID EnrollNo

1 1000

2 1000

3 1000

1 1002

2 1003
Following is a join query that shows the names of students enrolled in
different courseIDs.
SELECT StudentCourse.CourseID, Student.StudentName
FROM StudentCourse
INNER JOIN Student
ON StudentCourse.EnrollNo = Student.EnrollNo
ORDER BY StudentCourse.CourseID;
The above query would produce the following result.

CourseID StudentName

1 geek1

1 geek3

2 geek1

3 geek1
9. What is Identity?
Identity (or AutoNumber) is a column that automatically generates numeric
values. A start and increment value can be set, but most DBA leave these at 1. A
GUID column also generates numbers; the value of this cannot be controlled.
Identity/GUID columns do not need to be indexed.
10.What is a view in SQL? How to create a view?
A view is a virtual table based on the result-set of an SQL statement. We can
create it using create view syntax.

CREATE VIEW view_name AS


SELECT column_name(s)
FROM table_name
WHERE condition
11.What are the uses of view?
1. Views can represent a subset of the data contained in a table; consequently,
a view can limit the degree of exposure of the underlying tables to the outer
world: a given user may have permission to query the view, while denied access
to the rest of the base table.
2. Views can join and simplify multiple tables into a single virtual table.
3. Views can act as aggregated tables, where the database engine aggregates
data (sum, average, etc.) and presents the calculated results as part of the data.
4. Views can hide the complexity of data.
5. Views take very little space to store; the database contains only the definition
of a view, not a copy of all the data which it presents.
6. Depending on the SQL engine used, views can provide extra security.
12. What is a Trigger?
A Trigger is a code associated with insert, update or delete operations. The
code is executed automatically whenever the associated query is executed on
a table. Triggers can be useful to maintain integrity in the database.
13. What is a stored procedure?
A stored procedure is like a function that contains a set of operations
compiled together. It contains a set of operations that are commonly used in
an application to do some common database tasks.
14. What is the difference between Trigger and Stored Procedure?
Unlike Stored Procedures, Triggers cannot be called directly. They can only be
associated with queries.
15. What is a transaction? What are ACID properties?
A Database Transaction is a set of database operations that must be treated as
a whole, which means either all operations are executed or none of them. An
example can be a bank transaction from one account to another account.
Either both debit and credit operations must be executed or none of
them. ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties
that guarantee that database transactions are processed reliably.
16. What are indexes?
A database index is a data structure that improves the speed of data retrieval
operations on a database table at the cost of additional writes and the use of
more storage space to maintain the extra copy of data. Data can be stored only
in one order on a disk. To support faster access according to different values,
faster search like binary search for different values is desired, For this
purpose, indexes are created on tables. These indexes need extra space on the
disk, but they allow faster search according to different frequently searched
values.
17. What are clustered and non-clustered Indexes?
Clustered indexes are the index according to which data is physically stored
on a disk. Therefore, only one clustered index can be created on a given
database table.
Non-clustered indexes don’t define the physical ordering of data, but logical
ordering. Typically, a tree is created whose leaf point to disk records. B-Tree
or B+ tree are used for this purpose.
18. What is Denormalization?
Denormalization is a database optimization technique in which we add
redundant data to one or more tables.
19. What is CLAUSE in SQL?
A clause in SQL is a part of a query that lets you filter or customize how you
want your data to be queried to you.
20. What is a Live Lock?
Livelock situation can be defined as when two or more processes continually
repeat the same interaction in response to changes in the other processes
without doing any useful work These processes are not in the waiting state, and
they are running concurrently. This is different from a deadlock because in a
deadlock all processes are in the waiting state.
21. What is QBE?
Query-by-example represents a visual/graphical approach for accessing
information in a database through the use of query templates called skeleton
tables. It is used by entering example values directly into a query template to
represent what is to be achieved. QBE is used by many database systems for
personal computers. QBE is a very powerful facility that gives the user the
capability to access the information a user wants without the knowledge of any
programming language. Queries in QBE are expressed by skeleton tables. QBE
has two distinct features:
QBE has the two-dimensional syntax: Queries look like tables.
22. Why are cursors necessary in embedded SQL?
A cursor is an object used to store the output of a query for row-by-row
processing by the application programs. SQL statements operate on a set of
data and return a set of data. On other hand, host language programs operate
on a row at a time. The cursors are used to navigate through a set of rows
returned by an embedded SQL SELECT statement. A cursor can be compared to
a pointer.
23. What is the purpose of normalization in DBMS?
Database normalization is the process of organizing the attributes of the
database to reduce or eliminate data redundancy (having the same data but at
different places).
Purpose of normalization:
It is used to remove duplicate data and database anomalies from the relational
table.
Normalization helps to reduce redundancy and complexity by examining new
data types used in the table.
It is helpful to divide the large database table into smaller tables and link them
using relationships.
It avoids duplicate data or no repeating groups into a table.
It reduces the chances for anomalies to occur in a database.
24. What is the difference between a database schema and a database
state?
The collection of information stored in a database at a particular moment in
time is called database state while the overall design of the database is called
the database schema.
25. What is the purpose of SQL?
SQL stands for Structured Query Language whose main purpose is to interact
with the relational databases in the form of inserting, deleting and
updating/modifying the data in the database.
26. Explain the concepts of a Primary key and Foreign Key.
Primary Key is used to uniquely identify the records in a database table
while Foreign Key is mainly used to link two or more tables together, as this is
a particular field(s) in one of the database tables which are the primary key of
some other table.
Example: There are 2 tables – Employee and Department. Both have one
common field/column as ‘ID’ where ID is the primary key of the Employee table
while this is the foreign key for the Department table.
27.What are the main differences between Primary key and Unique Key?
Given below are few differences:
The main difference between the Primary key and the Unique key is that the
Primary key can never have a null value while the Unique key may consist of a
null value.
In each table, there can be only one primary key while there can be more than
one unique key in a table.
28. What is the concept of sub-query in terms of SQL?
Sub-query is basically the query that is included inside some other query and
can also be called an inner query which is found inside the outer query.
29. What is the use of the DROP command and what are the differences
between DROP, TRUNCATE and DELETE commands?
DROP command is a DDL command which is used to drop/delete the existing
table, database, index, or view from the database.
The major difference between DROP, TRUNCATE and DELETE commands are:
DROP and TRUNCATE commands are the DDL commands which are used to
delete tables from the database.
And when we make use of a DROP command, the tables get deleted
permanently all the privileges and indexes that are related to the table also get
deleted. This operation cannot be rolled back and so should be used only when
necessary.
However in case of TRUNCATE, only the data stored in a table is deleted and
the structure of the table is preserved and you can re-insert data by the use
of “INSERT INTO clause”. It can be rolled back until the commit has been made.
DELETE command, on the other hand, is a DML Command which is used to
delete rows from the table and this can be rolled back, however its considered
slower than truncate. Using the delete command, we can delete 1 or more
specific rows from the table.
30. What is the main difference between UNION and UNION ALL?
UNION and UNION ALL are used to join the data from 2 or more tables but
UNION removes duplicate rows and picks the rows which are distinct after
combining the data from the tables whereas UNION ALL does not remove the
duplicate rows, it just picks all the data from the tables.
31. What is Correlated Subquery in DBMS?
A Subquery is also known as a nested query i.e. a query written inside some
query. When a Subquery is executed for each of the rows of the outer query
then it is termed as a Correlated Subquery.
An example of Non-Correlated Subquery is:
SELECT * from EMP WHERE ‘RIYA’ IN (SELECT Name from DEPT
WHERE EMP.EMPID=DEPT.EMPID);

Here, the inner query is not executed for each of the rows of the outer query.
32. Explain Entity, Entity Type, and Entity Set in DBMS?
The entity is an object, place, or thing which has its independent existence in
the real world and about which data can be stored in a database. For
Example, any person, book, etc.
Entity Type is a collection of entities that have the same attributes. For
Example, the STUDENT table contains rows in which each row is an entity
holding the attributes like name, age, and id of the students, hence STUDENT is
an Entity Type that holds the entities having the same attributes.
Entity Set is a collection of entities of the same type. For Example, A collection
of the employees of a firm.
33. What are the different levels of abstraction in the DBMS?
There are 3 levels of data abstraction in the DBMS.
They include:
Physical Level: This is the lowest level of the data abstraction which states how
the data is stored in the database.
Logical Level: This is the next level of the data abstraction which states the type
of the data and the relationship among the data that is stored in the database.
View Level: This is the highest level in the data abstraction which shows/states
only a part of the database.
34 . What integrity rules exist in the DBMS?
There are two major integrity rules that exist in the DBMS.
Entity Integrity: This states a very important rule that the value of a Primary
key can never have a NULL value.
Referential Integrity: This rule is related to the Foreign key which states that
either the value of a Foreign key is a NULL value or it should be the primary key
of any other relation.
35. What is E-R model in the DBMS?
E-R model is known as an Entity-Relationship model in the DBMS which is
based on the concept of the Entities and the relationship that exists among
these entities.
36. What is a functional dependency in the DBMS?
This is basically a constraint that is useful in describing the relationship among
the different attributes in a relation.
Example: If there is some relation ‘R1’ which has 2 attributes as Y and Z then
the functional dependency among these 2 attributes can be shown as Y-
>Z which states that Z is functionally dependent on Y.
37. What is 1NF in the DBMS?
1NF is known as the First Normal Form.
This is the easiest form of the normalization process which states that
the domain of an attribute should have only atomic values. The objective of this
is to remove the duplicate columns that are present in the table.
38. What is 2NF in the DBMS?
2NF is the Second Normal Form.
Any table is said to have in the 2NF if it satisfies the following 2 conditions:
A table is in the 1NF.
Each non-prime attribute of a table is said to be functionally dependent in
totality on the primary key.
39. What is 3NF in the DBMS?
3NF is the Third Normal Form.
Any table is said to have in the 3NF if it satisfies the following 2 conditions:
A table is in the 2NF.
Each non-prime attribute of a table is said to be non-transitively dependent on
every key of the table.
40. What is BCNF in the DBMS?
BCNF is the Boyce Codd Normal Form which is stricter than the 3NF.
Any table is said to have in the BCNF if it satisfies the following 2 conditions:
A table is in the 3NF.
For each of the functional dependencies X->Y that exists, X is the super key of a
table.
41. What is a CLAUSE in terms of SQL?
This is used with the SQL queries to fetch specific data as per the requirements
on the basis of the conditions that are put in the SQL. This is very helpful in
picking the selective records from the complete set of records.
For Example, There is a query that has a WHERE condition or the query with
the HAVING clause.
42.How can you get the alternate records from the table in the SQL?
If you want to fetch the odd numbers then the following query can be used:
SELECT EmpId from (SELECT rowno,EmpId from Emp) WHERE mod(rowno,2)=1;

If you want to fetch the even numbers, then the following query can be used:

SELECT EmpId from (SELECT rowno,EmpId from Emp) WHERE mod(rowno,2)=0;

43. How is the pattern matching done in the SQL?


Answer: With the help of the LIKE operator, pattern matching is possible in the
SQL.’%’ is used with the LIKE operator when it matches with the 0 or more
characters, and ‘_’ is used to match the one particular character.
Example:

SELECT * from Emp WHERE name like ‘b%’;

SELECT * from Emp WHERE name like ‘hans_’;

44. What is a join in the SQL?


A Join is one of the SQL statements which is used to join the data or the rows
from 2 or more tables on the basis of a common field/column among them.
45. What are the different types of joins in SQL?
There are 4 types of SQL Joins:
Inner Join: This type of join is used to fetch the data among the tables which
are common in both tables.
Left Join: This returns all the rows from the table which is on the left side of the
join but only the matching rows from the table which is on the right side of the
join.
Right Join: This returns all the rows from the table which is on the right side of
the join but only the matching rows from the table which is on the left side of
the join.
Full Join: This returns the rows from all the tables on which the join condition
has been put and the rows which do not match hold null values.
46. Explain the Stored Procedure.
A Stored Procedure is a group of SQL statements in the form of a function that
has some unique name and is stored in relational database management
systems(RDBMS) and can be accessed whenever required.
47. What is RDBMS?
RDBMS is the Relational Database Management System which contains data in
the form of the tables and data is accessed on the basis of the common fields
among the tables.
48. What are the different types of relationships in the DBMS?
A Relationship in DBMS depicts an association between the tables.
Different types of relationships are:
One-to-One: This basically states that there should be a one-to-one
relationship between the tables i.e. there should be one record in both the
tables.
One-to-Many: This states that there can be many relationships for one i.e. a
primary key table hold only one record which can have many, one, or none
records in the related table.
Many-to-Many: This states that both the tables can be related to many other
tables.
49. What do you mean by Entity type extension?
Compilation of similar entity types into one particular type which is grouped
together as an entity set is known as entity type extension.
50. What is conceptual design in dbms?
Conceptual design is the first stage in the database design process. The goal at
this stage is to design a database that is independent of database software and
physical details. The output of this process is a conceptual data model that
describes the main data entities, attributes, relationships, and constraints of a
given problem domain.
51. Differentiate between logical database design and physical database
design. Show how this separation leads to data independence.

Parameters Logical Database Design Physical Database Design

Maps or transforms the


The specifications for the stored
conceptual schema (or an ER
database in terms of physical storage
Task schema) from the high-level
structures, record placement, and
data model into a relational
indexes are designed.
database schema.

The mapping can proceed in The following criteria are often used
two stages: to guide the choice of physical
Choice of database design options:
criteria • System-
independent • Response Time
mapping but • Space Utilization
Parameters Logical Database Design Physical Database Design

data model- • Transaction Throughput


dependent
• Tailoring the
schemas to a
specific DBMS

DDL statements in the


language of the chosen DBMS
that specify the conceptual and
external level schemas of the An initial determination of storage
database system. But if the structures and the access paths for the
Result DDL statements include some database files. This corresponds to
physical design parameters, a defining the internal schema in terms
complete DDL specification of Data Storage Definition Language.
must wait until after the
physical database design
phase is completed.

The database design is divided into several phases. The logical database design
and physical database design are two of them. This separation is generally
based on the concept of the three-level architecture of DBMS, which provides
data independence. Therefore, we can say that this separation leads to data
independence because the output of the logical database design is the
conceptual and external level schemas of the database system which is
independent of the output of the physical database design that is an internal
schema.
52. What are temporary tables? When are they useful?
Temporary tables exist solely for a particular session, or whose data persists
for the duration of the transaction. The temporary tables are generally used to
support specialized rollups or specific application processing requirements.
Unlike a permanent table, space is not allocated to a temporary table when it is
created. Space will be dynamically allocated for the table as rows are inserted.
The CREATE GLOBAL TEMPORARY TABLE command is used to create a
temporary table in Oracle.
53. Explain different types of failures that occur in the Oracle database.
Types of Failures – In the Oracle database following types of failures can
occur:
• Statement Failure·
• Bad data type
o Insufficient space
• Insufficient Privileges (e.g., object privileges to a role)
• User Process Failure
o The user performed an abnormal disconnect
o The user’s session was abnormally terminated
o The user’s program raised an address exception
• User Error
o The user drops a table
o User damages data by modification
• Instance Failure
• Media Failure
o The user drops a table
o User damages data by modification
• Alert Logs
o Records informational and error messages
o All Instance startups and shutdowns are recorded in
the log
54. What is the main goal of RAID technology?
RAID stands for Redundant Array of Inexpensive (or sometimes
“Independent”)Disks.
RAID is a method of combining several hard disk drives into one logical unit
(two or more disks grouped together to appear as a single device to the host
system). RAID technology was developed to address the fault-tolerance and
performance limitations of conventional disk storage. It can offer fault
tolerance and higher throughput levels than a single hard drive or group of
independent hard drives. While arrays were once considered complex and
relatively specialized storage solutions, today they are easy to use and essential
for a broad spectrum of client/server applications.

Q. There is a table where only one row is fully repeated. Write a


Query to find the Repeated row
Name Section

abc CS1

bcd CS2

abc CS1

In the above table, we can find duplicate row using below query.
SELECT name, section FROM tbl
GROUP BY name, section
HAVING COUNT(*) > 1

Q. Query to find 2nd highest salary of an employee?


SELECT max(salary) FROM EMPLOYEES WHERE salary IN
(SELECT salary FROM EMPLOYEEs MINUS SELECT max(salary)
FROM EMPLOYEES);

OR
SELECT max(salary) FROM EMPLOYEES WHERE
salary <> (SELECT max(salary) FROM EMPLOYEES);

Q.Consider the following Employee table. How many rows are there in
the result of the following query?
ID salary DeptName 1 10000 EC 2 40000 EC
3 30000 CS 4 40000 ME 5 50000 ME 6 60000 ME
7 70000 CS
How many rows are there in the result of the following query?
SELECT E.ID
FROM Employee E
WHERE EXISTS (SELECT E2.salary
FROM Employee E2
WHERE E2.DeptName = 'CS'
AND E.salary > E2.salary)

Following 5 rows will be the result of the query as 3000 is the minimum
salary of CS Employees and all these rows are greater than 30000. 2 4 5
67
Q. Write a trigger to update Emp table such that, If an updation is
done in Dep table then salary of all employees of that department
should be incremented by some amount (updation) Assuming Table
name are Dept and Emp, trigger can be written as follows:
CREATE OR REPLACE TRIGGER update_trig
AFTER UPDATE ON Dept
FOR EACH ROW
DECLARE
CURSOR emp_cur IS SELECT * FROM Emp;
BEGIN
FOR i IN emp_cur LOOP
IF i.dept_no = :NEW.dept_no THEN
DBMS_OUTPUT.PUT_LINE(i.emp_no); -- for printing those
UPDATE Emp -- emp number which are
SET sal = i.sal + 100 -- updated
WHERE emp_no = i.emp_no;
END IF;
END LOOP;
END;

Q. There is a table which contains two columns Student and Marks,


you need to find all the students, whose marks are greater than
average marks i.e. list of above-average students.
SELECT student, marks
FROM table
WHERE marks > SELECT AVG(marks) from table;

Q.Name the Employee who has the third-highest salary using sub
queries.
SELECT Emp1.Name
FROM Employee Emp1
WHERE 2 = (SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary
)

Logic: Number of people with a salary higher than this person will be 2.
Q. Why we cannot use WHERE clause with aggregate functions like
HAVING ? The difference between the having and where clause in SQL
is that the where clause canNOT be used with aggregates, but the
having clause can.
Note: It is not a predefined rule but by and large you’ll see that in a good
number of the SQL queries, we use WHERE prior to GROUP
BY and HAVING after GROUP BY. The Where clause acts as a pre
filter where as Having as a post filter. The where clause works on row’s
data, not on aggregated data. Let us consider below table ‘Marks’.
Student Course Score a c1 40
a c2 50 b c3 60 d c1 70
e c2 80 Consider the query
SELECT Student, sum(Score) AS total
FROM Marks

This would select data row by row basis. The having clause works on
aggregated data. For example, the output of the below query
SELECT Student, sum(score) AS total FROM Marks

Student Total a 90 b 60 d 70 e 80 When


we apply to have in above query, we get
SELECT Student, sum(score) AS total
FROM Marks having total > 70

Student Total a 90 e 80
Q. Difference between primary key and unique key and why one
should use a unique key if it allows only one null ?
Primary key:
• Only one in a row(tuple).
• Never allows null value(only key field).
• Unique key identifier can not be null and must be unique.
Unique Key:
• Can be more than one unique key in one row.
• Unique key can have null values(only single null is allowed).
• It can be a candidate key.
• Unique key can be null and may not be unique.
Q. What’s the difference between materialized and dynamic
view? Materialized views
• Disk-based and are updated periodically based upon the query
definition.
• A materialized table is created or updated infrequently and it
must be synchronized with its associated base tables.
Dynamic views
• Virtual only and run the query definition each time they are
accessed.
• A dynamic view may be created every time that a specific view is
requested by the user.
Q. What is embedded and dynamic SQL? Static or Embedded SQL
• SQL statements in an application that do not change at runtime
and, therefore, can be hard-coded into the application.
Dynamic SQL
• SQL statements that are constructed at runtime; for example,
the application may allow users to enter their own queries.
• Dynamic SQL is a programming technique that enables you to
buildSQL statements dynamically at runtime. You can create
more general purpose, flexible applications by using dynamic
SQL because the full text of a SQL statement may be unknown
at compilation.

Static (embedded) SQL Dynamic (interactive) SQL

In static SQL how database will be


In dynamic SQL, how database will be
accessed is predetermined in the
accessed is determined at run time.
embedded SQL statement.

It is more swift and efficient. It is less swift and efficient.

SQL statements are compiled at compile SQL statements are compiled at run
time. time.

Parsing, validation, optimization, and Parsing, validation, optimization, and


generation of application plan are done at generation of application plan are done
compile time. at run time.

It is generally used for situations where It is generally used for situations where
data is distributed uniformly. data is distributed non-uniformly.

EXECUTE IMMEDIATE, EXECUTE EXECUTE IMMEDIATE, EXECUTE


and PREPARE statements are not used. and PREPARE statements are used.

It is less flexible. It is more flexible.

Q. What is the difference between CHAR and VARCHAR?


• CHAR and VARCHAR differ in storage and retrieval.
• CHAR column length is fixed while VARCHAR length is variable.
• The maximum no. of characters CHAR data type can hold is 255
characters while VARCHAR can hold up to 4000 characters.
• CHAR is 50% faster than VARCHAR.
• CHAR uses static memory allocation while VARCHAR uses
dynamic memory allocation.

Explain different types of keys in a database.

There are mainly 7 types of keys in a database:

• Candidate Key: The candidate key represents a set of properties that can
uniquely identify a table. Each table may have multiple candidate keys. One
key amongst all candidate keys can be chosen as a primary key. In the below
example since studentId and firstName can be considered as a Candidate Key
since they can uniquely identify every tuple.
• Super Key: The super key defines a set of attributes that can uniquely identify
a tuple. Candidate key and primary key are subsets of the super key, in other
words, the super key is their superset.

• Primary Key: The primary key defines a set of attributes that are used to
uniquely identify every tuple. In the below example studentId and firstName
are candidate keys and any one of them can be chosen as a Primary Key. In the
given example studentId is chosen as the primary key for the student table.
• Unique Key: The unique key is very similar to the primary key except that
primary keys don’t allow NULL values in the column but unique keys allow
them. So essentially unique keys are primary keys with NULL values.
• Alternate Key: All the candidate keys which are not chosen as primary keys
are considered as alternate Keys. In the below example, firstname and
lastname are alternate keys in the database.
• Foreign Key: The foreign key defines an attribute that can only take the
values present in one table common to the attribute present in another table.
In the below example courseId from the Student table is a foreign key to the
Course table, as both, the tables contain courseId as one of their attributes.
• Composite Key: A composite key refers to a combination of two or more
columns that can uniquely identify each tuple in a table. In the below example
the studentId and firstname can be grouped to uniquely identify every tuple in
the table.

3. Explain different types of Normalization forms in a DBMS.

Following are the major normalization forms in a DBMS:


Considering the above Table-1 as the reference example for understanding
different normalization forms.

• 1NF: It is known as the first normal form and is the simplest type of
normalization that you can implement in a database. A table to be in its first
normal form should satisfy the following conditions:
o Every column must have a single value and should be atomic.
o Duplicate columns from the same table should be removed.
o Separate tables should be created for each group of related data and
each row should be identified with a unique column.
Table-1 converted to 1NF form

• 2NF: It is known as the second normal form. A table to be in its second normal
form should satisfy the following conditions:
o The table should be in its 1NF i.e. satisfy all the conditions of 1NF.
o Every non-prime attribute of the table should be fully functionally
dependent on the primary key i.e. every non-key attribute should be
dependent on the primary key in such a way that if any key element is
deleted then even the non_key element will be saved in the database.

Breaking Table-1 into 2 different tables to move it to 2NF.


• 3NF: It is known as the third normal form. A table to be in its third normal form
should satisfy the following conditions:
o The table should be in its 2NF i.e. satisfy all the conditions of 2NF.
o There is no transitive functional dependency of one attribute on any
attribute in the same table.

Breaking Table-1 into 3 different tables to move it to 3NF.

• BCNF: BCNF stands for Boyce-Codd Normal Form and is an advanced form of
3NF. It is also referred to as 3.5NF for the same reason. A table to be in its
BCNF normal form should satisfy the following conditions:
o The table should be in its 3NF i.e. satisfy all the conditions of 3NF.
o For every functional dependency of any attribute A on B
(A->B), A should be the super key of the table. It simply implies that A
can’t be a non-prime attribute if B is a prime attribute.

You might also like