DBMS
DBMS
1. What is DBMS?
DBMS stands for Database Management System, is a set of applications or programs that enable users to
create and maintain a database. DBMS provides a tool or an interface for performing various operations
such as inserting, deleting, updating, etc. into a database. It is software that enables the storage of data
more compactly and securely as compared to a file-based system. A DBMS system helps a user to
overcome problems like data inconsistency, data redundancy, etc. in a database and makes it more
convenient and organized to use it.
Some popular DBMS system - File systems, XML, Windows Registry etc.
2. What is a Database?
A Database is an organized, consistent, and logical collection of data that can easily be updated, accessed,
and managed. Database mostly contains sets of tables or objects (anything created using create command
is a database object) which consist of records and fields. A tuple or a row represents a single entry in a
table. An attribute or a column represents the basic units of data storage, which contain information about
a particular aspect of the table. DBMS extracts data from a database in the form of queries given by the
user.
Atomicity: This property reflects the concept of either executing the whole query or executing nothing at
all, which implies that if an update occurs in a database, then that update should either be reflected in the
whole database or should not be reflected at all.
Consistency: This property ensures that the data remains consistent before and after a transaction in a
database.
Isolation: This property ensures that each transaction is occurring independently of the others. This implies
that the state of an ongoing transaction doesn’t affect the state of another ongoing transaction.
Durability: This property ensures that the data is not lost in cases of a system failure or restart and is
present in the same state as it was before the system failure or restart.
9. What is RDBMS?
RDBMS stands for Relational Database Management System and was introduced to access and store data
more efficiently than DBMS. RDBMS stores data in the form of tables as compared to DBMS which stores
data as files. Storing data as rows and columns makes it easier to locate specific values in the database and
makes it more efficient as compared to DBMS.
Examples of popular RDBMS systems are MySQL, Oracle DB, etc.
One to Many Relationship: This type of relationship is applied when a single row in table X is related to
many rows in table Y.
Many to Many Relationship: This type of relationship is applied when multiple rows in table X can be
linked to multiple rows in table Y.
13. Explain the difference between the DELETE, TRUNCATE and DROP command.
DELETE command: this command is a DML Command which is used to delete rows from a table based on
the condition provided by the WHERE clause.
It deletes only the rows which are specified by the WHERE clause.
It can be rolled back if required.
It maintains a log to lock the row of the table before deleting it and hence it’s slow.
TRUNCATE command: this command is needed to remove complete data from a table in a database. It is
like a DELETE command which has no WHERE clause.
It removes complete data from a table in a database.
It can be rolled back even if required. (TRUNCATE can be rolled back in some databases depending
on their version but it can be tricky and can lead to data loss).
It doesn’t maintain a log and deletes the whole table at once and hence it’s fast.
DROP command: this command is a DDL command which is used to drop/delete the existing table,
database, index, or view from the database.
18. Explain the difference between a 2-tier and 3-tier architecture in a DBMS.
The 2-tier architecture refers to the client-server architecture in which applications at the client end
directly communicate with the database at the server end without any middleware involved.
Example – Contact Management System created using MS-Access or Railway Reservation System, etc.
The 3-tier architecture contains another layer between the client and the server to provide GUI to the
users and make the system much more secure and accessible. In this type of architecture, the application
present on the client end interacts with an application on the server end which further communicates with
the database system.
Example – Designing registration form which contains a text box, label, button or a large website on the
Internet, etc.
51) What is the difference between a shared lock and exclusive lock?
A database lock is a mechanism to protect a shared piece of data from getting updated by two or more
database users at the same time. When a single database user or session has acquired a lock then no other
database user or session can modify that data until the lock is released.
Shared lock: Shared lock is required for reading a data item. In the shared lock, many transactions may
hold a lock on the same data item. When more than one transaction is allowed to read the data items then
that is known as the shared lock.
Exclusive lock: When any transaction is about to perform the write operation, then the lock on the data
item is an exclusive lock. Because, if we allow more than one transaction then that will lead to the
inconsistency in the database.
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.
27. What do you understand by cursor? Mention the different types of cursors.
A cursor is a database object which helps in manipulating data, row by row and represents a result set. The
types of cursors are as follows:
Implicit cursor: This type of cursor is declared automatically as soon as the execution of SQL takes place.
Here, the user is not indicated about the declaration of the cursor.
Explicit cursor: This type of cursor is defined by the PL/ SQL, as it handles a query in more than a single
row.
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.
28. What is the concept of sub-query in terms of SQL? What is Correlated Subquery in DBMS?
Sub-query or nested 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. When a Subquery is executed for each of the
rows of the outer query then it is termed as a Correlated Subquery.
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.
51. Differentiate between logical database design and physical database design. Show how this
separation leads to data independence.
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.
53. Explain different types of failures that occur in the Oracle database.
In the Oracle database following types of failures can occur:
Statement Failure
Bad data type
Insufficient space
Insufficient Privileges (e.g., object privileges to a role)
User Process Failure
The user performed an abnormal disconnect
The user’s session was abnormally terminated
The user’s program raised an address exception
User Error
The user drops a table
User damages data by modification
Instance Failure
Media Failure
The user drops a table
User damages data by modification
Alert Logs
Records informational and error messages
All Instance start-ups and shutdowns are recorded in the log
3. SQL statements are compiled at compile time. SQL statements are compiled at run time.
It is generally used for situations where data It is generally used for situations where
5. is distributed uniformly. data is distributed non-uniformly.
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. Please 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.
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;
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 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 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 result of query as 3000 is the minimum salary of CS Employees and all these rows
are greater than 30000.
2
4
5
6
7
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 –
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 column 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 student who has secured third highest marks 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 salary higher than this person will be 2.