当前位置:网站首页>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’;

原网站

版权声明
本文为[weixin_ forty-seven million two hundred and fifty-four thousand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260545588071.html