Difference Between Primary Key and Unique Key
Difference Between Primary Key and Unique Key
Posted By : Shailendra Chauhan, 26 Sep 2012 Updated On : 08 Oct 2012 Keywords : primary key vs unique key,primary key advantage over unique key,primary key features,unique key features In SQL Server, we have two keys which distinctively or uniquely identify a record in the database. Both the keys seems identical, but actually both are different in features and behaviours. In this article, I would like to share the key difference between primary key and unique key. For more help about keys in SQL Server refer the article Different Types of SQL Keys. Difference between Primary Key & Unique Key Primary Key Unique Key Primary Key can't accept null values. Unique key can accept only one null value. By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index. By default, Unique key is non-clustered index. We can have only one Primary key in a table. We can have more than one unique key in a table. Primary key can be made foreign key into another table. Unique key can't be made foreign key into another table.
Posted By : Shailendra Chauhan, 26 Sep 2012 Updated On : 24 Nov 2012 Keywords : primary key vs foreign key,primary key advantage over foreign key,primary key features,foreign key features In SQL Server, there are two keys - primary key and foreign key which seems identical, but actually both are different in features and behaviours. In this article, I would like to share the key difference between primary key and foreign key. For more help about keys in SQL Server refer the article Different Types of SQL Keys. Difference between Primary Key & Foreign Key Primary Key Foreign Key Primary key uniquely identify a record in the table. Foreign key is a field in the table that is primary key in another table. Primary Key can't accept null values. Foreign key can accept multiple null value. By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index. Foreign key do not automatically create an index, clustered on non-clustered. You can manually create an index on foreign key. We can have only one Primary key in a table. We can have more than one foreign key in a table.
Don't worry, to do this task, you need to just write a simple update query for Customer table like as :
UPDATE Customer SET Name=Address , Address=Name
Now After query execution you will find the the values of columns Name and Address have been interchanged, that's so cool.
SELECT * FROM CUSTOMER
Note
1. Data type of both the columns should be same and also length should be enough to hold the swapped column data other wise data will be truncated.