You are on page 1of 4

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.

Define Primary key and Unique key


1. 2. 3. 4. 5. 6. 7. CREATE TABLE Employee ( EmpID int PRIMARY KEY, --define primary key Name varchar (50) NOT NULL, MobileNo int UNIQUE, --define unique key Salary int NULL )

Difference between Primary Key and Foreign Key

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.

Define Primary key and Foreign key


1. --Create Parent Table 2. CREATE TABLE Department 3. ( 4. DeptID int PRIMARY KEY, --define primary key 5. Name varchar (50) NOT NULL, 6. Address varchar(100) NULL 7. ) 8. GO 9. --Create Child Table 10. CREATE TABLE Employee 11. ( 12. EmpID int PRIMARY KEY, --define primary key 13. Name varchar (50) NOT NULL, 14. Salary int NULL, 15. --define foreign key 16. DeptID int FOREIGN KEY REFERENCES Department(DeptID) 17. )

Swap the values of two columns in SQL Server


Posted By : Shailendra Chauhan, 07 Nov 2012 Updated On : 12 Nov 2012 Version Support : SQL Server 2005,2008,2012 Keywords : interchange the values of two columns in the database table,swap the two columns values in Sql server,interchnage tables columns values in sql Do you have fun with SQL Server?. Let's start fun with SQL Server. Suppose you want to swap the values of two columns of a table in SQL Server, how could you achieve this tricky task?. Actually, it is simple and so funny. Suppose you have a Customer table in the database with the following data and you want to interchange the values of columns Name and Address then how do you do?
SELECT * FROM CUSTOMER

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.

You might also like