How do I 'DROP' and unamed relationship/constraint in MS Access with SQL?

后端 未结 4 1791
栀梦
栀梦 2021-01-19 18:32

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 s

4条回答
  •  逝去的感伤
    2021-01-19 18:46

    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
    

提交回复
热议问题