0

I'm trying to add referential integrity to a column. However, I can't know the proper syntax for an alter statement. I've attempted the below code. Can someone tell me what is wrong with this query?

alter table person_address modify column address_addressID int(11) not null,
on update cascade;

1 Answer 1

3

Not null has nothing to do with referential integrity. If you want to require an address ID here, that's separate from adding a foreign key link to the Address table.

alter table person_address modify column address_addressID int(11) not null;

That statement means you MUST have a value in this column. That's all it means; nothing more or less.

To link this table to the Address table (assuming that is the table's name), you need this:

ALTER TABLE person_address 
ADD FOREIGN KEY (address_addressID)
REFERENCES address_tbl_name (addressID)
ON UPDATE CASCADE;

I've no idea what the primary key field is named or what that table looks like, so replace address_tbl_name and addressID in line 3 with appropriate field/table name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.