简体   繁体   中英

ORA-01735 Primary key and Foreign Key

Alter table residential add constraint pk_restype primary key (customerID) REFERENCES customer(customerID);

I'd like to set primary key constraints on 'residential' table but ORA-01735 error appears indicating "invalid ALTER TABLE option". I've also tried the following to make foreign key relation but it also comes up as the same error code.

Alter table residential add constraint fk_restype foreign key (customerID, customertype) REFERENCES customer(customerID, customertype);

Your problem is you are creating a primary key as if it is a foreign key.

The proper PK syntax is:

alter table residential add constraint pk_restype primary key (customerID);

No references clause is allowed in a Primary Key, on a Foreign Key.

The PK says this column customerID is unique and identifies a unique row in the residential table. It has nothing to do with referencing another table.

A FK would be:

alter table tab_child add constraint fk_child FOREIGN key (child_id)
   REFERENCES tab_parent(id);

The FK says a column child_id in table tab_child refers to and is constrained by the column id in table tab_parent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM