Home Database Mysql Tutorial Oracle 数据库视图与基表的关系

Oracle 数据库视图与基表的关系

Jun 07, 2016 pm 04:08 PM

Oracle视图非常强大的功能之一在于其可以创 建一个带有错误的视图。比如说视图里的字段在基表里不存在,该视图仍然可以创建成功,

一:首先解释什么是视图:视图其实就是一条查询sql语句,用于显示一个或多个表或其他视图中的相关数据。视图将一个查询的结果作为一个表来使用,因此视图可以被看作是存储的查询或一个虚拟表,与真实表不同,视图不会要求分配存储空间,视图中也不会包含实际的数据。视图只是定义了一个查询,视图中的数据是从基表中获取,这些数据在视图被引用时动态的生成。由于视图基于数据库中的其他对象,因此一个视图只需要占用数据字典中保存其定义的空间,而无需额外的存储空间,并且基表的变化会导致视图相应的改变。

二:视图的创建:
 
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view_name [(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY]
其中:
OR REPLACE:若所创建的试图已经存在,Oracle自动重建该视图;
FORCE:不管基表是否存在ORACLE都会自动创建该视图;
NOFORCE:只有基表都存在ORACLE才会创建该视图:
alias:为视图产生的列定义的别名;
subquery:一条完整的SELECT语句,可以在该语句中定义别名 可以挑选某个表中你需要的属性;
WITH CHECK OPTION : 插入或修改的数据行必须满足视图定义的约束;
WITH READ ONLY : 该视图上不能进行任何DML操作。 
三:视图的修改:直接利用前边创建时的or replaece  重建即可。

四:视图上的DML 操作:

1,一般简单视图,也就是基表只有一个的视图,是可以通过修改视图来修改基表的,
Oracle是可以通过视图来修改Base table的。所谓base table就是用来构建视图的表,也就是视图的数据来源表。但是这种修改是有条件的。比如:
create view v_emp as select empno,ename,job,deptno from emp where deptno=10 with check option constraint emp_cnst;
如果有这个限制,那么通过视图v_emp 插入数据的deptno字段的值必须是10,否则就会报“ORA-01402: 视图 WITH CHECK OPTIDN 违反 where 子句”的异常。
2.针对复杂视图,也就是基表有多个表,通过内连接查询建立的视图,只能通过视图来修改key_preserved表,
什么是Key-Preserved Table呢.Oracle给出的定义是:
A table is key preserved if every key of  the table can also be a key of the result of the join.
It is not necessary that the key or keys of a table be selected for it to be key preserved.
 
It is sufficient that if the key or keys were selected, then they would also be key(s) of the result of the join.

如果某一个表的主键可以作为这个join结果(view通常是几个表的join结果)的主键,那么这个表就是key preserved table.

这个表的主键并非一定要出现在select出来的结果集中(select list里面),但是如果其出现在结果集中,那么它必须可以满足作为这个结果集的主键的要求.
 
通过下面的例子来解释:

create view liuwenhe as select e.ename, e.empno, e.job, d.deptno, d.dname from emp e,dept d where e.empno in (10,30) and e.deptno = d.deptno;  emp表的主键是empno,dept表的主键是deptno,
可以看出来emp表的主键empno的值是唯一的,并且非空,所以是可以做查询结果集的主键的,但是结果集的dempno的值就不一定唯一了。所以这个视图的key_preserved表是emp,
五;视图的作用:减少复杂性和增强安全性。

1.视图可以隐藏你的查询的复杂性,例如:

SELECT d.dname,count(*) as NUM_EMPS
 FROM emp e, dept d
 WHERE e.deptno=d.deptno
 GROUP BY dname;

不需要输入如此复杂的查询(按照今天的标准来说,那其实并不算是复杂),,我可以查询如下的一个视图:

SELECT dname,num_emps FROM my_view;      (前提是你创建了基于上面查询的视图)

你也能够将好多相当复杂的查询存储为一个视图,这样就会将查询大大的简化。通过这种方式,视图就是一种宏,它可以在幕后做许多事情,并使得这个过程让终端用户或者应用程序看起来非常简单。
2,视图也可以用于加强安全性。让我们假设一下,我只想要用户BOB看到EMP表中的ENAME 和DEPNO两个列。我可以使用如下所示:
GRANT select ON emp TO bob;

但是上面的命令会使BOB看到表的全部内容。我可以写下如下的一个视图,使BOB只看到自己能够看到的列:

CREATE VIEW bob_emp AS SELECT ename,deptno FROM emp; GRANT select ON bob_emp TO bob;

通过这两条命令,BOBO只能在视图中看到这个表中的两个列。

让我们更进一步的讨论一下安全性的概念。假设我们想要使得每个人都可以查询EMP表,但是只是他们自己的记录。我可以写出如下的视图:

CREATE VIEW my_emp AS SELECT * FROM emp WHERE ename=USER; GRANT select ON my_emp TO public;

当一个用户查询MY_EMP表示,上述视图只会返回那些ENAME列值为他们自己的用户名

3.Oracle视图非常强大的功能之一在于其可以创 建一个带有错误的视图。比如说视图里的字段在基表里不存在,该视图仍然可以创建成功,但是非法的且无法执行。当基表里加入了该字段,或者说某个字段修改成 视图里的该字段名称,那么视图马上就可以成为合法的。

本文永久更新链接地址

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

See all articles