Should the referencing table have primary key, auto increment, or index?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I'm trying to implement a list. Each list item will be associated with one or more tags. And each tag will be associated with one or more list items.



I'm still pretty new to this field. Fortunately, I managed to find a solution in this question, whose answer suggests the following database design:



Articles table
- id
- name
- etc

Relational Tag Arts table
- Fkey tags id
- Fkey article id

Tags table
- id
- name
- etc


I already created my three tables. Now what I don't understand is that, should the referencing table (the one that contains two foreign keys) have primary key, auto increment, or index?



And although I'm not 100% sure, I guess the referencing table's content is purposely "repetitive". For example, if I add a new item which is associated with 5 tags, then the referencing table should be added with 5 more records, which all have the same item id but have different tag id's. Am I correct?










share|improve this question



















  • 1




    Correct, about the repetition of the article and tag ids in the many-to-many table.
    – ypercubeᵀᴹ
    49 mins ago










  • Thanks for helping out!
    – Ian Y.
    8 mins ago

















up vote
1
down vote

favorite












I'm trying to implement a list. Each list item will be associated with one or more tags. And each tag will be associated with one or more list items.



I'm still pretty new to this field. Fortunately, I managed to find a solution in this question, whose answer suggests the following database design:



Articles table
- id
- name
- etc

Relational Tag Arts table
- Fkey tags id
- Fkey article id

Tags table
- id
- name
- etc


I already created my three tables. Now what I don't understand is that, should the referencing table (the one that contains two foreign keys) have primary key, auto increment, or index?



And although I'm not 100% sure, I guess the referencing table's content is purposely "repetitive". For example, if I add a new item which is associated with 5 tags, then the referencing table should be added with 5 more records, which all have the same item id but have different tag id's. Am I correct?










share|improve this question



















  • 1




    Correct, about the repetition of the article and tag ids in the many-to-many table.
    – ypercubeᵀᴹ
    49 mins ago










  • Thanks for helping out!
    – Ian Y.
    8 mins ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to implement a list. Each list item will be associated with one or more tags. And each tag will be associated with one or more list items.



I'm still pretty new to this field. Fortunately, I managed to find a solution in this question, whose answer suggests the following database design:



Articles table
- id
- name
- etc

Relational Tag Arts table
- Fkey tags id
- Fkey article id

Tags table
- id
- name
- etc


I already created my three tables. Now what I don't understand is that, should the referencing table (the one that contains two foreign keys) have primary key, auto increment, or index?



And although I'm not 100% sure, I guess the referencing table's content is purposely "repetitive". For example, if I add a new item which is associated with 5 tags, then the referencing table should be added with 5 more records, which all have the same item id but have different tag id's. Am I correct?










share|improve this question















I'm trying to implement a list. Each list item will be associated with one or more tags. And each tag will be associated with one or more list items.



I'm still pretty new to this field. Fortunately, I managed to find a solution in this question, whose answer suggests the following database design:



Articles table
- id
- name
- etc

Relational Tag Arts table
- Fkey tags id
- Fkey article id

Tags table
- id
- name
- etc


I already created my three tables. Now what I don't understand is that, should the referencing table (the one that contains two foreign keys) have primary key, auto increment, or index?



And although I'm not 100% sure, I guess the referencing table's content is purposely "repetitive". For example, if I add a new item which is associated with 5 tags, then the referencing table should be added with 5 more records, which all have the same item id but have different tag id's. Am I correct?







database-design






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago

























asked 2 hours ago









Ian Y.

1134




1134







  • 1




    Correct, about the repetition of the article and tag ids in the many-to-many table.
    – ypercubeᵀᴹ
    49 mins ago










  • Thanks for helping out!
    – Ian Y.
    8 mins ago













  • 1




    Correct, about the repetition of the article and tag ids in the many-to-many table.
    – ypercubeᵀᴹ
    49 mins ago










  • Thanks for helping out!
    – Ian Y.
    8 mins ago








1




1




Correct, about the repetition of the article and tag ids in the many-to-many table.
– ypercubeᵀᴹ
49 mins ago




Correct, about the repetition of the article and tag ids in the many-to-many table.
– ypercubeᵀᴹ
49 mins ago












Thanks for helping out!
– Ian Y.
8 mins ago





Thanks for helping out!
– Ian Y.
8 mins ago











1 Answer
1






active

oldest

votes

















up vote
2
down vote













You may want to consider taking one step back a think about the design process. Once you are familiar with that you can often skip it and directly implement it. I would suggest to start out with the tables (in theory called relations). You seem to have found:



* Articles
* Tags


Next step is to think about what uniquely identifies a row (tuple) in Articles and Tags. That may be a single column (attribute) or a combination of columns. What would you use to identify an article if you talked to someone about it? All such attributes or combination of attributes is known as candidate keys. In this process, you may choose to add an artificial (surrogate) key, I assume id is such an attribute. The reasons for adding surrogate keys can, for example, be that the natural keys are too complex, not stable enough, etc.



