473,554 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(con stid), fktbl =
object_name(fke yid),
fkcol = col_name(fkeyid , fkey), refcol = col_name(rkeyid ,
rkey), reftbl = object_name(rke yid), 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 1335

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

Similar topics

0
2615
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 the following = MySQL error: INSERT INTO product_access_level (product_id,access_level_id) VALUES
1
8869
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 automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
2
39417
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
18803
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, CustomerId Numeric(10,0) Not Null, CustomerRefId Numeric (10,0) Not Null, ... )
31
3336
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 customer subtypes: 1 - business, 2 - home, 3 - university
14
5770
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 the problem is. I keep getting errors like (the first is my debug output): NOTICE: last cycle is: 11 WARNING: Error occurred while executing...
0
2685
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. That works fine. I don' want to add a record to MgrXRF unless both field values exist in Employees. Problem is I also want to set the delete rule...
5
2484
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 CONSTRAINT code_not_fk check(code not in (select code from IS3.AUCTION_CATEGORIES where auction_id=auction_id)) Is there any way other than a trigger...
1
2126
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` (
0
7539
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7819
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7581
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5165
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3589
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1149
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.