Sql Server Sequence Generated Primary Key On Multiple Columns

  1. Primary Key With Multiple Columns
  2. Sql Server Sequence Generated Primary Key On Multiple Columns In Microsoft
  3. Sql Alter Column Primary Key
  4. Sql Server Sequence Generated Primary Key On Multiple Columns Definition
  • Mar 20, 2020 Stop spreading that nonsense. It is not necessarily. You can define a clustered index and primary key separate from each other and the primary key itself only has the unique constraint plus is not allowed to contain NULL (even not once).
  • I am currently undertaking a review of the primary keys in a SQL Server 2000 database with a view to improving performance of queries. I have heard that, in the case of compound primary keys, it is important to select the correct order for the columns within the key. For instance, imagine a table called OrderLine which has primary key.
  • The SEQUENCE statement introduced in SQL Server 2012 brings the ANSI SQL 2003 standard method of generating IDs. This is a great relief to database professionals as it solves some problems what are awkward to solve with the IDENTITY property. Joe Celko explains the basics of using a SEQUENCE.
  • APPLIES TO: SQL Server Azure SQL Database Azure Synapse Analytics (SQL DW) Parallel Data Warehouse. Creates a sequence object and specifies its properties. A sequence is a user-defined schema bound object that generates a sequence of numeric values according to the specification with which the sequence was created.
  • May 29, 2012  Jamie King of Neumont University demonstrating how auto-generated primary key values behave with INSERT. SQL Primary Keys - Auto-Generating With Identity Columns.
By: Ben Snaidero Updated: 2014-05-02 Comments (3) Related: 1 2 3 4 5 6 7 8 9 More >Query Optimization

Problem

You've installed SQL Server with all the defaults, created your database and schema then loaded a bunch of data into your database. Now you realize that some or all of the columns in your database needed to have the COLLATION set to be case sensitive. This tip will take you through the steps required to change the column COLLATION of your tables from SQL_Latin1_General_CP1_CI_AS to SQL_Latin1_General_CP1_CS_AS.

Solution

Table Setup

As discussed in the documentation, a sequence in Oracle prevents duplicate values from being created simultaneously because multiple users are effectively forced to “take turns” before each sequential item is generated. For the purposes of creating a unique primary key for a new table, first we must CREATE the table we’ll be using.

Let's first setup a couple of tables in order to walkthrough these steps required to update the column collation. The following script will create two tables as well as some constraints, indexes and statistics in order to illustrate each of the steps that may be required depending on the design of your schema. For this example we won't add any data to our tables as it wouldn't have any effect on the update process since we are going from case insensitive to case sensitive. It's important to note that if you were performing the reverse update that is from case sensitive to case insensitive, you would also have an extra step of resolving any data issues as the update could result in duplicate data in columns with unique constraints. That said, here is the complete setup script listing.

Identify columns

Depending on your situation you may need to only change the collation of a few columns or possible every column in your database. You can use the following query which will list any columns in your database that have the default collation.

For this tip let's assume that we are changing the collation of every column in our database. If you read the following link, Set or Change the Column Collation, from TechNet you'll notice that there are some restrictions on when you can change the collation of a column. Basically, if a column is referenced by any of the following objects you will not be able to change the collation without first removing these references.

  • A computed column
  • An index
  • Distribution statistics, either generated automatically or by the CREATE STATISTICS statement
  • A CHECK constraint
  • A FOREIGN KEY constraint
Identify referencing objects

Let's now identify which objects need to be removed before we can make the collation update to our columns. The following queries, one for each category listed above, will identify objects that have a dependency on a column that we are going to be updating. Since we are updating all the columns in the database the only filter I have is 'WHERE c.collation_name like 'SQL_Latin1_General_CP1_CI_AS'. If your requirements are different you can expand on this as required. I've also include a sample output after each query for reference. One other item to note is with the foreign key section. We need to remove the referenced primary key index in order to make the collation update to that column as well so that the section includes two queries. One to identify the foreign key and the other for the primary key.


Table NameColumn NameCollationDefinition
MainTableCharColumnSQL_Latin1_General_CP1_CI_AS([CharColumn]+[VarcharColumn])
MainTableVarcharColumnSQL_Latin1_General_CP1_CI_AS([CharColumn]+[VarcharColumn])

Foreign Key NameTable NameColumn NameCollation
FK_MainTable_FKTableMainTableCharColumnSQL_Latin1_General_CP1_CI_AS
Reference Table NameReference Column NameCollation
FKTableCharColumnSQL_Latin1_General_CP1_CI_AS

Primary Key NameTable NameColumn NameCollation
PK_FKTableFKTableCharColumnSQL_Latin1_General_CP1_CI_AS

Index NameTable NameColumn NameCollation
IX_Table_NVarcharColumnMainTableNVarcharColumnSQL_Latin1_General_CP1_CI_AS

Statistics NameTable NameColumn NameCollation
Stats_MainTable_VarcharMaxColumnMainTableVarcharMaxColumnSQL_Latin1_General_CP1_CI_AS

Constraint NameTable NameColumn NameCollation
CK_Table_NVarCharMaxColumnMainTableNVarcharMaxColumnSQL_Latin1_General_CP1_CI_AS
Definition
([NVarCharMaxColumn]='Banana' OR [NVarCharMaxColumn]='Orange' OR [NVarCharMaxColumn]='Pear' OR [NVarCharMaxColumn]='Apple')

Drop referencing objects

