Oracle Constraint Check - oracle

I want to check if film.language_id refers to language.language_id.
I used the code:
SELECT *
FROM all_tab_columns
WHERE column_name = 'film.language';
Result:
no rows selected
Does this mean there are no references/referential constraints?

If you want to find out if a column refers to another column by foreign key constraint you can do the following:
Find out if the column is in a constraint:
select constraint_name from user_cons_columns
where table_name='<Your_table>'
and column_name='<Your_column>';
If it is this will give you the name of that constraint.
Next you can find out if that constraint is a foreign key constraint and where the foreign key points to:
select constraint_type
,r_constraint_name
from user_constraints
where constraint_name='<your constraint name>';
If the constraint is a foreign key constraint it is of type 'R'. This will also give you the name of the primary key constraint the foreign key relates to.
Given the name of the primary key constraint you can find the table and column(s) as follows:
select table_name
,column_name
from user_cons_columns
where constraint_name = 'Your PK constraint'
To make life easier you can join all these queries together. But I leave that to you.

Related

DROP TABLE QUERY ORA -02449 [duplicate]

This question already has answers here:
Dropping a table in Oracle SQL
(5 answers)
Closed 2 years ago.
DROP TABLE Errors
results in
ORA-02449: unique/primary keys in table referenced by foreign keys
How to drop the table?
Handle referential integrity first.
What will you do with data that references values in a table you'd want to drop?
if you don't care, then drop foreign key constraint(s), then drop the errors table
if you do care, then - obviously - you can't drop it
If you don't know how to find foreign keys, have a look here.
ALTER TABLE tablename
DROP PRIMARY KEY CASCADE;
it drops your foreign key or any dependencies in other tables. Only use it if you know you don't need this relation. Once committed it won't rollback.
If you don't want to drop them you can disable them.
ALTER TABLE tablename
DISABLE CONSTRAINT constraintname CASCADE;
You can find the names of constraints by:
SELECT constraint_name, constraint_type, search_condition
FROM user_constraints
WHERE table_name = 'tablename';
Find out the table where the table you are dropping(Let's say XYZ) is referenced.
You can use the following query to find such tables and constraint name.
select * from user_constraints u
where u.constraint_type = 'R'
and u.constraint_name = (select p.constraint_name from user_constraints p
where p.table_name = 'XYZ' and p.constraint_type = 'P')

Oracle foreign key constraints - check constraint syntax?

I have a child table in oracle that has two foreign key columns, relating to two different parent tables. I want to create a constraint that says the child must have at least one of those parents - e.g.
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column1)
REFERENCES parent_table (column1)
OR
FOREIGN KEY (column2)
REFERENCES parent_table_2 (column1)
This won't work with a foreign key constraint because that can only relate to one parent table - is it possible to do this with a check constraint instead?
Foreign key constraints ensure the referential integrity, not mandatory values.
I think you have to have to separate FK contraints and additional check constraint like this:
alter table table_name
add constraint c_check_cols
check(column1 is not null or column2 is not null);
You can do it with constraints:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_1
FOREIGN KEY (column1)
REFERENCES parent_table (column1);
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_2
FOREIGN KEY (column2)
REFERENCES parent_table_2 (column1);
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_3
check (COALESCE(column1, column2) IS NOT NULL);
Of course for column1 and column2 column you must permit NULL values.

Alter table enable novalidate constraint

I am trying to add UNIQUE KEY on a previously existing table with duplicate records by setting ENABLE NOVALIDATE.
But I am getting ORA-02299: cannot validate (my_owner.my_key_UK ) - duplicate keys found
ALTER TABLE my_owner.my_table
ADD CONSTRAINT my_key_UK UNIQUE (ID1,ID2)
ENABLE NOVALIDATE;
A unique constraint uses an index to enforce the noduplicates rule. By default it will create a unique index (makes sense right?). It is this index creation which is hurling ORA-02299.
However, if this is an existing index on the constrained columns the constraint will use that. The good news is, the index doesn't need to be unique for the constraint to use it.
So what you need to do is build a non-unique index first:
create index whatever_idx on my_table (ID1,ID2);
Then you will be able to create your constraint:
ALTER TABLE my_owner.my_table
ADD CONSTRAINT my_key_UK UNIQUE (ID1,ID2)
ENABLE NOVALIDATE;
You can check this by querying the data dictionary:
select uc.constraint_name
, uc.constraint_type
, uc.index_name
, ui.uniqueness as idx_uniqueness
from user_constraints uc
join user_indexes ui
on ui.index_name = uc.index_name
where uc.table_name = 'MY_TABLE'
Oracle ensure unique values using indexes. And if you create unique constrains db automatic creates unique index. Workaround is add DEFERRABLE options. In this case oracle creates normal index. Check example.
create table table_abc (a number,b number);
insert into table_abc values(1,1);
insert into table_abc values(1,2);
ALTER TABLE table_abc ADD CONSTRAINT my_key_a UNIQUE (a) DEFERRABLE enable novalidate; -- no error but in table nonunique values
ALTER TABLE table_abc ADD CONSTRAINT my_key_b UNIQUE (b) ENABLE NOVALIDATE; --no error
select * from user_indexes where table_name ='TABLE_ABC';

