SQL For Beginners - DML Statements

In the previous article SQL For Beginners - DDL Statements, we learned about the various DDL Commands. In this article, we will learn about the various DML Commands of SQL.

If you don't know anything about the DML and DDL statements, then you can read the very first introductory article of this series: SQL For Beginners - Introduction.
 
The various DML Commands in SQL are INSERT, UPDATE and DELETE.
 
INSERT Statement
 
The INSERT Statement is used to insert data into the table.

Syntax:
 
INSERT INTO TableName Values(value1,value2,...,valueN);
 
Also, suppose if one doesn't remembers the order in which the columns are present in the table, one can use:
INSERT INTO TableName (column1,column2,...) Values (Value1,Value2,...);
 
Example: 
  1. Insert into Students values (1,'Raj','Pune',15);  
  2.    
  3. Insert into Students(StudentID,StudentName,marks,City) Values (6,'Karan',68,'Pune');  
In the second example, you can see that we have specified the Column Names and then the respective values to be inserted. An important thing to notice over here is that the order of Column Names can be any. But, the Values should be also inserted in the same order.
 
UPDATE Statement
 
There may be situations when we need to modify the data that we have inserted in the table. This can be achieved with the help of the UPDATE Statement in SQL.

UPDATE Statement allows us to modify/change the data in our tables.
 
Syntax:

UPDATE TableName
SET column1=value1, column2=value2,...
WHERE Condition;
 
Example
  1. Update Students  
  2. Set StudentName='Raju'  
  3. Where StudentID=1;  
This will change the StudentName of the Student who has StudentID=1 to Raju. Originally, the name of Student having StudentID = 1 was Raj. But, we changed it to Raju.

DELETE Statement:

As the name suggests, the Delete Statement is used to delete records/rows from the table.

Syntax:

DELETE From TableName
Where Condition;


Example:

Suppose, we need to delete the record of the student having StudentID=6. This can be done very easily by writing the following query.
  1. DELETE From Students  
  2. WHERE StudentID=6;  
It is important to specify the WHERE condition along with the delete statement. If we do not specify the WHERE Condition, all the rows will be deleted.

DELETE From Students

This query will delete all the records from Student Table.
 
I hope that this article was useful to you.