What's the point of using AUTO_INCREMENT and PRIMARY KEY at the same time in MySQL?









up vote
3
down vote

favorite












In MySQL, I understand below




AUTO_INCREMENT: used to make INT value to increase automatically every time a user creates a row.



PRIMARY KEY: used to make value unique in that table.




However, I oftentimes see them used together when defining id.



I don't understand the point of using AUTO_INCREMENT with PRIMARY KEY since AUTO_INCREMENT itself would make id unique.



Is there any reason they are used together when creating id?










share|improve this question

























    up vote
    3
    down vote

    favorite












    In MySQL, I understand below




    AUTO_INCREMENT: used to make INT value to increase automatically every time a user creates a row.



    PRIMARY KEY: used to make value unique in that table.




    However, I oftentimes see them used together when defining id.



    I don't understand the point of using AUTO_INCREMENT with PRIMARY KEY since AUTO_INCREMENT itself would make id unique.



    Is there any reason they are used together when creating id?










    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      In MySQL, I understand below




      AUTO_INCREMENT: used to make INT value to increase automatically every time a user creates a row.



      PRIMARY KEY: used to make value unique in that table.




      However, I oftentimes see them used together when defining id.



      I don't understand the point of using AUTO_INCREMENT with PRIMARY KEY since AUTO_INCREMENT itself would make id unique.



      Is there any reason they are used together when creating id?










      share|improve this question













      In MySQL, I understand below




      AUTO_INCREMENT: used to make INT value to increase automatically every time a user creates a row.



      PRIMARY KEY: used to make value unique in that table.




      However, I oftentimes see them used together when defining id.



      I don't understand the point of using AUTO_INCREMENT with PRIMARY KEY since AUTO_INCREMENT itself would make id unique.



      Is there any reason they are used together when creating id?







      mysql sql primary-key auto-increment






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 16:14









      Poream3387

      513214




      513214






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          The primary key has three properties:



          1. It is unique.

          2. It is non-null.

          3. There is only one per table.

          Defining the key as a primary key means that it should also be used for foreign key references.



          In addition, MySQL clusters the data by the primary key. So the declaration instructs new rows to go at the "end" of the table -- meaning adjacent to the most recent inserts on the data pages.



          In addition, duplicate values for the auto-incremented id could be created in various ways. One way is that the increment counter can be reset, causing duplicates. MySQL should be pretty thread-safe on duplicates for concurrent updates, but bugs have been reported. As a primary key, no duplicates will be allowed into the table.






          share|improve this answer




















          • I still didn't get the part "Defining the key as a primary key means that it should also be used for foreign key references." is it mandatory to do so? Could you elaborate on that so that I could understand clearly, please?
            – Poream3387
            Nov 12 at 7:15










          • @Poream3387 . . . Do you understand what a foreign key reference is?
            – Gordon Linoff
            Nov 12 at 13:01










          • Oh yes, I know what foreign key is. You are saying that if I were to use foreign key to reference some key, it has to be the primary key that is getting referenced right?
            – Poream3387
            Nov 12 at 13:22











          • @Poream3387 . . . Unfortunately, it does not have to be. But it is a best practice to only use primary keys for foreign key references.
            – Gordon Linoff
            Nov 12 at 13:31










          • Oh that's what you meant on your post! Thanks!
            – Poream3387
            Nov 12 at 13:59

















          up vote
          2
          down vote













          You understand it correctly, but they are doing different things.



          PRIMARY KEY with AUTO_INCREMENT means we want this column isn't duplicate on the value and it will be auto increase if we didn't set the value.



          but how about we only set AUTO_INCREMENT it only means it will be auto increase if we didn't set the value. but didn't make sure the value is unique.






          share|improve this answer



























            up vote
            2
            down vote













            AUTO_INCREMENT doesn't make the column uniqe. It only "automatically" fills a value when creating a row, if missing. But you can later update the values, or also create a row by explicitly providing the value.



            PRIMARY KEY denies any modification sql statement that would cause 2 different entries storing equal values. So it guarantees you, that your DB is in a correct state.






            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fstackoverflow.com%2fquestions%2f53250643%2fwhats-the-point-of-using-auto-increment-and-primary-key-at-the-same-time-in-mys%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote



              accepted










              The primary key has three properties:



              1. It is unique.

              2. It is non-null.

              3. There is only one per table.

              Defining the key as a primary key means that it should also be used for foreign key references.



              In addition, MySQL clusters the data by the primary key. So the declaration instructs new rows to go at the "end" of the table -- meaning adjacent to the most recent inserts on the data pages.



              In addition, duplicate values for the auto-incremented id could be created in various ways. One way is that the increment counter can be reset, causing duplicates. MySQL should be pretty thread-safe on duplicates for concurrent updates, but bugs have been reported. As a primary key, no duplicates will be allowed into the table.






              share|improve this answer




















              • I still didn't get the part "Defining the key as a primary key means that it should also be used for foreign key references." is it mandatory to do so? Could you elaborate on that so that I could understand clearly, please?
                – Poream3387
                Nov 12 at 7:15










              • @Poream3387 . . . Do you understand what a foreign key reference is?
                – Gordon Linoff
                Nov 12 at 13:01










              • Oh yes, I know what foreign key is. You are saying that if I were to use foreign key to reference some key, it has to be the primary key that is getting referenced right?
                – Poream3387
                Nov 12 at 13:22











              • @Poream3387 . . . Unfortunately, it does not have to be. But it is a best practice to only use primary keys for foreign key references.
                – Gordon Linoff
                Nov 12 at 13:31










              • Oh that's what you meant on your post! Thanks!
                – Poream3387
                Nov 12 at 13:59














              up vote
              3
              down vote



              accepted










              The primary key has three properties:



              1. It is unique.

              2. It is non-null.

              3. There is only one per table.

              Defining the key as a primary key means that it should also be used for foreign key references.



              In addition, MySQL clusters the data by the primary key. So the declaration instructs new rows to go at the "end" of the table -- meaning adjacent to the most recent inserts on the data pages.



              In addition, duplicate values for the auto-incremented id could be created in various ways. One way is that the increment counter can be reset, causing duplicates. MySQL should be pretty thread-safe on duplicates for concurrent updates, but bugs have been reported. As a primary key, no duplicates will be allowed into the table.






              share|improve this answer




















              • I still didn't get the part "Defining the key as a primary key means that it should also be used for foreign key references." is it mandatory to do so? Could you elaborate on that so that I could understand clearly, please?
                – Poream3387
                Nov 12 at 7:15










              • @Poream3387 . . . Do you understand what a foreign key reference is?
                – Gordon Linoff
                Nov 12 at 13:01










              • Oh yes, I know what foreign key is. You are saying that if I were to use foreign key to reference some key, it has to be the primary key that is getting referenced right?
                – Poream3387
                Nov 12 at 13:22











              • @Poream3387 . . . Unfortunately, it does not have to be. But it is a best practice to only use primary keys for foreign key references.
                – Gordon Linoff
                Nov 12 at 13:31










              • Oh that's what you meant on your post! Thanks!
                – Poream3387
                Nov 12 at 13:59












              up vote
              3
              down vote



              accepted







              up vote
              3
              down vote



              accepted






              The primary key has three properties:



              1. It is unique.

              2. It is non-null.

              3. There is only one per table.

              Defining the key as a primary key means that it should also be used for foreign key references.



              In addition, MySQL clusters the data by the primary key. So the declaration instructs new rows to go at the "end" of the table -- meaning adjacent to the most recent inserts on the data pages.



              In addition, duplicate values for the auto-incremented id could be created in various ways. One way is that the increment counter can be reset, causing duplicates. MySQL should be pretty thread-safe on duplicates for concurrent updates, but bugs have been reported. As a primary key, no duplicates will be allowed into the table.






              share|improve this answer












              The primary key has three properties:



              1. It is unique.

              2. It is non-null.

              3. There is only one per table.

              Defining the key as a primary key means that it should also be used for foreign key references.



              In addition, MySQL clusters the data by the primary key. So the declaration instructs new rows to go at the "end" of the table -- meaning adjacent to the most recent inserts on the data pages.



              In addition, duplicate values for the auto-incremented id could be created in various ways. One way is that the increment counter can be reset, causing duplicates. MySQL should be pretty thread-safe on duplicates for concurrent updates, but bugs have been reported. As a primary key, no duplicates will be allowed into the table.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 11 at 16:20









              Gordon Linoff

              751k34286394




              751k34286394











              • I still didn't get the part "Defining the key as a primary key means that it should also be used for foreign key references." is it mandatory to do so? Could you elaborate on that so that I could understand clearly, please?
                – Poream3387
                Nov 12 at 7:15










              • @Poream3387 . . . Do you understand what a foreign key reference is?
                – Gordon Linoff
                Nov 12 at 13:01










              • Oh yes, I know what foreign key is. You are saying that if I were to use foreign key to reference some key, it has to be the primary key that is getting referenced right?
                – Poream3387
                Nov 12 at 13:22











              • @Poream3387 . . . Unfortunately, it does not have to be. But it is a best practice to only use primary keys for foreign key references.
                – Gordon Linoff
                Nov 12 at 13:31










              • Oh that's what you meant on your post! Thanks!
                – Poream3387
                Nov 12 at 13:59
















              • I still didn't get the part "Defining the key as a primary key means that it should also be used for foreign key references." is it mandatory to do so? Could you elaborate on that so that I could understand clearly, please?
                – Poream3387
                Nov 12 at 7:15










              • @Poream3387 . . . Do you understand what a foreign key reference is?
                – Gordon Linoff
                Nov 12 at 13:01










              • Oh yes, I know what foreign key is. You are saying that if I were to use foreign key to reference some key, it has to be the primary key that is getting referenced right?
                – Poream3387
                Nov 12 at 13:22











              • @Poream3387 . . . Unfortunately, it does not have to be. But it is a best practice to only use primary keys for foreign key references.
                – Gordon Linoff
                Nov 12 at 13:31










              • Oh that's what you meant on your post! Thanks!
                – Poream3387
                Nov 12 at 13:59















              I still didn't get the part "Defining the key as a primary key means that it should also be used for foreign key references." is it mandatory to do so? Could you elaborate on that so that I could understand clearly, please?
              – Poream3387
              Nov 12 at 7:15




              I still didn't get the part "Defining the key as a primary key means that it should also be used for foreign key references." is it mandatory to do so? Could you elaborate on that so that I could understand clearly, please?
              – Poream3387
              Nov 12 at 7:15












              @Poream3387 . . . Do you understand what a foreign key reference is?
              – Gordon Linoff
              Nov 12 at 13:01




              @Poream3387 . . . Do you understand what a foreign key reference is?
              – Gordon Linoff
              Nov 12 at 13:01












              Oh yes, I know what foreign key is. You are saying that if I were to use foreign key to reference some key, it has to be the primary key that is getting referenced right?
              – Poream3387
              Nov 12 at 13:22





              Oh yes, I know what foreign key is. You are saying that if I were to use foreign key to reference some key, it has to be the primary key that is getting referenced right?
              – Poream3387
              Nov 12 at 13:22













              @Poream3387 . . . Unfortunately, it does not have to be. But it is a best practice to only use primary keys for foreign key references.
              – Gordon Linoff
              Nov 12 at 13:31




              @Poream3387 . . . Unfortunately, it does not have to be. But it is a best practice to only use primary keys for foreign key references.
              – Gordon Linoff
              Nov 12 at 13:31












              Oh that's what you meant on your post! Thanks!
              – Poream3387
              Nov 12 at 13:59




              Oh that's what you meant on your post! Thanks!
              – Poream3387
              Nov 12 at 13:59












              up vote
              2
              down vote













              You understand it correctly, but they are doing different things.



              PRIMARY KEY with AUTO_INCREMENT means we want this column isn't duplicate on the value and it will be auto increase if we didn't set the value.



              but how about we only set AUTO_INCREMENT it only means it will be auto increase if we didn't set the value. but didn't make sure the value is unique.






              share|improve this answer
























                up vote
                2
                down vote













                You understand it correctly, but they are doing different things.



                PRIMARY KEY with AUTO_INCREMENT means we want this column isn't duplicate on the value and it will be auto increase if we didn't set the value.



                but how about we only set AUTO_INCREMENT it only means it will be auto increase if we didn't set the value. but didn't make sure the value is unique.






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  You understand it correctly, but they are doing different things.



                  PRIMARY KEY with AUTO_INCREMENT means we want this column isn't duplicate on the value and it will be auto increase if we didn't set the value.



                  but how about we only set AUTO_INCREMENT it only means it will be auto increase if we didn't set the value. but didn't make sure the value is unique.






                  share|improve this answer












                  You understand it correctly, but they are doing different things.



                  PRIMARY KEY with AUTO_INCREMENT means we want this column isn't duplicate on the value and it will be auto increase if we didn't set the value.



                  but how about we only set AUTO_INCREMENT it only means it will be auto increase if we didn't set the value. but didn't make sure the value is unique.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 16:19









                  D-Shih

                  24.7k61431




                  24.7k61431




















                      up vote
                      2
                      down vote













                      AUTO_INCREMENT doesn't make the column uniqe. It only "automatically" fills a value when creating a row, if missing. But you can later update the values, or also create a row by explicitly providing the value.



                      PRIMARY KEY denies any modification sql statement that would cause 2 different entries storing equal values. So it guarantees you, that your DB is in a correct state.






                      share|improve this answer
























                        up vote
                        2
                        down vote













                        AUTO_INCREMENT doesn't make the column uniqe. It only "automatically" fills a value when creating a row, if missing. But you can later update the values, or also create a row by explicitly providing the value.



                        PRIMARY KEY denies any modification sql statement that would cause 2 different entries storing equal values. So it guarantees you, that your DB is in a correct state.






                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          AUTO_INCREMENT doesn't make the column uniqe. It only "automatically" fills a value when creating a row, if missing. But you can later update the values, or also create a row by explicitly providing the value.



                          PRIMARY KEY denies any modification sql statement that would cause 2 different entries storing equal values. So it guarantees you, that your DB is in a correct state.






                          share|improve this answer












                          AUTO_INCREMENT doesn't make the column uniqe. It only "automatically" fills a value when creating a row, if missing. But you can later update the values, or also create a row by explicitly providing the value.



                          PRIMARY KEY denies any modification sql statement that would cause 2 different entries storing equal values. So it guarantees you, that your DB is in a correct state.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 11 at 19:07









                          fairtrax

                          23616




                          23616



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250643%2fwhats-the-point-of-using-auto-increment-and-primary-key-at-the-same-time-in-mys%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3

                              Museum of Modern and Contemporary Art of Trento and Rovereto