Sie sind auf Seite 1von 3

Q1. What is normalization? Explain different levels of normalization?

Normalization is the process of efficiently organizing data by eliminating redun


dant data and maintaining dependencies.
Summary: First Normal Form
A relation is in 1NF if it contains no repeating groups
To convert an unnormalised relation to 1NF either:
Flatten the table and change the primary key, or
Decompose the relation into smaller relations, one for the repeating groups and
one for the non-repeating groups.
Remember to put the primary key from the original relation into both new relatio
ns.
This option is liable to give the best results.
Summary: Second Normal Form
A relation is in 2NF if it contains no repeating groups and no partial key funct
ional dependencies
Rule: A relation in 1NF with a single key field must be in 2NF
To convert a relation with partial functional dependencies to 2NF. create a set
of new relations:
One relation for the attributes that are fully dependent upon the key.
One relation for each part of the key that has partially dependent attributes
Summary: Third Normal Form
A relation is in 3NF if it contains no repeating groups, no partial functional d
ependencies, and no transitive functional dependencies
To convert a relation with transitive functional dependencies to 3NF, remove the
attributes involved in the transitive dependency and put them in a new relation
Rule: A relation in 2NF with only one non-key attribute must be in 3NF
In a normalised relation a non-key field must provide a fact about the key, the
whole key and nothing but the key.
Relations in 3NF are sufficient for most practical database design problems. How
ever, 3NF does not guarantee that all anomalies have been removed. See example
Q2. What is denormalization and when would you go for it?
As the name indicates, denormalization is the reverse process of normalization.
It s the controlled introduction of redundancy in to the database design. It helps
improve the query performance as the number of joins could be reduced. Generall
y done in the data warehouse systems.
Q3. What is a Primary Key?
A primary key is a column or combination of columns which uniquely identifies a
specific a row in a table. Primary key does not allow NULL values.
Q4. What is Unique Key?
A unique key is a column or combination of columns which uniquely identifies a s
pecific a row in a table. Unique key allows one NULL value.
Q5. What is a Surrogate Key?
A surrogate key is used as a substitution for the natural primary key in situati
ons where the natural primary key values change over time. Generally surrogate k
ey is a number(sequence) or a unique identifier. Commonly used in Data warehouse
dimension tables.
Q6. What s the difference between a primary key and a unique key?
Both primary key and unique enforce uniqueness of the column on which they are d
efined. But by default primary key creates a clustered index on the column, wher
e are unique creates a nonclustered index by default. Another major difference i
s that, primary key doesn t allow NULLs, but unique key allows one NULL only.
Q7. How do you implement one-to-one, one-to-many and many-to-many relationships
while designing tables?
One-to-One relationship can be implemented as a single table and rarely as two t
ables with primary and foreign key relationships.
One-to-Many relationships are implemented by splitting the data into two tables
with primary key and foreign key relationships.
Many-to-Many relationships are implemented using a junction table with the keys
from both the tables forming the composite primary key of the junction table.
Q8. Define candidate key, alternate key and composite key.

A candidate key is one that can identify each row of a table uniquely.
Generally a candidate key becomes the primary key of the table. If the table has
more than one candidate key, one of them will become the primary key, and the r
est are called alternate keys.
A key formed by combining at least two or more columns is called composite key.
Q9. What is a database index?
A database index is a data structure that improves the speed of data retrieval o
perations on a database table at the cost of slower writes and increased storage
space.
Indexes can be created using one or more columns of a database table, providing
the basis for both rapid random look ups and efficient access of ordered records
.
The disk space required to store the index is typically less than that required
by the table (since indexes usually contain only the key-fields according to whi
ch the table is to be arranged, and exclude all the other details in the table),
yielding the possibility to store indexes in memory for a table whose data is t
oo large to store in memory.
In a relational database, an index is a copy of one part of a table. Some databa
ses extend the power of indexing by allowing indexes to be created on functions
or expressions.
Indexes may be defined as unique or non-unique. A unique index acts as a constra
int on the table by preventing duplicate entries in the index and thus the backi
ng table.
Q10. Explain different types of index architectures?
Index architecture generally classified into Clustered indexes and Non-Clustered
indexes.
Clustered Index: In a clustered index, all the rows in the table are stored in t
he order of the clustered index key. So, there can be only one clustered index p
er table. Basically the data is re-ordered and stored everytime when new records
are added.
Non-clustered Index: The non-clustered index tree contains the index keys in sor
ted order, with the leaf level of the index containing the pointer to the page a
nd the row number in the data page. Typically created on column used in JOIN, WH
ERE, and ORDER BY clauses.
Q1. What are constraints? Give some examples.
Constraints enable the RDBMS enforce the integrity of the database automatically
, without needing you to create triggers, rule or defaults.
Types of constraints: NOT NULL, CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY.
Q2. Give two examples of referential integrity constraints.
A primary key and a foreign key.
Q3. What is a join and explain different types of joins.
Joins are used in queries to explain how different tables are related. Joins als
o let you select data from a table depending upon data from another table.
Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER JOINs are further c
lassified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER JOINS.
Q4. What is a self join?
Self join is just like any other join, except that two instances of the same tab
le will be joined in the query.
Q5. What is a Cartesian product? What causes it?
A Cartesian product is the result of an unrestricted join of two or more tables.
The result set of a three table Cartesian product will have x * y * z number of
rows where x, y, z correspond to the number of rows in each table involved in t
he join. It is caused by specifying a table in the FROM clause without joining i
t to another table.
Q6. What is the difference of a LEFT JOIN and an INNER JOIN statement?
A LEFT JOIN will take ALL the values from the first declared table and matching
values from the second declared table based on the column the join has been decl
ared on.
An INNER JOIN will take only matching values from both tables.

Q7. What is a Procedure?


Procedures or Stored Procedures are procedural extensions or subroutines to stan
dard SQLs provided by database systems. Generally compiled and stored inside the
databases which enhances performance.
Q8. What is a Function?
A Function is a subprogram written to perform certain computations and return a
single value. A function can be used in SELECT statements, provided they don t do
any data manipulation.
Q9. What are the advantages of using stored procedures?
Overhead: Because stored procedure statements are stored directly in the databas
e, they may remove all or part of the compilation overhead that is typically req
uired in situations where software applications send inline (dynamic) SQL querie
s to a database. (However, most database systems implement statement caches and ot
her mechanisms to avoid repetitive compilation of dynamic SQL statements.) In ad
dition, while they avoid some overhead, pre-compiled SQL statements add to the c
omplexity of creating an optimal execution plan because not all arguments of the
SQL statement are supplied at compile time. Depending on the specific database
implementation and configuration, mixed performance results will be seen from st
ored procedures versus generic queries or user defined functions.
Avoidance of network traffic: A major advantage with stored procedures is that t
hey can run directly within the database engine. In a production system, this ty
pically means that the procedures run entirely on a specialized database server,
which has direct access to the data being accessed. The benefit here is that ne
twork communication costs can be avoided completely. This becomes particularly i
mportant for complex series of SQL statements.
Encapsulation of business logic: Stored procedures allow for business logic to b
e embedded as an API in the database, which can simplify data management and red
uce the need to encode the logic elsewhere in client programs. This may result i
n a lesser likelihood of data becoming corrupted through the use of faulty clien
t programs. The database system can ensure data integrity and consistency with t
he help of stored procedures.
Delegation of access-rights: In many systems, stored-procedures can be granted a
ccess rights to the database which the users who will execute those procedures d
o not directly have.
Some protection from SQL injection attacks: Stored procedures can be used to pro
tect against injection attacks. Stored procedure parameters will be treated as d
ata even if an attacker inserts SQL commands. Also, some DBMSs will check the pa
rameter s type.
Q10. What are differences between functions and procedures?
Functions must return a value (using the RETURN keyword), but for stored procedu
res this is not compulsory.
Stored procedures can use RETURN keyword but without any value being passed.
Functions could be used in SELECT statements, provided they don t do any data mani
pulation. However, procedures cannot be included in SELECT statements.
A function can have only IN parameters, while stored procedures may have OUT or
INOUT parameters.
A stored procedure can return multiple values using the OUT parameter or return
no value at all.

Das könnte Ihnen auch gefallen