获取ms-access中表的外键约束列表?

时间:2012-07-31 14:14:33

标签: sql ms-access foreign-keys jet

我正在尝试使用关系窗口来查看MS-Access数据库中表之间的关系。我隐藏了所有其他表,并在其中一个表中找到了一个外键,它应该链接到另一个带有主键的表,我需要跟踪的信息。

有没有办法列出Access中表的现有外键约束,就像SHOW CREATE TABLE syntax in MySQL

1 个答案:

答案 0 :(得分:8)

DAO和ADOSchemas中的tabledef集合将显示外键。

Dim tdf As TableDef
Dim db As Database
Dim ndx As Index

Set db = CurrentDb

For Each tdf In db.TableDefs
    For Each ndx In tdf.Indexes
        If ndx.Foreign = True Then
            Debug.Print tdf.Name, ndx.Name
            For Each fld In ndx.Fields
                Debug.Print fld.Name
            Next
        End If
    Next
Next

关系

Dim rel As DAO.Relation
Dim db As Database
Set db = CurrentDb

For Each rel In db.Relations
    Debug.Print rel.Name, rel.ForeignTable
    For Each fld In rel.Fields
        Debug.Print fld.Name
    Next
Next

ADOSchema

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = CurrentProject.Connection

Set rs = cn.OpenSchema(adSchemaForeignKeys)

Do While Not rs.EOF
    For i = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(i).Name & ":- " & rs.Fields(i)
    Next
    rs.MoveNext
Loop

最后,有一个隐藏的系统表:

SELECT * FROM MSysRelationships