Solved

How to delete relationship in Access using SQL query?

Posted on 2004-08-22
9
798 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

688 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