Using UPDATE Statements to Modify Data in Joined Tables : Update « Insert Delete Update « SQL / MySQL






Using UPDATE Statements to Modify Data in Joined Tables

  
Drop table DVDs;
Drop table Studios;


CREATE TABLE DVDs
(
   DVDID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   DVDName VARCHAR(60) NOT NULL,
   NumDisks TINYINT NOT NULL DEFAULT 1,
   YearRlsd YEAR NOT NULL,
   StudID VARCHAR(4) NOT NULL
) ENGINE=INNODB;


CREATE TABLE Studios
(
   StudID VARCHAR(4) NOT NULL,
   StudDescrip VARCHAR(40) NOT NULL,
   PRIMARY KEY (StudID)
)
ENGINE=INNODB;

INSERT INTO Studios VALUES ('s101', 'Universal Studios'),
                           ('s102', 'Warner Brothers'),
                           ('s103', 'Time Warner'),
                           ('s104', 'Columbia Pictures'),
                           ('s105', 'Paramount Pictures'),
                           ('s106', 'Twentieth Century Fox'),
                           ('s107', 'Merchant Ivory Production');


INSERT INTO DVDs 
(DVDName, NumDisks, YearRlsd, StudID)
VALUES 
     ('Christmas', 1, 2000, 's105'),
     ('What',      1, 2001, 's103'),
     ('Out',       1, 2000, 's101'),
     ('Falcon',    1, 2000, 's103'),
     ('Amadeus',   1, 1997, 's103'),
     ('Show',      2, 2000, 's106'),
     ('View',      1, 2000, 's107'),
     ('Mash',      2, 2001, 's106');

select * from DVDs;

select * from Studios;


UPDATE DVDs, Studios
SET DVDs.StutID='s2'
WHERE DVDs.StudID=Studios.StudID
   AND Studios.StudDescrip='Time Warner';

select * from DVDs;

select * from Studios;

           
         
    
  








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.Update with condition
8.UPDATE statement includes a WHERE clause
9.An UPDATE statement can be qualified by the use of the ORDER BY clause and the LIMIT clause.
10.Update statement using table name alias
11.Update value by calculation
12.Update statement with variable (ERROR 1093 (HY000): You can't specify target table 'PENALTIES' for update in FROM clause)
13.Update and order
14.Update with limit clause
15.UPDATE IGNORE
16.Update statement with case statement
17.Update statement with subquery (ERROR 1093 (HY000): You can't specify target table 'PENALTIES' for update in FROM clause)
18.Updates all rows with an InStock value of less than 30
19.Adding Subqueries to Your UPDATE Statements
20.Updating a joined table
21.When you update values in a joined table, you can update more than one value at a time
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