Re: A trigger that prevents a duplicate value based on a foreign k

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Tom Moreau (tom_at_dont.spam.me.cips.ca)
Date: 10/06/04


Date: Wed, 6 Oct 2004 14:48:33 -0400

If there are dupes, it won't work WITH NOCHECK either, since it creates a
unique index to support the constraint.

-- 
Tom
---------------------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com
"Toby Herring" <therring*@*teletrack.com> wrote in message
news:OrspVR9qEHA.2904@TK2MSFTNGP15.phx.gbl...
See if it works WITH NOCHECK instead of WITH CHECK.
I would almost bet you already have duplicates in the table, which would
prevent you from creating the constraint WITH CHECK.
-- 
Toby Herring
MCDBA, MCSD, MCP+SB
Need a Second Life?
http://secondlife.com/ss/?u=03e0e5b303c234bf08e80ee40119a65e
"beacon-dartmouth" <beacondartmouth@discussions.microsoft.com> wrote in
message news:6671C471-3CCF-4DD0-946A-1E70DD39BC48@microsoft.com...
> Thank you for the suggestion, Toby.
>
> I tried it as:
>
> ALTER TABLE A_T_FIELD WITH CHECK ADD CONSTRAINT CK_A_T_FIELD UNIQUE
> (Logical_Name, Table_ID)
>
> but the server returns "Error validating constraint 'CK_A_T_FIELD'"
> without
> any more clues. Clicking Help online doesn't offer any other possible
> reasons
> for the error..
>
> I'm the dbo for the database so I should have permissions to commit
> changes.
> I don't think column modifications would cause data loss, or result in an
> index greater than 900 bytes . I'm clue less.
>
>
> "Toby Herring" wrote:
>
>> A unique constraint/index can span multiple columns:
>>
>> ALTER TABLE Field
>> WITH CHECK ADD CONSTRAINT UN_TableField UNIQUE (Table_ID, Logical_Name)
>>
>> -- 
>> Toby Herring
>> MCDBA, MCSD, MCP+SB
>> Need a Second Life?
>> http://secondlife.com/ss/?u=03e0e5b303c234bf08e80ee40119a65e
>>
>>
>> "beacon-dartmouth" <beacon-dartmouth@discussions.microsoft.com> wrote in
>> message news:A4BFF8BB-5795-414D-9F5B-4D33E82D9F39@microsoft.com...
>> >I need to come up with a trigger that would prevent an insert if the
>> >value
>> >of
>> > the column "Logical_Name" is a duplicate of another with the same value
>> > in
>> > the "Table_ID" column.
>> >
>> > In other words, I have two tables (Table and Field) where there may be
>> > one
>> > or more instances of a field for one table. I need to prevent two
>> > fields
>> > with
>> > the same name in the same table.
>> >
>> > I tried using constraints but the "uniqueness" would to all the values
>> > and
>> > not just to those with a foreign key ID.
>> >
>> > Any examples???
>>
>>
>>


Relevant Pages

  • Re: Linking SQL Server tables
    ... The linked table will be read only if there is no unique index or constraint ... >>> I'm trying to link tables from SQL Server to my Access 2003 database. ... >>> in the Microsoft SQL Server database. ...
    (microsoft.public.access.externaldata)
  • Re: Nonclustered UNIQUE INDEX
    ... Table window for a table in SQL Server 2000, ... UNIQUE setting: Constraint, and Index. ... >CREATE UNIQUE INDEX splunge ON blat ... >INSERT blat SELECT 1 ...
    (microsoft.public.sqlserver.server)
  • Re: A trigger that prevents a duplicate value based on a foreign k
    ... Toby Herring ... > any more clues. ... >> A unique constraint/index can span multiple columns: ... >> WITH CHECK ADD CONSTRAINT UN_TableField UNIQUE ...
    (microsoft.public.sqlserver.programming)
  • Re: Unique Constraint on Multiple columns
    ... > Can you create a unique constraint on multiple columns, ... A UNIQUE constraint is always implemented as a unique index. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: Table Design Advice
    ... I will be creating db in SQL server though. ... MaterialCost (Currency) ... CONSTRAINT pk_products PRIMARY KEY, ...
    (microsoft.public.sqlserver.setup)