tags:

views:

68

answers:

1

I have two tables miusernames with columns where UserNamesID is the primary key for table miusernames..

UserNamesID       UserName  
1                  senthil

2                  robert

and miemailids with columns where Emailid is the primary key for table miemailids ..

Emailid   UserNamesID  
1             2

I forgot to create column UserNamesID in table miemailids as foreign key relationship with parent table miusernames .. I need values of column UserNamesID in table miemailids should not be the values other than values in UserNamesID column in parent table miusernames.

I need help in alter table miemailids as adding foreign key relationship for column UserNamesID with parent table miusernames.. Help me with the query to alter table..I feel good if i get exact query.. thanx in advance

+2  A: 
ALTER TABLE miemailids
ADD FOREIGNKEY (UserNamesID) REFERENCES miusernames(UserNamesID)

should do it if you are on mysql and MSSQL

ALTER TABLE miemailids
ADD (CONSTRAINT fk_UserNamesID) FOREIGN KEY (UserNamesID) REFERENCES miusernames(UserNamesID);

should do it on Oracle

psychoschlumpf
Hi ,I am using mysql only. If i put your above code i am getting syntax error as check for the right syntax to use near (UserNamesID).. How to solve this ?