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
Release: 2025-01-07 11:16:44
Original
1032 people have browsed it

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,
Copy after login
  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)
Copy after login

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)
);
Copy after login

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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template