Wednesday, March 7, 2012

Multiple column unique constraint

I am new to 05 and I am searching for a way to add uniqueness to two
fields that are not the PK. I am searching for a built in way to add
uniquness that doesn't require writing a trigger or use the primary key.CREATE TABLE dbo.foo
(
col1 INT PRIMARY KEY,
col2 INT,
col3 INT
);
GO
ALTER TABLE dbo.foo
ADD CONSTRAINT myConstraint
UNIQUE (col2, col3);
GO
DROP TABLE dbo.foo;
GO
"danielhoin" <danielhoin@.hotmail.com> wrote in message
news:1145990176.065271.100630@.t31g2000cwb.googlegroups.com...
>I am new to 05 and I am searching for a way to add uniqueness to two
> fields that are not the PK. I am searching for a built in way to add
> uniquness that doesn't require writing a trigger or use the primary key.
>|||There are several ways to do this.
You could use the create index statement to create an index.
You could create a table constraint that enforces the uniqueness.
CREATE TABLE SOMETABLE (
SOMETABLE_PRIMARYKEY NUMERIC IDENTITY,
COLUMN_1_OF_UNIQUEINDEX VARCHAR(255) NULL,
COLUMN_2_OF_UNIQUEINDEX VARCHAR(255) NULL,
CONSTRAINT PK_SOMETABLE PRIMARY KEY (SOMETABLE_PRIMARYKEY),
CONSTRAINT AK_SOMETABLE_AK_SOMETABL UNIQUE (COLUMN_1_OF_UNIQUEINDEX,
COLUMN_2_OF_UNIQUEINDEX)
)

No comments:

Post a Comment