Friday, 31 July 2015

Difference between Primary Key and Unique Key

PRIMARY KEY
UNIQUE KEY
It Accepts Only Unique Records. Duplicates won't be allowed.
Just Like Primary Key, Unique Key also accepts only Unique Records, It won't allow the duplicates.
It Doesn't allow Null Values.
It Allows Only One Null Value.
A Table Can have Only One Primary Key.
A Table can have more than One Unique Column.
By Default whenever we go with Primary Key it adds a Clustered Index.
By Default whenever we go with Unique Key it adds a Non-Clustered Index.

Wednesday, 29 July 2015

Difference between Delete & Truncate

DELETE
TRUNCATE
Delete is a DML Command.
Truncate is a DDL Command.
We can use Where Clause with Delete to delete Specific Records.
We Cannot use Where Clause with Truncate.
It Maintains the Logs, So It Slower than Truncate
It Won't maintain the Logs, So it is Performance Wise Faster
Identity Of Column Keep Delete Retain the Identity
Identity Column is Reset to its seed Value if the table Contains any Identity Column
To Use Delete we need Delete Permission on the Table
To Use Truncate on Table we need at least Alter Permission on the Table
Delete Can be used with Indexed Views
Truncate Cannot be used with Indexed Views
Delete Activates a Trigger because the Operation are logged Individually. When we Execute Delete Command, Delete Trigger will be initiated if present. Delete is a DML Command and it deletes the data Row-by-Row basis from a Table. Which means delete is modifying the data by deleting it from the table. Triggers are fired when a DML statement executed on a table, So Trigger will be fired in case of Delete Command Execution.
Truncate Table Can't activate a Trigger because the Operation does not log individual row deletions. When we run truncate command to remove all rows of table then it actually doesn't removes any row, rather it de-allocates the data pages. In case of Truncate triggers will not be fired because no modifications takes place, we have just de-allocated the data pages not deleted any row from table.

Monday, 13 July 2015

Difference between Stored Procedures and User Defined Functions

STORED PROCEDURES
USER DEFINED FUNCTIONS
Stored Procedures May or May not Return the Values.
Functions Must Return a Value.
Stored Procedures will have Input and Output Parameters.
Function Can have only Input Parameters.
Stored Procedures Cannot be called from Functions.
Functions can be called from Stored Procedures.
Stored Procedures Allow Select as well as DML (Insert/Update/Delete) Statements in it.
Function allows only Select Statements.
Stored Procedures cannot be called from Select/ Where/Having etc statements.
Functions can be called from Select Statement.
Exceptions can be handled by try-catch block in stored procedure.
Try-Catch block cannot be used in a Function.
We can use Transactions with in the Stored Procedures.
Function won't allow Transactions.
Procedures cannot be used in join clause.
Functions that returns the table can be treated as another row set. And this can be used in JOINS with another table.


Difference Between Temporary Tables and Table Variables

TEMPORARY TABLES
TABLE VARIABLES
There are Two types Temporary tables, One is Local Temporary Tables whose name starts with Single (#) Sign and the Second one is Global Temporary Tables whose name starts with Two (##) sign.
Local Temporary Tables are visible only to the current connection for the user and they are deleted when user disconnects  or we can drop them explicitly.
Global Temporary Table are Visible to any user after they created, and they are deleted when all users referencing to the table disconnects from server or we can drop them explicitly.
These are Stored in Temp DB.
Table Variable is like a temporary tables with slight changes. Table Variables are deleted automatically after the execution. We cannot drop table variables explicitly.
These are Stored in Temp DB.
Syntax:-

CREATE TABLE #TEST (ID INT ,NAME VARCHAR(20) ,GENDER CHAR (10) ,SALARY MONEY)

Syntax:-

DECLARE @TEST TABLE (ID INT ,NAME VARCHAR(20) ,GENDER CHAR (10),SALARY MONEY )

We can Modify the structure of the table after the creation of Table.
Temporary Tables Supports DDL Statements.
We cannot Modify the structure of the table variables after the creation.
Table Variables doesn't  Support DDL Statements.
Temporary Tables Supports adding constraints after Creation of the Table. Which means Temporary Tables supports adding Indexes explicitly after Creation of Temporary Tables.
Table Variables doesn't allow the explicit addition of constraints/indexes after it's declaration. Which means we can specify indexes or constraints only while declaring the table variable itself.
Temporary Tables cannot be used in user defined Functions.
Table Variables can be used in User Defined Functions.