Link to home
Start Free TrialLog in
Avatar of LazyStudent
LazyStudent

asked on

How to delete relationship in Access using SQL query?

How to delete relationship in Access using SQL query?
ASKER CERTIFIED SOLUTION
Avatar of 91mustang
91mustang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of solution46
solution46

I'm absolutley amazed... been using virtually every version of Access for ten years and never realised you could do that...

cheers, 91mustang.

s46.
Avatar of LazyStudent

ASKER

how can I know what is the constraint name of relationship in the table?
Unfortunately, I dont think you can get the constraint name out of the database unless you created it SQL as well:


ALTER TABLE M_Employees ADD CONSTRAINT fk_Employee_Dept FOREIGN KEY (Dept_ID) REFERENCES L_Departments (Dept_ID);

and then to drop:

ALTER TABLE M_Employees DROP CONSTRAINT fk_Employee_Dept;
I will check around and see if it is possible
OK, I knew it was possible, just needed to research it. Its also a good reason to stop using the stupid MS wizards all the time, you won't need to search the web for object properties properties when you want to delete a FK through SQL.

Now that my rant is over, Here it is:

select szRelationship from MSysRelationships where szobject ="Table name where FK exists"

That will give you the name of the constraint.

Oh and by the way, I tested it on a few database's and it always seems to be named "parent table""child table" (with no space), but I would not trust that, its easy enough to check yourself.

cheers
One more thing, If you are dealing with a table that has multiple foreign keys you can use the following query to ensure you get the correct relationship.

select szRelationship from MSysRelationships where szobject="child table" and szreferencedobject="parent table"
Brilliant explanation and assist - thank you!
glad to help
:)