在sql server中使用view有什么用?

Sur*_*har 9 sql

可能重复:
哪些观点适合?

嗨,我怀疑是什么观点.我们应该在哪里使用以及视图的用途是什么.对于观点有什么好的参考.谢谢

Wil*_*ler 13

请关注这个View(数据库)维基百科文章.

简而言之,一个View可以与SQL Server一起使用,它是一个SQL标准.

VIEW通常使用A :

  1. 以不同的观点呈现信息数据;
  2. 简化对数据库模式高级复杂性信息的访问;
  3. 防止直接访问数据库表(出于安全目的)(由于ORM工具,这些天不太流行);
  4. 一些"现实"只是更简单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
Run Code Online (Sandbox Code Playgroud)

这种关系模型本身并不是很全面.我们可以使用一个唯一的表来插入它们的所有规范,并让NULL没有数据的字段.但是,由于某些层次结构的原因,我们已经创建了有关不同类型的Vehicules的规范表.因此,当您想要特别获得有关vehicule的信息时,它不是很实用,因为您必须每次都加入表,然后您必须检索信息.在这里,有一个像这样的视图可能变得切实可行:

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
Run Code Online (Sandbox Code Playgroud)

然后,每次您需要访问有关给定车辆的某些信息数据时,您只需查询相应的视图本身,而不必在每次上面创建的视图中都写入包含的选择:

select *
    from MotoredVehiculesView
Run Code Online (Sandbox Code Playgroud)

或者您需要的任何信息.

免责声明:此代码尚未经过测试,仅作为示例目的直接编写.它可能无法正常工作.

我希望这能帮助你更好地理解观点.

  • 这个例子很古老,但是仍然很相关,并且很好地解释了一个实际的用例,即表示可以存储在视图中的复杂查询。但是请记住,视图不存储任何内容,它是在查询视图时从真实数据库中动态填充的。 (2认同)