Solved

How to delete relationship in Access using SQL query?

Posted on 2004-08-22
9
767 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 250 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

696 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