Sql Server Sequence Generation Primary Key

Sql Server Sequence Generation Primary Key 6,3/10 1628 reviews

Learn how to define an auto increment primary key in SQL Server. This data tutorial will explain basic table creation and information around using identity a. Apr 24, 2007  There are effectively two ways to do using the built-in features that T-SQL provides: Identity Columns - An identity is a common 'auto generated' primary key to use in a SQL Server database these days. An identity is simply an integer value that 'auto increments' as each new row is. I automated my database/table generation, it has a 'flaw,' is this because it's SQLite? I used SQLite because the connection string was easy to do, I may port it over to SQL server instead, basically it adds any new items it finds, but only if I first delete the old database and rerun the startup. Help with sending a primary key from table. You can define a primary key in SQL Server by using SQL Server Management Studio or Transact-SQL. Creating a primary key automatically creates a corresponding unique clustered index, or a nonclustered index if specified as such.

A sequence is an object in SQL Server (Transact-SQL) that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key. Create Sequence.

By Jeff Smith on 24 April 2007 Tags: Table Design , Identity

This article by Jeff Smith covers different ways to create sequence numbers. It starts with the basic identity and GUIDs and quickly moves to much more complicated sequence numbers including those with auto-incrementing mixed letters and numbers such as 'A0001', etc. Jeff shows one of the easiest ways I've ever seen to efficiently handle very odd sequence numbers.

Introduction

Sometimes you'd like the SQL Server itself to automatically generate a sequence for entities in your table as they are created. For example, assigning each new Customer added to your table a unique 'CustomerNumber'. There are effectively two ways to do using the built-in features that T-SQL provides:

  • IdentityColumns - An identity is a common 'auto generated' primary key to use in a SQL Server database these days. An identity is simply an integer value that 'auto increments' as each new row is added to your table. You can specify when it should start and how it should increment when you add the column to your table:
  • GUID Columns - A 'GUID' is a Globally Unique Identifier that can be assigned a unique yet random long string of characters like 'B3FA6F0A-523F-4931-B3F8-0CF41E2A48EE'. You can either use the NEWID() function when inserting into your table or set a default like this to implement a GUID column in your tables:

However, we often see questions in the forums regarding how to create other types of auto-generated sequences in tables. For example, you might want your customers to automatically be assigned 'Customer Numbers' as formatted like this:

In other cases, people would like to use incrementing letters instead of numbers, some combination of both, or for the digits to be 'reset' on some specific condition, and so on.

The most important and crucial part of implementing this is not writing the code! It is clearly defining your specification and ensuring that it is logical and works for you. Before you can write code that will automatically generate sequences for you, you must consider:

  • How many numbers will you ever need? Does your specification handle this?
  • What happens when values are deleted? Are they re-used?
  • Are these sequences dependant on data that might ever change? What happens to these values when the data does change? Does it make sense, then, to incorporate this data into your sequence algorithm?
  • If you have a complicated rule (i.e., 'AA-00' through 'ZZ-99'), is every step and possibility clearly defined? In this example, what comes after AA-99? Is it 'BA-00', 'AB-00', 'BB-00', or something else? What comes after 'ZZ-99'?

So, the very first step is to clearly, accurately, and completely define how your sequence values will be generated. You must explicitly map out how to handle all possible situations and you must do some research to ensure that your specification will work for the data you are handling. A primary key of 'A0' through 'Z9' will only work for 26*10 = 260 values -- is this really what you want?

There are a few different approaches you can take in order to facilitate this behavior in SQL Server, so let's take a look.

Option 1: Determine the next value by querying a table

This first approach is usually the most common, and in my opinion also the worst. What people try to do here is to query the existing table to retrieve the last sequence value created, and then use that value to create the next one.

For example, you might create a User-Defined Function that you could use in a trigger like this:

This can cause some issues, however:

  • What if two processes attempt to add a row to the table at the exact same time? Can you ensure that the same value is not generated for both processes?
  • There can be overhead querying the existing data each time you'd like to insert new data
  • Unless this is implemented as a trigger, this means that all inserts to your data must always go through the same stored procedure that calculates these sequences. This means that bulk imports, or moving data from production to testing and so on, might not be possible or might be very inefficient.
  • If it is implemented as a trigger, will it work for a set-based multi-row INSERT statement? If so, how efficient will it be? This function wouldn't work if called for each row in a single set-based INSERT -- each NextCustomerNumber() returned would be the same value.

Overall, if this approach is absolutely required, then it's what you've got to do, but be sure that you consider the next two options first, which are much easier to implement and will generally work in most cases.

Option 2: Make it a presentation issue

