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

FS-Python-3M - IP-Database

Uploaded by

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

FS-Python-3M - IP-Database

Uploaded by

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

DBMS

(Interview preparation)

Q: What is a database?
Ans: A database is an organized collection of structured information, or data, typically stored electronically
in a computer system.

Q: What is a database management system?


Ans: A Database Management System (DBMS) is software designed to store, retrieve, define, and
manage data in a database. DBMS is a computer software providing the interface between users and a
database.

Q: What is RDBMS(Relational Database Management System)?


Ans: The RDBMS is type of database management system specially designed for creating and managing
the relational databases.Oracle Database, MySQL, Microsoft SQL Server etc are examples for RDBMS.A
relational database organizes data into tables which can be linked or related based on data common to
each. Relational Model is the most widely used database model.
● In the relational data model, the database is represented as a collection of related tables called
Relations.
● A relation (table) consists of unique attributes (columns) and tuples (rows).

The figure above indicates a relation in a relational model


The figure above shows a Student relation and it has entries of 7 students (tuples) in it.
● The database is a set of related relations (tables).
● Each relation has a name which indicates what type of tuples the relation has. For example, a
relation named ‘Student’ indicates that it has student entities in it.
● Each relation has a set of attributes (column names) which represents, what type of information, the
entities or tuples have? For example, Student relation has a set of attributes Roll_No., Name,
Department. It indicates that the Student relation has student entities/tuples that have information
about their roll_no., name and department.
● A tuple (row) in a relation, represents a real-world entity, it has a set of values corresponding to
each attribute.
● Each data value in a row or tuple is called a field.

Q: What is a primary key?


Ans: A column or combination of columns that contain values used to uniquely identify each row in the
table. This column or combination of columns is called PRIMARY KEY.It can be created by defining a
PRIMARY KEY CONSTRAINT while creating a table. A table can have only one PRIMARY KEY. A
PRIMARY KEY column cannot contain NULL values.
In the table given below, “Employee_ID” field is the primary key.
What is foreign key?
Ans: A foreign key is a column or combination of columns which can be used to set a link between the
data in two tables. PRIMARY KEY of a table is linked to the FOREIGN KEY of another table to enhance
data integrity. Foreign keys can have only values present in the primary key column of another table, this is
called referential integrity.

Syntax:-
FOREIGN KEY [column list] REFERENCES [primary key table] ([column list]);

Q: What is normalization?
Ans: Normalization is a database design technique that reduces data redundancy(undesired duplication)
and eliminates undesirable characteristics like Insert, Update and Delete Anomalies. Normalization rules
divide larger tables into smaller tables and link them using relationships(foreign key).

Q: What is an anomaly? Explain insert, update and delete anomaly.


Ans: Anomalies are problems that can occur in poorly planned, unnormalized databases where all the data
is stored in one table.Three types of anomalies are
1. Insert Anomaly
2. Delete Anomaly
3. Update Anomaly

Student_id Roll_no Name Address City Department dept_ph_ext

101 1 Aravind Street 17, House Kozhikode Computer 1234


no. 5 Science

102 2 Rameeza Street 20, Chennai Electronics 1235


House no. 10
Table 1: Table without normalization
Student_id Roll_no Name Address City dept_id

101 1 Aravind Street 17, House Kozhikode 1


no. 5

102 2 Rameeza Street 20, Chennai 2


House no. 10
Table 2: Normalized table
dept_id Department dept_ph_ext

1 Computer Science 1234

2 Electronics 1235
Table 3: Normalized table

INSERT Anomaly:
If we want to insert a new department named Information Technology in the table(Table 1) above, we
need to add a student also. Suppose we need to add a student, the student should have a department.
This is called insertion anomaly.
We can avoid this problem by splitting the table into two(Table 2 and 3).

UPDATE Anomaly:
Suppose I would like to change the name of the department Electronics to Applied Electronics in the
table(Table 1). And the Electronics department has 100 students in it. So we need to update 100 records.
But if we split into two tables(Table 2 and Table 3), we need to update a single record only.

