When you update values in a joined table, you can update more than one value at a time : Update « Insert Delete Update « SQL / MySQL






When you update values in a joined table, you can update more than one value at a time

     
mysql>
mysql>
mysql> CREATE TABLE Books
    -> (
    ->     BookID SMALLINT NOT NULL PRIMARY KEY,
    ->     BookName VARCHAR(40) NOT NULL,
    ->     InStock SMALLINT NOT NULL
    -> )
    -> ENGINE=INNODB;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> INSERT INTO Books
    -> VALUES (101, 'C: Writing on Writing', 12),
    -> (102, 'Oracle', 17),
    -> (103, 'Opera', 23),
    -> (104, 'Notebook', 32),
    -> (105, 'Pascal', 6),
    -> (106, 'One Hundred Years of Solitude', 28);
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql> CREATE TABLE Orders
    -> (
    ->     OrderID SMALLINT NOT NULL PRIMARY KEY,
    ->     BookID SMALLINT NOT NULL,
    ->     Quantity TINYINT (40) NOT NULL DEFAULT 1,
    ->     DateOrdered TIMESTAMP,
    ->     FOREIGN KEY (BookID) REFERENCES Books (BookID)
    -> )
    -> ENGINE=INNODB;
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO Orders VALUES (1001, 103, 1, '2004-10-12 12:30:00'),
    -> (1002, 101, 1, '2004-10-12 12:31:00'),
    -> (1003, 103, 2, '2004-10-12 12:34:00'),
    -> (1004, 104, 3, '2004-10-12 12:36:00'),
    -> (1005, 102, 1, '2004-10-12 12:41:00'),
    -> (1006, 103, 2, '2004-10-12 12:59:00'),
    -> (1007, 101, 1, '2004-10-12 13:01:00'),
    -> (1008, 103, 1, '2004-10-12 13:02:00'),
    -> (1009, 102, 4, '2004-10-12 13:22:00'),
    -> (1010, 101, 2, '2004-10-12 13:30:00'),
    -> (1011, 103, 1, '2004-10-12 13:32:00');
Query OK, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> UPDATE Books, Orders
    -> SET Orders.Quantity=Orders.Quantity+2,
    -> Books.InStock=Books.InStock-2
    -> WHERE Books.BookID=Orders.BookID
    -> AND Orders.OrderID = 1002;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql>
mysql>
mysql>
mysql> drop table Orders;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table books;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
  








Related examples in the same category

1.Do PLUS calculation in where clause
2.Assign value in select clause
3.Modifying Row Data
4.Update with limitation and calculation
5.Update records with calculation based on two tables
6.Update two tables with calculation
7.Using UPDATE Statements to Modify Data in Joined Tables
8.Update with condition
9.UPDATE statement includes a WHERE clause
10.An UPDATE statement can be qualified by the use of the ORDER BY clause and the LIMIT clause.
11.Update statement using table name alias
12.Update value by calculation
13.Update statement with variable (ERROR 1093 (HY000): You can't specify target table 'PENALTIES' for update in FROM clause)
14.Update and order
15.Update with limit clause
16.UPDATE IGNORE
17.Update statement with case statement
18.Update statement with subquery (ERROR 1093 (HY000): You can't specify target table 'PENALTIES' for update in FROM clause)
19.Updates all rows with an InStock value of less than 30
20.Adding Subqueries to Your UPDATE Statements
21.Updating a joined table
22.Joining Tables in an UPDATE Statement
23.A different join is defined in the UPDATE clause.
24.SET clause uses the Quantity column to specify the new value for that column