SQL Query (constraints on a column)

  • 1. Can a column be set to not null, unique and primary?

    2. Can a column be set to not null and unique?

    3. Can a column be set to not null and primary?

    4. Can a column be set to unique and primary?

    :unsure:

  • Sounds a lot like homework to me. What have you tried, what do you think and where are having having troubles validating your assumptions?

  • Answering it myself...;-)

    Tried on SQL Server 2008 management studio:

    and it turns out answer for all Ques is Yes!!

    ...even though it is useless to set not null and/or unique when the column is declared a primary key!!

  • Hmmm...this is strange !!

    1. create table temp6 (id int unique primary key)

    shows error: Both a PRIMARY KEY and UNIQUE constraint have been defined for column 'id', table 'temp6'. Only one is allowed.

    2. create table temp6 (id int unique ,primary key(id))

    Works!! LOL

  • a unique primary key is like saying the white white rabbit. It's redundant and the code parser is likely picking this up.

    However you ARE allowed to have 2 unique constraint, identical in every way except for their name. Of course it makes no logical sense to do it but it's allowed.

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

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