Appear in Online Interview to test your skills and be ready for job!
Twitter Twitter LinkedIn YouTube Google
Be Interview ready ! | Search
Make friends on DotNetFunda.com and expand your network!
Winners

Win Prizes

Announcements
Like us on Facebook
Sponsor
Top Articles Author
Thu, 24-Apr-2014 Authors
All Time Authors
Sourav.Kayal
39750
Sheonarayan
34800
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 : 2606 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
Found interesting? Add this to:




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.

Comments or Responses


Login to post response.

Comments / Responses
Select text & click toolbar to format. Formatting appears in viewmode only. HTML Tags are not allowed.
Bold Italic Underline Paragraph Title Code  Link 
 Wait ... Processing ..... please wait.

Comment using Facebook