这个问题已经在这里有了答案:
已关闭10年。
Possible Duplicate:
What are views good for?
你好,
我怀疑是什么看法。我们应该在哪里使用以及 View 的用途是什么。
有没有很好的引用意见。谢谢你
最佳答案
请注意这个View (database) Wikipedia article。
简而言之,View
可以与SQL Server一起使用,它是一种SQL标准。
通常使用VIEW
:
VIEW
可以使某些“现实”变得更简单。 这是一个数据库架构将层次结构数据存储在多个数据库表中的示例。
CREATE TABLE Vehicules (
VId int IDENTITY(1, 1) PRIMARY KEY
, VDescription nvarchar(20) NOT NULL
)
CREATE TABLE MotoredVehicules (
MvId int IDENTITY(1, 1) PRIMARY KEY
, MvVId int NOT NULL REFERENCES Vehicules (VId)
, MvMake nvarchar(20) NOT NULL
)
CREATE TABLE MotorHP (
MhpId int IDENTITY(1, 1) PRIMARY KEY
, MhpMvId int NOT NULL REFERENCES MotoredVehicules (MvId)
, MhpHP decimal(6, 2) NOT NULL
)
CREATE TABLE VehiculesWheels (
VwId int IDENTITY(1, 1) PRIMARY KEY
, VwVId int NOT NULL REFERENCES Vehicules (VId)
, VwNumberOfWheels int NOT NULL
)
insert into Vehicules (VDescription) values (N'Bicycle')
GO
insert into Vehicules (VDescription) values (N'Motorcycle')
GO
insert into Vehicules (VDescription) values (N'Automobile')
GO
insert into Vehicules (VDescription) values (N'Yacht')
GO
-- Inserting the information about the vehicules that have a motor.
insert into MotoredVehicules (MvVId, MvMake) (
select v.VId
, N'Harley Davidson'
from Vehicules as v
where v.VDescription LIKE N'Motorcycle'
)
GO
insert into MotoredVehicules (MvVId, MvMake) (
select v.VId
, N'Sea-Ray'
from Vehicules as v
where v.VDescription LIKE N'Yacht'
)
GO
insert into MotoredVehicules (MvVId, MvMake) (
select v.VId
, N'Mercedes'
from Vehicules as v
where v.VDescription LIKE N'Automobile'
)
GO
-- Inserting motor HP for the motorized vehicules.
insert into MotorHP (MhpMvId, MhpHP) (
select mv.MvId
, 350
from MotoredVehicules as mv
where mv.MvMake LIKE N'Sea-Ray'
)
GO
insert into MotorHP (MhMvId, MhpHP) (
select mv.MvId
, 280
from MotoredVehicules as mv
where mv.MvMake LIKE N'Mercedes'
)
GO
insert into MotorHP (MhpMvId, MhpHP) (
select mv.MvId
, 930
from MotoredVehicules as mv
where mv.MvMake LIKE N'Harley Davidson'
)
GO
-- Inserting the number of wheels for wheeled vehicules.
insert into VehiculesWheels (VwVId, VwNumberOfWheels) (
select v.VId
, 2
from Vehicules as v
where v.VDescription IN (N'Bicycle', N'Motorcycle')
)
GO
insert into VehiculesWheels (VwVId, VwNumberOfWheels) (
select v.VId
, 4
from Vehicules as v
where v.VDescription LIKE N'Automobile'
)
GO
这种关系模型本身并不是很全面。我们本可以使用一个唯一的表来插入所有它们的所有规范,然后将没有数据的字段设为NULL。但是,由于某些层次结构的原因,我们为不同类型的车辆创建了规范表。因此,当您想特别获得有关车辆的信息时,这不是很实用,因为您必须不时加入表,然后才需要检索信息。在这里,具有如下所示的View可能会变得实用:CREATE VIEW WheeledMotoredVehiculesView AS
select v.VId
, mv.MvMake
, hp.MhpHP
, vw.NumberOfWheels
, v.VDescription
from Vehicules as v
left join MotoredVehicules as mv on mv.MvVId = v.VId
left join MotorHP as hp on hp.MhpMvId on mv.MvId
left join VehiculesWheels as vw on vw.VwVId = v.VId
GO
CREATE VIEW MotoredVehiculesView AS
select v.VId
, mv.MvMake
, hp.MhpHP
, v.VDescription
from Vehicules as v
left join MotoredVehicules as mv on mv.MvId = v.Id
left join MotorHP as hp on hp.MhpMvId = mv.MvId
GO
CREATE VIEW WheeledVehicules AS
select v.VId
, vw.NumberOfWheels
, v.VDescription
from Vehicules as v
left join VehiculesWheels vw on vw.VwVId = v.VId
GO
然后,不必在每次需要访问有关给定车辆的一些信息数据时都必须写出上述两个 View 中每个 View 中包含的选择,而只需针对适当的 View 本身进行查询:select *
from MotoredVehiculesView
或您需要的任何信息。免责声明:该代码未经测试,仅出于示例目的直接编写。它可能无法按原样工作。
我希望这可以帮助您以某种方式更好地理解观点。
关于sql - sql server中 View 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2944597/