DML in Mysql
DML in Mysql
DML in Mysql
Data Manipulation Language (DML) statements are used for managing data within schema objects (e.g. table). It includes Inserting, Updating and deleting of data.
INSERT statement allows you to insert one or more rows to the table. INTO clause in the INSERT statement is optional.
It is possible to write the INSERT INTO statement in two forms. The first form doesn't specify the column names where the data will be inserted, only their values. It is used where users need to insert values to all available columns of a table.
Syntax:
mysql>INSERTINTOtable_name VALUES(value1,value2,value3,...);
Example:
mysql>INSERTINTOstudent VALUES(1,'Ajay','Hyderabad');
The second form specifies both the column names and the values to be inserted. It is used where users need to insert value to specific column(s) of a given table.
Syntax:
mysql>INSERTINTOtable_name(column1,column2, column3,...)VALUES(value1,value2,value3,...)
Example:
mysql>INSERTINTOstudent(Rollno,Name) VALUES(2,'Rahul');
In another form, instead of providing explicit data, you select it from other table by using SELECT statement. This form allows you to copy all or some part of data from other table to the inserted table.
As an example, we can create a temporary table and insert all offices which locate in US into that one by using this query:
Suppose you want to delete all employees in an office with officeNumber is 4, just execute the following query:
mysql>DELETEFROMemployeesWHEREofficeCode=4;
To delete all employees from all offices, just remove the WHERE condition as follows:
mysql>DELETEFROMemployees;