The most common rules seem to be the simple ones, such as previous example ('C0000' to 'C9999'), or something similar. Looking at this closely, we see that it is really just simply a number from 1-x, formatted with leading zeroes to be 4 digits, and then prefixed with a 'C'. Which means that all the database needs to do is generate a number from 1-x, which means ... why not just use an identity and let your front-end format the sequence value? Do you really need to store it in the database as a VARCHAR? What does this gain? If you simply use an identity and return an integer to the front-end, it is trivial to format it with a 'C' in the front and with the necessary leading zeroes. By doing this, you have all of the advantages of a built-in SQL Server generated identity value with none of the headaches -- no worries about concurrency, performance, set-based triggers, and so on.

So, really carefully think to yourself: 'Do I really need to format a simple integer in T-SQL and store that formatted value in my tables? Or can I simply use that integer internally throughout the database and format it any way I want at my presentation layer?'

The advantage of this approach is simplicity, but the disadvantage is that you must rely on your front-end applications and reports to understand how to format your codes.

Option 3: Let an Identity be your guide

Sql Server Sequence Generation Primary Key

If you really need to store the actual, auto-generated sequence in your tables, or if a simple incrementing integer formatted a certain way isn't enough, there is another simple option: Use a regular identity column internally as your table's primary key, but then use some math to calculate your external 'auto-generated sequence' in another column using the identity value generated. This column can be stored in your table via a trigger, added as a computed column, or calculated using a View. You can implement it any way you wish.

The advantage of this approach is that we are using the database's built-in feature that guarantees that no two identities in a table will be alike, and that every row will get one; therefore, if we map each integer to a unique value in our sequence, we are guaranteed that all of our sequence values will also be unique. And this will work for set-based operations as well.

Let's start with the simple 'C0000' - 'C9999' example. First, let's create our Customers table like this:

Note that the dbID column is standard, database-generated identity which will be our physical primary key of the table. However, we will add a CustomerNumber column which will be what we expose to the outside world in the 'C0000' format, as described.

Let's create a function accepts an integer, and uses that integer to return our CustomerNumber:

Using that function, we can simply add a computed column to our table like this:

Or, we could also create a column in our table to store the Customer Number, and use a trigger to populate it:

Using either method, once they are in place, we can simply insert into our table, and for each Row added a unique 'Customer Number' is assigned:

The key to making this work is the formula that maps a 1:1 relation between integers and your sequence values.

That example was very trivial to implement; let's try some more complicated ones.

For example, suppose that the CustomerNumber will be in this format:

How can we take an integer and map it to this? In this case, it is the same way you convert digits from decimal to hexadecimal or any other 'base' -- we are converting from a base 10 (decimal) to base 26 (A-Z). Since we are working with a 4 'digit' value in base 26, this gives us 26 to the power of 4 different possibilities -- 456,976 different Customer Numbers can generated. (Remember the most important step before writing any code: is this acceptable?)

A simple algorithm to convert our integers to this base 26 number could be like this:

Starting with an integer identity value x:

  1. x mod 26 corresponds to the RIGHTMOST letter (0=A, 25=Z)
  2. x divided by 26 (26 to the power of 1) mod 26 corresponds to the NEXT letter from right to left
  3. x divided by 26*26 (26 to the power of 2) mod 26 corresponds to the NEXT letter, from right to left.
  4. x divided by 26*26*26 (26 to the power of 3) mod 26 corresponds to the LEFTMOST (first) letter.

To convert a number from 0-25 to a letter from A-Z, we add 65 to it and use the CHAR() function, since the ASCII value of 'A' is 65 and the rest of the letters all follow in sequence (i.e., 'B' is 66, 'C' is 67, etc).

Thus, to implement this method, our CustomerNumber function becomes:

Equally as important as writing the algorithm out on paper is then testing your implementation. We can do some simple testing like this to help us ensure that our function is working:

And that appears to do what we need. You should of course test the higher values as well.

The key is that we have mapped our integer values using our function to Customer Numbers from AAAA-ZZZZ, all guaranteed to be unique, and we don't need to worry about set-based inserts or lookups on our existing table.

Let's try one more. How about

In this one, the left 2 digits are base 26; the right 3 are base 10. This gives us 26*26*10*10*10 = 676,000 possible values.

We use the same basic algorithm, starting from an integer x and working right to left:

  1. x mod 10 equals that is the rightmost digit
  2. x / 10 mod 10 equals the next digit
  3. x / 10*10 mod 10 equals the next digit
  4. x / 10*10*10 mod 26 equals the next letter
  5. x / 26*10*10*10 mod 26 equals the first letter.

Let's put this logic into a User Defined Function, which makes testing easier:

And let's test this function to ensure that it works:

