Solved

How to delete relationship in Access using SQL query?

Posted on 2004-08-22
9
733 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
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

821 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