473,441 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,441 developers and data experts.

Views and Why We Love Them

pbmods
5,821 Expert 4TB
Today I'd like to talk about a new feature introduced in MySQL 5: Views.

Views are like snapshots of queries. You can save any query as a view, even complex queries with joins and subqueries, and MySQL will save the results of that query in a format that can be searched (and in some cases, modified) just like a table. But unlike tables, you never have to worry about updating the data in your view; MySQL handles all of the details for you!

For the purposes of an example, let's pretend you built a database for Wally's Widget Works. The good folks at Wally's decided that they needed to keep track of the different kinds of widget that they produce. To make it easier to sort the different widgets, they have decided to give each widget one or more 'tags' that describes some aspect of that widget's function or purpose.

So you start out with a database that looks something like this:
  • Data_Widgets (`widgetid` serial, `desc` varchar)
  • Data_Tags (`tagid` serial, `desc` varchar)
  • Map_WidgetTag (`widgetid` bigint, `tagid` bigint)

Since each Widget can have more than one tag, and each tag could be associated with more than one Widget, we have a database design professor's favorite topic of lecture: a many-to-many relationship.

Let's suppose we wanted to find all the Widgets that matched the tag, "lifesupport".

Expand|Select|Wrap|Line Numbers
  1. SELECT `Data_Widgets`.* FROM (`Data_Tags` LEFT JOIN `Map_WidgetTag` USING(`tagid`) LEFT JOIN `Data_Widgets` USING(`widgetid`)) WHERE `Data_Tags`.`desc` = 'lifesupport' ORDER BY `desc` ASC;
  2.  
Oh, and a little further down the page, we want to get all the tags for widget #15806:

Expand|Select|Wrap|Line Numbers
  1. SELECT `Data_Tags`.* FROM (`Data_Widgets` LEFT JOIN `Map_WidgetTag` USING(`widgetid`) LEFT JOIN `Data_Tags` USING(`tagid`)) WHERE `Data_Widgets`.`widgetid` = '15806' ORDER BY `desc` ASC;
  2.  
Oh, and a little further, we need all the tags that are associated with any widget whose description starts with 'Green':

(well, you get the idea; there's a lot of queries, and they're all really similar)

Now, let's say that the BOSS comes around and says, "Hey, that looks really good, but we don't need to worry about widgets 0-999. Those belong to the accounting department, and they have their own system."

So off you go to add a "AND `widgetid` > 999" to *every* *single* *one* *of* *your* *queries*.

And you also missed one, which caused a great deal of embarrassment at the company picnic the following month.

There's got to be a better way.

And there is! Enter views.

Expand|Select|Wrap|Line Numbers
  1. CREATE VIEW `View_WidgetTags` AS SELECT `Data_Widgets`.`widgetid`, `Data_Widgets`.`desc` AS `widgetdesc`, `Data_Tags`.`tagid`, `Data_Tags`.`desc` AS `tagdesc` FROM (`Data_Widgets` LEFT JOIN `Map_WidgetTag` USING(`widgetid`) LEFT JOIN `Data_Tags` USING(`tagid`)) WHERE `Data_Widgets`.`widgetid` > 999;
  2.  
Going back to our exmples, to find all "lifesupport" widgets:

Expand|Select|Wrap|Line Numbers
  1. SELECT `widgetid`, `widgetdesc` FROM `View_WidgetTags` WHERE `tagdesc` = 'lifesupport' ORDER BY `widgetdesc` ASC;
  2.  
And to find all tags for widget #15806:

Expand|Select|Wrap|Line Numbers
  1. SELECT `tagid`, `tagdesc` FROM `View_WidgetTags` WHERE `widgetid` = '15806' ORDER BY `tagdesc` ASC;
  2.  
And incidentally:

Expand|Select|Wrap|Line Numbers
  1.     SELECT * FROM `View_WidgetTags` WHERE `widgetdesc` LIKE 'Green%' ORDER BY `widgetdesc`, `tagdesc` ASC;
  2.  
Remember that the view is a hybrid query/table; you define it like a query, but you access it like a table. When you go to SELECT data from your view, MySQL has already compiled the data that matches the query you used to define the view, so your SELECT query will execute even faster!

And when the BOSS stops by your cube and says, "Hey, Murray from accounting says that they're giving us their widgets, so we need to include them in our searches again," you smile and walk over to your terminal and work your magic:

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE VIEW `View_WidgetTags` AS SELECT `Data_Widgets`.`widgetid`, `Data_Widgets`.`desc` AS `widgetdesc`, `Data_Tags`.`tagid`, `Data_Tags`.`desc` AS `tagdesc` FROM (`Data_Widgets` LEFT JOIN `Map_WidgetTag` USING(`widgetid`) LEFT JOIN `Data_Tags` USING(`tagid`));
  2.  
And instantly, without having to change a single query in your PHP code (or what-have-you), *all* your queries return the previously-forbidden widgets!

For more information on views (especially the quirks when it comes to UPDATEing or DELETEing data in a view), check out the MySQL manual page here:
http://dev.mysql.com/doc/refman/5.0/en/create-view.html
May 6 '07 #1
1 9565
bucabay
18
How is this better than creating a table based on a select query?

CREATE TABLE `View_WidgetTags` SELECT `Data_Widgets`.`widgetid`, `Data_Widgets`.`desc` AS `widgetdesc`, `Data_Tags`.`tagid`, `Data_Tags`.`desc` AS `tagdesc` FROM (`Data_Widgets` LEFT JOIN `Map_WidgetTag` USING(`widgetid`) LEFT JOIN `Data_Tags` USING(`tagid`)) WHERE `Data_Widgets`.`widgetid` > 999;

And then running queries on that table?

---

Answered my own question with a simple test. I tried updating the table which the VIEW selects from and the VIEW's result is updated also.

The CREATE VIEW FROM QUERY "caches" the QUERY not the SQL Result Set. So if the Original table in the QUERY is updated, your VIEW table is updated, and yet you can treat a VIEW just like a table, set permissions etc...

very cool.
Jun 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: asj | last post by:
awhile back, eBay decided to switch from a Microsoft/.NET/Windows architecture on the backend to a J2EE one, which might explain why their java backend will handle up to 1 BILLION page views a day!...
8
by: Mike N. | last post by:
Hello: I am new to T-SQL programing, and relativly new to SQL statements in general, although I have a good understanding of database theory. I'm a little confused as to the fundamental...
3
by: dbtoo_dbtoo | last post by:
One of the databases has 50 views and when I do a db2look I only get schema for 40 of them. If I select from the sysviews, I can see all 50 (the text column contains schema for all 10 (missing)...
5
by: Bruce | last post by:
I have several user defined functions which are referenced in triggers and views. For software upgrades, I need to be able to drop the triggers and views which reference these user defined...
224
by: VB6 User | last post by:
Hi all devies! Many (.NUT, .NOT or whatever), APIs, VB6, Views & Questions Your can not call APIs directly in .NET, only via P/Invoke. There are some things that cannot be done in...
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
15
by: rod.weir | last post by:
Fellow database developers, I would like to draw on your experience with views. I have a database that includes many views. Sometimes, views contains other views, and those views in turn may...
33
by: Peter | last post by:
People are telling me it is bad to put select * from <atable> in a view. I better should list all fields of the table inside the definition of the view. I dont know exactly why but some...
7
by: Gary | last post by:
Hello guys! Bear with me, I am a newbie. She is the Data Warehouse manager. She has about 50 users to use the Oracle database from M$ Access via ODBC connection. All those users have only...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.