views:

92

answers:

3

I have a Microsoft Access database and I have two tables. Table1 has a primary key and Table2 has a foreign key that references Table1's primary key. This relationship is set up and viewable in the Relationship viewer in MS Access, the 'Enforce Referential Integrity' check box is checked, and the Join type is an inner join. The relationship is:

[Table1]--1---------N--[Table2]

I need to be able to 'DROP' this relationship/constraint via SQL. How do I do this? I have no name for this relationship/constraint as it was set up in Access manually, not with SQL. Is there a way to do what I need to do?

+1  A: 

Determine the relationship using

SELECT szRelationship FROM Msysrelationships WHERE szObject = 'childtablename' and szReferencedObject = 'parenttablename'

THEN

Use the ALTER TABLE command. Something along the line of this

ALTER TABLE Table2 DROP CONSTRAINT Relation1

Mowgli
The thing is that 'Relation1' would be the name of the constraint. As I mentioned I have no name for my constraint and don't know how to find it. Also, when I run this statement, with either table, it says, "CHECK constraint 'Relation1' does not exist."
Mike Webb
try this to find out what the constraint name isSELECT szRelationshipFROM MsysrelationshipsWHERE szObject = 'childtablename' and szReferencedObject = 'parenttablename'
Mowgli
Thanks! That did the trick.
Mike Webb
+2  A: 

To drop a relationship named with a GUID, as relationships created in the relationship window are named, you need to use square brackets, like so:

ALTER TABLE TableName 
DROP CONSTRAINT [{0992AADA-3921-4AC0-8E95-745A87709D91}]

It is not too difficult to find the name of a relationship using the system table MsysRelationships, the columns are:

ccolumn
grbit
icolumn 
szColumn    
szObject    
szReferencedColumn  
szReferencedObject  
szRelationship

In your case, the name will be a GUID, say {A869FC34-81AF-4D29-B81D-74180BF73025}

You can also use VBA and ADO schemas to list relationships.

If you would like to say what is available to you, it will be easier to suggest a suitable method to get the name.

EDIT in VBA

Sub ListRelations()
Dim rel As DAO.Relation

For Each rel In CurrentDb.Relations
    Debug.Print rel.Name
    Debug.Print rel.ForeignTable
    Debug.Print rel.Table
    For Each fld In rel.Fields
        Debug.Print fld.Name
    Next
Next
End Sub
Remou
@Remou: To change the constraint I must use SQL, but to get the name I can use whatever resource I need. I have a copy of the database on my work machine, but the real one is on a production server for which I only have access to through SQL.
Mike Webb
Well that should be easy enough then, I have added a note for VBA.
Remou
Thanks for the suggestion. It wound up being easier to do with SQL, but I appreciate your help.
Mike Webb
The system tables, mentioned in the first part of this answer, can be accessed with SQL as Mowgli now shows.
Remou
I have been looking into this a little more closely, and do not think that finding the name will be the end of your problems.
Remou
I have added a note on how to drop a constraint named by Access relationship window.
Remou
A: 

If you can see it in the relationship viewer, you can click on it and delete it from there.

Beth
I have access to the live database only through SQL. I have a copy on my local work machine, but ultimately can only use SQL to achieve what I need.
Mike Webb