公司同事对于 schema 的概念比较模糊。 PostgreSQL 、MySQL 、SQL Server 、Oracle 都有 schema (模式)的概念,并且在实际应用中体现的作用不大一样,这里重点解释 PostgreSQL 和 MySQL 的 schema 。
schema 在同一个数据库中可以创建多个,每个 schema 可以拥有相同表名的表。
假设有 a_schema 和 b_schema ,里面都可以存在 test_table 这张表,并且在同一个数据库中可以通过语句查看到不同 schema 里面的数据。
select column1
from a_schema.test_table
union all
select column1
from b_schema.test_table;
与 PostgreSQL 不大一样,在 MySQL 的 database 和 schema 是同一个概念,所以在 MySQL 中不会特别提及 schema 。
MySQL 中,可以同时操作多个数据库( 和 schema 的概念一致 )
select column1
from a_database1.test_table
union all
select column1
from a_database2.test_table;