Combining 3 steps into one SQL script - sql

I'm pretty new to SQL, so it sould be rather easy to answer my questions.
Here is what I want to do:
Deactivate constraints:
Deactivate constraints in the database:
begin
for cur in (select fk.owner, fk.constraint_name , fk.table_name
from all_constraints fk, all_constraints pk
where fk.CONSTRAINT_TYPE = 'R' and
pk.owner = 'USER1' and
fk.R_CONSTRAINT_NAME = pk.CONSTRAINT_NAME ) loop
execute immediate 'ALTER TABLE '||cur.owner||'.'||cur.table_name||' MODIFY CONSTRAINT '||cur.constraint_name||' DISABLE';
end loop;
end;
Delete from tables:
delete from USER_TEST.Table1;
delete from USER_TEST.Table2;
delete from USER_TEST.Table3;
Reactivate Constraints:
begin
for cur in (select fk.owner, fk.constraint_name , fk.table_name
from all_constraints fk, all_constraints pk
where fk.CONSTRAINT_TYPE = 'R' and
pk.owner = 'USER1' and
fk.R_CONSTRAINT_NAME = pk.CONSTRAINT_NAME ) loop
execute immediate 'ALTER TABLE '||cur.owner||'.'||cur.table_name||' MODIFY CONSTRAINT '||cur.constraint_name||' ENABLE NOVALIDATE';
end loop;
end;
Does anyone know how to combine these steps into one .sql script so I can run this on Oracle SQLDeveloper? Or maybe a more elegant way to perform the deletion from the tables?
I'd be very thankful

If it is just about deleting from tables which have foreign keys to each other, you can always delete in constraint order ("children first"). Then you won't have to mess with your constraints at all. Don't forget to commit at the end.
If it is about speed, then you may want to disable constraints and empty the tables via TRUNCATE rather then DELETE. You should however, not delete ALL constraints of the schema, but just the foreign keys of the affected tables which point to another affected table. This will prevent you from shooting into your foot. You can to this without looping, just disable the constraints explicitly. E
So the entire script should look like this
Alter Table User_test.Table1 Modify Constraint FK... Disable;
Alter Table User_test.Table2 Modify Constraint FK... Disable;
...
Trunacte Table user_test.Table1;
Trunacte Table user_test.Table2;
...
Alter Table User_test.Table1 Modify Constraint FK... Enable;
Alter Table User_test.Table2 Modify Constraint FK... Enable;
...

Related

drop check constraint when constraint name is not known

Is it possible to delete check constraint(no constraint name declared) that is created within CREATE TABLE command?
Run the below query to find the list of check constraints in your table, including what they are checking for:
select constraint_name, search_condition
from user_constraints
where table_name = 'Your table name'
and constraint_type = 'C' -- To filter Check constraints alone
Copy the name of the relevant constraint and paste it in the Alter table drop constraint command.
Alter Table Your_table_name
Drop Constraint constraint_name; -- Replace constraint_name from above query result

Dropping a table in Oracle SQL

