How to define composite keys in MS Access?

房东的猫 提交于 2020-01-30 04:12:12

问题


I am new to MS Access. Could anybody tell me how to define a composite primary key in MS Access 2007.


回答1:


If memory serves correctly, you can hold SHIFT while selecting the fields in the design view. Once you've selected all of the fields that are to be part of the key, press the primary key button.




回答2:


Already answered, but I wanted to chime in with additional info.

After solving my related problem, holding "ctrl" + clicking multiple columns in design view and then clicking the "primary key" icon in the upper left worked great.

I was receiving an error that stated,

The changes you requested to the table were not successful because they would create duplicate values in the index, primary key or relationship.

I wasn't sure if it was my data, or if I was doing something wrong. Sure enough it was my data - I had accidentally entered duplicate data without realizing it.

I used the following query that I skimmed off of a dbforums.com post to quickly identify the problem:

SELECT ColumnName1
     , ColumnName2
     , ColumnName3
     , ColumnName4
FROM   TableName 
GROUP
    BY ColumnName1
     , ColumnName2
     , ColumnName3
     , ColumnName4
HAVING Count(*) > 1

After adapting the SQL to my tables the query immediately pointed out the offending entry. I eliminated it, fixed the issue in my table that allowed the duplicate to be created, and was able to create my composite primary key no problem.



来源:https://stackoverflow.com/questions/6335094/how-to-define-composite-keys-in-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!