What is difference between unique key with 'not null' constraint and primary key?

后端 未结 4 1139
深忆病人
深忆病人 2020-12-31 11:02

I want to know difference between these two key.

When the Unique key with not null constrain in terms of how they are stored in database

and wh

相关标签:
4条回答
  • 2020-12-31 11:41

    Primary key cannot be null, there can be only one per table.

    Unique keys can contain nulls, and you can have more than one per table.

    0 讨论(0)
  • 2020-12-31 11:49

    To answer you comment, Yes! In general there is one huge difference in how unique keys and primary keys are stored in SQL Server 2008.

    A unique key by default (you can change that) will be created as a non-clustered index and a PK will be created as a clustered index by default (you can change that also).

    Non-clustered means it will be stored in a structure "attached" to the table and will consume disk space.

    Clustered means the records will be actually stored in that physical order, no consuming disk space, and that's why your table can own just one clustered index. (Just if you are wondering... no, you cannot get 2 non-clustered PK in a table, PK are unique even if they are non-clustered.)

    0 讨论(0)
  • 2020-12-31 11:53

    A primary key must be unique and non-null, so they're the same from that standpoint. However, a table can only have one primary key, while you can have multiple unique non-null keys.

    Most systems also use metadata to tag primary keys separately so that they can be identified by designers, etc.

    What are the differences between a primary key and a Unique key with not null constrain in terms of how they are stored in database

    If both are either CLUSTERED or NON CLUSTERED then the only difference is metadata in most systems to tag a index as a PK.

    what difference are there when we making Select,Insert,Update, Delete operation for these keys

    None.

    0 讨论(0)
  • 2020-12-31 11:54

    a. Both primary key and unique key enforce uniqueness of the column on which they are defined. But by default, the primary key creates a clustered index on the column, whereas unique key creates a non-clustered index by default. Another major difference is that primary key doesn’t allow NULLs, but unique key allows one NULL only.

    0 讨论(0)
提交回复
热议问题