Mastering MySQL 4 [Electronic resources]

Ian Gilfillan

نسخه متنی -صفحه : 229/ 24
نمايش فراداده

More about Adding Records

INSERT also allows a syntax similar to the one used by an UPDATE statement. Instead of saying the following:

mysql> INSERT INTO customer_sales_values(first_name, surname, value)
 VALUES('Charles', 'Dube', 0);

you could say this:

mysql> INSERT INTO customer_sales_values SET first_name =
 'Charles', surname='Dube', value=0;

You can also do a limited form of calculation when you add records. To demonstrate, add another field onto the customer_sales_value table:

mysql> ALTER TABLE customer_sales_values ADD value2 INT;

Now, you can insert into this table and populate value2 with twice the value:

mysql> INSERT INTO customer_sales_values(first_name, surname, value,
 value2) VALUES('Gladys', 'Malherbe', 5, value*2);

This record now contains the following:

mysql> SELECT * FROM customer_sales_values WHERE first_name='Gladys';
+------------+----------+-------+--------+
| first_name | surname  | value | value2 |
+------------+----------+-------+--------+
| Gladys     | Malherbe |     5 |     10 |
+------------+----------+-------+--------+