Pages

Monday, April 13, 2009

SQL Server Interview Question Answers -- Part 1

1) What is a Primary Key?

==> A Primary Key in a table uniquely identifies each and every row with out allowing Nulls. There are two types of Primary Keys:
1.1) Simple Primary Key ==> Defining primary key on a single column is called Simple Primary Key.
2.1) Composite Primary Key==> Defining Primary Key on more than one column is called Composite Primary Key.

2) What is Unique Key?
==> A Unique Key in a table uniquely identifies each and every row and allowing Nulls per column Combination.
There are two types of Unqiue Keys:
1.1) Simple Unique Key ==> Defining Unique key on a single column is called Simple Unique Key.
2.1) Composite Unique Key==> Defining Unique Key on more than one column is called Composite Unique Key.

3) What is the difference between Primary Key and Unique Key?
3.1)Primary Key by definition cannot be null, where as unique key can accept null values but if the unique key is defined on a column which is not null , then this unique key can also be used as an alternate primary key functionality to identify unique rows in a table.
3.2)By definition you can have only one primary key defined on a table where as you can have multiple unique keys defined on a table
3.3)Also by default Primary key is created as clustured index and unique key is created as non clustered index.


Please let me know, if you have simple definitions!!!!


Lets try to prepare list of possible interview questions on "Constraints".
I will answer all of these in next section.
1) What is a Primary Key?
2) What is Unique Key?
3) What is the difference between Primary Key and Unique Key?
4) What is Check Constraint?
5) What is RULE?
6) What is the difference between RULE and Check Constraint?
7) How to create a relationship between two tables?
8) Can we create a Foreign Key with out Primary Key?
9) What is the table name,that contains the Primary Key, Unique Key and Foreign Key Information?
10) What is the table name, that contains Check Constraint Definition?
11) What is the table name, that contains RULE Constraint definition?
12) Can we have RULE and Check Constraint on the same column?
13) Can we apply Integrity Constraints on Computed Columns?
14) Can you write a query to find out the Child Table Name and Parent Table Name and the Columns to join?
15) Can you drop a Parent Table with out affecting its child tables?
16) How to disable and Enable the constraints?
17) What is the order of Constraints execution?

5 comments:

James said...

you have nice blog. I like all the articles on constraints.

Can you also talk about Cluster Vs nonCluster index?

Mark Austin said...

wow, somany questions just on constraints? great.

Mridula said...

One more question is what is difference between Primary Key, Unique Key and Alternative Key?

Shan said...

Perfect questions for Interviews

Vijaya Kadiyala said...

Hi
First of all thank you for visiting my blog.

James, Your point is well taken, In fact i wanted to cover this in Indexes section.

Mridula, Your point id well taken. Yes, i am going to add this to my questions list.

It would be great, if you guys has answers to any of the above questions and want to share it with me.....

Thanks -- Vijaya Kadiyala