Whenever I try to drop a table or create a table it is showing these errors:
DROP TABLE SUBURB;
DROP TABLE STOCKITEM;
DROP TABLE MANUFACTURER;
DROP TABLE WAREHOUSE;
DROP TABLE CITY;
DROP TABLE STATE;
Error at line 1: ORA-02449: unique/primary keys in table referenced
by foreign keys
CREATE TABLE STATE (
statecode varchar(3)
,statename varchar(30)
,population number(8)
,primary key(statecode)
);
Error at line 1: ORA-00955: name is already used by an existing object
Can anybody explain why this happens?
If you're really sure you want to drop the table even though it's referenced in foreign keys you can force it like this:
drop table state cascade constraints;
This syntax is defined in the Oracle SQL Reference.
Note that this drops any foreign key relationships. So you will need to recreate them after you have rebuilt the table (and its primary key). Normally this is okay because the most common use case is trashing and re-creating schemas in Development or CI environments.
We can use cascade constraints to make our build scripts easier to maintain. There are two alternatives:
Explicitly drop the foreign key constraints before dropping the
tables, either with a script or with dynamic SQL.
Order the DROP
TABLE statements so that dependent tables are zapped first, along
with their pesky foreign keys. Easy enough for a handful of tables,
more painful with a large schema.
You can use below query to fetch the references of table which should be dropped before dropping the table.
select table_name, constraint_name, status, owner
from dba_constraints
where 1=1
--and r_owner = :p_owner --if you know schema
and constraint_type = 'R'
and r_constraint_name in
(
select constraint_name from dba_constraints
where constraint_type in ('P','U')
and lower(table_name) = lower(:p_table_name)
--and r_owner = :p_owner
)
order by table_name, constraint_name
if you create the primary key and also create the foreign key than you cannot drop the table
you drop the table in this way for example if you have the table of students or teachers you want to drop this table you should write
DROP TABLE students CASCADE CONSTRAINTS;
and also you drop the table of teachers
DROP TABLE teachers CASCADE CONSTRAINTS;
SUBURB
Table is a parent table for any other table First you drop the child table then you can drop the SUBURB table....
And a table named as STATE is already present in your database...so you cant create the table having the same name....once if you drop the STATE table you can create another....
here's the solution that works for me fine in Oracle sample database :
DROP TABLE ['Your_Table_Name'] STATE CASCADE CONSTRAINTS;

Truncate table in Oracle getting errors