And, again, that is just a guideline, but you should do as much testing as you can before implementing any of these features to ensure that they accurately do what you need. The key is to identify and test the 'boundary' numbers where the values need to reset or change in a more complicated manner than simply incrementing a single digit.

If you need to add dashes, or a constant prefix or suffix or anything else, you can easily do it as well, all in your UDF. The key is to make your logic entirely dependant on an integer value, and to map each integer value to a unique value in your designated sequence. By doing this, you are letting SQL Server do the hard part -- ensure that your keys are consistent and unique -- but now you have the flexibility of creating your 'CustomerNumbers' or other values in the exact format that you choose.

Summary

So, if you do find that you need to custom sequences of some sort at the database layer, here's my recommendation:

  1. Be sure that your code generation algorithm is well defined and handles your needs
  2. Be sure that it isn't just a presentation issue
  3. Write a UDF to map integers to your sequence values and let SQL Server generate those integers via an Identity column
  4. Test your UDF on its own thoroughly to ensure that it produces unique values in the format that you want
  5. Decide if a computed column, trigger, or View is the best way to incorporate these codes into your database layer
  6. Use identities internally as physical primary keys to your tables and for relations. (Note that this is optional; you may wish to use your sequence column, but be sure that it is indexed and constrained properly in your database.)

It can sometimes be tricky to come up with an algorithm and/or write the code to convert integers into your format, but if you can do it, I have found that in general it is the best approach to take.

Related Articles

Efficiently Reuse Gaps in an Identity Column (9 February 2010)

Implementing Table Interfaces (19 May 2008)

Implementing Table Inheritance in SQL Server (20 February 2008)

How to Insert Values into an Identity Column in SQL Server (6 August 2007)

Using the OUTPUT Clause to Capture Identity Values on Multi-Row Inserts (14 August 2006)

The Case for the Surrogate Key (9 August 2002)

Using TABLE Variables (7 June 2002)

More Trees & Hierarchies in SQL (1 May 2002)

Other Recent Forum Posts

Error encountered while Creating a Publications (2h)

Oracle developer Excel export daily (4h)

Want to split the string after comma (5h)

Making a simple webshop with SQL and C+ (12h)

Job Truncates Data (14h)

Convert Text to Date and Order By Date (1d)

Migrating to a new server WEDNESDAY morning, April 15 (2d)

Procedure edit for accumulated points (2d)

Subscribe to SQLTeam.com

Resources

-->

APPLIES TO: SQL Server 2016 and later Azure SQL Database Azure Synapse Analytics (SQL DW) Parallel Data Warehouse

You can define a primary key in SQL Server by using SQL Server Management Studio or Transact-SQL. Creating a primary key automatically creates a corresponding unique clustered index, or a nonclustered index if specified as such.

Before You Begin

Limitations and Restrictions

  • A table can contain only one PRIMARY KEY constraint.

  • All columns defined within a PRIMARY KEY constraint must be defined as NOT NULL. If nullability is not specified, all columns participating in a PRIMARY KEY constraint have their nullability set to NOT NULL.

Security

Permissions

Creating a new table with a primary key requires CREATE TABLE permission in the database and ALTER permission on the schema in which the table is being created.

Creating a primary key in an existing table requires ALTER permission on the table.

Using SQL Server Management Studio

To create a primary key

Sql Server Sequence Generation Primary Key Of Life

  1. In Object Explorer, right-click the table to which you want to add a unique constraint, and click Design.
  2. In Table Designer, click the row selector for the database column you want to define as the primary key. If you want to select multiple columns, hold down the CTRL key while you click the row selectors for the other columns.
  3. Right-click the row selector for the column and select Set Primary Key.

Caution

If you want to redefine the primary key, any relationships to the existing primary key must be deleted before the new primary key can be created. A message will warn you that existing relationships will be automatically deleted as part of this process.

A primary key column is identified by a primary key symbol in its row selector.

If a primary key consists of more than one column, duplicate values are allowed in one column, but each combination of values from all the columns in the primary key must be unique.

If you define a compound key, the order of columns in the primary key matches the order of columns as shown in the table. However, you can change the order of columns after the primary key is created. For more information, see Modify Primary Keys.

Using Transact-SQL

To create a primary key in an existing table

The following example creates a primary key on the column TransactionID in the AdventureWorks database.

Sql Server Sequence Generation Primary Key Mean

To create a primary key in a new table

The following example creates a table and defines a primary key on the column TransactionID in the AdventureWorks database.

To create a primary key with clustered index in a new table

Sequence In Sql Server

The following example creates a table and defines a primary key on the column CustomerID and a clustered index on TransactionID in the AdventureWorks database.

Sql Server Sequence Generation Primary Key 2017

See Also