简体   繁体   中英

SQL “Alter Table” for foreign key

So I'm learning some SQL as a bit of a side project since my SQL sucks. I have the following table I created:

CREATE TABLE deliveries (
  pid INTEGER,
  FOREIGN KEY (pid) REFERENCES person_lives_at,
  );

I want to alter it to be a table like this one:

CREATE TABLE deliveries (
  pid INTEGER,
  FOREIGN KEY (pid) REFERENCES employee,
  );

How can I achieve that? I'm using Oracle SQL Developer

Since you created an FK constraint without a name, oracle assigned a system-generated name to the constraint like SYS_xxxxxx. To find the constraint name:

select constraint_name from all_Constraints
where table_name = 'DELIVERIES'

In my test case, it returned "SYS_C0016779". I then drop the constraint:

alter table deliveries drop constraint SYS_C0016779

Then add the new constraint:

ALTER TABLE deliveries
ADD CONSTRAINT PID_EMP_FK -- or whatever you want to call it.
   FOREIGN KEY (pid)
   REFERENCES employee(pid); -- or whatever the name of the column is 

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