.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Interview Question >> Sql Server >> Post New Question Subscribe to Interview Questions

What is difference between Primary Key and Unique Key?

Posted By :volvo     Posted Date :November 21, 2013    Points :40   Category :Sql Server 
1.Primary Key can't accept null values Whereas Unique key can accept only one null value.

2.By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index whereas Unique key is a unique non-clustered index.

3.We can have only one Primary key in a table Whereas We can have more than one unique key in a table.

You can also find related Interview Question to What is difference between Primary Key and Unique Key?  below: 

What is the difference between unique and primary keys in Oracle SQL?

A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys. The columns that compose PrimaryKey are automatically defined as NOT NULL, whereas a column that compose a UNIQUE is not automatically defined to be mandatory must also specify the column is NOT NULL.

What's the difference between a primary key and a unique key?

Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only. (More...)

What's the difference between a primary key and a unique key?

Both primary key and unique key enforces uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only (More...)

Difference between primary and unique key constraints

Unique Key Constraints
The column values should retain uniqueness.
It allows null values in the column.
It will create non-clustered index by default.
Any number of unique constraints can be added to a table.

Primary Key Constraints
Primary key will create column data uniqueness in the table.
It will not allow Null values.
By default Primary key will create clustered index.
Only one Primary key can be created for a table.

Difference between Primary key and unique key?

Primary key dont allow null values where as Unique key allow null value.
Primary key create clustured index on column whereas unique key create non-clustured index. (More...)

Difference between Primary and Unique key.

Primary Key:
A column defined as primary key doesn’t allow null value.
By default, clustered index in created with the column having primary key.
Unique Key:
A column with unique key defined allows null value.
By default, it creates non-clustered index. (More...)

Difference between Unique Key and Primary Key

1. A UNIQUE constraint is similar to PRIMARY key, but you can have more than one UNIQUE constraint per table.
2. Primary Key does not allow null values whereas unique constraint allows 'single' null value.
3. A table can have only single Primary Key where as it can have multiple unique constraints ( max 16)
4. Primary Key creates clustered index by default whereas Unique Key creates non-clustered index by default

what is the difference between primary key and foreign key?

A primary key is a column which uniquely identifies the
records in a table. In a broad sense, a primary key is the
mixture of a unique key and an index: A collumn with a primary
key is indexed to deliver a faster query, and doesn't allow
duplicate values to ensure specific data.

A foreign key is a column (the child collumn) in a table which
has a corresponding relationship and a dependency on another
collumn (the parent collumn) that is usually in a different
table. Parent collumns can have multiple child collumns, but a
child collumn can only have one parent collumn.

Primary key does't allow null value
while foreign key allow null value

Database: What is the difference between primary key and foreign key?


PK will enforce entity integrity whereas FK will enforce referential integrity.

Difference Between Delete and Truncate

.Delete table is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow.

. Truncate table also deletes all the rows in a table, but it won't log the deletion of each row, instead it logs the de-allocation of the data pages of the table, which makes it faster. Of course, truncate table cannot be rolled back.

. Truncate table is functionally identical to delete statement with no "where clause" both remove all rows in the table. But truncate table is faster and uses fewer system and transaction log resources than delete.

. Truncate table removes all rows from a table, but the table structure and its columns, constraints, indexes etc., remains as it is.

. In truncate table the counter used by an identity column for new rows is reset to the seed for the column.

. If you want to retain the identity counter, use delete statement instead.

. If you want to remove table definition and its data, use the drop table statement.

. You cannot use truncate table on a table referenced by a foreign key constraint; instead, use delete statement without a where clause. Because truncate table is not logged, it cannot activate a trigger.

. Truncate table may not be used on tables participating in an indexed view. (More...)

Difference between assembly manifest & metadata?

assembly manifest - An integral part of every assembly that renders the assembly self-describing. The assembly manifest contains the assembly's metadata. The manifest establishes the assembly identity, specifies the files that make up the assembly implementation, specifies the types and resources that make up the assembly, itemizes the compile-time dependencies on other assemblies, and specifies the set of permissions required for the assembly to run properly. This information is used at run time to resolve references, enforce version binding policy, and validate the integrity of loaded assemblies. The self-describing nature of assemblies also helps makes zero-impact install and XCOPY deployment feasible.

metadata - Information that describes every element managed by the common language runtime: an assembly, loadable file, type, method, and so on. This can include information required for debugging and garbage collection, as well as security attributes, marshaling data, extended class and member definitions, version binding, and other information required by the runtime. (More...)

What is the difference between ref & out parameters?

An argument passed to a ref parameter must first be initialized. Compare this to an out parameter, whose argument does not have to be explicitly initialized before being passed to an out parameter. (More...)

What is the difference between a Struct and a Class?

The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.

When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.

It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values.

It is an error to initialize an instance field in a struct.

There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.

A struct is a value type, while a class is a reference type. (More...)

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend