Monday, May 31, 2010

SQL

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.


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 defined. But by default primary key creates a clustered index on the column, where as unique key creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only.


What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors?

Cursors allow row-by-row prcessing of the resultsets.

Disadvantages of cursors: Each time you fetch a row from the cursor,it results in a network roundtrip, where as a normal SELECT query makes only one rowundtrip, however large the resultset is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations).



What are triggers? How many triggers you can have on a table? How to invoke a trigger on demand?

Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table.
Triggers can't be invoked on demand. They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens on the table on which they are defined.

What is a self join? Explain it with an example.

Self join is just like any other join, except that two instances of the same table will be joined in the query. Here is an example:Employees table which contains rows for normal employees as well as managers. So, to find out the managers of all the employees, you need a self join.

ACID stands for Atomicity, Consistency, Isolation, Durability. These are the properties of a transaction.


What is lock escalation?

Lock escalation is the process of converting a lot of low level locks (like row locks, page locks) into higher level locks (like table locks). Every lock is a memory structure too many locks would mean,more memory being occupied by locks. To prevent this from happening,SQL Server escalates the many fine-grain locks to fewer coarse-grain locks.


SQL Joins

ANSI standard SQL specifies four types of JOINs: INNEROUTERLEFT, and RIGHT. In special cases, a table (base table, view, or joined table) can JOIN to itself in a self-join.




An outer join does not require each record in the two joined tables to have a matching record. 


The result of a left outer join (or simply left join) for table A and B always contains all records of the "left" table (A), even if the join-condition does not find any matching record in the "right" table (B).
 If the left table returns one row and the right table returns more than one matching row for it, the values in the left table will be repeated for each distinct row on the right table.


full outer join combines the results of both left and right outer joins.