Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Articles Author
Mon, 28-Jul-2014 Authors
All Time Authors
Sourav.Kayal
39750
Sheonarayan
37300
Niladri.Biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads

How to set Primary Key and Foreign Key in SQL Server 2008

Rajarajah
Posted by under Sql Server category on for Beginner level | Points: 150 | Views : 2817 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
This article will be useful for beginners of T-SQL to create Primary Key and Foreign Key in SQL Server 2008.

Introduction
Hi everyone! This article shows how to use primary key and foreign key in SQL Server.

Objective
Create and use primary key and foreign key in SQL Server.

Using the code  

--create tables with Primary and foreign key


create table Tbl_Country(cnid int primary key,cnname varchar(max)) create table Tbl_State(stid int primary key,cnid int foreign key references Tbl_Country,stname varchar(max)) create table Tbl_City(ctid int primary key,stid int foreign key references Tbl_State,ctname varchar(max)) 

--select all table.

select * from Tbl_Country

select * from Tbl_State

select * from Tbl_City

--INSERT DATA TO COUNTRIES
insert into Tbl_Country(cnid,cnname) values(101,'INDIA')

insert into Tbl_Country(cnid,cnname) values(102,'AMERICA')

insert into Tbl_Country(cnid,cnname) values(103,'PAKISTAN')

 --INSERT DATA TO STATES OF INDIA

insert into Tbl_State(stid,cnid,stname)values(201,101,'TAMILNADU')
insert into Tbl_State(stid,cnid,stname)values(202,101,'MAHARASTRA')
insert into Tbl_State(stid,cnid,stname)values(203,101,'AP')
insert into Tbl_State(stid,cnid,stname)values(204,101,'KERALA')
insert into Tbl_State(stid,cnid,stname)values(205,101,'PUNJAB')
  --INSERT DATA TO STATES OF AMERICA 
insert into Tbl_State(stid,cnid,stname)values(207,102,'WASHINGTON')

insert into Tbl_State(stid,cnid,stname)values(208,102,'NEWYORK')

--INSERT DATA TO STATES OF PAKISTAN 

 insert into Tbl_State(stid,cnid,stname)values(209,103,'ISLAMABAD')

--INSERT DATA TO CITY OF TAMILNADU

insert into Tbl_City(ctid,stid,ctname)values(301,201,'KUMBAKONAM')
insert into Tbl_City(ctid,stid,ctname)values(302,201,'TRICHY')
insert into Tbl_City(ctid,stid,ctname)values(303,201,'THANJOR')
insert into Tbl_City(ctid,stid,ctname)values(304,201,'CHENNAI')
insert into Tbl_City(ctid,stid,ctname)values(305,201,'MADHURAI')
insert into Tbl_City(ctid,stid,ctname)values(306,201,'KOVI')



Page copy protected against web site content infringement by Copyscape
About the Author

Rajarajah

Full Name: Raja Rajan
Member Level: Starter
Member Status: Member
Member Since: 8/13/2012 11:11:48 AM
Country: India
N.RAJA RAJAN 8608382317
http://www.dotnetfunda.com

LIFE IS EXPERIENCE ~kasadara karka~
Login to vote for this post.
Found interesting? Add this to:


Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)