当前位置:网站首页>SQL Server view
SQL Server view
2022-06-26 05:57:00 【weixin_ forty-seven million two hundred and fifty-four thousand】
Writing time :2022 year 5 month 26 Japan
SQL Server View
Views are named queries stored in the database directory , Allow clients to reference it later . A view graph can contain columns from multiple tables using joins , You can also include only a subset of the columns of a single table . This makes views useful for abstracting or hiding complex queries .
Views have the following advantages :
Security
You can restrict users' direct access to tables , And allow them to access a subset of the data through the view . for example , You can allow users to access customer names through views , Telephone , E-mail , But restrict their access to bank accounts and other sensitive information .
Simple
A relational database can have many tables with complex relationships , for example , One to one and one to many make navigation difficult . however , You can use a set of views to simplify complex queries with joins and conditions .
Uniformity
Sometimes , You need to write complex formulas or logic in each query . To make it consistent , You can hide the complex query logic and calculations in the view . After defining the view , You can reference logic from views , Instead of rewriting it in a separate query .
SQL Server Create view :
SQL Server Create a new view : Use create view sentence :
create view [or alter] schema_name.view_name [(column_list)] as select_statement;
In the above grammar :
stay create view Keyword to specify the name of the view .schema_name Is the name of the schema to which the view belongs . Specify definition as Keyword after the view select sentence (select_statement).select Statement can reference one or more tables . If the column list of the view is not explicitly specified ,SQL Server Will use from select Statement .、
If you want to redefine the view , for example , Add more columns to it or remove some from it , Can be in create view Use after keyword or alter keyword .
Create a simple view example :
The following statement is based on orders,order_time and products Table creation name is daily_sales The view of :
create view sales.daily_sales as select
year(order_date )as y,
month(order_date ) as m,
day(order_date ) as d,
p.product_id,
product_name,
quantity * i.list_price as sales
from
sales.order_id as o
inner join sales.order_items as i
on o.order_id = i.order_id
inner join production.products as p
on p.product_id = i.product_id;
establish daily_sales After view , You can use simple select Statement to query data against the underlying table on the view :
select
*
from
sales.daily_sales
order by
y, m , d, product_name;
SQL Server Rename view
Before renaming the view , It must be noted that all view dependent objects can fail . These objects include stored procedures , User defined functions , trigger , Inquire about , Other views and client applications . therefore , After renaming the view , You must ensure that all objects to which the old name of the view is applied use the new name .
Use SSMS Rename view
To rename the name of the view , Follow these steps :
First , In object Explorer (Server Server Management Studio) in , an “ database ”, Select the database name that contains the view you want to rename , And then unfold “ View ” Folder . secondly , Right click the view you want to rename , And then choose “ rename ”
Use SQL Rename view
If you want to rename the view programmatically , have access to sp_rename stored procedure :
exec sp_rename
@objname = ‘sales.product_catalog’,
@newname =’sales.product_list’;
边栏推荐
猜你喜欢
On site commissioning - final method of kb4474419 for win7 x64 installation and vs2017 flash back
[C language] deep analysis of data storage in memory
Bubble sort
Unicloud cloud development obtains applet user openid
Detailed explanation of serial port communication principle 232, 422, 485
How to associate wechat applet QR code to realize two code aggregation
String class learning
423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)
小程序如何关联微信小程序二维码,实现二码聚合
Record how to modify the control across threads
随机推荐
Selective Search for Object Recognition 论文笔记【图片目标分割】
适配器模式
Combined mode, transparent mode and secure mode
小程序如何关联微信小程序二维码,实现二码聚合
卷妹带你学jdbc---2天冲刺Day2
RIA ideas
新的征程
Kolla ansible deploy openstack Yoga version
[C language] deep analysis of data storage in memory
Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
Ribbon负载均衡服务调用
C generic speed
Consul服务注册与发现
Consul service registration and discovery
Force buckle 875 Coco, who likes bananas
Vs2022 offline installation package download and activation
Operator priority, associativity, and whether to control the evaluation order [detailed explanation]
Old love letters
The difference between abstract and interface interface
Describe an experiment of Kali ARP in LAN