November 6, 2009

Sql Server: Type of Keys

1- Super key - Any Unique key with some Non Unique key combination is called a super key of the relationship.
Example :
+----------+----------+----------+
Roll Number First Name Last Name
+----------+----------+----------+
CSU0001 Shabbir Bhimani
CSU0002 SomeName SurName
CSU0003 Larry page
+------+-----+----------+
Now here we have the following as super keysa). Roll Number First Nameb). Roll Number First Name Last Name

2- Unique key - Unique key contain No duplicate data and one NULL value in a attribute (column).
3- Candidate key - More than one column carry unique values across the table is said to a candidate key. One of those key column becomes Primary key and other column become alternate key.
Example :
+-----------+-----------+---------+-----------+
Roll Number First Name Last Name student_id
+-----------+----------+-----------+----------+
CSU0001 Shabbir Bhimani 212
CSU0002 SomeName SurName 260
CSU0003 Larry page 300
+--------+-----+----+----------------+
Now, RollNumber and student_id is not null unique values so both are candidate key. if one is choice a Primary key i.e. roll number other column (Student_id) become a alternate key.
4- Primary key -
5- Alternate key
6- Composite key - combination of two or more column is called as compite key.Example :
CREATE TABLE Authors (AuthorID INT NOT NULL,Name VARCHAR(100) NOT NULL,PRIMARY KEY(AuthorID,Name))
7- Foreign Key -

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 are unique creates a
nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only.

No comments:

Post a Comment