Speaking of id, I strongly suggest that you name it article_id or similar, and stick with that name through the model. Names such as Id, Name, etc are too vague to be meaningful and usually collides with reserved words in the DBMS that you use. Assume we have concluded this:



CREATE TABLE Articles
( article_id ... not null primary key -- surrogate key
, article_name ... not null
, publishing_date date not null
, ...
, primary key (article_id)
, unique ( article_name, publishing_date )
);

CREATE TABLE Tags
( tag_id ... not null primary key
, tag_name ... not null unique
);


How are these tables related? You seem to have concluded that there is an N-N relationship between an Article and a Tag and this is usually implemented with a third table (sometime named junction or link table). Let's call this table:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)

);


Now for your original question, what uniquely identifies this relationship? I would suggest here a composite key like:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)
, primary key (article_id, tag_id)
);


If you sometimes want to retrieve all articles for a given tag you can add an index like:



CREATE UNIQUE INDEX ... ON Article_Tags (tag_id, article_id);


The above is much simplified but may give you an idea or two on how to start thinking on the design process.






share|improve this answer




















  • Thank you for such a detailed explanation! Could you please explain the purpose of primary key and UNIQUE INDEX in the Article_Tags table? If I understand correctly, queries will only use either article_id or tag_id.
    – Ian Y.
    8 mins ago











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f221700%2fshould-the-referencing-table-have-primary-key-auto-increment-or-index%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













You may want to consider taking one step back a think about the design process. Once you are familiar with that you can often skip it and directly implement it. I would suggest to start out with the tables (in theory called relations). You seem to have found:



* Articles
* Tags


Next step is to think about what uniquely identifies a row (tuple) in Articles and Tags. That may be a single column (attribute) or a combination of columns. What would you use to identify an article if you talked to someone about it? All such attributes or combination of attributes is known as candidate keys. In this process, you may choose to add an artificial (surrogate) key, I assume id is such an attribute. The reasons for adding surrogate keys can, for example, be that the natural keys are too complex, not stable enough, etc.



Speaking of id, I strongly suggest that you name it article_id or similar, and stick with that name through the model. Names such as Id, Name, etc are too vague to be meaningful and usually collides with reserved words in the DBMS that you use. Assume we have concluded this:



CREATE TABLE Articles
( article_id ... not null primary key -- surrogate key
, article_name ... not null
, publishing_date date not null
, ...
, primary key (article_id)
, unique ( article_name, publishing_date )
);

CREATE TABLE Tags
( tag_id ... not null primary key
, tag_name ... not null unique
);


How are these tables related? You seem to have concluded that there is an N-N relationship between an Article and a Tag and this is usually implemented with a third table (sometime named junction or link table). Let's call this table:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)

);


Now for your original question, what uniquely identifies this relationship? I would suggest here a composite key like:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)
, primary key (article_id, tag_id)
);


If you sometimes want to retrieve all articles for a given tag you can add an index like:



CREATE UNIQUE INDEX ... ON Article_Tags (tag_id, article_id);


The above is much simplified but may give you an idea or two on how to start thinking on the design process.






share|improve this answer




















  • Thank you for such a detailed explanation! Could you please explain the purpose of primary key and UNIQUE INDEX in the Article_Tags table? If I understand correctly, queries will only use either article_id or tag_id.
    – Ian Y.
    8 mins ago















up vote
2
down vote













You may want to consider taking one step back a think about the design process. Once you are familiar with that you can often skip it and directly implement it. I would suggest to start out with the tables (in theory called relations). You seem to have found:



* Articles
* Tags


Next step is to think about what uniquely identifies a row (tuple) in Articles and Tags. That may be a single column (attribute) or a combination of columns. What would you use to identify an article if you talked to someone about it? All such attributes or combination of attributes is known as candidate keys. In this process, you may choose to add an artificial (surrogate) key, I assume id is such an attribute. The reasons for adding surrogate keys can, for example, be that the natural keys are too complex, not stable enough, etc.



Speaking of id, I strongly suggest that you name it article_id or similar, and stick with that name through the model. Names such as Id, Name, etc are too vague to be meaningful and usually collides with reserved words in the DBMS that you use. Assume we have concluded this:



CREATE TABLE Articles
( article_id ... not null primary key -- surrogate key
, article_name ... not null
, publishing_date date not null
, ...
, primary key (article_id)
, unique ( article_name, publishing_date )
);

CREATE TABLE Tags
( tag_id ... not null primary key
, tag_name ... not null unique
);


How are these tables related? You seem to have concluded that there is an N-N relationship between an Article and a Tag and this is usually implemented with a third table (sometime named junction or link table). Let's call this table:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)

);


Now for your original question, what uniquely identifies this relationship? I would suggest here a composite key like:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)
, primary key (article_id, tag_id)
);


If you sometimes want to retrieve all articles for a given tag you can add an index like:



CREATE UNIQUE INDEX ... ON Article_Tags (tag_id, article_id);


