FS-Python-3M - IP-Database
FS-Python-3M - IP-Database
(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.
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).
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.
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
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
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.
Example:
Pk_int_id Vchr_place
1 Mumbai
2 Kolkata
3 Bangalore
4 Cochin
Table 2
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.
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
COUNT() Returns the number of rows either based on a condition, or without a condition.
Function Description
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.
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 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.
Only one primary key can be present in Multiple Unique Keys can be present in a table
a table
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.
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.
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.