SQL table, 2 columns and keys placement [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this questionHere is a problem. I need to have 2 columns in a table both varchar(50)
. First Column is name of certain thing within a certain group which is defined by the second column.
First column | Second Column
thingone | group one
thingtwo | group one
thingone 开发者_JS百科 | group two
Above is an example which I want to achieve by having these to columns. And my question is how to place keys in order to do it. I know there is a easier way by having third column with id. But I am asking for possibilities looking similar to one above.
You can make a primary key that contain's both columns.
Read the link's article http://weblogs.sqlteam.com/jeffs/archive/2007/08/23/composite_primary_keys.aspx
You shouldn't store the name of your group in your table. You should create another table for groups, maybe called Groups
and store your groups there, using GroupId
and GroupName
columns. Then in your main table, you should have a column named GroupId
which only accepts the Id of the group, or technically speaking, is the foreign key
to the Groups
table.
精彩评论