DML command
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.
1) INSERT command
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_id | S_Name | age |
---|
INSERT into Student values(101,'Adam',15);
The above command will insert a record into Student table.
S_id | S_Name | age |
---|---|---|
101 | Adam | 15 |
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_id | S_Name | age |
---|---|---|
101 | Adam | 15 |
102 | Alex |
Example to Insert Default value to a column
INSERT into Student values(103,'Chris',default)
S_id | S_Name | age |
---|---|---|
101 | Adam | 15 |
102 | Alex | |
103 | chris | 14 |
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_id | S_Name | age |
---|---|---|
101 | Adam | 15 |
102 | Alex | 18 |
103 | chris | 14 |
Example to Update multiple columns
UPDATE Student set s_name='Abhi',age=17 where s_id=103;
The above command will update two columns of a record.
S_id | S_Name | age |
---|---|---|
101 | Adam | 15 |
102 | Alex | 18 |
103 | Abhi | 17 |
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
DELETE from Student;
The above command will delete all the records from Student table.
Example to Delete a particular Record from a Table
Consider the following Student table
S_id | S_Name | age |
---|---|---|
101 | Adam | 15 |
102 | Alex | 18 |
103 | Abhi | 17 |
DELETE from Student where s_id=103;
The above command will delete the record where s_id is 103 from Student table.
S_id | S_Name | age |
---|---|---|
101 | Adam | 15 |
102 | Alex | 18 |
No comments:
Post a Comment