Updating data using a view that has multiple tables

You can update data via a view that has multiple tables. First, you need to set up a copy of an existing table so you don't update the data in the original table. You can do that with the following query: 

USE lahmansbaseballdb;
DROP TABLE IF EXISTS collegeplaying_copy;
CREATE TABLE collegeplaying_copy
SELECT * FROM collegeplaying;

Next, you can create a view with this new table in it with the following query: 

USE lahmansbaseballdb;
DROP VIEW IF EXISTS collegeplayingbyname;
CREATE VIEW collegeplayingbyname AS
SELECT namefirst, namelast, schoolid, yearid
FROM collegeplaying_copy c
INNER JOIN people p
ON p.playerid = c.playerid;

You can see what the view contains by executing the following query: 

USE lahmansbaseballdb;
SELECT * FROM collegeplayingbyname;

The previous query returns the results shown in the following screenshot:

namefirst and namelast come from the people table, while schoolid and yearid come from the collegeplaying_copy table. Let's update a value in the collegeplaying_copy table with the following query: 

USE lahmansbaseballdb;
UPDATE collegeplayingbyname
SET schoolid = 'testing', yearid = 2004
WHERE (namefirst = 'David' and namelast = 'Aardsma')
and (schoolid = 'rice' and yearid = 2003);

Execute the following query again: 

USE lahmansbaseballdb;
SELECT * FROM collegeplayingbyname;

Now, you will see that the collegeplaying_copy table has been updated, as shown in the following screenshot:

You can also update the people table from this view, but this shows you how you can update fields in one table via a view that has multiple tables joined. You can't update more than one table at a time in a view. Execute the following query, which tried to update both tables at the same time, via the view: 

USE lahmansbaseballdb; 
UPDATE collegeplayingbyname
SET schoolid = 'testing', yearid = 2004, namefirst = 'Peter'
WHERE (namefirst = 'David' and namelast = 'Aardsma')
and (schoolid = 'rice' and yearid = 2003);

The preceding query will give you an error:Error Code: 1393. Can not modify more than one base table through a join view 'lahmansbaseballdb.collegeplayingbyname'. You will need to do use update statements instead of one to get to the end results that you want, but to show you this, you need to create a copy of the people table first to ensure that you don't update the original table: 

USE lahmansbaseballdb;
DROP TABLE IF EXISTS people_copy;
CREATE TABLE people_copy
SELECT * FROM people;

You will also need to update the view so that it includes the people_copy table instead of the people table: 

USE lahmansbaseballdb; 
DROP VIEW IF EXISTS collegeplayingbyname;
CREATE VIEW collegeplayingbyname AS
SELECT namefirst, namelast, schoolid, yearid
FROM collegeplaying_copy c
INNER JOIN people_copy p
ON p.playerid = c.playerid;

Now, we can update the school information with the following query:

USE lahmansbaseballdb; 
UPDATE collegeplayingbyname
SET schoolid = 'testing', yearid = 2004
WHERE (namefirst = 'David' and namelast = 'Aardsma')
and (schoolid = 'rice' and yearid = 2003);

You can see that it updated the school information with the following query:

Then, update the player name with the following query: 

USE lahmansbaseballdb; 
UPDATE collegeplayingbyname
SET namefirst = 'Peter'
WHERE (namefirst = 'David' and namelast = 'Aardsma')
and (schoolid = 'rice' and yearid = 2003);

Here, you can see that it didn't just update the one row contained the new player's name. It updated them all because the people_copy table holds the player name, not the school table, so when you updated the player's name, it updated it for all the rows in the view, as shown in the following screenshot:

You need to be very careful when updating multiple tables at the same time in a view to avoid updating data you didn't intend to update. 

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.177.14