DML is one of the building block of SQL DML queries are used to manipulate the data and they are useful to manage data in the database.
This article will describe all the DML queries one by one with examples.We will use following sample table, Tbl_Sample_Details , in this article,
S_NO Name Country
1 Faran Pakistan
2 Alex USA
Following are the commands that are part of DML queries,
- Select
- Insert
- Update
- Delete
- Merge
- Call
- Explain Plan
- Lock Table
Select Command:
The Select command is a simple and most basic command in Microsoft SQL. It is used to fetch data from a database table. Following is the syntax,
Select * from Tbl_Sample_Details
The above query will fetch complete table details,
S_NO Name Country
1 Faran Pakistan
2 Alex USA
Insert Command:
The Insert Command is used to insert data into the database table. Following is the syntax,
Insert into Tbl_Sample_Details values (3, ‘Ahmed’, ‘Pakistan’)
The above query will insert a 3rd row in the table, the resulting table will look like this,
S_NO Name Country
1 Faran Pakistan
2 Alex USA
3 Ahmed Pakistan
Update Command:
The Update Command will update the current records in the table, the following query updates Alex into David.
Update Tbl_Sample_Details set Name = ‘David’ where Name = ‘Alex’
S_NO Name Country
1 Faran Pakistan
2 David USA
3 Ahmed Pakistan
Delete Command:
The Delete command deletes a specific record from the table. Following query deletes row 2 from the table.
Delete from Tbl_Sample_Details where name = ‘David’
S_NO Name Country
1 Faran Pakistan
3 Ahmed Pakistan
Merge Command:
Using the Merge Command you can perform insert, update or delete operations in a single statement. The Merge statement was introduced in SQL Server 2008.
