sql - sql server中 View 的用途是什么?

标签 sql

这个问题已经在这里有了答案:




已关闭10年。




Possible Duplicate:
What are views good for?



你好,
我怀疑是什么看法。我们应该在哪里使用以及 View 的用途是什么。
有没有很好的引用意见。谢谢你

最佳答案

请注意这个View (database) Wikipedia article
简而言之,View可以与SQL Server一起使用,它是一种SQL标准。
通常使用VIEW:

  • 从不同的角度展示信息数据;
  • 简化对数据库模式高层复杂性的信息的访问;
  • 防止直接访问数据库表(出于安全目的)(由于ORM工具,如今不那么流行);
  • 使用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/

    相关文章:

    sql查询添加列值

    mysql - 为什么 1582 中的两个 MySQL 日期看起来相同,但比较结果是错误的?

    sql - 在 Postgresql 查询中定义“强制性或非强制性”的标准是什么?

    sql - 使用参数调用函数或 View

    python - Django/db-api 无法创建数据库?

    sql - 使用 ms Access 中的查询从表中选择并插入到另一个表的不同类型的列

    AS 的 SQL 语法错误

    sql - 在同一个表中查询 MS SQL Server 中的数据库列表

    Php 选择 * 喜欢的地方

    sql - 从外表中存储和检索多个值