difference between primary key and foreign

Posted by Shanky11 under Sql Server on 1/10/2013 | Points: 10 | Views : 1755 | Status : [Member] | Replies : 11
what is the practical difference b/w
difference between primary key and foreign???????????????




Responses

Posted by: muralikrishnasurap-12704 on: 1/10/2013 [Member] Starter | Points: 25

Up
0
Down
hi Shanky11

refer this link
http://www.dotnetfunda.com/interview/exam5332-difference-between-primary-key-and-unique-key-in-sql-server.aspx

Best Regards
Murali Krishna.S

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 1/10/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi Shanky11,

Let me explain you with a simple example:

let us assume that we have two tables 1)Students 2)Courses

In students table studentId is the primary key which means it should be unique and notnull
Now we use this studentId as a foreign in 2)Courses table . here the condition is studentId should be present in the students table and we can have no need of unique key maintenance

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubh on: 1/11/2013 [Member] Starter | Points: 25
Posted by: Shanky11 on: 1/11/2013 [Member] Starter | Points: 25

Up
0
Down
can we apply more than 1 foreign key on a table

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 1/11/2013 [Member] Starter | Points: 25

Up
0
Down
itmeans that if id 2 is not present on table 2 we cant enter the info on courses table with this id
first it should exists n table student
m i right?????

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/12/2013 [Member] Starter | Points: 25

Up
0
Down
hi!!! Shanky

ya its possible to u can apply more than one foreign key on a table.

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/12/2013 [Member] Starter | Points: 25

Up
0
Down
yes shanky !!! u r right
if id is does not exist on 1st table so u can not insert details about those id on second table

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Wadhwanisanju on: 1/16/2013 [Member] Starter | Points: 25

Up
0
Down
Guys,
Make it short and sweet that any table in sql having more than one Primary key and Foreign key.

The basic difference is Primary key can not be null and its thumb rule while foreign key can be null also based on table structure.

This is the only difference apart from above differences.

Hope this helps !!

Thanks



Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kundan64 on: 1/16/2013 [Member] Starter | Points: 25

Up
0
Down
Primary key creates Clustered Index, and Foreign key is a constraint, a relationship between two tables - that has nothing to do with an index.
Pls check the above link:
http://dptechnicalblog.blogspot.in/2011/03/sql-server-difference-between-primary.html

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/17/2013 [Member] Starter | Points: 25

Up
0
Down
Hi!!! @Wadhwanisanju
please make ur answer improved

Each table should have a primary key, and each table can have only ONE primary key.
ok
not more than one primary key

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramnandha on: 5/11/2013 [Member] Starter | Points: 25

Up
0
Down

Primary key:

1) Primary Key Restrict Duplicate values and Null values.
2) Each Table can have only one primary key possible.
3) Primary key default clustered index.

unique Key

1) Unique key Restrict Duplicate values and allow one null values
2) Each table can have only one unique key.
3) unique key default non clustered index

Foreign key:

1) Parent Table child Table Relation ship.
2) which values am inserting parent table those values only i can able to insert child table .


Example

create table table2(empid int references table1 (table1.columname),empname varchar(90))



sriram

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response