?
Solved

How to delete relationship in Access using SQL query?

Posted on 2004-08-22
9
Medium Priority
?
837 Views
Last Modified: 2008-01-09
How to delete relationship in Access using SQL query?
0
Comment
Question by:LazyStudent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 4

Accepted Solution

by:
91mustang earned 1000 total points
ID: 11864633
ALTER TABLE table_name DROP CONSTRAINT constraint_name;

0
 
LVL 9

Expert Comment

by:solution46
ID: 11865062
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
ID: 11865891
how can I know what is the constraint name of relationship in the table?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 4

Expert Comment

by:91mustang
ID: 11866193
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
 
LVL 4

Expert Comment

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

Expert Comment

by:91mustang
ID: 11866346
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
ID: 11866437
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
ID: 11868482
Brilliant explanation and assist - thank you!
0
 
LVL 4

Expert Comment

by:91mustang
ID: 11868901
glad to help
:)
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

777 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