Database Reference
In-Depth Information
INSERT INTO employees VALUES
(NULL, "Kimball", "Spencer", NULL, NULL);
Full documentation of the
INSERT
command is found at
https://mariadb.com/kb/
en/insert/
.
Updating data
When data in a table needs to be updated, we use the
UPDATE
command. The basic
syntax is as follows:
UPDATE
<table_name>
SET
column_name1
={
expression
|DEFAULT}
[,
column_name2
={
expression
|DEFAULT}] …
[WHERE
<where_conditions>
];
Unlike the
INSERT
command, when we are updating data we specify the data we
want to insert right after each column name. Another difference is inclusion of a
WHERE
section. The
WHERE
section is very important because we use it to specify
the exact column or columns of data in the table we want to change. If we omit the
WHERE
section the
UPDATE
statements will update every instance of that column. For
example, we could accidentally change every employee's phone number to the same
number when all we wanted was to update Gordon's.
One thing we should do in our example
employees
table is add birthdays and
preferred names for some of our employees:
UPDATE employees SET
pref_name = "John", birthday = "1958-11-01"
WHERE surname = "Taylor" AND givenname = "John";
UPDATE employees SET
pref_name = "Will", birthday = "1957-03-01"
WHERE surname="Woodruff";
UPDATE employees SET
birthday = "1964-04-03"
WHERE surname = "Snow";
For each of the preceding commands, MariaDB should output the following two
lines (the amount of time, 0.03 seconds in the example, may be different):
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0