The above is much simplified but may give you an idea or two on how to start thinking on the design process.






share|improve this answer




















  • Thank you for such a detailed explanation! Could you please explain the purpose of primary key and UNIQUE INDEX in the Article_Tags table? If I understand correctly, queries will only use either article_id or tag_id.
    – Ian Y.
    8 mins ago













up vote
2
down vote










up vote
2
down vote









You may want to consider taking one step back a think about the design process. Once you are familiar with that you can often skip it and directly implement it. I would suggest to start out with the tables (in theory called relations). You seem to have found:



* Articles
* Tags


Next step is to think about what uniquely identifies a row (tuple) in Articles and Tags. That may be a single column (attribute) or a combination of columns. What would you use to identify an article if you talked to someone about it? All such attributes or combination of attributes is known as candidate keys. In this process, you may choose to add an artificial (surrogate) key, I assume id is such an attribute. The reasons for adding surrogate keys can, for example, be that the natural keys are too complex, not stable enough, etc.



Speaking of id, I strongly suggest that you name it article_id or similar, and stick with that name through the model. Names such as Id, Name, etc are too vague to be meaningful and usually collides with reserved words in the DBMS that you use. Assume we have concluded this:



CREATE TABLE Articles
( article_id ... not null primary key -- surrogate key
, article_name ... not null
, publishing_date date not null
, ...
, primary key (article_id)
, unique ( article_name, publishing_date )
);

CREATE TABLE Tags
( tag_id ... not null primary key
, tag_name ... not null unique
);


How are these tables related? You seem to have concluded that there is an N-N relationship between an Article and a Tag and this is usually implemented with a third table (sometime named junction or link table). Let's call this table:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)

);


Now for your original question, what uniquely identifies this relationship? I would suggest here a composite key like:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)
, primary key (article_id, tag_id)
);


If you sometimes want to retrieve all articles for a given tag you can add an index like:



CREATE UNIQUE INDEX ... ON Article_Tags (tag_id, article_id);


The above is much simplified but may give you an idea or two on how to start thinking on the design process.






share|improve this answer












You may want to consider taking one step back a think about the design process. Once you are familiar with that you can often skip it and directly implement it. I would suggest to start out with the tables (in theory called relations). You seem to have found:



* Articles
* Tags


Next step is to think about what uniquely identifies a row (tuple) in Articles and Tags. That may be a single column (attribute) or a combination of columns. What would you use to identify an article if you talked to someone about it? All such attributes or combination of attributes is known as candidate keys. In this process, you may choose to add an artificial (surrogate) key, I assume id is such an attribute. The reasons for adding surrogate keys can, for example, be that the natural keys are too complex, not stable enough, etc.



Speaking of id, I strongly suggest that you name it article_id or similar, and stick with that name through the model. Names such as Id, Name, etc are too vague to be meaningful and usually collides with reserved words in the DBMS that you use. Assume we have concluded this:



CREATE TABLE Articles
( article_id ... not null primary key -- surrogate key
, article_name ... not null
, publishing_date date not null
, ...
, primary key (article_id)
, unique ( article_name, publishing_date )
);

CREATE TABLE Tags
( tag_id ... not null primary key
, tag_name ... not null unique
);


How are these tables related? You seem to have concluded that there is an N-N relationship between an Article and a Tag and this is usually implemented with a third table (sometime named junction or link table). Let's call this table:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)

);


Now for your original question, what uniquely identifies this relationship? I would suggest here a composite key like:



CREATE TABLE Article_Tags
( article_id ... not null references Articles (article_id)
, tag_id ... not null references Tags (tag_id)
, primary key (article_id, tag_id)
);


If you sometimes want to retrieve all articles for a given tag you can add an index like:



CREATE UNIQUE INDEX ... ON Article_Tags (tag_id, article_id);


The above is much simplified but may give you an idea or two on how to start thinking on the design process.







share|improve this answer












share|improve this answer



share|improve this answer










answered 51 mins ago









Lennart

12.2k21243




12.2k21243











  • Thank you for such a detailed explanation! Could you please explain the purpose of primary key and UNIQUE INDEX in the Article_Tags table? If I understand correctly, queries will only use either article_id or tag_id.
    – Ian Y.
    8 mins ago

















  • Thank you for such a detailed explanation! Could you please explain the purpose of primary key and UNIQUE INDEX in the Article_Tags table? If I understand correctly, queries will only use either article_id or tag_id.
    – Ian Y.
    8 mins ago
















Thank you for such a detailed explanation! Could you please explain the purpose of primary key and UNIQUE INDEX in the Article_Tags table? If I understand correctly, queries will only use either article_id or tag_id.
– Ian Y.
8 mins ago





Thank you for such a detailed explanation! Could you please explain the purpose of primary key and UNIQUE INDEX in the Article_Tags table? If I understand correctly, queries will only use either article_id or tag_id.
– Ian Y.
8 mins ago


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f221700%2fshould-the-referencing-table-have-primary-key-auto-increment-or-index%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery