senthil
senthil

Reputation:

Sql query to alter table

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

Upvotes: 0

Views: 193

Answers (1)

psychoschlumpf
psychoschlumpf

Reputation: 3049

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

Upvotes: 3

Related Questions