Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> Database >> Primary key
Primary key

In this blog I will explain you that how to work with primary key in sql server. I will show some examples in whic you see different mechanism to create primary key.
Views: 1290     Comments: 0
by Awadhendra Tiwari on 8/1/2011

Primary key in SqlServer

In this blog I will explain some examples that how to work with primary keys in sql server. Here in following examples I will show you that how to create primary key, how to create primary key using alter statement and how to drop primary key using alter statement.

--This is a demonstration of primary key in Sql server.

--Firstly we needs to create a table in which we can apply this demonstration.

--Creating a primary key on column label.

create table Employee

(

     EmpId varchar(5) not null constraint pk1 primary key,

     EmpRefNo varchar(10) not null,

     EmpName varchar(50) not null,

     EmpUId varchar(15) not null,

     EmpPanNo varchar(20) null

)

--Creating primary key on table label.

create table Employee

(

     EmpId varchar(5) not null,

     EmpRefNo varchar(10) not null,

     EmpName varchar(50) not null,

     EmpUId varchar(15) not null,

     EmpPanNo varchar(20) null,

     constraint pk1 primary key(EmpId)

)

--Creating primary key on multiple columns.

--When we create primary key on more than one column then

--This key is also known as composite key.

create table Employee

(

     EmpId varchar(5) not null,

     EmpRefNo varchar(10) not null,

     EmpName varchar(50) not null,

     EmpUId varchar(15) not null,

     EmpPanNo varchar(20) null,

     constraint pk1 primary key(EmpId,EmpUId)

)

--Creating primary key using alter staement.

--When we create primary key using alter statement

--Then we have to make sure that column have "not null"

--attributes and if it don't have "not null"

--attributes then we needs to assign "not null"

--attribute first using alter statement then we

--create primary key on that.

--Statements to create Employee table.

create table Employee

(

     EmpId varchar(5) not null,

     EmpRefNo varchar(10) not null,

     EmpName varchar(50) not null,

     EmpUId varchar(15) not null,

     EmpPanNo varchar(20) null,

)

--Statements to create primary key using alter statement.

alter table Employee

add constraint pk1 primary key(EmpId)

--Statements to drop primary key using alter statement.

alter table Employee

drop constraint pk1

Thanks.

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 6391
  
Copyright © 2013MindStick. All Rights Reserved.