Now that we've identified everything that we need to remove we can use any tool, including SSMS, to create drop scripts for these objects. Remember to at the same time create a script that will recreate these objects so we can run that after our column definition update is done. Here is the script to drop the objects identified above.

Update the collation

Primary Key With Multiple Columns

After all that work we can now issue simple ALTER TABLE .. ALTER COLUMN .. statements to update the collation for each column in our database. Here is the script to perform this operation.

Recreate referencing objects

Once the schema update completes we can run the script we generated above to recreate all of the referencing objects that we dropped earlier. Here is the script for our example.

Next Steps
  • Read other tips on SQL Server Collation:

Last Updated: 2014-05-02



About the author
Ben Snaidero has been a SQL Server and Oracle DBA for over 10 years and focuses on performance tuning.
View all my tips
Related Resources


Summary: in this tutorial, you will learn about the SQL Server Sequence objects to generate a sequence of numeric values based on a specified specification.

What is a sequence

A sequence is simply a list of numbers, in which their orders are important. For example, the {1,2,3} is a sequence while the {3,2,1} is an entirely different sequence.

In SQL Server, a sequence is a user-defined schema-bound object that generates a sequence of numbers according to a specified specification. A sequence of numeric values can be in ascending or descending order at a defined interval and may cycle if requested.

SQL Server CREATE SEQUENCE statement

To create a new sequence object, you use the CREATE SEQUENCE statement as follows:

Let’s examine the syntax in detail:

sequence_name

Specify a name for the sequence which is uniquely in the current database.

AS integer_type

Use any valid integer type for the sequence e.g., TINYINT, SMALLINT, INT, BIGINT, or DECIMAL and NUMERIC with a scale of 0. By default, the sequence object uses BIGINT.

START WITH start_value

Specify the first value that the sequence returns. The start_value must be between the range (min_value, max_value).

The start_value defaults to the min_value in an ascending sequence and max_value in a descending sequence.

INCREMENT BY increment_value

Specify the increment_value of the sequence object when you call the NEXT VALUE FOR function.

If increment_value is negative, the sequence object is descending; otherwise, the sequence object is ascending. Note that the increment_value cannot be zero.

[ MINVALUE min_value NO MINVALUE ]

Specify the lower bound for the sequence object. It defaults to the minimum value of the data type of the sequence object i.e., zero for TINYINT and a negative number for all other data types.

[ MAXVALUE max_value NO MAXVALUE]

Specify the upper bound for the sequence object. It defaults to the maximum value of the data type of the sequence object.

Sql server sequence generated primary key on multiple columns in photoshop

[ CYCLE NO CYCLE ]

Use CYCLE if you want the value of the sequence object to restart from the min_value for the ascending sequence object, or max_value for the descending sequence object or throw an exception when its min_value or max_value is exceeded. SQL Server uses NO CYCLE by default for new sequence objects.

[ CACHE cache_size ] NO CACHE ]

Specify the number of values to cache to improve the performance of the sequence by minimizing the number of disk I/O required to generate sequence numbers. By default, SQL Server uses NO CACHE for new sequence objects.

SQL Server Sequence examples

Let’s take some examples of creating sequences.

A) Creating a simple sequence example

The following statement uses the CREATE SEQUENCE statement to create a new sequence named item_counter with the type of integer (INT), which starts from 10 and increments by 10:

You can view the sequence object under in the Programmability > Sequences as shown in the following picture:

The following statement returns the current value of the item_counter sequence: /frontlines-fuel-of-war-cd-key-generator.html.

Here is the output:

In this example, the NEXT VALUE FOR function generates a sequence number from the item_counter sequence object.

Each time you execute the following statement again, you will see that the value of the item_counter will be incremented by 10:

This time the output is:

B) Using a sequence object in a single table example

First, create a new schema named procurement:

Next, create a new table named orders:

Sql Server Sequence Generated Primary Key On Multiple Columns In Microsoft

Then, create a new sequence object named order_number that starts with 1 and is incremented by 1:

After that, insert three rows into the procurement.purchase_orders table and uses values generated by the procurement.order_number sequence:

Finally, view the content of the procurement.purchase_orders table:

Here is the output:

C) Using a sequence object in multiple tables example

First, create a new sequence object:

Sql Alter Column Primary Key

Second, create procurement.goods_receipts and procurement.invoice_receipts tables:

Note that both tables have the receipt_id whose values are derived from the procurement.receipt_no sequence.

Third, insert some rows into both tables without supplying the values for the receipt_id columns:

Fourth, query data from both tables:

Here is the output:

Sequence vs. Identity columns

Sequences, different from the identity columns, are not associated with a table. The relationship between the sequence and the table is controlled by applications. In addition, a sequence can be shared across multiple tables.

The following table illustrates the main differences between sequences and identity columns:

When to use sequences

You use a sequence object instead of an identity column in the following cases:

  • The application requires a number before inserting values into the table.
  • The application requires sharing a sequence of numbers across multiple tables or multiple columns within the same table.
  • The application requires to restart the number when a specified value is reached.
  • The application requires multiple numbers to be assigned at the same time. Note that you can call the stored procedure sp_sequence_get_range to retrieve several numbers in a sequence at once.
  • The application needs to change the specification of the sequence like maximum value.

Getting sequences information

You use the view sys.sequences to get the detailed information of sequences.

Sql Server Sequence Generated Primary Key On Multiple Columns Definition

In this tutorial, you have learned about the SQL Server sequences to generate a sequence of numbers by a specified specification.