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
?
mysql sql primary-key auto-increment
add a comment |
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
?
mysql sql primary-key auto-increment
add a comment |
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
?
mysql sql primary-key auto-increment
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
mysql sql primary-key auto-increment
asked Nov 11 at 16:14
Poream3387
513214
513214
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
The primary key has three properties:
- It is unique.
- It is non-null.
- 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.
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
add a comment |
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.
add a comment |
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.
add a comment |
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:
- It is unique.
- It is non-null.
- 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.
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
add a comment |
up vote
3
down vote
accepted
The primary key has three properties:
- It is unique.
- It is non-null.
- 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.
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
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The primary key has three properties:
- It is unique.
- It is non-null.
- 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.
The primary key has three properties:
- It is unique.
- It is non-null.
- 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.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 16:19
D-Shih
24.7k61431
24.7k61431
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 19:07
fairtrax
23616
23616
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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