Вы находитесь на странице: 1из 6

Dot Net Tricks

Category Tutorial Search C# Design Patterns Asp.Net MVC SQL Server Book Training Advertise About Me Contact

"Coding , A Rhythmic Literary Job"


Handy Tricks For Beginners & Professionals

SQL Server 2012, 2008 - Tutorial, Article, Archive, Pdf, Handy Tricks, Training, Code Snippets, Reference Manual, T-SQL, PL/SQL, Development, Administration, Performance Tuning, Backup, Recovery, Reporting, Analysis, Security, XML, CLR Objects

Different Types of SQL Keys


Po ste d B y : S ha i l e nd r a C ha uha n, 2 7 Ja n 2 0 11 Upda t ed O n : 0 5 Ma y 20 13 Ve rsio n S up po rt : S Q L S e rv e r 200 5 , 20 0 8, 2 01 2 Key wo rd s : S ql K e ys , S u p er K e y, Ca nd ida te K e y ,P ri ma r y K e y , Alt er na t e K e y , Co mpo s ite K e y , U ni qu e K e y , Fo r ei g n K e y , Types of Keys i n Sql Server pd f
A key is a single or combination of multiple fields in a table. Its is used to fetch or retrieve records/data-rows from data table according to the condition/requirement. Keys are also used to create relationship among different database tables or views.

Types of SQL Keys


We have following types of keys in SQL which are used to fetch records from tables and to make relationship among tables or views.

1. 2.

Super Key Candidate Key

Super key is a set of one or more than one keys that can be used to identify a record uniquely in a table. Example :Primary key, Unique key, Alternate key are subset of Super Keys.

A Candidate Key is a set of one or more fields/columns that can identify a record uniquely in a table. There can be multiple Candidate Keys in one table. Each Candidate Key can work as Primary Key. Example: In below diagram ID, RollNo and EnrollNo are Candidate Keys since all these three fields can be work as Primary Key.

3. 4. 5.

Primary Key Alternate key Composite/Compound Key

Primary key is a set of one or more fields/columns of a table that uniquely identify a record in database table. It can not accept null, duplicate values. Only one Candidate Key can be Primary Key.

A Alternate key is a key that can be work as a primary key. Basically it is a candidate key that currently is not primary key. Example: In below diagram RollNo and EnrollNo becomes Alternate Keys when we define ID as Primary Key.

Composite Key is a combination of more than one fields/columns of a table. It can be a Candidate key, Primary key.

6.

Unique Key
between

Uniquekey is a set of one or more fields/columns of a table that uniquely identify a record in database table. It is like Primary key but it can accept only one null value and it can not have duplicate values. For more help refer the articleDifference

primary key and unique

key.

7.

Foreign Key
between primary key and foreign key.

Foreign Key is a field in database table that is Primary key in another table. It can accept multiple null, duplicate values. For more help refer the article Difference

Example : We can have a DeptID column in the Employee table which is pointing to DeptID column in a department table where it a primary key. Defined Keys -

1. CREATE TABLE Department 2. ( 3. DeptID int PRIMARY KEY, 4. Name varchar (50) NOT NULL, 5. Address varchar (200) NOT NULL, ) 6. CREATE TABLE Student 7. ( 8. ID int PRIMARY KEY, 9. RollNo varchar(10) NOT NULL, 10. Name varchar(50) NOT NULL, 11. EnrollNo varchar(50) UNIQUE, 12. Address varchar(200) NOT NULL, 13. DeptID int FOREIGN KEY REFERENCES Department(DeptID) 14. )

Note
1. Practically in database, we have only three types of keys Primary Key, Unique Key and Foreign Key. Other types of keys are only concepts of RDBMS that we need to know.

Summary
In this article I try to explain types of keys in Sql Server with example. I hope after reading this article you will be aware of keys in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Share this article with your friends !!

Prev Next

Recommended Articles !!
1. Different

Types of SQL Joins to SQL Server 3. SQL Server Exception Handling by TRYCATCH 4. Different Types of SQL Server Stored Procedures 5. CRUD Operations using Stored Procedures 6. Definition, Use of Group by and Having Clause 7. Basics of SQL Commands 8. Different Types of SQL Server Views 9. SQL Server Exceptions Working 10. SQL Integrity Constraints or Constraints
2. Introduction

Subscribe & follow Us


For latest article subscribe by Email

Submit

Search Articles
For finding articles enter search terms

Search

.Net Interview Handy Q&A (coming soon..)

Instant Razor View Engine How-to

Browse By Category
.Net Framework6 Ado.Net6

Ajax2 Asp.Net14

Asp.Net Web Page1 C5

C#19 C# Windows Apps6

CSS5 Dependency Injection4

Design Patterns17 Entity Framework3

Fluent Nhibernate1 JavaScript4

JQuery4 Knockout5

LINQ6 MVC44

Nhibernate1 OOPS6

SQL Server46 SQL Server 20121

SVN1 TFS3

Visual Studio3 WCF8

Web API6 Web Service2

Windows Azure2 Windows Phone Apps6

WPF4

Recent Articles
Passing multiple complex type parameter to Web API A Deep Dive into C# Abstract Class A Deep Dive into C# Interface Difference Between Finalize and Dispose Method

Understanding Detailed Architecture of ASP.NET 4.5 Difference Between Generalization & Specialization Understanding Inheritance and Types of Inheritance Understanding Relationship Between CTS and CLS Different Types of JIT Complier Understanding .Net Framework 4.5 Architecture

Popular Articles
Undestanding Cloud Computing and Windows Azure Association, Aggregation, Composition & Dependency Gang of Four (GOF) Design Patterns in .Net Understanding Inversion of Control, DI and SL Difference between WCF and Web API and WCF REST Routing in Asp.Net MVC with example Difference between Generics and Collections CRUD Operations using jQuery dialog and EF Asp.net MVC Request Life Cycle SQL Joins with C# LINQ

Like us on Facebook

Who The ?
If you're looking to learn a trick or technique which you have seen somewhere else. Let me know and I'll do my best to explain how it was done. If I don't know how to do, I'll take it as an exciting topic & I'll find upright resources and share.

More..

How The ?
Ultimately you come across to know new and fresh recipes. You're keen to learn those recipes but you don't know - how do that? Trust me, I have been there to help you for learning these new and fresh recipes.

More..

Disclaimer !
This is my personal blog and having articles on .net and others .net related technologies. The opinions expressed here are my own and not belongs to my employer or other organization. I have listed my own learning experience on this blog. Copyright 2012-2013. The content is copyright to Shailendra Chauhan and may not be reproduced on other websites without permission from the owner.

Join our Training programs in Delhi/Noida - o n C # , A s p . N e t , M V C , W C F , j Q u e r y , E n t i t y


Framework, SQL Server
Ads not by this site

Вам также может понравиться