MS Access - 关系
我继承了 Microsoft Access 2000 中的一个项目,其中包含代码、表格和数据。查询。我需要将其转换为 SQL。
我看到几个带有 INNER JOINS
和 Select 命令的查询。所以我猜这些表已经建立了关系?
我使用了关系工具(图形工具)和添加所有表。关系没有显现出来。
关系如何建立? 如何检索已设置的关系?
I inherited a project in Microsoft Access 2000 that has code, tables & queries. I need to convert this to SQL.
I see several queries with INNER JOINS
with Select commands. So I am guessing the tables have relations set up?
I used the Relationships tools (Graphical Tool) & add all the tables. The relationships are not showing up.
How has the relationships setup?
How do I retrieve the relations that has been set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论是否为连接表定义了关系,数据库引擎都将接受 JOIN 语句。因此,这些 INNER JOIN 查询并不一定意味着您的数据库已定义任何关系。
有关关系的信息存储在隐藏的系统表 MSysObjects 和 MSysRelationships 中。您可以使用数据库的关系集合来查看该信息。
编辑:您可以检查关系的属性。我在示例中包含了 dbRelationDontEnforce。其他属性(例如 dbRelationDeleteCascade 和 dbRelationUpdateCascade)可能有用。查看 RelationAttributeEnum 枚举的帮助主题以查看可用属性的完整列表。
编辑2:要使用该代码,请在 Access 中打开数据库。然后使用 Ctrl+g 快捷键在 Visual Basic 编辑器中打开立即窗口。然后从VBE主菜单中选择插入->模块。复制代码并将其粘贴到模块中。保存模块,要么接受 Access 提供的名称(类似于“Module1”),要么给它一个对您更有意义的不同名称...但不要将模块命名为 InspectRelations ——模块和过程应该有不同的名字。
从主菜单中选择“调试”->“编译”,确保编译器接受代码。由于您使用的是 Access 2000,因此您可能需要设置“Microsoft DAO 3.6 对象库”的引用。在列表中找到它并在其旁边放置复选标记,然后单击“确定”。然后再次编译。
如果编译没有错误,您可以通过将光标定位在过程体内并按 F5 来运行该过程。 Debug.Print 语句将其输出发送到立即窗口。
The database engine will accept JOIN statements regardless of whether or not relationships have been defined for the joined tables. So those INNER JOIN queries don't necessarily mean your database has any relationships defined.
Information about relationships is stored in the hidden system tables, MSysObjects and MSysRelationships. You can use the database's Relations collection to view that information.
Edit: You can examine Attributes of a relation. I included dbRelationDontEnforce in the example. Other attributes such as dbRelationDeleteCascade and dbRelationUpdateCascade might be useful. Look at the help topic for the RelationAttributeEnum Enumeration to see the full list of available attributes.
Edit2: To use the code, open your database in Access. Then use Ctrl+g shortcut to open the Immediate Window in the Visual Basic Editor. Then select Insert->Module from the VBE main menu. Copy the code and paste it into the module. Save the module, either accept the name (which will be like "Module1") Access offers, or give it a different name which is more meaningful to you ... but do not name the module InspectRelations --- the module and procedure should have different names.
Make sure the compiler accepts the code by choosing Debug->Compile from the main menu. Since you're using Access 2000, you may need to set a reference for "Microsoft DAO 3.6 Object Library". Find it in the list and place a check mark next to it, then click OK. Then compile again.
If it compiled without errors, you can run the procedure by locating the cursor within the body of the procedure and pressing F5 to run it. The Debug.Print statements send their output to the Immediate Window.
如果没有使用关系工具来指示关系并强制引用完整性,那么您就不走运了,因为您可以在不使用关系工具的情况下在表之间建立关系。
连接向您显示所涉及表的主键/外键,因此您可能必须对查询中的
JOIN
子句对关系进行逆向工程。If the Relationship tool was not used to indicate the relationships and enforce referential integrity, you are out of luck as you can have relationships between tables without using the Relationship tool.
The joins show you the primary keys/foreign keys of the involved tables, so you probably have to reverse engineer the relationships from the
JOIN
clauses in the queries.