MySQL 什么是表前缀?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4508590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:05:06  来源:igfitidea点击:

What is a table prefix?

mysqldatabasedatabase-designprefix

提问by alioygur

What is a table prefix, and what are their advantages and disadvantages? This is in relation to MySQL.

什么是表前缀,它们的优缺点是什么?这与 MySQL 相关。

采纳答案by alioygur

This is often used to distinguish different installations of the same script from each other. For example let′s say you have two Joomla Installations with different content on your server, but only one MySQL Database.

这通常用于区分同一脚本的不同安装。例如,假设您的服务器上有两个内容不同的 Joomla 安装,但只有一个 MySQL 数据库。

Now, for obvious reasons both Joomla installations can′t share the same database tables, as that would result in both installations displaying the same contents. And that is where the prefix kicks in.

现在,出于显而易见的原因,两个 Joomla 安装不能共享相同的数据库表,因为这将导致两个安装显示相同的内容。这就是前缀的作用所在。

By using different table prefixes you can let Joomla Installation #1 know that it is supposed to use all the table with Prefix JOS_ and Joomla Installation #2 has to use all the tables with the prefix JOS2_

通过使用不同的表前缀,您可以让 Joomla 安装 #1 知道它应该使用所有带有前缀 JOS_ 的表,而 Joomla 安装 #2 必须使用所有带有前缀 JOS2_ 的表

回答by Adriaan Stander

Tables do not require prefixes.

表不需要前缀。

This is purely up to you.

这完全取决于你。

However, we prefix tables with relation to the MODULES in the application they belong to, just to group the tables more easily.

但是,我们将表与它们所属应用程序中的模块相关联,只是为了更容易地对表进行分组。

回答by Chris W

Some people advocate tbl or tbl_ (e.g. tbl_MyTable or tblMyTable) whilst others go with a suffix such as MyTable_T.

有些人提倡使用 tbl 或 tbl_(例如 tbl_MyTable 或 tblMyTable),而其他人则使用后缀,例如 MyTable_T。

Personally I avoid the prefixes/suffixes. I may substitute in a View in place of a Table if a schema is changing over time so I don't really distinguish between the two types of object.

我个人避免使用前缀/后缀。如果架构随时间变化,我可能会用视图代替表,因此我并没有真正区分这两种类型的对象。

The most important thing is that you have your naming guidelines documented within your team and you all stick to the same set of guidelines for consistency.

最重要的是,您在团队中记录了命名准则,并且为了一致性,你们都遵守相同的准则集。

回答by kojow7

In a small amount of cases, such as those where malware scripts have been created to target specific types of sites such as WordPress etc., changing the table prefixes has been useful as an extra security measure.

在少数情况下,例如创建恶意软件脚本以针对特定类型的网站(如 WordPress 等),更改表前缀作为一种额外的安全措施很有用。

For example, adding table prefixes obscures common table names making it harder for hackers to access data in your database through SQL injection or other security holes because they will first need to discover what your table names are.

例如,添加表前缀会掩盖常见的表名,使黑客更难通过 SQL 注入或其他安全漏洞访问数据库中的数据,因为他们首先需要发现您的表名。

Be sure, however, to look at table prefixes as only a very minor layer of security. It should NOT be your main security method. You should still be taking other more important security measures to prevent SQL injection and other similar threats. For example, depending on how your code is set up it may still be possible for a hacker to run a "show tables" command through SQL injection to get the names of your database tables.

但是,请务必将表前缀视为非常小的安全层。它不应该是您的主要安全方法。您仍然应该采取其他更重要的安全措施来防止 SQL 注入和其他类似威胁。例如,根据代码的设置方式,黑客仍有可能通过 SQL 注入运行“show tables”命令来获取数据库表的名称。

回答by Tommy

Strange no one mentioned that you also can use table prefixes to use normally reserved keywords as tablenames.

奇怪的是没有人提到你也可以使用表前缀来使用通常的保留关键字作为表名。

E.g. t_user or t_order are now possible.

例如 t_user 或 t_order 现在是可能的。

回答by Scott M. Stolz

If you have a complicated website and database structure, table prefixes may help prevent naming conflicts in the database.

如果您的网站和数据库结构复杂,表前缀可能有助于防止数据库中的命名冲突。

You often see table prefixes in situations where:

在以下情况下,您经常会看到表前缀:

  • Multiple scripts are being integrated together into one website, and the finished website needs to share data, but the table names would conflict without a prefix unique to each script.

  • You are adding functionality to a script you acquired, and you want to distinguish between tables native to that script and new tables you are manually creating. That way if you create a new table, it won't conflict with any future updates to the base script, since it has a different table prefix.

  • You have a hosting plan that only gives you one database and you want to use that database to service multiple scripts. (This isn't recommended for a variety of reasons, but I've seen users do this.)

  • 多个脚本被集成到一个网站中,完成的网站需要共享数据,但是如果每个脚本没有唯一的前缀,表名就会发生冲突。

  • 您正在向获得的脚本添加功能,并且想要区分该脚本的本机表和您手动创建的新表。这样,如果您创建一个新表,它就不会与基础脚本的任何未来更新发生冲突,因为它具有不同的表前缀。

  • 您有一个仅提供一个数据库的托管计划,并且您希望使用该数据库为多个脚本提供服务。(出于多种原因,不建议这样做,但我已经看到用户这样做。)

When you are writing a script from scratch, table prefixes usually aren't necessary since you control all aspects of the database structure. It's when you start integrating multiple scripts together that it becomes useful and sometimes even necessary. It allows you to create unique data views, and join tables, and such between multiple scripts without worrying about naming conflicts in the database.

当您从头开始编写脚本时,通常不需要表前缀,因为您可以控制数据库结构的所有方面。只有当您开始将多个脚本集成在一起时,它才会变得有用,有时甚至是必要的。它允许您在多个脚本之间创建唯一的数据视图和连接表等,而不必担心数据库中的命名冲突。

回答by sjngm

It may help to distinguish between tables and views depending on what your naming convention is.

根据您的命名约定,区分表和视图可能会有所帮助。

The disadvantage is that you may be limited as far as the name of a table is concerned. Oracle has a limit of 30 characters for this. If you use "Tbl_" as the prefix, you automatically lose 4 characters. That may be a problem.

缺点是就表的名称而言,您可能会受到限制。Oracle 对此有 30 个字符的限制。如果使用“Tbl_”作为前缀,则会自动丢失 4 个字符。那可能是个问题。