Do I need to drop a foreign key on one table to delete a row on another using oracle?

I have two tables
Parent table
(account_number varchar(15) not null,
branch_name varchar(50) not null,
balance number not null,
primary key(account_number));
Child table
account_number varchar(15) not null,
foreign key(account_number) references parent table(account_number));
I am trying this:
DELETE FROM parent table
WHERE balances > 1000;
I am deleting accounts by balances on the parent but I get an error message about the child relationship.
My assumption is a DELETE CASCADE has to be added to the foreign key in the child table. All the documentation shows how to alter the table when the constraint is named. I do not have that situation. Is there a way to do it, or do I have to specify the cascade in the delete statement I am writing?
Every constraint in Oracle has a name. If a name isn't specified when the constraint is created, Oracle will autogenerate a name for the constraint. If you don't know what the name of a constraint is, try running a SQL statement that violates the constraint and reading the constraint name from the error message:
SQL> delete from parent where account_number = 1234;
delete from parent where account_number = 1234
*
ERROR at line 1:
ORA-02292: integrity constraint (LUKE.SYS_C007357) violated - child record
found
In this case the name of the constraint is SYS_C007357.
If that doesn't work, you can query the data dictionary view user_constraints:
SQL> select constraint_name from user_constraints where table_name = 'CHILD' and constraint_type = 'R';
CONSTRAINT_NAME
------------------------------
SYS_C007357
As far as I can tell, you can't modify a foreign key constraint to enable ON DELETE CASCADE. Instead you must drop the constraint and recreate it.
I don't believe you can apply the CASCADE option to a DELETE statement either, but you can delete the child rows before deleting from the parent:
DELETE FROM child
WHERE account_number IN (SELECT account_number FROM parent WHERE balance > 1000);
DELETE FROM parent
WHERE balance > 1000;
However, I don't know how many other tables you have with foreign key constraints referencing your parent table, nor in how many places you are deleting from the parent table, so I can't say how much work it would be to use this approach.
yes you can set DELETE CASCADE
see more info here FOREIGN KEYS WITH CASCADE DELETE
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);
for example
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE CASCADE
);

reference a compound key in Oracle

I want to add a foreign key which reference the column in itself
FOREIGN KEY ACCREDITATION_BODY_ID NOT NULL REFERENCES
ACCREDITATION_BODY_LOOK_UP(ACCREDITATION_BODY_ID),
and the SQL in the table is:
CREATE TABLE "COURSE_ACCREDITED"
("COURSE_ID" VARCHAR2(50) NOT NULL ENABLE,
"ACCREDITATION_BODY_ID" VARCHAR2(50) NOT NULL ENABLE,
"DATE_OBTAINED" VARCHAR2(50),
PRIMARY KEY ("COURSE_ID", "ACCREDITATION_BODY_ID", "DATE_OBTAINED") ENABLE)
When I add this foreign key, it appears ORA-02270: no matching unique or primary key for this column-list
What is the problem?
Does ACCREDITATION_BODY_LOOK_UP have primary key (or unique key)?
select constraint_name, constraint_type
from user_constraints
where table_name = 'ACCREDITATION_BODY_LOOK_UP'
and constraint_type in ('P', 'U');
If yes, what are its columns? You need to reference all those columns in the same order when you add a foreign key to a dependent table.
select column_name, position
from user_cons_columns
where table_name = 'ACCREDITATION_BODY_LOOK_UP'
and constraint_name = '<< constraint from previous query >>';
If no, then you need to create a primary key on that table before you can reference it in a foreign key.
alter table ACCREDITATION_BODY_LOOK_UP
add constraint ACCR_BODY_LKUP_PK primary key (ACCREDITATION_BODY_ID);
This means that the child table has values which are not found in the parent table.
You just need to delete these orphaned values or define the foreign key with "novalidate" which skips checking the integrity between the child and parent tables.
CORRECTION: THIS ADDRESS A DIFFERENT PK/FK ERROR
ORA-02270 is because you're trying to create a foreign key and the key is not referencing a primary key or column with a unique constraint.

Resources