Home >Database >Mysql Tutorial >How to Create a Composite Primary Key in SQL Server 2008?

How to Create a Composite Primary Key in SQL Server 2008?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-07 11:16:441037browse

How to Create a Composite Primary Key in SQL Server 2008?

Creating Composite Primary Keys in SQL Server 2008

A composite primary key is a unique combination of two or more columns that identifies each row of a table. This ensures that each row in the table is distinct. Creating a composite primary key in SQL Server 2008 is a straightforward process.

To create a composite primary key, follow these steps:

  1. Declare the columns that you want to include in the primary key as NOT NULL to enforce uniqueness. For example:
column_a integer not null,
column_b integer not null,
  1. Use the PRIMARY KEY constraint with the ( and ) symbols to specify the columns that comprise the primary key. For instance:
primary key (column_a, column_b)

This creates a primary key consisting of the column_a and column_b columns. Each combination of values in these columns must be unique for each row in the table.

Example:

Let's create a table named my_table with a composite primary key consisting of the column_a and column_b columns:

create table my_table (
     column_a integer not null,
     column_b integer not null,
     column_c varchar(50),
     primary key (column_a, column_b)
);

Now, each row in the my_table table will be uniquely identified by the combination of column_a and column_b.

The above is the detailed content of How to Create a Composite Primary Key in SQL Server 2008?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn