Portal | IBM Manuals | Downloads | Products | Refer | Info | Programs | JCLs | Forum Rules*| Site Map | Mainframe CD 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index
 
Register
 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index FAQ Search Memberlist Usergroups Profile Log in to check your private messages Log in
 
Example of defining foreign key

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
Author Message
ysuryawanshi_mcs
Warnings : 1

New User


Joined: 25 Mar 2006
Posts: 15
Location: pune(India)

PostPosted: Mon Apr 03, 2006 4:33 pm    Post subject: Example of defining foreign key
Reply with quote

could anybody givi me example of defining foreign key
suppose i hav 2 tables 1.employee 2. dept
emp(eno,ename)
dept(dno,dname)

now i want dno as foreign key in emp so how should i write this
Back to top
View user's profile Send private message
References
appasi

New User


Joined: 12 Dec 2005
Posts: 20
Location: Bangalore

PostPosted: Mon Apr 03, 2006 4:56 pm    Post subject:
Reply with quote

Hi ysuryawanshi_mcs,

If you want dno as foreign key then dno colomn must be in emp table as primary key colomn ( not the same colomn but same type and length should be same).

Please correct me if i am wrong

Appasi
Back to top
View user's profile Send private message
Mr.Niceguy

Active User


Joined: 16 Jun 2005
Posts: 55

PostPosted: Mon Apr 03, 2006 5:49 pm    Post subject:
Reply with quote

Hi,

Create one more field (DNO) in emp table having same data type and length. Assign DNO(dept) as primary key and DNO(emp) as foreign key.
Back to top
View user's profile Send private message
perez_a

New User


Joined: 02 Feb 2006
Posts: 25
Location: Italy

PostPosted: Wed Apr 05, 2006 5:57 pm    Post subject: Re: Example of defining foreign key
Reply with quote

HI ysuryawanshi_mcs,

CREATE TABLE DEPT (
DNO <SAME TYPE OF ENO>,
DNAME ,
CONSTRAINT
FK01 FOREIGN KEY (DNO)
REFERENCES EMP(ENO)
);

AND REMEMBER THAT ENO MUST BE PRIMARY KEY IN EMP.
HAVE A NICE DAY.
Back to top
View user's profile Send private message
friend_prasad

New User


Joined: 06 Jan 2006
Posts: 13

PostPosted: Mon Apr 10, 2006 10:29 am    Post subject: Re: Example of defining foreign key
Reply with quote

hi yogesh suryawanshi this is prasad kulkarni

how r u ?
what is going on ?
have u completed u r syllabus
the answer of u r query is that which is given by the last guy
some body perez try it .if u havenot understand then mail me on my mail id

thanks
prasad
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1