V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
samin
V2EX  ›  数据库

PostgreSQL 和 MySQL 中 schema 的区别

  •  
  •   samin · May 7, 2021 · 1523 views
    This topic created in 1819 days ago, the information mentioned may be changed or developed.

    背景

    公司同事对于 schema 的概念比较模糊。 PostgreSQL 、MySQL 、SQL Server 、Oracle 都有 schema (模式)的概念,并且在实际应用中体现的作用不大一样,这里重点解释 PostgreSQL 和 MySQL 的 schema 。

    区别

    PostgreSQL

    schema 在同一个数据库中可以创建多个,每个 schema 可以拥有相同表名的表。

    假设有 a_schemab_schema ,里面都可以存在 test_table 这张表,并且在同一个数据库中可以通过语句查看到不同 schema 里面的数据。

    select column1
    from a_schema.test_table
    union all
    select column1
    from b_schema.test_table;
    

    MySQL

    与 PostgreSQL 不大一样,在 MySQL 的 database 和 schema 是同一个概念,所以在 MySQL 中不会特别提及 schema 。

    MySQL 中,可以同时操作多个数据库( 和 schema 的概念一致 )

    select column1
    from a_database1.test_table
    union all
    select column1
    from a_database2.test_table;
    

    示意图

    PostgreSQL 和 MySQL 中 schema 的区别

    PostgreSQL 中 schema 的优势

    1. 对于需要统一编码规则、排序规则等配置,可以做到配置一次,达到多个 schema 复用配置的效果,换成 MySQL 的 schema 来实现,则需要配置多次。
    2. 可以减少 root 权限的使用,通过数据库的权限管理,让用户自己在特定数据库管理 schema 。
    3. 在一个数据库中可以通过不同的 schema 创建相同的表。
    1 replies    2021-05-10 14:55:24 +08:00
    zhangysh1995
        1
    zhangysh1995  
       May 10, 2021
    形式化定义里面,scheme 是一个表的结构,schema 是一堆表结构的集合(或者数据库)。
    具体可以参考 Database: The Complete Book 。
    从这个定义来讲,PostgreSQL 的概念脱离了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5097 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 09:09 · PVG 17:09 · LAX 02:09 · JFK 05:09
    ♥ Do have faith in what you're doing.