how to don't have a same name in my table.

  • hi Guy i need ur help please i create a table Auto with column name and Immatriculation

    i try to make that when i write for example in my column name 'Ferrari ' that when somebody else write again ferrari it will not accept because ferrari always exist in my table. i try this.

    create table auto

    (pid int not null

    ,name varchar (50)

    ,Immatriculation varchar(50)

    ,CONSTRAINT name UNIQUE(name ))

    create table auto

    (pid int not null

    ,name varchar (50)

    ,Immatriculation varchar(50)

    ,CHECK (name <> name))

    i receive a fault that it is a conflict. Please can someone help me??

  • From what I understand...you want the Name field in your table to be Unique....right??

    You should add a UNIQUE Constraint to the field "Name".

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

  • Wow!!...I even voted....:-D

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

  • inf1154 (4/30/2012)


    hi Guy i need ur help please i create a table Auto with column name and Immatriculation

    i try to make that when i write for example in my column name 'Ferrari ' that when somebody else write again ferrari it will not accept because ferrari always exist in my table. i try this.

    create table auto

    (pid int not null

    ,name varchar (50)

    ,Immatriculation varchar(50)

    ,CONSTRAINT name UNIQUE(name ))

    That code will do exactly what you want, though you should avoid reserved words as object names.

    CREATE TABLE automobile (

    pid INT NOT NULL ,

    name VARCHAR(50) ,

    Immatriculation VARCHAR(50) ,

    CONSTRAINT uq_automobile_name UNIQUE ( name )

    )

    INSERT INTO automobile

    ( pid, name, Immatriculation )

    VALUES ( 1, 'Ferrari', 'abc' )

    INSERT INTO automobile

    ( pid, name, Immatriculation )

    VALUES ( 2, 'Ferrari', 'def' )

    The second insert fails with

    Msg 2627, Level 14, State 1, Line 4

    Violation of UNIQUE KEY constraint 'uq_automobile_name'. Cannot insert duplicate key in object 'dbo.automobile'. The duplicate key value is (Ferrari).

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • thanks Guy it walk i use

    UNIQUE NONCLUSTERED and it walk

    thx for ur help.

  • Great....good to hear that it worked 🙂

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply