none
SQL Unique Key

    Question

  • hello guys, I created one table and with 15 columns on it.

    Is it possible on one Table; column 1 -3 are unique columns 4 -6 - are not unique columns then 7 -10 are unique columns and 11 to 15 are not unique columns.

    How do I do it?

    Thank you.


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.
    IT Stuff Quick Bytes

    Thursday, August 14, 2014 3:54 AM

Answers

  • You can apply unique constraints.

    UNIQUE Constraints


    Satheesh
    My Blog | How to ask questions in technical forum


    • Marked as answer by cguan Thursday, August 14, 2014 6:39 AM
    Thursday, August 14, 2014 4:18 AM
  • you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

    major difference is that primary key doesn’t allow NULLs, but unique key allows one NULL only.

    ADD CONSTRAINT uc_Name UNIQUE (Columnname1,Columnname2)
    Please Mark This As Answer if it helps to solve the issue


    • Edited by VINO-TH Thursday, August 14, 2014 4:27 AM *
    • Marked as answer by cguan Thursday, August 14, 2014 6:39 AM
    Thursday, August 14, 2014 4:26 AM
  • Hi Cguan,

    I think this query syntax will be of some help.

    CREATE TABLE tablename (
    columnname1 datatype UNIQUE,
    columnname2 datatype UNIQUE,
    columnname3 datatype UNIQUE,
    columnname4 datatype,
    columnname5 datatype,
    columnname6 datatype,
    columnname7 datatype UNIQUE,
    columnname8 datatype UNIQUE,
    columnname9 datatype UNIQUE,
    ) 


    Regards, Ram.

    • Marked as answer by cguan Thursday, August 14, 2014 6:40 AM
    Thursday, August 14, 2014 5:48 AM

All replies

  • You can apply unique constraints.

    UNIQUE Constraints


    Satheesh
    My Blog | How to ask questions in technical forum


    • Marked as answer by cguan Thursday, August 14, 2014 6:39 AM
    Thursday, August 14, 2014 4:18 AM
  • you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

    major difference is that primary key doesn’t allow NULLs, but unique key allows one NULL only.

    ADD CONSTRAINT uc_Name UNIQUE (Columnname1,Columnname2)
    Please Mark This As Answer if it helps to solve the issue


    • Edited by VINO-TH Thursday, August 14, 2014 4:27 AM *
    • Marked as answer by cguan Thursday, August 14, 2014 6:39 AM
    Thursday, August 14, 2014 4:26 AM
  • hello guys, I created one table and with 15 columns on it.

    Is it possible on one Table; column 1 -3 are unique columns 4 -6 - are not unique columns then 7 -10 are unique columns and 11 to 15 are not unique columns.

    How do I do it?

    Thank you.


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.
    IT Stuff Quick Bytes

    Yes

    Unique constraints has no relation with columns ordinal position

    You can even have a unique constraint linking column1 ,column50 and column100


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Thursday, August 14, 2014 5:37 AM
  • Hi Cguan,

    I think this query syntax will be of some help.

    CREATE TABLE tablename (
    columnname1 datatype UNIQUE,
    columnname2 datatype UNIQUE,
    columnname3 datatype UNIQUE,
    columnname4 datatype,
    columnname5 datatype,
    columnname6 datatype,
    columnname7 datatype UNIQUE,
    columnname8 datatype UNIQUE,
    columnname9 datatype UNIQUE,
    ) 


    Regards, Ram.

    • Marked as answer by cguan Thursday, August 14, 2014 6:40 AM
    Thursday, August 14, 2014 5:48 AM
  • Hi guys, thanks for all your help.

    Yes I got it, needs to specify the Unique Key to each column name, as per Ramachandran example.

    I'm using the GUI to do it, design table on Management studio.

    Thank you. :)


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.
    IT Stuff Quick Bytes

    Thursday, August 14, 2014 6:39 AM