search
HomeDatabaseMysql TutorialOracle笔记:创建视图

视图这个名词太抽象了,乍一看完全不明白是什么意思,更不知道有何用处,其实了解清楚它的含义后你就会恍然大悟。表和视图的区别

视图这个名词太抽象了,乍一看完全不明白是什么意思,更不知道有何用处,其实了解清楚它的含义后你就会恍然大悟。表和视图的区别,表是占用硬盘空间物理表,而视图可以理解为一个虚表,并不存储在硬盘上,不占用硬盘空间,实际上就是一个查询语句,方便查询。对视图里面的数据操作(增、删、改) 其实就是对真实的表增、删、改,它们始终保持一致性。
    为什么还需要视图 ?
    视图可以理解成一个封装过的表,,例如不让用户清楚知道表的某些字段信息,比较安全。
延续之前的表tableA,创建一个select * from tableA的视图a_view:
SQL>CREATE VIEW a_view AS SELECT * FROM tableA;
SQL>SELECT * FROM a_view;

clip_image002


clip_image004


视图创建成功,查询a_view的结果,和select * from tableA结果一样。
如果创建视图时提示以下错误,说明该用户没有创建视图的权限,需要用后面的方法进行授权。
ERROR at line 1:
ORA-01031: insufficient privileges
----------------------------------
SQL> conn /as sysdba
Connected.
SQL> grant connect,dba to user;
Grant succeeded.
SQL> conn user/password
Connected.
往视图插入一条记录
SQL> INSERT INTO a_view(id,name,age) VALUES(106,'susan',23); 

Oracle笔记:创建视图

 

记录被成功添加进视图中,同时也加进了表里。
修改视图:与创建视图不同,创建视图使用create创建,而修改视图是用create or replace。如果要设置视图只读权限,可以在SQL语句末尾添加with read only。设为只读之后如果再往视图里添加记录的话,Oracle即会报错,如下图所示。
SQL> CREATE OR REPLACE VIEW a_view AS SELECT * FROM tableA WITH READ ONLY;

clip_image010


如果要添加有额外条件的查询视图,可以在末尾加上条件。例如查询年龄大于等于24岁的人,可用下图所示的方式:
SQL> CREATE OR REPLACE VIEW a_view AS SELECT * FROM tableA where age>=24;

clip_image012


多表查询视图,如果表tableA和tableB的字段ID有相同记录的话,把相应的tableA的name字段记录和tableB的class、grade字段记录罗列出来,创建到视图b_view里,如下图所示:
SQL> CREATE VIEW b_view as select a.name,b.class,b.grade from tableA a,tableB b where a.id=b.id;

clip_image014


最后有关查看视图中所包含的字段,方法和查看表的一样
SQL>DESC b_view;

clip_image016

linux

Statement
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
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns

See all articles

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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft