Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to the primary key Id when it goes over the limit?

If you add a new record, then delete the record, then add it again over and over, in time, sooner or later, when you add a new record the integer primary key id, it will eventually exceed 2 billion.

  1. Now what happens? SQL Server will start the primary key id from 1 again? or -1?

  2. What happens if it cycles 4 billion times; how does SQL Server know not to replace the previous data?

like image 668
001 Avatar asked Mar 14 '11 22:03

001


People also ask

Can a primary key take a value more than once?

It cannot have more than one primary key.

What is primary key ID?

A primary key, also called a primary keyword, is a key in a relational database that is unique for each record. It is a unique identifier, such as a driver license number, telephone number (including area code), or vehicle identification number (VIN). A relational database must always have one and only one primary key.

Is ID always primary key?

In many cases an identity column is used as a primary key; however, this is not always the case. It is a common misconception that an identity column will enforce uniqueness; however, this is not the case.

Can primary key be disabled?

You can disable a primary key using the ALTER TABLE statement in SQL Server (Transact-SQL).


1 Answers

You get an error if the identity would exceed the bounds of the datatype making the rest of your question moot. You can see this by

CREATE TABLE #T
(
id INT IDENTITY(2147483647,1)
)

INSERT INTO #T
DEFAULT VALUES

INSERT INTO #T
DEFAULT VALUES /*Arithmetic overflow error converting IDENTITY to data type int.*/

GO

SELECT * FROM #T

DROP TABLE #T
like image 146
Martin Smith Avatar answered Oct 17 '22 06:10

Martin Smith