Tuesday, 14 February 2017

SQL—DML Commands

Data Manipulation Language (DML) statements are used for managing data in database. DML commands are not auto-committed. It means changes made by DML command are not permanent to database, it can be rolled back.
It has three Attributes: 1. Insert 2.Update 3. Delete
  1. Insert :Insert command is used to insert data into a table. Following is its general syntax.
INSERT into table-name values(data1,data2,..)
Lets see an example,
Consider a table Student with following fields.
S_idS_Nameage
INSERT into Student values(101,'Adam',15);
The above command will insert a record into Student table.
S_idS_Nameage
101Adam15

EXAMPLE TO INSERT NULL VALUE TO A COLUMN

Both the statements below will insert NULL value into age column of the Student table.
INSERT into Student(id,name) values(102,'Alex');
OR
INSERT into Student values(102,'Alex',null);
The above command will insert only two column value other column is set to null.
S_idS_Nameage
101Adam15
102Alex

EXAMPLE TO INSERT DEFAULT VALUE TO A COLUMN

INSERT into Student values(103,'Chris',default)
S_idS_Nameage
101Adam15
102Alex
103chris14

Suppose the age column of student table has default value of 14.
Also, if you run the below query, it will insert default value into the age column, whatever the default value may be.
INSERT into Student values(103,'Chris')

2. Update Command
Update command is used to update a row of a table. Following is its general syntax,
UPDATE table-name set column-name = value where condition;
Lets see an example,
update Student set age=18 where s_id=102;
S_idS_Nameage
101Adam15
102Alex18
103chris14
3. Delete Command
Delete command is used to delete data from a table. Delete command can also be used with condition to delete a particular row. Following is its general syntax,
DELETE from table-name;

EXAMPLE TO DELETE ALL RECORDS FROM A TABLE

Consider the following Student table
S_idS_Nameage
101Adam15
102Alex18
103Abhi17
DELETE from Student where s_id=103;
The above command will delete the record where s_id is 103 from Student table.
S_idS_Nameage
101Adam15
102Alex18

No comments:

Post a Comment