I got the problem is when I run following command in Oracle, I encounter the error.
Truncate table mytable;
Errors:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
I found that, this mytable has relationship with other tables. That's why Truncate command cannot proceed anymore. How to delete data from myTable with the SQL scripts using Truncate command?
You have to swap the TRUNCATE statement to DELETE statements, slower and logged but that's the way to do it when constraints are in place.
DELETE mytablename;
Either that or you can find the foreign keys that are referencing the table in question and disable them temporarily.
select 'ALTER TABLE '||TABLE_NAME||' DISABLE CONSTRAINT '||CONSTRAINT_NAME||';'
from user_constraints
where R_CONSTRAINT_NAME='<pk-of-table>';
Where pk-of-table is the name of the primary key of the table being truncated
Run the output of the above query. When this has been done, remember to enable them again, just change DISABLE CONSTRAINT into ENABLE CONSTRAINT
this page offers a very good solution ...
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
I'm here copying from it the Solution:
Find the referenced ENABLED foreign key constraints and disable them.
truncate/delete from the table .
using any text editor .. just change disable to enable in the output you get from the query , then run it.
select 'alter table '||a.owner||'.'||a.table_name||' disable constraint '||a.constraint_name||';'
from all_constraints a, all_constraints b
where a.constraint_type = 'R' and a.status='ENABLED'
and a.r_constraint_name = b.constraint_name
and a.r_owner = b.owner
and b.table_name = upper('YOUR_TABLE');
The error message is telling you that there are other table(s) with a foreign key constraint referring to your table.
According to the Oracle docs
You cannot truncate the parent table
of an enabled foreign key constraint.
You must disable the constraint before
truncating the table.
The syntax for disabling a foreign key is:
ALTER TABLE table_name disable
CONSTRAINT constraint_name;
Issue:
Error “ORA-02266: unique/primary keys in table referenced by enabled foreign keys” when trying to truncate a table.
Error Message:
SQL> truncate table TABLE_NAME;
truncate table TABLE_NAME
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
Solution:
-- Find the referenced foreign key constraints.
SQL> select 'alter table '||a.owner||'.'||a.table_name||' disable constraint '||a.constraint_name||';'
2 from all_constraints a, all_constraints b
3 where a.constraint_type = 'R'
4 and a.r_constraint_name = b.constraint_name
5 and a.r_owner = b.owner
6 and b.table_name = 'TABLE_NAME';
'ALTER TABLE'||A.OWNER||'.'||A.TABLE_NAME||'DISABLE CONSTRAINT'||A.CONSTRAINT_NAME||';'
---------------------------------------------------------------------------------------------------------
alter table SCHEMA_NAME.TABLE_NAME_ATTACHMENT disable constraint CONSTRAINT_NAME;
alter table SCHEMA_NAME.TABLE_NAME_LOCATION disable constraint CONSTRAINT_NAME;
-- Disable them
alter table SCHEMA_NAME.TABLE_NAME_ATTACHMENT disable constraint CONSTRAINT_NAME;
alter table SCHEMA_NAME.TABLE_NAME_LOCATION disable constraint CONSTRAINT_NAME;
-- Run the truncate
SQL> truncate table TABLE_NAME;
Table truncated.
-- Enable the foreign keys back
SQL> select 'alter table '||a.owner||'.'||a.table_name||' enable constraint '||a.constraint_name||';'
2 from all_constraints a, all_constraints b
3 where a.constraint_type = 'R'
4 and a.r_constraint_name = b.constraint_name
5 and a.r_owner = b.owner
6 and b.table_name = 'TABLE_NAME';
'ALTER TABLE'||A.OWNER||'.'||A.TABLE_NAME||'ENABLE CONSTRAINT'||A.CONSTRAINT_NAME||';'
--------------------------------------------------------------------------------
alter table SCHEMA_NAME.TABLE_NAME_ATTACHMENT enable constraint CONSTRAINT_NAME;
alter table SCHEMA_NAME.TABLE_NAME_LOCATION enable constraint CONSTRAINT_NAME;
-- Enable them
alter table SCHEMA_NAME.TABLE_NAME_ATTACHMENT enable constraint CONSTRAINT_NAME;
alter table SCHEMA_NAME.TABLE_NAME_LOCATION enable constraint CONSTRAINT_NAME;
Oracle 12c introduced a feature to truncate a table that is a parent of a referential integrity constraint having ON DELETE rule.
Instead of truncate table tablename; use:
TRUNCATE TABLE tablename CASCADE;
From Oracle truncate table documentation:
If you specify CASCADE, then Oracle Database truncates all child tables that reference table with an enabled ON DELETE CASCADE referential constraint. This is a recursive operation that will truncate all child tables, granchild tables, and so on, using the specified options.
A typical approach to delete many rows with many constraints is as follows:
create mytable_new with all the columns but without constrains (or create constraints disabled);
copy whatever data you need from mytable to mytable_new.
enable constraints on mytable_new to see that everything is ok.
alter any constraints that reference mytable to reference mytable_new instead and see that everything is ok.
drop table mytable.
alter table mytable_new rename to mytable.
It's far faster than deleting a million records with many slow constraints.
I had the similar issue and I sorted it out by the following scripts.
begin
for i in (select constraint_name, table_name from user_constraints a where a.owner='OWNER' and a.table_name not in
(select b.table_name from user_constraints b where b.table_name like '%BIN%')
and a.constraint_type not in 'P')
LOOP
execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';
end loop;
end;
/
truncate table TABLE_1;
truncate table TABLE_2;
begin
for i in (select constraint_name, table_name from user_constraints a where a.owner='OWNER' and a.table_name not in
(select b.table_name from user_constraints b where b.table_name like '%BIN%')
and a.constraint_type not in 'P')
LOOP
execute immediate 'alter table '||i.table_name||' enable constraint '||i.constraint_name||'';
end loop;
end;
/
This script will first disable all the Constraints. Truncates the data in the tables and then enable the contraints.
Hope it helps.
cheers..
TRUNCATE TABLE TEST2 DROP ALL STORAGE;
This statement Actually works when there is an foreign key constraint applied on a .table
As mentioned by the error message, you cannot truncate a table that is referenced by enabled foreign keys. If you really want to use the truncate DDL command, disable the foreign key constraint first, run the truncate command, and enable it back.
Reference: Difference between TRUNCATE, DELETE and DROP commands

