drop foreign key without name Oracle - oracle

I want to ask a very basic question here.
We may/may not name a constraint while creating a table or after creating the table.
Suppose I chose not to name the foreign key constraint.
The table is having no records.
Can I delete the foreign key name without naming it.
I know how to get name of foreign key and then delete using it like
alter table my_table drop constraint fk_name;
but I want to delete/drop the foreign key constraint without mentioning its name.
Is there anyway to do it?

but i want to delete/drop the foreign key constraint without mentioning its name.
That's not possible. The dropping a foreign key constraint requires a name. However you can find out the system generated name:
select constraint_name
from user_constraints
where table_name = 'MY_TABLE'
and constraint_type = 'R';
Will show you all foreign keys defined on the table MY_TABLE. Using that statement you can even generate the necessary DDL statement:
select 'alter table "'||table_name||'" drop constraint "'||constraint_name||'";'
from user_constraints
where table_name = 'MY_TABLE'
and constraint_type = 'R';
Save the output of that select into a file and you have the statement(s) to drop all foreign keys from that table.

Related

H2 ALTER TABLE DROP CONSTRAINT only if table exists

I need to drop a constraint (say, foreign key). But the table, the constraint on it, or both, may not exist.
I tried:
ALTER TABLE IF EXISTS Table1 - invalid syntax
ALTER TABLE Table1 DROP CONSTRAINT IF EXISTS Constraint - fails if Table1 is missing
funky CASE WHEN stuff like
select CASE (select count(*)
from INFORMATION_SCHEMA.CONSTRAINTS
where CONSTRAINT_SCHEMA = 'DBO'
and CONSTRAINT_NAME = upper('FK_Table1_Col1_Table2_Col2'))
WHEN 1 THEN 'select 5'
end
In MS SQL for example you have all the IF EXISTS clauses allowing you to branch script easily. Not so with H2, it would seem. There should be some way to make it work similar to my attempt with CASE WHEN, no ?
If possible, I want to avoid writing Java code in the sql file.

Oracle identify if a delete will cascade

I am working on a legacy oracle database system (10g) and I do not have detailed schema information. I need to find out if deleting a particular record in a table will cause cascading deletes in other tables. I have checked for triggers. But, I am not sure about cascading due to referential constraints. Is there a simple way to identify this?
Assuming you know (or can determine) the foreign key constraint(s) involved, you can look at the DELETE_RULE column from DBA_CONSTRAINTS
SELECT constraint_name, delete_rule
FROM dba_constraints
WHERE r_constraint_name = <<name of the primary key constraint>>
AND r_owner = <<owner of the primary key constraint>>
AND delete_rule = 'CASCADE'
will show you all the foreign key constraints that refer to a particular primary key constraint and will cascade the deletes. If you care about constraints that will do a SET NULL when the parent row is deleted, you could look for rows where the delete_rule was SET NULL as well.
Note that if you do not have privileges on the DBA_CONSTRAINTS table, you can use ALL_CONSTRAINTS instead assuming that you're really only concerned with tables that you have SELECT privileges on.

oracle - drop a constraint defined within

I have a oracle query as -
occupation varchar2(50) CHECK(occupation IN ('student','govt_service','private','business')));
now i need to remove the check constraint so i use the following query-
ALTER TABLE registration drop constraint occupation;
but since i haven't defined the constraint name it says invalid constraint name. Is there any way to delete the constraint ? I guess i can alter the table and add the name of the constraint, then delete it but is there any other way ?
run this query:
select * from all_constraints where table_name = 'REGISTRATION';
And you'll find the constraint name.
EDIT: I also recommend you to have a table Occupations and replace the current constraint with a foreign key. (If you are already want to do this, pls apologize me ). Further, normalising, the Occupations may have IDs and and the foreign should be defined on these ids.
Try querying USER_CONSTRAINTS table for CONSTRAINT_NAME, it must have generated a system name for the constraint you have created.
select * from USER_CONSTRAINTS
where owner='<your_schema>' and CONSTRAINT_TYPE='C';

Foreign key constraint error 1452 in MySQL - Magento import

i am trying to import a sql dump of magento along with some product data and i get this foreign key constraint error:
`ERROR 1452 (23000) at line 231680: Cannot add or update a child row: a foreign key constraint fails:
`magento`.`#sql-b33_27`, CONSTRAINT `FK_CATALOG_COMPARE_ITEM_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID` FOREIGN KEY (`customer_id`) REFERENCES `customer_entity` (`entity_id`) ON DELETE CASCADE ON )`
This is the sql code which is causing the error :
--
-- Constraints for table `catalog_eav_attribute`
--
ALTER TABLE `catalog_eav_attribute`
ADD CONSTRAINT `FK_CATALOG_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID` FOREIGN KEY (`attribute_id`) REFERENCES `eav_attribute` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE;
I am not very comfortable with sql queries. Could some one please explain me what this query does and guide me to resolve this? Thanks.
You are trying to add a record into catalog_eav_attribute, but you do not have a corresponding record in eav_attribute that matches on attribute_id
If you are also inserting bulk data into eav_attribute, I would recommend doing that first, and then the data would be in the table before the foreign key on catalog_eav_attribute needed to reference it.
This article discusses how you can use:
SET FOREIGN_KEY_CHECKS = 0;
--Do your update here
SET FOREIGN_KEY_CHECKS = 1;
If you cannot change the order that you are inserting data. You just have to make sure your data follows the Foreign Keys once everything has been inserted into the database, before you can re-enable the FOREIGN_KEY_CHECKS
I used a database repair tool, after that did this in SQL:
DROP TABLE catalog_product_flat_1 ,
catalog_product_flat_2 ,
catalog_product_flat_3 ;
Now the index is built succesfully.

oracle 10g foreign key constraints

ALTER TABLE employees
ADD CONSTRAINT emp_dno_fk FORIEGN KEY(Dno) REFERENCES Departments(Dno);
When I use this command it shows an error like
CONSTRAINT Specification not allowed here.
But when I use this command it works:
ALTER TABLE employees
ADD CONSTRAINT emp_dno_fk Dno REFERENCES Departments(Dno);
Can anyone tell me why Oracle doesn't allow FOREIGN KEY KEYWORD in the first command?
The error message, admittedly, is not very helpful. The following are examples of how a referential integrity constraint may be created in Oracle:
The following examples assume that the column Dno already exists in employees:
ALTER TABLE employees ADD
CONSTRAINT emp_dno_fk FOREIGN KEY (Dno) REFERENCES Departments (Dno);
ALTER TABLE employees ADD
FOREIGN KEY (Dno) REFERENCES Departments (Dno);
The following examples assume that the column Dno does not already exist in employees:
ALTER TABLE employees ADD
CONSTRAINT emp_dno_fk Dno REFERENCES Departments (Dno);
ALTER TABLE employees ADD
Dno REFERENCES Departments (Dno);
Personally, I avoid the syntax versions which add the column and prefer to add it myself.

Resources