[go: up one dir, main page]

0% found this document useful (0 votes)
214 views3 pages

DML in Mysql

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

DML Statement 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.

Inserting Data into Tables:


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:

mysql> INSERT INTO temp_table SELECT * FROM offices WHERE country=US;

Updating Data in Tables:


SQL UPDATE statement is used to update existing data in database tables. It can be used to change values of single row, group of rows or even all rows in a table. Followed by the UPDATE keyword is the name of a table you want to change the data. If an UPDATE command violates any integrity constraint, MySQL does not perform the update and it will issue an error message. The SET clause determines the column(s) name and the changed value(s). The changed values could be a constant value, expression or even a subquery. WHERE clause determines which rows of the tables will be updated. It is an optional part of SQL UPDATE statement. If WHERE clause is ignored, all rows in the tables will be updated. In employees table, if you want to update the email of Mary with employeeid 1 with the new email as mpatterson@talentsprint.com, you can execute the following query. mysql>UPDATEemployeesSETemail=mpatterson@talentsprint.com WHEREemployeeid=1; The following syntax will update the email of all employees with the new email as mpatterson@talentsprint.com.
mysql>UPDATEemployeesSETemail=mpatterson@talentsprint.com;

Deleting Records from Tables:


The DELETE FROM statement is used to delete records from a database table. The WHERE clause in DELETE statement specifies condition to limit which rows you want to remove. If a record meets WHERE condition, it will be removed from the database table permanently. If the WHERE clause is ignored in the MySQL DELETE statement, all rows of the table are deleted.

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;

You might also like