View


VIEW

It is a set of data that can be built from a SELECT command, and the user cannot see the difference between a view and a table. Users with the appropriate privileges can query views like any other table. To create a view, you may use the command CREATE VIEW or CREATE OR REPLACE VIEW. To destroy a view use the command DROP VIEW. The following example illustrates how to create a view to joint information from the account and branch tables in the city_bank database.
Es un conjunto de datos que se forma de un comando SELECT y que desde el punto de vista del usuario se ve como otra tabla. Si se tienen los permisos apropiados los usuarios pueden hacer consultas desde una vista como si fuera una tabla regular. Para crear una vista se usa el comando CREATE VIEW o CREATE OR REPLACE VIEW. Para destruir una vista se usa DROP VIEW. El ejemplo siguiente ilustra cómo crear una vista para juntar información de las tablas account y branch en la base de datos de city_bank.

city_bank.sql
...
GO
CREATE VIEW vw_account_branch AS
SELECT a.account_id, b.name
FROM account a, branch b
WHERE a.branch_id = b.branch_id
GO


Tip
Views allow you to hide and combine data behind a SELECT statement. Users who execute the query will only see the result of the query, not the tables or views from which the data is extracted.
Las vistas permiten esconder y combinar datos detrás de una consulta del tipo SELECT. El usuario sólo ve los resultados de la vista, sin importarle de que tablas o vistas vienen los datos.

Views Advantages

  1. They replace very complex SELECT queries
  2. They put together information that has been split during Normalization
  3. Views are useful because they can divide a complex problem in several simple ones
  4. A view is created during the execution of a SQL script, and therefore can be only modified by the user with the right permissions
  5. As it is stored in the database, it is very easy to make changes to the view (instead of storing a SQL statement in an application)

  1. Estas reemplazan consultas SELECT complejas
  2. Juntan información que se ha dividido durante el proceso Normalización
  3. Las vistas son útiles porque permiten simplificar problemas complejos al dividir el problema en varias consultas
  4. Una vista se crea durante la ejecución de un script de SQL, y por lo tanto puede ser modificada solamente por el usuario con los permisos correctos
  5. Como se almacena en la base datos, es muy fácil hacer cambios a la vista (en lugar de almacenar un comando de SQL en un programa)

Tip
When creating software that access a database, do not include complex queries in the software instead access the data using views that are inside the database. In the case of an update, it is easier to update the view the database than to the update the software.
Cuando cree software que accesa una base de datos, no incluya consultas complejas en el software en su lugar acceda a los datos usando vistas que están dentro de la base de datos. En el caso de una actualización, es más fácil actualizar la vista en la base de datos que actualizar el software.

WITH READ ONLY

It prevents insertions, deletions, or updates of the underlying tables through the view.
Esta opción de una vista previene la inserción, el borrado o la modificación de los datos a través del uso de una vista.

WITH CHECK OPTION

Inserts and updates performed through the view must result in rows that the view query can select. As shown in the next example. Note that WITH CHECK OPTION does NOT work if you use sub-queries on the view. Prevents that updating visible rows modifies invisible rows.
Garantiza que cuando se inserten o modifiquen renglones a través de una vista el resultado sea tal que el renglón pueda seleccionarse en forma única. Como se muestra en el ejemplo siguiente. Note que WITH CHECK OPTION no opera cuando se usan sub-consultas. Evita que la actualización de los renglones visibles, modifiquen los renglones invisibles.

MyDatabase.sql
CREATE VIEW vw_myview
SELECT ...
FROM ...
WHERE ...
WITH CHECK OPTION
GO


Problem 1
city_bank > Create and test the view as shown. After the view has been created, it is possible to perform any SELECT command on the view as shown.
Cree y pruebe la vista como se muestra. Después que la vista se ha creado, es posible hacer cualquier comando SELECT en la vista como se muestra.

city_bank.sql
...
GO -- BE SURE TO EXECUTE GO BEFORE CREATING THE VIEW
CREATE VIEW vw_buss AS
SELECT name, REPLACE(type, 'B', 'Business') AS type
FROM client
WHERE type='B';
GO

SQL
SELECT name, type
FROM vw_buss;

cb_view

Tip
Even though a view behaves similarly to a regular table there are some restrictions when updating, inserting or deleting from a view. For instance, in the previous problem it is not possible to perform an UPDATE in type to modify Bussines.
Aunque una vista se comporta como una tabla regular existen algunas restricciones cuando se trata de actualizar, insertar y borrar datos desde una vista. Por ejemplo, en el problema previo no es posible hacer un UPDATE a type para modificar Bussines.

Tip
Once a view has been created using CREATE VIEW, the view can be accessed until it is destroyed with the DROP VIEW command, or when the database is destroyed.
Una vez que una vista ha sido creada con el comando CREATE VIEW, esta vista seguirá existiendo hasta que se elimine usando el comando DROP VIEW o cuando se destruya la base de datos.

Tip
To create a view, it is recommended to design and test the SELECT command separately. Once the SELECT command is working, the query can be converted to a view.
Para crear una vista se recomienda diseñar y probar el comando SELECT por separado. Una vez que el comando SELECT funciona, la consulta se puede convertir en una vista.

Problem 2
city_bank > Create a view called vw_client to show the name of the clients, their accounts, and their balances. First, create and test the SELECT statement, then create the view.
Cree una vista llamada vw_client para mostrar los nombres de los clientes, sus cuentas y sus balances. Primero, cree y pruebe el comando SELECT, entonces cree la vista.

SQL
SELECT client, account_id, balance
FROM vw_client;

cb_vw_client

Problem 3
motorola> Create the vw_emp view to produce the output shown (usando WITH CHECK OPTION) in: (a) Microsoft SQL Server, (b) Oracle.
Cree una vista llamada vw_emp para producir la salida mostrada (usando WITH CHECK OPTION) en: (a) Microsoft SQL Server, (b) Oracle.

SQL
SELECT name, department, skill
FROM vw_emp;

mot_vw_emp

Problem 4
city_bank > Create a view called vw_clientx to produce the output shown.
Cree una vista llamada vw_clientx para producir la salida mostrada.

SQL
SELECT client, account_id, type
FROM vw_clientx;

cb_vw_clientx

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home