I create a table ASSIGNMENTS:
CREATE TABLE assignments (
id_project NUMBER(4) NOT NULL CONSTRAINT fk_assignments_01 REFERENCES projects (id_project),
empl_number NUMBER(6) NOT NULL CONSTRAINT fk_assignments_02 REFERENCES employees (empl_id),
start DATE DEFAULT SYSDATE,
end DATE,
CONSTRAINT chk_assignements_dates CHECK (end > start),
rate NUMBER(7, 2),
CONSTRAINT chk_assignements_rate CHECK (rate > 0),
role VARCHAR2(20),
CONSTRAINT chk_assignements_role CHECK (rola IN ('ANALYST', 'DEVELOPER'))
);
And now I have to modify constraint fk_assignments_02. I want to set fk_assignments_02 ON DELETE CASCADE. I tried do it as following:
ALTER TABLE assignments
MODIFY id_project NUMBER(4) NOT NULL CONSTRAINT fk_assignments_01 REFERENCES projects (id_project) ON DELETE CASCADE;
But I get an error:
ORA-02275: such a referential constraint already exists in the table
You can't do that - you'll have to drop and re-add the constraint. There is no provision in Oracle to change the ON DELETE action, which is part of the REFERENCES clause rather than being part of the constraint state.
So to accomplish this you'd need to do
ALTER TABLE ASSIGNMENTS DROP CONSTRAINT FK_ASSIGNMENTS_02;
followed by
ALTER TABLE ASSIGNMENTS
ADD CONSTRAINT FK_ASSIGNMENTS_02
FOREIGN KEY (ID_PROJECT) REFERENCES PROJECT (ID_PROJECT)
ON DELETE CASCADE;
Best of luck.
Related
I want to add multiple foreign keys to existing table in Oracle database. Following sql query gives me an error. One by one I can add foreign key constraint. But I want to do this within one statement like below.
ALTER TABLE address
ADD CONSTRAINT fk_customer_id FOREIGN KEY (customer_id)
REFERENCES customer (id) ON DELETE CASCADE ,
ADD CONSTRAINT fk_city_id FOREIGN KEY (city_id)
REFERENCES city (id) ON DELETE CASCADE;
Any idea how to do this?
That's not entirely true (what #Thorsten has said). You can add two constraints at a time.
SQL> create table test (empno number, deptno number);
Table created.
SQL>
SQL> alter table test add
2 ( constraint fk_test_emp foreign key (empno) references emp (empno),
3 constraint fk_test_dept foreign key (deptno) references dept (deptno)
4 );
Table altered.
SQL>
My problem is that i have two tables with each table having a foreign key to the other table.
Each time , i execute the SQL file containing the creation of the two tables, it gives me an error that he doesn't find the other table. I'm working with sqlplus to execute the sql file.
Here's an example of SQL file i tried with :
create table A(
Age number(3),
name number(3) constraint A_FK references B(name))
/
create table B(
Age number(3) constraint B_FK references A(Age),
name number(3))
And even if i reverse the order, it gives the same error.
Thanks for help.
This is a problem of cycles in foreign keys. One method is to add all foreign keys after table creation (as I think the other answers propose).
You can also just do that for the first table:
create table A (
Age number(3) primary key,
name number(3)
);
create table B (
name number(3) primary key,
Age number(3),
constraint B_FK foreign key (age) references A(Age)
);
alter table B add constraint A_FK foreign key (name) references B(name);
Here is a db<>fiddle.
Notes:
Foreign keys should reference primary keys, so I added that declaration as well.
I recommend making the primary key the first column in the table.
You can also define the constraint inline for one of the tables (i.e. age number(3) constraint b_fk references a(age)).
The table column(s) that is referred by a foreign key must exist at the time when the constraint is created. Since you have some kind of cyclic reference between the tables, you need to do this in three steps:
first create one table without the foreign key
create the second table (with its foreign key)
finally add the foreign key to the first table with an alter table statement
You also need the referred column to have a unique or primary key constraint set up, otherwise you would get error ORA-02270: no matching unique or primary key for this column-list.
create table A(
age number(3) primary key,
name number(3)
);
create table B(
age number(3) constraint B_FK references A(Age),
name number(3) primary key
);
alter table A add constraint A_FK foreign key (name) references B(name);
Demo on DB Fiddle
Side note: I am quite suspicious about your sample structure, but this could be because your oversimplified it in the question.
It fails because the reference table doesn't exist yet.
Create the tables without the key first. Then drop one and recreated it with the reference. Then drop the 2nd and recreate it with the reference.
Create table first and then ADD the CONSTRAINT
ALTER TABLE A
ADD FOREIGN KEY (name) REFERENCES B(name);
ALTER TABLE B
ADD FOREIGN KEY (age) REFERENCES A(age);
QUESTION:
Create a table named REP_CONTRACTS containing the columns listed in the following chart. A composite PRIMARY KEY constraint including the REP_ID, STORE_ID, and QUARTER columns should be assigned. In addition, FOREIGN KEY constraints should be assigned to both the REP_ID and STORE_ID columns.
create table REP_CONTRACTS
( STORE_ID number(5) not null
, NAME number(5)
, QUARTER char(3) not null
, REP_ID number(5) not null
);
alter table REP_CONTRACTS
add constraint FK_ID_STORE foreign key(STORE_ID)
reference REP_CONTRACTS(STORE_ID)
add constraint FK_ID_REP foreign key(REP_ID)
reference REP_CONTRACTS(REP_ID)
add constraint PK_REP_CONTRACTS primary key(STORE_ID, REP_ID, QUARTER)
reference REP_CONTRACTS(REP_ID, STORE_ID, QUARTER)
;
Even with out the "not null", I still get the same result. I have tried adding the primary and foreign keys with and without the references and constraint but I always keep getting this as a result.
Error starting at line : 618 in command -
alter table REP_CONTRACTS
add constraint FK_ID_STORE foreign key(STORE_ID)
reference REP_CONTRACTS(STORE_ID)
add constraint FK_ID_REP foreign key(REP_ID)
reference REP_CONTRACTS(REP_ID)
add constraint PK_REP_CONTRACTS primary key(STORE_ID,
REP_ID, QUARTER) reference REP_CONTRACTS(REP_ID, STORE_ID, QUARTER)
Error report -
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
You should define your primary key( there may exist once per table ) or unique keys firstly, and use keyword references instead of reference like in the following :
alter table REP_CONTRACTS add constraint PK_REP_CONTRACTS primary key(STORE_ID, REP_ID, QUARTER);
alter table REP_CONTRACTS add constraint PK_REP_CONTRACTS_ST unique(STORE_ID);
alter table REP_CONTRACTS add constraint PK_REP_CONTRACTS_REP unique(REP_ID);
alter table REP_CONTRACTS add constraint FK_ID_STORE foreign key(STORE_ID) references REP_CONTRACTS(STORE_ID);
alter table REP_CONTRACTS add constraint FK_ID_REP foreign key(REP_ID) references REP_CONTRACTS(REP_ID);
alter table REP_CONTRACTS add constraint FK_ID_REP_ST_QU foreign key(REP_ID, STORE_ID, QUARTER) references REP_CONTRACTS(REP_ID, STORE_ID, QUARTER);
Create your table and add a composite primary key that consists of rep_id, store_id and quarter and then add two foreign keys (rep_id and store_id). You can use the Table level method to create the table and add the constraints in one command.
CREATE TABLE REP_CONTRACTS(
Store_ID NUMBER(8),
Name NUMBER(5),
Quarter CHAR(3),
Rep_ID NUMBER(5),
CONSTRAINT rep_contracts_composite_pk PRIMARY KEY (Rep_ID, Store_ID, Quarter),
CONSTRAINT rep_contracts_Store_ID_fk FOREIGN KEY (Store_ID)
REFERENCES BOOK_STORES(Store_ID),
CONSTRAINT rep_contracts_Rep_ID_fk FOREIGN KEY (Rep_ID)
REFERENCES store_reps(Rep_ID)
);
I have a simple table like below.
create table chemlab.rule_header (
id serial PRIMARY KEY,
name varchar(50),
grade varchar(20),
class_tag varchar(20), --tag added to sammple if match
parent_id int REFERENCES chemlab.rule_header(id) DEFAULT NULL,
unique( grade, class_tag )
)
But afterwards, I found that I need to add ON DELETE action, the default is NO ACTION. I couldn't figure out how to change the action.
Now I have to DROP & ADD
ALTER table chemlab.rule_header
DROP CONSTRAINT rule_header_parent_id_fkey ;
ALTER TABLE rule_header
ADD CONSTRAINT rule_header_parent_id_fkey
FOREIGN KEY (parent_id) REFERENCES chemlab.rule_header(id) ON DELETE RESTRICT;
So what is the correct syntax to alter an action on foreign key constraint ?
Well, this not directly altering FOREIGN KEY constraint, and there are DROP and ADD still, though this is only one statement:
ALTER table chemlab.rule_header
DROP CONSTRAINT rule_header_parent_id_fkey,
ADD CONSTRAINT rule_header_parent_id_fkey
FOREIGN KEY (parent_id) REFERENCES chemlab.rule_header(id) ON DELETE RESTRICT;
Take a look at the documentation at https://www.postgresql.org/docs/current/sql-altertable.html. There are options to alter a few things about a constraint (like DEFERRABLE) but not for changing the action, as I understand you need.
can't delete the primary and foreign key, it's simple table but i don't why this error
create table student (
s_ID int ,
S_NAMe varchar2 (10),
S_major varchar2(20),
D_ID number (10) ,
Constraint PK_s_ID primary key (s_ID),
constraint FK_D_ID foreign key (D_ID) references dep (D_ID) );
ALTER TABLE student DROP CONSTRAINT PK_s_ID cascade;
alter table student drop constraint FK_D_ID;
ERROR at line 1:
ORA-02443: Cannot drop constraint - nonexistent constraint
You SQL commands looks correct. You can use following command to make sure constraint exists.
SELECT * FROM user_cons_columns WHERE table_name = 'STUDENT'
If it returns no result, that means you either did not create a constraint or already dropped. You may be trying to re-run the same alter command more than once.