Jump to content

Recommended Posts

What is the use of primary key and foreign key?

ill xplain u considering two tables .suppose emp and department are two tables in a schema which contain employeee info. like employee number,employee name,department number. and department table has details like department no and dept.nameemp has following details.empno. empname deptno.1 rama 102 krishna 203 sita 104 sheela 20department table has following detailsdeptno deptname10 electrical20 mechanicalnow if u write the department details in emp table then electrical dept will be repeated for rama and krishna similarly mech will be repeated.THis will not be a problem for small tables for storing small info.But for bigger tables u need to consider this redundancy of data.hence u create seperate table.In emp table emp no. is unique and it cannot be null.This is primary key in emp table.Since emp name can be repeated .so u identify it with unique and not null column which is a primary key.Now to link emp and department table u need 1 element which is common for both table.Here its dept no.To link this we declare dept no. as foreign key .finally primary key is used to define a unique element in a table and foreign key is used to link two tablesthink the issue is clear.

Share this post

Link to post
Share on other sites

hiprimary key is the unique key in a table where foreign key is the relationship of a table from another table.foreign key is the reference key.e.g.i have a table named student which have personal information regarding student likerollnonameaddressbranchin which we make primary key on rollno.it means no two students have the same rollno. if we enter same rollno error will come.it have records101 deepti ambala ce102 vandu ynr cewe have one another table named marks which contain marks of those students who exists in the student table. now we r creating relationship b/w two.informtion in this table arerollno (we link this with student)marks1marks2record is101 45 56102 90 67 if we try to enter record 103 then it will show error parent key not found. so we have to first enter in the student table then in marks table.deepti

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this