473,378 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Convert Foreign Key to Foreign Key with Delete Cascade

Here is the script which generates drop and add fkey constraint
This is based on another posting
<<
Erland posted this a short time ago, you should be able to modify it to
meet
your needs:
http://tinyurl.com/6o9af


Thanks Erland and every one.

.....................

SET NOCOUNT ON

DECLARE @dummy tinyint,
@fktbl sysname,
@fkcol sysname,
@refcol sysname,
@reftbl sysname,
@keyno smallint,
@constr sysname,
@prev_constr sysname,
@drop varchar(4000),
@add1 varchar(4000),
@add2 varchar(4000),
@tmpname sysname,
@no smallint,
@err int
DECLARE sql_fkey_cur INSENSITIVE CURSOR FOR
SELECT k = 1, constr = object_name(constid), fktbl =
object_name(fkeyid),
fkcol = col_name(fkeyid, fkey), refcol = col_name(rkeyid,
rkey), reftbl = object_name(rkeyid), keyno
FROM sysforeignkeys

OPEN sql_fkey_cur

FETCH NEXT FROM sql_fkey_cur INTO @dummy, @constr, @fktbl, @fkcol,
@refcol,@reftbl, @keyno

WHILE @@fetch_status = 0
Begin

-- SELECT @no = @no + 1
-- And set up command to drop current constraint.
SELECT @drop = 'ALTER TABLE ' + @fktbl +
' DROP CONSTRAINT ' + @constr
Print @drop
SELECT @add1 = 'ALTER TABLE ' + cast(@fktbl as varchar)
+ ' ADD CONSTRAINT ' + @constr +
' FOREIGN KEY (' + cast(@fkcol as varchar) + ')
REFERENCES ' + @reftbl + ' (' +
cast(@refcol as varchar) + ')' + ' on delete
cascade'

print @add1
Print '-------------'
FETCH NEXT FROM sql_fkey_cur INTO @dummy, @constr, @fktbl, @fkcol,
@refcol,@reftbl, @keyno
End
CLOSE sql_fkey_cur
DEALLOCATE sql_fkey_cur

Aug 2 '05 #1
0 1328

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

Similar topics

0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: Gunnar Vøyenli | last post by:
Hi! For the sake of simplicity, I have three tables, Employee, Department and Work Employee >---- Department \ / \ / ^ ^ Work
0
by: Rob Panosh | last post by:
Hello, If I have table the following table structure: Table Customer ( CustomerId Numeric(10,0) Not Null, ... ) Table CustomerOrders ( CustomerOrderId As Numeric(10,0) Not Null,...
31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
14
by: Karl O. Pinc | last post by:
Hi, Thought perhaps some other eyes than mine can tell if I'm doing something wrong here or if there's a bug somewhere. I've never passed a ROWTYPE varaible to a function but I don't see where...
0
by: mrhodes02 | last post by:
I have two simple tables Employees (primary key table) Empid, empname,title MgrXrf (foreign key table) MgrId,Empid MgrID & Empid in MgrXrf have a foreign contstrant to EmpID in Employees....
5
by: Bob Stearns | last post by:
For good and sufficient reasons I wish to insure that a primary key of table 1 is not a primary key of table 2. The following does not work: ALTER TABLE IS3.AUCTION_SUPER_CATEGORIES ADD...
1
by: poopsy | last post by:
hello all i am getting confused with foreign key constraints.. i have the following table: -- -- Table structure for table `reviewer` -- CREATE TABLE `reviewer` (
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.