What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 11959 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > How to set Primary Key and Foreign Key in SQL Server 2008

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

Article posted by Rajarajah on 11/16/2012 | Views: 1644 | Category: Sql Server | Level: Beginner | Points: 150 red flag


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')



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Raja Rajan

Experience:0 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, August 13, 2012
Level:Starter
Status: [Member]
Biography:LIFE IS EXPERIENCE
~kasadara karka~
>> Write Response - Respond to this post and get points
Related Posts

In this article we will learn First_Value function of Sql Server 2012 (Denali).

In this article, we will look into the Try_Convert function of SQL Server 2012 (Denali).

This is part 19 of the series of article on SSIS. It describes using ActiveX Script task container in SSIS package.

In an aim to save time and key strokes while writing codes.

We all knows that the SQL provides the basic functionality, in terms of what can be searched for or filtered by. But some more advanced functions may be obtained, if the user knows a few tricks. Here are given few very basic tricks which can help to speed up the executions of your SQL scripts

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 8:17:46 AM