177 lines
9.7 KiB
HTML
177 lines
9.7 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Create and use views" />
|
||
|
<meta name="abstract" content="A view can be used to access data in one or more tables or views. You create a view by using a SELECT statement." />
|
||
|
<meta name="description" content="A view can be used to access data in one or more tables or views. You create a view by using a SELECT statement." />
|
||
|
<meta name="DC.subject" content="CREATE VIEW statement, statements, CREATE VIEW, examples, CREATE VIEW, view, creating, using, using UNION" />
|
||
|
<meta name="keywords" content="CREATE VIEW statement, statements, CREATE VIEW, examples, CREATE VIEW, view, creating, using, using UNION" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafysqltech.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafywcohdg.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafytexas.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafykeyu.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafyussisql.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmsthcview.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="rbafyviewnt" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Create and use views</title>
|
||
|
</head>
|
||
|
<body id="rbafyviewnt"><a name="rbafyviewnt"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Create and use views</h1>
|
||
|
<div><p>A view can be used to access data in one or more tables or views.
|
||
|
You create a view by using a SELECT statement.</p>
|
||
|
<div class="section"><p>For example, to create a view that selects only the family name
|
||
|
and the department of all the managers, specify: </p>
|
||
|
<pre> <strong>CREATE VIEW</strong> CORPDATA.EMP_MANAGERS <strong>AS</strong>
|
||
|
<strong>SELECT</strong> LASTNAME, WORKDEPT <strong>FROM</strong> CORPDATA.EMPLOYEE
|
||
|
<strong>WHERE</strong> JOB = 'MANAGER'</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>Once you have created the view, you can use it in SQL statements
|
||
|
just like a table name. You can also change the data in the base table. The
|
||
|
following SELECT statement displays the contents of EMP_MANAGERS: </p>
|
||
|
<pre> <strong>SELECT *</strong>
|
||
|
<strong>FROM</strong> CORPDATA.EMP_MANAGERS</pre>
|
||
|
<p>The results are:</p>
|
||
|
|
||
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="border" border="1" rules="all"><thead align="left"><tr><th valign="top" id="d0e75">LASTNAME</th>
|
||
|
<th valign="top" id="d0e77">WORKDEPT</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td valign="top" headers="d0e75 ">THOMPSON</td>
|
||
|
<td valign="top" headers="d0e77 ">B01</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e75 ">KWAN</td>
|
||
|
<td valign="top" headers="d0e77 ">C01</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e75 ">GEYER</td>
|
||
|
<td valign="top" headers="d0e77 ">E01</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e75 ">STERN</td>
|
||
|
<td valign="top" headers="d0e77 ">D11</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e75 ">PULASKI</td>
|
||
|
<td valign="top" headers="d0e77 ">D21</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e75 ">HENDERSON</td>
|
||
|
<td valign="top" headers="d0e77 ">E11</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e75 ">SPENSER</td>
|
||
|
<td valign="top" headers="d0e77 ">E21</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p>If the select list contains elements other than columns such as
|
||
|
expressions, functions, constants, or special registers, and the AS clause
|
||
|
was not used to name the columns, a column list must be specified for the
|
||
|
view. In the following example, the columns of the view are LASTNAME and YEARSOFSERVICE.</p>
|
||
|
<pre> <strong>CREATE VIEW</strong> CORPDATA.EMP_YEARSOFSERVICE
|
||
|
(LASTNAME, YEARSOFSERVICE) <strong>AS</strong>
|
||
|
<strong>SELECT</strong> LASTNAME, <strong>YEAR</strong> (<strong>CURRENT DATE</strong> - HIREDATE)
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE</pre>
|
||
|
<p>Since the results of querying
|
||
|
this view change as the current year changes, they are not included here.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>The previous view can also be defined by using the AS clause in
|
||
|
the select list to name the columns in the view. For example:</p>
|
||
|
<pre> <strong>CREATE VIEW</strong> CORPDATA.EMP_YEARSOFSERVICE <strong>AS</strong>
|
||
|
<strong>SELECT</strong> LASTNAME,
|
||
|
<strong>YEARS</strong> (<strong>CURRENT_DATE</strong> - HIREDATE) <strong>AS</strong> YEARSOFSERVICE
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>Using the UNION keyword, you can combine two or more subselects
|
||
|
to form a single view. For example: </p>
|
||
|
<pre><strong>CREATE VIEW</strong> D11_EMPS_PROJECTS <strong>AS</strong>
|
||
|
<strong>(SELECT</strong> EMPNO
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE
|
||
|
<strong>WHERE</strong> WORKDEPT = 'D11'
|
||
|
<strong>UNION</strong>
|
||
|
<strong>SELECT</strong> EMPNO
|
||
|
<strong>FROM</strong> CORPDATA.EMPPROJACT
|
||
|
<strong>WHERE</strong> PROJNO = 'MA2112' <strong>OR</strong>
|
||
|
PROJNO = 'MA2113' <strong>OR</strong>
|
||
|
PROJNO = 'AD3111')</pre>
|
||
|
<p>Results in a view with the following data:</p>
|
||
|
|
||
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><caption>Table 1. Create a view as UNION results</caption><thead align="left"><tr valign="top"><th valign="top" width="100%" id="d0e212">EMPNO</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td valign="top" width="100%" headers="d0e212 ">000060 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000150 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000160 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000170 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000180 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000190 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000200 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000210 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000220 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000230 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">000240 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">200170 </td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" width="100%" headers="d0e212 ">200220</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p>Views are created with the sort sequence in effect at the time
|
||
|
the CREATE VIEW statement is run. The sort sequence applies to all character,
|
||
|
or UCS-2 or UTF-16 graphic comparisons in the CREATE VIEW statement subselect.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>Views can also be created using the WITH CHECK OPTION to specify
|
||
|
the level of checking that should be done when data is inserted or updated
|
||
|
through the view.</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<ul class="ullinks">
|
||
|
<li class="ulchildlink"><strong><a href="rbafywcohdg.htm">WITH CHECK OPTION on a View</a></strong><br />
|
||
|
WITH CHECK OPTION is an optional clause on the CREATE VIEW statement that specifies the level of checking to be done when inserting or updating data through a view. If the option is specified, every row that is inserted or updated through the view must conform to the definition of that view.</li>
|
||
|
</ul>
|
||
|
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafysqltech.htm" title="Data definition language (DDL) describes the portion of SQL that allows you to create, alter, and destroy database objects. These database objects include schemas, tables, views, sequences, catalogs, indexes, and aliases.">Data definition language (DDL)</a></div>
|
||
|
</div>
|
||
|
<div class="relconcepts"><strong>Related concepts</strong><br />
|
||
|
<div><a href="rbafytexas.htm" title="Learn a variety of ways of tailoring your query to gather data using the SELECT statement. One way to do this is to use the SELECT statement in a program to retrieve a specific row (for example, the row for an employee). Furthermore, you can use clauses to gather data in a specific way.">Retrieve data using the SELECT statement</a></div>
|
||
|
<div><a href="rbafyussisql.htm" title="A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters.">Sort sequences and normalization in SQL</a></div>
|
||
|
</div>
|
||
|
<div class="relref"><strong>Related reference</strong><br />
|
||
|
<div><a href="rbafykeyu.htm" title="Using the UNION keyword, you can combine two or more subselects to form a fullselect.">Use UNION keyword to combine subselects</a></div>
|
||
|
</div>
|
||
|
<div class="relinfo"><strong>Related information</strong><br />
|
||
|
<div><a href="../db2/rbafzmsthcview.htm">CREATE VIEW</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|