Solved

How to delete relationship in Access using SQL query?

Posted on 2004-08-22
9
703 Views
Last Modified: 2008-01-09
How to delete relationship in Access using SQL query?
0
Comment
Question by:LazyStudent
  • 6
  • 2
9 Comments
 
LVL 4

Accepted Solution

by:
91mustang earned 250 total points
Comment Utility
ALTER TABLE table_name DROP CONSTRAINT constraint_name;

0
 
LVL 9

Expert Comment

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

cheers, 91mustang.

s46.
0
 
LVL 5

Author Comment

by:LazyStudent
Comment Utility
how can I know what is the constraint name of relationship in the table?
0
 
LVL 4

Expert Comment

by:91mustang
Comment Utility
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;
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Expert Comment

by:91mustang
Comment Utility
I will check around and see if it is possible
0
 
LVL 4

Expert Comment

by:91mustang
Comment Utility
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
0
 
LVL 4

Expert Comment

by:91mustang
Comment Utility
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"
1
 
LVL 5

Author Comment

by:LazyStudent
Comment Utility
Brilliant explanation and assist - thank you!
0
 
LVL 4

Expert Comment

by:91mustang
Comment Utility
glad to help
:)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now