Thursday, June 18, 2015

Delete and Truncate in SQL

In SQL we have to use DELETE command in order to delete the data.
To completely remove the data from a table we can use TRUNCATE command.

Find below the differences between TRUNCATE and DELETE command
DELETE
TRUNCATE
It's DML command
It's DDL command
It's a row lock. Whenever you fire DELETE command it will lock the rows which satisfies deletion criteria
It will lock table and page but not the row
It removes only the data which satisfies deletion criteria
It removes all the data
WHERE clause is allowed to specify filters
WHERE clause is not allowed
It activates a trigger because the operation are logged individually
It cannot activate a trigger because the operation does not log individual row deletions
Compared to TRUNCATE it is slower because it logs everything
Faster than DELTE as it don't log anything
Rollback is possible
Rollback is not possible.
But inside the transaction we can rollback both DELETE and TRUNCATE
It will not reset IDENTITY property
It will reset IDENTITY property

Syntax:
DELETE:
DELETE
FROM
Employee
WHERE FirstName = 'Anil'

It will remove all the rows where FirstName is Anil
TRUNCATE:
TRUNCATE TABLE Employee
It will remove all the data present in Employee table


Labels: , , , , ,

Tuesday, June 16, 2015

SQL Server - DDL, DML, DQL, DCL and TCL

Today we are going to learn different categories of languages available in SQL:

We have four categories of languages in SQL:

DDL – Data Definition Language:
It is used to modify the structure of the table
      CREATE, ALTER, DROP, TRUNCATE

There are too many posts where we are seeing whether TRUNCATE is DDL/DML command.
Its DDL command because
a. It internally performs DROP and CREATE of the table
b. If you want to TRUNCATE the table you require ALTER permissions not only Write permissions
c. As it is DDL command, it will reset IDENTITY property of the table

DML – Data Manipulation Language:
It is used to manipulate the data available in table
      INSERT, UPDATE, DELETE

DQL – Data Query Language:
It is used to fetch the data from table
     SELECT

DCL – Data Control Language:
It is used to create roles, permissions, and referential integrity and also to control access to database by securing it
     GRANT, REVOKE

TCL – Transaction Control Language:
It is used to control/manage different transactions performed on a table
     COMMIT, ROLLBACK

Labels: , , , , , , , , ,

Querying Database

Querying Database:


Today we will see how can we write different queries to get the information from Employee table.



Find below the list of columns available in Employee table:

ColumnName
EmpID
EmpName
FirstName
LastName
Salary
Location



a. Select all the employees whose FirstName starts with a and salary greater than $10000

SELECT

      EmpName

FROM

      Employee

WHERE

      Salary > 10000

      AND FirstName LIKE 'a%'



Explanation:

To identify all the employees whose name starts with 'a' we have to use regular expression.

Here, 'a%' tells the query that FirstName should start with 'a' and remaining letters can be anything

LIKE Usage:

In SQL we have to use LIKE keyword whenver we are using regular expressions, if we use '=' operator then SQL will search for those employees whose name is 'a%' but not for those whose name starts with 'a'.

AND Usage:

We need only those employees who satisfy both the conditions that is the reason we are using AND operator over here, if our requirement is to get those employees whose salary great than 10000 or FirstName starts with 'a' then we can use OR in place of AND



b. Get the employee names whose FirstName is not available

SELECT

      EmpName

FROM

      Employee

WHERE

      FirstName IS NULL

Explanation:

Here IS NULL tells that FirstName details are not provided by employee



Try writing queries for below Questions:

a. Write a query to get the list of employees whose name is 'Arun' and they should live in Hyderabad

b. Get the list of EMPIDs whose salary is less than 20000 and LastName is not mentioned or FirstName is not mentioned


Labels: , , , , , ,