DELETE Anomaly:
Now if someone decides to delete the Computer Science department in Table 1 , he may end up deleting
all student’s data who had the department of Computer Science. So to say deletion of some attribute which
causes deletion of other attributes is deletion anomaly.

Q: What is a join? Explain inner join and outer join.


Ans: When we apply normalization rules in a database data will be spread across different tables. But we
might need to display the data in different tables on a single screen. So it is convenient if we can pull data
from different tables using a single query. Join helps to write a single query to fetch data from multiple
tables so as to meet this requirement.
Types of joins:
1. Inner join
2. Outer Join
a. Left Outer Join
b. Right Outer Join

Inner Join:
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the
columns in both tables.


Syntax:
SELECT column_name(s) FROM table1 INNER JOIN table2 ON
table1.column_name=table2.column_name;

Example:
SELECT emp_id,emp_name, vchr_place from tbl_employee Join tbl_place on fk_int_place_id=pk_int_id

Emp_id Emp_name fk_int_place_id

1000 Deepak 1
1001 Aneesh 3
1002 Naveen 2
1003 Jacob 5
Table 1

Pk_int_id Vchr_place
1 Mumbai
2 Kolkata
3 Bangalore
4 Cochin
Table 2

Emp_id Emp_name Vchr_place


1000 Deepak Mumbai
1001 Aneesh Banglore
1002 Naveen Kolkata
Table 3 (output table after inner joining table1 and table2)

Left outer join:

The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right
table (table2). The result is NULL on the right side when there is no match.

SELECT column_name(s) FROM table1 LEFT JOIN table2 ON


table1.column_name=table2.column_name;

Example:

SELECT emp_id,emp_name, vchr_place from tbl_employee Left Join tbl_place on


fk_int_place_id=pk_int_id

Emp_id Emp_name fk_int_place_id


1000 Deepak 1
1001 Aneesh 4
1002 Naveen 2
1003 Jacob 5
1004 Alex 6
Table 1

Pk_int_id Vchr_place
1 Mumbai
2 Kolkata
3 Bangalore
4 Cochin
Table 2

Emp_id Emp_name Vchr_place

1000 Deepak Mumbai


1001 Aneesh Cochin
1002 Naveen Kolkata
1003 Jacob NULL
1004 Alex NULL
Table 3 (output table after left outer joining table1 and table2)

Right outer join

The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left
table (table1). The result is NULL in the left side when there is no match.

SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON


table1.column_name=table2.column_name;

Example:SELECT emp_id,emp_name, vchr_place from tbl_employee Right Join tbl_place on


fk_int_place_id=pk_int_id

Emp_id Emp_name fk_int_place_id


1000 Deepak 1
1001 Aneesh 4
1002 Naveen 2
1003 Jacob 5
1004 Alex 6
Table 1

Pk_int_id Vchr_place
1 Mumbai
2 Kolkata
3 Bangalore
4 Cochin
5 Trivandrum
7 Delhi
Table 2
Emp_id Emp_name Vchr_place
1000 Deepak Mumbai
1001 Aneesh Cochin
1002 Naveen Kolkata
1003 Jacob Trivandrum
NULL NULL Banglore
NULL NULL Delhi
Table 3 (output table after right outer joining table1 and table2)

Q: What is aggregate function and scalar function? What is the difference between aggregate and
scalar function?
Ans: The Aggregate Functions in SQL perform calculations on a group of values in a column and returns a
single value. Following are a few of the most commonly used Aggregate Functions:

Function Description

SUM() Used to return the sum of a group of values.

COUNT() Returns the number of rows either based on a condition, or without a condition.

AVG() Used to calculate the average value of a numeric column.

MIN() This function returns the minimum value of a column.

MAX() Returns a maximum value of a column.

FIRST() Used to return the first value of the column.

LAST() This function returns the last value of the column.

Scalar SQL Functions:


The Scalar Functions in SQL are used to return a single value from the given input value. Following are a
few of the most commonly used scalar Functions:

Function Description

LOWER() Used to convert string column values to lowercase

