Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
In this part of the MySQL tutorial, we will insert, update and delete data from MySQL tables. We
will use the INSERT, DELETE and UPDATE statements. These statements are part of the SQL Data
Manipulation Language, DML.
Inserting data
We create a new table Books, with Id, Title and Author columns.
mysql> INSERT INTO Books(Id, Title, Author) VALUES(1, 'War and Peace',
This is the classic INSERT SQL statement. We have specified all column names after the table
name and all values after the VALUES keyword. We add our first row into the table.
+----+---------------+-------------+
| Id | Title | Author |
+----+---------------+-------------+
| 1 | War and Peace | Leo Tolstoy |
+----+---------------+-------------+
We add a new title into the Books table. We have omitted the Id column. The Id column has
AUTO_INCREMENT attribute. This means that MySQL will increase the Id column automatically.
The value by which the AUTO_INCREMENT column is increased is controlled by
auto_increment_increment system variable. By default it is 1.
+----+------------------------+--------------------+
| Id | Title | Author |
+----+------------------------+--------------------+
+----+------------------------+--------------------+
In this SQL statement, we did not specify any column names after the table name. In such a case,
we have to supply all values.
The REPLACE statement is a MySQL extension to the SQL standard. It inserts a new row or
replaces the old row if it collides with an existing row. In our table, there is a row with Id=3. So
our previous statement replaces it with a new row. There is a message that two rows were
affected. One row was deleted and one was inserted.
+----+---------------+-------------+
| Id | Title | Author |
+----+---------------+-------------+
+----+---------------+-------------+
We can use the INSERT and SELECT statements together in one statement.
Here we insert all data into the Books2 that we select from the Books table.
+----+------------------------+--------------------+
| Id | Title | Author |
+----+------------------------+--------------------+
+----+------------------------+--------------------+
mysql> INSERT INTO Books(Title, Author) VALUES ('The Insulted and Humiliated',
We can insert more than one row into the table with the INSERT statement. Here we show how.
We can insert data from a file on the filesystem. First, we dump data from the Books table in a
books.csv file.
mysql> SELECT * INTO OUTFILE '/tmp/books.csv'
We write data from the Books table into the books.csv file. The data will be in a CSV format.
$ cat /tmp/books.csv
We use the LOAD DATA INFILE syntax to populate the Books table from the books.csv file.
+----+-----------------------------+--------------------+
| Id | Title | Author |
+----+-----------------------------+--------------------+
+----+-----------------------------+--------------------+
All OK.
We can load data from XML files as well. First, we write data from the Books table into an XML
file.
The mysql monitor has an --xml option, which enables us to dump data in XML format. The -e
option executes a statement and quits the monitor.
$ cat books.xml
<?xml version="1.0"?>
" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="Id">1</field>
</row>
<row>
<field name="Id">2</field>
</row>
<row>
<field name="Id">3</field>
</row>
<row>
<field name="Id">4</field>
</row>
<row>
<field name="Id">5</field>
</row>
</resultset>
We load data from the XML file. Note that LOAD XML statement is available for MySQL 5.5 and
newer.
Deleting data
In MySQL, we can delete data using the DELETE and TRUNCATE statements. The TRUNCATE
statement is a MySQL extension to the SQL specification. First, we are going to delete one row
from a table. We will use the Books2 table that we have created previously.
+----+------------------------+--------------------+
| Id | Title | Author |
+----+------------------------+--------------------+
+----+------------------------+--------------------+
Updating data
The UPDATE statement is used to change the value of columns in selected rows of a table.
+----+-----------------------------+--------------------+
| Id | Title | Author |
+----+-----------------------------+--------------------+
+----+-----------------------------+--------------------+
Say we wanted to change 'Leo Tolstoy' to 'Lev Nikolayevich Tolstoy' table. The following
statement shows, how to accomplish this.
The SQL statement sets the author column to 'Lev Nikolayevich Tolstoy' for the column with Id=1.
+----+---------------+--------------------------+
| Id | Title | Author |
+----+---------------+--------------------------+
+----+---------------+--------------------------+
In this part of the MySQL tutorial, we have inserted, deleted, and updated data in databas