FireBird adding dropping constraint

In Firebird I want to generate the scripts for adding all the constraints in a database and also I want to generate the script for dropping all the foreign key constraints.
How do I do this?
To generate a script which in turns drop all the foreign keys, use the following script in any firebird >= 2.0 (maybe it works on pre 2.0, just can't remember if data dictionary changed in involved system table):
--generate a script which drops all foreign keys
--by jachguate http://jachguate.wordpress.com
-- http://stackoverflow.com/users/255257/jachguate
select 'alter table '||c.rdb$relation_name||' drop constraint '||c.rdb$constraint_name||';' script_lines
from rdb$relation_constraints c
where c.rdb$constraint_type = 'FOREIGN KEY';
Edit
To generate a script to re-create all the foreign keys, this script will do the trick (for firebird >= 2.0). Remember to run this before actually deleting the foreign keys.
select 'alter table '||trim(c.rdb$relation_name)
||' add constraint '||trim(c.rdb$constraint_name)
||' foreign key ('
||(select list(trim(imast.rdb$field_name)) from rdb$index_segments imast where rdb$index_name = c.rdb$index_name)
||') references '||trim(uqc.rdb$relation_name)
||' ('
||(select list(trim(idet.rdb$field_name)) from rdb$index_segments idet where rdb$index_name = uqc.rdb$index_name)
||');'
from rdb$relation_constraints c
inner join rdb$ref_constraints rc
on rc.rdb$constraint_name = c.rdb$constraint_name
inner join rdb$relation_constraints uqc
on uqc.rdb$constraint_name = rc.rdb$const_name_uq;
Best regards.

How to add 'ON DELETE CASCADE' in ALTER TABLE statement

I have a foreign key constraint in my table, I want to add ON DELETE CASCADE to it.
I have tried this:
alter table child_table_name
modify constraint fk_name
foreign key (child_column_name)
references parent_table_name (parent_column_name) on delete cascade;
Doesn't work.
EDIT:
Foreign key already exists, there are data in foreign key column.
The error message I get after executing the statement:
ORA-02275: such a referential constraint already exists in the table
You can not add ON DELETE CASCADE to an already existing constraint. You will have to drop and re-create the constraint. The documentation shows that the MODIFY CONSTRAINT clause can only modify the state of a constraint (i-e: ENABLED/DISABLED...).
First drop your foreign key and try your above command, put add constraint instead of modify constraint.
Now this is the command:
ALTER TABLE child_table_name
ADD CONSTRAINT fk_name
FOREIGN KEY (child_column_name)
REFERENCES parent_table_name(parent_column_name)
ON DELETE CASCADE;
As explained before:
ALTER TABLE TABLENAME
drop CONSTRAINT FK_CONSTRAINTNAME;
ALTER TABLE TABLENAME
ADD CONSTRAINT FK_CONSTRAINTNAME
FOREIGN KEY (FId)
REFERENCES OTHERTABLE
(Id)
ON DELETE CASCADE ON UPDATE NO ACTION;
As you can see those have to be separated commands, first dropping then adding.
Answer for MYSQL USERS:
ALTER TABLE ChildTableName
DROP FOREIGN KEY `fk_table`;
ALTER TABLE ChildTableName
ADD CONSTRAINT `fk_t1_t2_tt`
FOREIGN KEY (`parentTable`)
REFERENCES parentTable (`columnName`)
ON DELETE CASCADE
ON UPDATE CASCADE;
This PL*SQL will write to DBMS_OUTPUT a script that will drop each constraint that does not have delete cascade and recreate it with delete cascade.
NOTE: running the output of this script is AT YOUR OWN RISK. Best to read over the resulting script and edit it before executing it.
DECLARE
CURSOR consCols (theCons VARCHAR2, theOwner VARCHAR2) IS
select * from user_cons_columns
where constraint_name = theCons and owner = theOwner
order by position;
firstCol BOOLEAN := TRUE;
begin
-- For each constraint
FOR cons IN (select * from user_constraints
where delete_rule = 'NO ACTION'
and constraint_name not like '%MODIFIED_BY_FK' -- these constraints we do not want delete cascade
and constraint_name not like '%CREATED_BY_FK'
order by table_name)
LOOP
-- Drop the constraint
DBMS_OUTPUT.PUT_LINE('ALTER TABLE ' || cons.OWNER || '.' || cons.TABLE_NAME || ' DROP CONSTRAINT ' || cons.CONSTRAINT_NAME || ';');
-- Re-create the constraint
DBMS_OUTPUT.PUT('ALTER TABLE ' || cons.OWNER || '.' || cons.TABLE_NAME || ' ADD CONSTRAINT ' || cons.CONSTRAINT_NAME
|| ' FOREIGN KEY (');
firstCol := TRUE;
-- For each referencing column
FOR consCol IN consCols(cons.CONSTRAINT_NAME, cons.OWNER)
LOOP
IF(firstCol) THEN
firstCol := FALSE;
ELSE
DBMS_OUTPUT.PUT(',');
END IF;
DBMS_OUTPUT.PUT(consCol.COLUMN_NAME);
END LOOP;
DBMS_OUTPUT.PUT(') REFERENCES ');
firstCol := TRUE;
-- For each referenced column
FOR consCol IN consCols(cons.R_CONSTRAINT_NAME, cons.R_OWNER)
LOOP
IF(firstCol) THEN
DBMS_OUTPUT.PUT(consCol.OWNER);
DBMS_OUTPUT.PUT('.');
DBMS_OUTPUT.PUT(consCol.TABLE_NAME); -- This seems a bit of a kluge.
DBMS_OUTPUT.PUT(' (');
firstCol := FALSE;
ELSE
DBMS_OUTPUT.PUT(',');
END IF;
DBMS_OUTPUT.PUT(consCol.COLUMN_NAME);
END LOOP;
DBMS_OUTPUT.PUT_LINE(') ON DELETE CASCADE ENABLE VALIDATE;');
END LOOP;
end;
Here is an handy solution!
I'm using SQL Server 2008 R2.
As you want to modify the FK constraint by adding ON DELETE/UPDATE CASCADE, follow these steps:
NUMBER 1:
Right click on the constraint and click to Modify
NUMBER 2:
Choose your constraint on the left side (if there are more than one). Then on the right side, collapse "INSERT And UPDATE Specification" point and specify the actions on Delete Rule or Update Rule row to suit your need. After that, close the dialog box.
NUMBER 3:
The final step is to save theses modifications (of course!)
PS: It's saved me from a bunch of work as I want to modify a primary key referenced in another table.
For anyone using MySQL:
If you head into your PHPMYADMIN webpage and navigate to the table that has the foreign key you want to update, all you have to do is click the Relational view located in the Structure tab and change the On delete select menu option to Cascade.
Image shown below:
If you want to change a foreign key without dropping it you can do:
ALTER TABLE child_table_name WITH CHECK ADD FOREIGN KEY(child_column_name)
REFERENCES parent_table_name (parent_column_name) ON DELETE CASCADE
for postgresql
BEGIN TRANSACTION ;
ALTER TABLE bank_accounts
DROP CONSTRAINT bank_accounts_company_id_fkey;
ALTER TABLE bank_accounts
ADD CONSTRAINT bank_accounts_company_id_fkey FOREIGN KEY (company_id)
REFERENCES companies (id)
ON DELETE CASCADE;
END;
ALTER TABLE `tbl_celebrity_rows` ADD CONSTRAINT `tbl_celebrity_rows_ibfk_1` FOREIGN KEY (`celebrity_id`)
REFERENCES `tbl_celebrities`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
MySQL workbench img
Right click at the table you want to alter and click alter table, then click Foreign Keys. You can see Foreign Keys Options on the right side and just select cascade and click apply!