UPPER() This function is used to convert a string column values to Uppercase.

LEN() Returns the length of the text values in the column.

SUBSTRING() Extracts substrings in SQL from column values having String data type.
ROUND() Rounds off a numeric value to the nearest integer.

NOW() This function is used to return the current system date and time.

FORMAT() Used to format how a field must be displayed.

Q: What is DML and DDL?


Ans:
DDL(Data Definition Language) : DDL or Data Definition Language actually consists of the SQL commands
that can be used to define the database schema. It simply deals with descriptions of the database schema
and is used to create and modify the structure of database objects(tables, functions, views etc) in the
database.
Examples of DDL commands:
CREATE – is used to create the database or its objects (like table, index, function, views, stored procedure
and triggers).
DROP – is used to delete objects from the database.
ALTER-is used to alter the structure of the database objects.
TRUNCATE–is used to remove all records from a table, including all spaces allocated for the records are
removed.

DML(Data Manipulation Language) : The SQL commands that deal with the manipulation of data present in
the database belong to DML or Data Manipulation Language.
Examples of DML:
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.
Q: What is a transaction?
Ans: A transaction in SQL is a sequential group of statements, queries, or operations such as select,
insert, update or delete to perform as a one single work unit that can be committed or rolled back. If the
transaction makes multiple modifications into the database, two things happen:

Either all modification is successful when the transaction is committed.Or, all modifications are undone
when the transaction is rolled back.In other words, a transaction cannot be successful without completing
each operation available in the set. It means if any statement fails, the transaction operation will roll back all
the changes.

Q: What are ACID properties?


Ans: A transaction is a single logical unit of work which accesses and possibly modifies the contents of a
database. Transactions access data using read and write operations.
In order to maintain consistency in a database, before and after the transaction, certain properties are
followed. These are called ACID properties.

Q: What is a constraint?
Ans: SQL CONSTRAINT is used to define rules to allow or restrict what values can be stored in columns.
The purpose of inducing constraints is to enforce the integrity of a database.
SQL CONSTRAINTS are used to limit the type of data that can be inserted into a table.
MySQL CONSTRAINTS can be classified into two types - column level and table level.
The column level constraints can apply only to one column where as table level constraints are applied to
the entire table.
SQL CONSTRAINT is declared at the time of creating a table.
SQL CONSTRAINTs are :
● NOT NULL
● UNIQUE
● PRIMARY KEY
● FOREIGN KEY
● CHECK
● DEFAULT

CONSTRAINT DESCRIPTION

NOT NULL SQL NOT NULL constraint allows to specify that a column can not contain
any NULL value.

UNIQUE The UNIQUE constraint in SQL does not allow to insert a duplicate value
in a column. More than one UNIQUE column can be used in a table.

PRIMARY KEY A PRIMARY KEY constraint for a table enforces the table to accept unique
data for a specific column and this constraint creates a unique index for
accessing the table faster. It cannot be null.

FOREIGN KEY A FOREIGN KEY in SQL creates a link between two tables by one specific
column of both tables. The specified column in one table must be a
PRIMARY KEY and referred by the column of another table known as
FOREIGN KEY.

CHECK A CHECK constraint controls the values in the associated column. The
CHECK constraint determines whether the value is valid or not from a
logical expression.

DEFAULT If no value is supplied to a column in an insert statement, then the column


gets the value set as DEFAULT.

Q: What is the difference between primary key and unique constraint?


Ans:

Primary Key Unique Key


Unique identifier for rows of a table Unique identifier for rows of a table when primary
key is not present

Cannot be NULL Can be NULL

Only one primary key can be present in Multiple Unique Keys can be present in a table
a table

Q: What is subquery? Or nested queries?


Ans: A Subquery or Inner query or a Nested query is a query within another SQL query and embedded
within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further restrict the
data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.
There are a few rules that subqueries must follow −

● Subqueries must be enclosed within parentheses.


