472,363 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,363 software developers and data experts.

Drop all foreign keys

What is the quickest way to drop all the foreign keys in a database?
Only FKs, not PKs.

Cheers,
San.

Oct 6 '06 #1
8 11581

shsandeep wrote:
What is the quickest way to drop all the foreign keys in a database?
Only FKs, not PKs.
You'll have to do this with some manual processing.

The following query will give you a list of all foreign key constraints
in the database:

select tabname, constname from syscat.tabconst where type='F';

Then you can issue a "ALTER TABLE <tabnameDROP FOREIGN KEY
<constname>" for each key listed.

Oct 6 '06 #2
me******@yahoo.com wrote:
>
shsandeep wrote:
>What is the quickest way to drop all the foreign keys in a database?
Only FKs, not PKs.

You'll have to do this with some manual processing.

The following query will give you a list of all foreign key constraints
in the database:

select tabname, constname from syscat.tabconst where type='F';

Then you can issue a "ALTER TABLE <tabnameDROP FOREIGN KEY
<constname>" for each key listed.
You could wrap this in a stored procedure and issue automatically execute
the DDL statements inside a loop that goes through the result of your above
query. Then you don't have any manual processing any more.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 6 '06 #3
shsandeep wrote:
What is the quickest way to drop all the foreign keys in a database?
Only FKs, not PKs.

Cheers,
San.
I do this routinely for certain types of database maintenance. Here's
my UNIX shell script to do so. It generates a file that contains all of
the alter table statements to drop the FKs on the table. (One word of
caution, make sure you have a a copy of the FK defintions elsewhere in
the case you need to reapply them. The output of db2look can come in
handy here.) You'll have to replace some of the variables with info
pertaining to your database.

Regards,
Evan

################################################## ###
#!/bin/ksh

outfile=fkdrop.ddl

db2 connect to $yourdb user $youruserid using $yourpasswd

db2 "export to fkgen.dat of del \
select constname, tabschema, tabname, reftabschema, reftabname,
fk_colnames, pk_
colnames \
from syscat.references "

echo "-- Foreign Keys as of $(date)" $outfile

cat fkgen.dat |while read line
do
IFS=","
let i=0
for token in $line
do
arr[$i]=$token
let i=$i+1
done

print "\nALTER TABLE ${arr[1]}.${arr[2]}" >$outfile
print "DROP CONSTRAINT ${arr[0]} ;" >$outfile
done

Oct 6 '06 #4
me******@yahoo.com wrote:
shsandeep wrote:
What is the quickest way to drop all the foreign keys in a database?
Only FKs, not PKs.

You'll have to do this with some manual processing.

The following query will give you a list of all foreign key constraints
in the database:

select tabname, constname from syscat.tabconst where type='F';

Then you can issue a "ALTER TABLE <tabnameDROP FOREIGN KEY
<constname>" for each key listed.
Or:

SELECT 'ALTER TABLE' || tabname || 'DROP FOREIGN KEY' || constname ||
';' FROM SysCat.TabConst WHERE Type='F';

Though, i usually wrap the names so the 128 characters don't push
things off the screen.

SELECT 'ALTER TABLE' || VARCHAR(TabName, 30) || 'DROP FOREIGN KEY' ||
VARCHAR(ConstName, 30) || ';' FROM SysCat.TabConst WHERE Type='F';
B.

Oct 6 '06 #5

shsandeep wrote:
What is the quickest way to drop all the foreign keys in a database?
Only FKs, not PKs.

Cheers,
San.
I use a pythonscript with the following sql (it also droppes
alternative keys and check constraints):

[...]
sql = """
select tabname, constname from syscat.tabconst
where tabschema = ? and type <'P'
order by type
"""
c1.execute(sql,(schema))
for row in c1.fetchall():
table = row[0]
const = row[1]
drop = """
alter table %s.%s drop constraint %s
""" % (schema,table,const)
try:
c2.execute(drop)
except Exception, DB2.error:
print "Warning: %s" % (DB2.error)
pass
[...]

