Thread: Correct Syntax for alter table ..add constraint
Hi All, What is the correct syntax for adding a foreign key constraint from the command line. I am using v7.1 beta3..... I am doing the follwoing: alter table users add constraint age_fk foreign key(age) references age_list(id); And I get the following error: flipr=# alter table users flipr-# add constraint age_fk foreign key(age) references age_list(id); NOTICE: ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for FOREIGN KEY check(s) ERROR: <unnamed> referential integrity violation - key referenced from users not found in age_list I have define attribute id as primary key of the table....... Thanks for your help. Regards,Najm.
Najm, > references age_list(id); > And I get the following error: > flipr=# alter table users > flipr-# add constraint age_fk foreign key(age) references > age_list(id); > NOTICE: ALTER TABLE ... ADD CONSTRAINT will create > implicit trigger(s) > for FOREIGN KEY check(s) > ERROR: <unnamed> referential integrity violation - key > referenced from > users not found in age_list Simple ... you have values in the AGE column that are not in the age_list table. Thus you're in violation of the foriegn key you're trying to establish. -Josh Berkus
Josh Berkus wrote: > Najm, > > > references age_list(id); > > And I get the following error: > > flipr=# alter table users > > flipr-# add constraint age_fk foreign key(age) references > > age_list(id); > > NOTICE: ALTER TABLE ... ADD CONSTRAINT will create > > implicit trigger(s) > > for FOREIGN KEY check(s) > > ERROR: <unnamed> referential integrity violation - key > > referenced from > > users not found in age_list > > Simple ... you have values in the AGE column that are not in > the age_list table. Thus you're in violation of the foriegn > key you're trying to establish. > > -Josh Berkus HI Josh, All values in age column of are null..... I have no value there...... and I have specified to accetp nulls i.e. I dont have constraint not null on this particular column... Najm