● A subquery can have only one column in the SELECT clause, unless multiple columns are in the
main query for the subquery to compare its selected columns.
● An ORDER BY command cannot be used in a subquery, although the main query can use an
ORDER BY. The GROUP BY command can be used to perform the same function as the ORDER
BY in a subquery.
● Subqueries that return more than one row can only be used with multiple value operators such as
the IN operator.
● The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY, CLOB,
or NCLOB.
● The BETWEEN operator cannot be used with a subquery. However, the BETWEEN operator can be
used within the subquery.
Ex: SELECT * FROM product WHERE price = (SELECT MIN(price) FROM products)

Q: What is union?
Ans: The MySQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It
removes duplicate rows between the various SELECT statements.

Each SELECT statement within the UNION operator must have the same number of fields in the result sets
with similar data types.
SELECT supplier_id
FROM suppliers
UNION
SELECT supplier_id
FROM order_details;

In this UNION operator example, if a supplier_id appeared in both the suppliers and order_details table, it
would appear once in your result set. The UNION operator removes duplicates. If you do not wish to
remove duplicates, try using the UNION ALL operator.

Q: What is a stored procedure? What are the advantages and disadvantages?


Ans: A stored procedure is a method to encapsulate repetitive tasks. They allow for variable declarations,
flow control and other useful programming techniques.
Example:
CREATE PROCEDURE GetProductDesc
AS
BEGIN
SET NOCOUNT ON

SELECT P.ProductID,P.ProductName,PD.ProductDescription FROM


Product P
INNER JOIN ProductDescription PD ON P.ProductID=PD.ProductID
WHERE P.ProductID=@PID
END

Advantages:
● Share logic with other applications. Stored procedures encapsulate functionality; this ensures that
data access and manipulation are coherent between different applications.
● Isolate users from data tables. This gives you the ability to grant access to the stored procedures
that manipulate the data but not directly to the tables.
● Provide a security mechanism. Considering the prior item, if you can only access the data using the
stored procedures defined, no one else can execute a DELETE SQL statement and erase your
data.
● To improve performance because it reduces network traffic. With a stored procedure, multiple calls
can be melded into one.
● Stored procedures are precompiled, so it is very fast.

Disadvantages:
● Increased load on the database server -- most of the work is done on the server side, and less on
the client side.
● You are repeating the logic of your application in two different places: your server code and the
stored procedures code, making things a bit more difficult to maintain.
● Migrating to a different database management system (DB2, SQL Server, etc) may potentially be
more difficult.

Q: What is an index?
Ans: Indexes are used to find rows with specific column values quickly. Without an index, SQL must begin
with the first row and then read through the entire table to find the relevant record. The larger the table, the
more this costs. If the table has an index for the columns in question, MySQL can quickly determine the
position to seek in the middle of the data file without having to go through all the data. This is much faster
than reading every row sequentially.The users cannot see the indexes, they are just used to speed up
queries and will be used by the Database Search Engine to locate records very fast.

The INSERT and UPDATE statements take more time on tables having indexes, whereas the SELECT
statements become fast on those tables. The reason is that while doing insert or update, a database needs
to insert or update the index values as well.
Example:
PRIMARY KEY , UNIQUE etc
Q: What is a view?
Ans: VIEWS are virtual tables that do not store any data of their own but display data stored in other
tables. In other words, VIEWS are nothing but SQL Queries. A view can contain all or a few rows from a
table. A MySQL view can show data from one or many tables.

MySQL Views syntax:


CREATE VIEW `view_name` AS
SELECT statement;
● "CREATE VIEW `view_name`" tells MySQL server to create a view object in the database named
`view_name`
● "AS SELECT statement" is the SQL statement to be packed in the view. It can be a SELECT
statement that contains data from one table or multiple tables.

Q: What is a cursor?
Ans: A database cursor is a control structure that enables traversal over the records in a database. Cursor
is a kind of loop facility given to traverse through the result of SQL one by one.

Q: What is the difference between WHERE clause and HAVING clause?


Ans: WHERE clause is used for filtering data before selecting it. HAVING clause is used with GROUP BY
clause to filter the result of aggregate function.

You might also like