相当于 MySQL 中的 SQL Server 模式吗?

Sae*_*ati 4 mysql schema sql-server migration

我有一个正在迁移到 MySQL 的模块化系统。

在 SQL Server 中,存在将表和数据库对象分组在一起的架构概念。例如,所有与用户相关的表都可以放入auth模式中,所有博客表可以放入博客模式中。这意味着我们可以在对象资源管理器中看到这些表名称:

  • 授权用户
  • 授权角色
  • auth.UserRoles
  • 授权令牌
  • 博客.帖子
  • 博客.评论
  • 博客作者

这是一种将事物放在一起并对它们进行排序的巧妙方法,这对于理解数据库和模块化结构非常有帮助,并有助于降低维护成本。

我知道MySQL不支持这个功能。

我还知道我们可以为表名称添加前缀来模仿这种行为。但我想知道这是否是最好的方法?

是否有一种我不知道的已知方法可以实现这一目标?我对 MySQL 还很陌生。

Bil*_*win 5

在 MySQL 中,SCHEMA 和 DATABASE 是同义词。他们指的是同一件事。您可以使用 CREATE SCHEMA 或 CREATE DATABASE 等语句。您可以显示架构或显示数据库。

这是表的结构命名空间,因此您可以拥有两个具有相同名称的表,只要它们位于不同的架构中即可。

mysql> create table test.mytable( ... );
mysql> create table test2.mytable( ... );
Run Code Online (Sandbox Code Playgroud)

在内部,MySQL 服务器将模式简单地实现为服务器数据目录下的子目录。MySQL仅支持这种方式的一级子目录。

有人说你不能进行架构间事务。这不是真的。您可以在单个事务中引用不同模式的表。您甚至可以在同一查询中引用不同模式的表:

SELECT * FROM test.mytable JOIN test2.mytable ON ...
Run Code Online (Sandbox Code Playgroud)

You can make a table with a foreign key referencing another table in a different schema. It's just a kind of namespace for the tables, so you need to reference tables with the schema qualifier if the table isn't in your current default database. Read about qualified table names here: https://dev.mysql.com/doc/refman/8.0/en/identifier-qualifiers.html

MySQL also supports schema-level privileges in GRANT statements:

GRANT SELECT ON test2.* TO 'user'@'%';
Run Code Online (Sandbox Code Playgroud)

Other SQL implementations like Microsoft's have further levels of namespacing. I've seen terms "schema," "database," and "catalog" used differently in some brands of SQL database. These things are not implemented in a consistent manner, so you need to learn how the product you are using treats them.

MySQL 没有任何“目录”的实现,但您仍然可以在 INFORMATION_SCHEMA 表中看到对目录的引用,因为这些表的列在 SQL 规范中是标准化的。在 MySQL 中,信息模式表中的目录始终为 NULL。