视图让你的机房查询语句更简单
使用视图背景 刚开始做机房收费系统的时候师傅说过要用上视图、存储过程、触发器这些东西。当时没有太多的感觉,不知道什么时候该用,用到哪里。就在几天前因为在做关于两个表的查询语句时,就自己建立一个视图,然后查询出来结果,然后就成功了。我做的有点
使用视图背景
刚开始做机房收费系统的时候师傅说过要用上视图、存储过程、触发器这些东西。当时没有太多的感觉,不知道什么时候该用,用到哪里。就在几天前因为在做关于两个表的查询语句时,就自己建立一个视图,然后查询出来结果,然后就成功了。我做的有点太简单了,在想是不是自己做错了。上网查了一下,看了一些博客。事实证明,它就是来简化数据库的操作,同时提高了速度。
视图简介:
视图是一张虚拟表,是存储在数据库中的查询的SQL语句,与此相关的数据并没有再存一份于数据库中。通过视图看到的数据仍然存放在基表中。视图看上去非常像数据库中的物理表,对它的操作同任何其它的表一样。当通过视图修改数据时,实际上是在改变基表中的数据;相反地,基表数据的改变也会自动反映在由基表产生的视图中。由于逻辑上的原因,有些视图可以修改对应的基表,而有些则不能(仅仅能查询)。
视图的用途:
—简单性:简化用户对数据的理解,也可以简化他们的操作。那些被经常使用的查询可以被定义为视图,从而使得用户不必为以后的操作每次指定全部的条件。
— 安全性:通过视图用户只能查询和修改他们所能见到的数据。数据库中其他的数据既看不到也取不到。
— 效率性:简化了数据操作,从很大程度上提高了查询速度。
— 独立性:视图可以使应用程序和数据库表在一定程度上独立。
如何创建视图?
有两种方法:
1、——Sql Server Management Studio 中直接新建视图:
(1)选择数据库中的视图选择新建视图:
(2)选择需要创建视图的表:
(3)选择需要显示信息的字段:
(4)直接生成的查询语句:喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgICAgICAgIDxpbWcgc3JjPQ=="http://www.2cto.com/uploadfile/Collfiles/20140623/2014062309113733.jpg" alt="\">
(5)最后保存视图名称以后即可使用。
2、——使用 Transant—SQl语句创建视图
(1)创建视图的SQL语句的语句形式:
其中:
view_name:为新创建的视图指定的名字,视图名称必须符合标识符规则。
column_name:在视图中包含的列名,也可以在Select语句中指定列名。
table_name:视图基表的名字。
select_statement:选择哪些列进入视图的Select语句。
WHERE search_condition:基表数据进入视图所应满足的条件。
WITH CHECK OPTION:迫使通过视图执行的所有数据修改语句必须符合视图定义中设置的条件。
WITH ENCRYPTION:对视图的定义进行加密。
(2)使用SQL语句创建视图的步骤:
—编写用户创建视图的Select语句。
—对select语句进行测试。
—检查测试结果是否正确,是否和预期的一样。
—创建视图。
(3)在创建视图的时候,应该考虑一下因素:
—在CREATE VIEW语句中,不能包含order by 、compute或者compute by子句,也不能出现into关键字。
—创建视图所参考基表的列数最多为1024列。
—创建视图不能参考临时表。
—在一个批处理语句中,CREATE VIEW语句不能和其他Tracsact-SQL语句混合使用。
—尽量避免使用外连接创建视图。
视图的使用:
视图在程序中使用时就像普通表一样使用,例如:
<span style="font-size:18px;"> </span><span style="font-size:14px;"> '组合查询 管理学生信息 查询视图Stu_View 连接卡和学生两个表 Public Function CombinQueryStudent(ByVal student As Entity.ConbinQueryInfo) As DataTable Implements IDAL.IStudent.CombinQueryStudent Dim strSql As String '定义一个查询语句字符串 Dim tableName As String '定义一个数据库表名字符串 Dim table As DataTable = New DataTable Dim helper As Helper.SqlHelper = New Helper.SqlHelper Try tableName = "Student_View" '需要操作的视图 strSql = CombinQueryDAL.CreateSqlString(student, tableName) '生成需要查询的sql语句 table = helper.ExecuteNonQuery(strSql, CommandType.Text) Return table Catch ex As Exception Throw ex End Try End Function</span>
视图的优点:
在程序设计的时候必须先了解视图的优缺点,这样可以扬长避短视图除了上面所述的一些优点之外,视图也存在一些缺点,主要如下。
● 性能:SQL Server必须把视图的查询转化成对基本表的查询,如果这个视图是由一个复杂的多表查询所定义,那么,即使是视图的一个简单查询,SQL Server也把它变成一个复杂的结合体,需要花费一定的时间。
● 修改限制:当用户试图修改视图的某些行时,SQL Server必须把它转化为对基本表的某些行的修改。对于简单视图来说,这是很方便的,但是,对于比较复杂的视图,可能是不可修改的。
所以,在定义数据库对象时,不能不加选择地来定义视图,应该权衡视图的优点和缺点,合理地定义视图。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Apple rolled out the iOS 17.4 update on Tuesday, bringing a slew of new features and fixes to iPhones. The update includes new emojis, and EU users will also be able to download them from other app stores. In addition, the update also strengthens the control of iPhone security and introduces more "Stolen Device Protection" setting options to provide users with more choices and protection. "iOS17.3 introduces the "Stolen Device Protection" function for the first time, adding extra security to users' sensitive information. When the user is away from home and other familiar places, this function requires the user to enter biometric information for the first time, and after one hour You must enter information again to access and change certain data, such as changing your Apple ID password or turning off stolen device protection.
