Venkat
Venkat

Reputation: 2156

two primary key fields for a column in ms-sql

I have a table with the following fields:

  searchID ( I have set this as a primary key )
  SearchText nvarchar(MAX)
  .......
  and so on

I want to make SearchText also as an additional primary field. How should this be done? Is that a good procedure to make two primary columns for a table?

Upvotes: 0

Views: 513

Answers (2)

Backs
Backs

Reputation: 24903

It's impossible to have more than one primary key on one table and store unique values.

Your primary key must be as short as possible.

If you need to keep unique data in other column, you can create unique key on this column:

CREATE TABLE dbo.Table
(
   SearchText nvarchar(MAX)NOT NULL, 
   CONSTRAINT AK_SearchText UNIQUE(SearchText) 
); 

Or with management studio:

enter image description here

Upvotes: 1

DevelopmentIsMyPassion
DevelopmentIsMyPassion

Reputation: 3591

You can create composite key as below

create table myTable 
(
    SearchId int not null,
SearchText nvharchar not null

) GO

-- Add Constraint
ALTER TABLE myTable
ADD CONSTRAINT pk_myConstraint PRIMARY KEY (SearchId ,SearchText)
GO

Upvotes: 0

Related Questions