Oct 6 '06 #6
Brian Tkatch wrote:
me******@yahoo.com wrote:
>shsandeep wrote:
What is the quickest way to drop all the foreign keys in a database?
Only FKs, not PKs.

You'll have to do this with some manual processing.

The following query will give you a list of all foreign key constraints
in the database:

select tabname, constname from syscat.tabconst where type='F';

Then you can issue a "ALTER TABLE <tabnameDROP FOREIGN KEY
<constname>" for each key listed.

Or:

SELECT 'ALTER TABLE' || tabname || 'DROP FOREIGN KEY' || constname ||
';' FROM SysCat.TabConst WHERE Type='F';

Though, i usually wrap the names so the 128 characters don't push
things off the screen.

SELECT 'ALTER TABLE' || VARCHAR(TabName, 30) || 'DROP FOREIGN KEY' ||
VARCHAR(ConstName, 30) || ';' FROM SysCat.TabConst WHERE Type='F';
Some spaces are missing. Besides, using RTRIM would be much safer because
it doesn't cause truncation if there are longer table/constraint names.

SELECT 'ALTER TABLE ' || RTRIM(TabName) || ' DROP FOREIGN KEY ' ||
RTRIM(ConstName) || ';' FROM SysCat.TabConst WHERE Type='F';

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 6 '06 #7
Thanks to everyone for replying!!

Cheers,
San.

Oct 8 '06 #8
Knut Stolze wrote:
Brian Tkatch wrote:
me******@yahoo.com wrote:
shsandeep wrote:
What is the quickest way to drop all the foreign keys in a database?
Only FKs, not PKs.

You'll have to do this with some manual processing.

The following query will give you a list of all foreign key constraints
in the database:

select tabname, constname from syscat.tabconst where type='F';

Then you can issue a "ALTER TABLE <tabnameDROP FOREIGN KEY
<constname>" for each key listed.
Or:

SELECT 'ALTER TABLE' || tabname || 'DROP FOREIGN KEY' || constname ||
';' FROM SysCat.TabConst WHERE Type='F';

Though, i usually wrap the names so the 128 characters don't push
things off the screen.

SELECT 'ALTER TABLE' || VARCHAR(TabName, 30) || 'DROP FOREIGN KEY' ||
VARCHAR(ConstName, 30) || ';' FROM SysCat.TabConst WHERE Type='F';

Some spaces are missing. Besides, using RTRIM would be much safer because
it doesn't cause truncation if there are longer table/constraint names.

SELECT 'ALTER TABLE ' || RTRIM(TabName) || ' DROP FOREIGN KEY ' ||
RTRIM(ConstName) || ';' FROM SysCat.TabConst WHERE Type='F';

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oops,iforgotaboutthosespaces!

RTRIM() is nice, but, if the command are to be copied off the screen,
there will be a lot of white space to sift through. Being the command
generates a warning on truncation, and i do such things manually, i use
VARCHAR() instead, and modify the length as required.

B.

Oct 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joy | last post by:
MySQL automatically creates an index for the primary key. Now, I want to drop it before I start inserting a million rows of data. Can I drop it at all? Thanks.
1
by: Sabrina | last post by:
Hi everybody, I need some help in SQL Server. I am looking for a command that will "Drop all user table" in a user database. Can anyone help me? Thank you very much Sabrina
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
1
by: Woody Rao | last post by:
I'm trying to drop all indexes and primary keys so that i can rebuild them (from a script created from same database on another server). when i go to the 'generate sql scripts', it has the...
0
by: Bart | last post by:
Hello Sometimes i need to drop one of my tables. DROP TABLE removes all foreign keys connected with that table. After this i need to recreate FK. It is not easy to find dropped keys, so i...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
2
by: clickon | last post by:
I am using ASP.net 2.0 and trying to take advantage of the updated data editing facilities provided through the SQLDataSource control and the DetailsView control. The data is a record from a...
1
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
1
jamesd0142
by: jamesd0142 | last post by:
select 'ALTER TABLE ' + primary_table + ' drop CONSTRAINT ' + primary_key_name + ' FOREIGN KEY (' + foreign_column_1 + ')' from sysfkeys order by foreign_table How can I change this code to...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.