sql:命名管道管道程序
A naming convention is a set of unwritten rules you should use if you want to increase the readability of the whole data model. Today, I’ll try to explain how you should formulate your naming convention and, maybe even more important, why should you do it and what is the overall benefit from using it.
命名约定是一组不成文的规则,如果要提高整个数据模型的可读性,应使用这些规则。 今天,我将尝试解释您应该如何制定命名约定,甚至可能更重要的是,为什么要这么做以及使用它的总体好处是什么?
We’ll use the same data model we’re using in this series.
我们将使用与本系列相同的数据模型。
This time we won’t talk about the data itself, but rather about the database objects and the way they were named.
这次我们不讨论数据本身,而是讨论数据库对象及其命名方式。
I’ve already stated it in the intro, but more generally, a naming convention is a set of rules you decide to go with before you start modeling your database. You’ll apply these rules while naming anything inside the database – tables, columns, primary and foreign keys, stored procedures, functions, views, etc. Of course, you could decide to only set naming convention rules for tables and column names. That part is completely up to you.
我已经在介绍中进行了说明,但更笼统地说,命名约定是在开始对数据库进行建模之前决定要遵循的一组规则。 在命名数据库内部的任何内容(表,列, 主 键和外键 , 存储过程 , 函数 , 视图等)时,将应用这些规则。当然,您可以决定只为表和列名设置命名约定规则。 那部分完全取决于您。
Also, using the naming convention is not the rule, but it’s desired. While most rules are pretty logical, you could go with some you’ve invited (e.g., you could call a primary key attribute “id”, or “ID”), and that is completely up to you. In this article, I’ll try to use these rules you’ll meet in most cases.
另外,使用命名约定不是规则,但它是所希望的。 尽管大多数规则是合乎逻辑的,但是您可以选择一些已邀请的规则(例如,可以将主键属性称为“ id”或“ ID”),而这完全取决于您。 在本文中,我将尝试使用大多数情况下会遇到的这些规则。
Maybe the most important reason to use it is to simplify life to yourself. Databases rarely have a small number of tables. Usually, you’ll have hundreds of tables, and if you don’t want to have a complete mess, you should follow some organizational rules. One of these rules would be to apply a naming convention. It will increase the overall model readability, and you’ll spend less time finding what you need. Also, it will be much easier to query the INFORMATION_SCHEMA database in search of specific patterns – e.g., checking if all tables have the primary key attribute named “id”; do we have a stored procedure that performs an insert for each table, etc.
也许使用它的最重要原因是简化自己的生活。 数据库很少有少量的表。 通常,您将有数百个表,如果您不希望一团糟,则应遵循一些组织规则。 这些规则之一就是应用命名约定。 它将提高整体模型的可读性,并且您将花费更少的时间查找所需的内容。 同样,查询INFORMATION_SCHEMA数据库以查找特定模式将容易得多,例如,检查所有表是否具有名为“ id”的主键属性; 我们是否有一个存储过程为每个表等执行插入操作?
If that wasn’t enough, there is also one good reason. The database shall live for a long time. Changes at the database level are usually avoided and done only when necessary. The main reason is that if you change the name of the database object that could affect many places in your code. On the other hand, the code can change during time. Maybe you’ll even change the language used to write the code. Therefore, you can expect that the database will stay, more or less, very similar to its’ initial production version. If you apply best practices from the start and continue using them when you add new objects, you’ll keep your database structure well organized and easily readable.
如果那还不够,还有一个很好的理由。 该数据库应存在很长时间。 通常避免在数据库级别进行更改,并且仅在必要时进行更改。 主要原因是,如果您更改数据库对象的名称,这可能会影响代码中的许多位置。 另一方面,代码可以随时间变化。 也许您甚至会更改用于编写代码的语言。 因此,您可以预期数据库将或多或少地保持与其初始生产版本非常相似。 如果您从一开始就应用最佳实践,并在添加新对象时继续使用它们,则可以使数据库结构井井有条,易于阅读。
One more reason to use it is that you probably won’t be the only one working with the database. If it’s readable, anybody who jumps into the project should be aware of what is where and how the data is related. That shall be especially the case if you’re using the most common naming convention rules. In case you have something specific for your database, you can list all such exceptions in one short document.
使用它的另一个原因是您可能不是唯一使用该数据库的人。 如果可读性强,那么跳入项目的任何人都应该知道数据在什么地方以及如何关联。 如果您使用的是最常见的命名约定规则,则尤其如此。 如果您有特定于数据库的内容,则可以在一个简短的文档中列出所有此类异常。
Hint: Use lower letters when naming database objects. For separating words in the database object name, use underscore
提示 :命名数据库对象时,请使用小写字母。 要在数据库对象名称中分隔单词,请使用下划线
When naming tables, you have two options – to use the singular for the table name or to use a plural. My suggestion would be to always go with names in the singular.
命名表时,有两种选择–使用单数表示表名或使用复数。 我的建议是始终使用单数形式的名称。
If you’re naming entities that represent real-world facts, you should use nouns. These are tables like employee, customer, city, and country. If possible, use a single word that exactly describes what is in the table. On the example of our 4 tables, it’s more than clear what data can be found in these tables.
如果要命名代表现实世界事实的实体,则应使用名词。 这些表包括员工 , 客户 , 城市和国家 。 如果可能,请使用一个单词来准确描述表中的内容。 在我们的4个表的示例中,不仅仅可以在这些表中找到什么数据。
Hint: Use singular for table names (user, role), and not plural (users, roles). The plural could lead to some weird table names later (instead of user_has_role, you would have users_have_roles, etc.)
提示 :表名(用户,角色)使用单数,而不是表名(用户,角色)。 复数形式可能会在以后导致一些怪异的表名(而不是user_has_role,而您将拥有users_have_roles等)。
If there is a need to use more than 1 word to describe what is in the table – do it so. In our database, one such example would be the call_outcome table. We can’t use only “call”, because we already have the table call in the database. On the other hand, using the word outcome wouldn’t clearly describe what is in the table, so using the call_outcome as the table name seems like a good choice.
如果需要使用多个单词来描述表中的内容,请执行此操作。 在我们的数据库中, call_outcome表就是这样的示例。 我们不能只使用“调用”,因为我们已经在数据库中进行了表调用 。 另一方面,使用结果一词并不能清楚地描述表中的内容,因此使用call_outcome作为表名似乎是一个不错的选择。
For relations between two tables, it’s good to use these two tables’ names and maybe add a verb between these names to describe what that action is.
对于两个表之间的关系,最好使用这两个表的名称,并可能在这些名称之间添加一个动词来描述该动作是什么。
Imagine that we have tables user and role. We want to add a many-to-many relation telling us that a user had a certain role. We could use names user_has_role, or if we want to be shorter – user_role.
想象我们有表user和role 。 我们想要添加一个多对多关系来告诉我们用户具有特定角色。 我们可以使用名称user_has_role ,也可以使用更短的名称– user_role 。
We could always make exceptions if they are logical. If we have tables product and invoice, and we want to specify which products were on which invoice, we could name that table invoice_product or invoice_contains_product. Still, using the name invoice_item is much closer to the real world. Still, that decision is completely up to you.
如果异常是合乎逻辑的,我们总是可以做出例外。 如果我们有表product和invoice ,并且想要指定哪个产品在哪个发票上,则可以将该表命名为invoice_product或invoice_contains_product 。 尽管如此,使用名称invoice_item更接近现实世界。 不过,该决定完全取决于您。
I would separate the naming convention for columns in a few categories:
我将列的命名约定分为几类:
CONSTRAINT [call_pk] PRIMARY KEY CLUSTERED
Hint: It’s not a problem if two columns, in different tables in the database, have the same name. Still, having unique names for each column is OK because we reduce the chance to later mix these two columns while writing queries
提示 :如果数据库中不同表中的两列具有相同的名称,这不是问题。 不过,每列都具有唯一的名称是可以的,因为我们减少了稍后在编写查询时将这两列混合的机会
I won’t go into details here, but rather give a brief explanation of the naming convention I use when I do name these objects.
我在这里不做详细介绍,而是简要说明我为这些对象命名时使用的命名约定。
Foreign keys. You should name them in such a manner that they uniquely and clearly describe what they are – which tables they relate. In our database, the foreign key that relates tables call and call_outcome is called call_call_outcome.
外键。 您应该以独特的方式命名它们,并清楚地描述它们是什么-它们相关的表。 在我们的数据库中,与表call和call_outcome相关的外键称为call_call_outcome 。
ALTER TABLE [dbo].[call] WITH CHECK ADD CONSTRAINT [call_call_outcome] FOREIGN KEY([call_outcome_id])
REFERENCES [dbo].[call_outcome] ([id])
Stored procedures. Stored procedures usually run a set of actions and return a dataset. My rule is to have two approaches to their naming.
存储过程。 存储过程通常运行一组动作并返回数据集。 我的规则是采用两种命名方法。
Functions. Functions usually perform simple calculations and return values. Therefore, the best way to name them would be to describe what the function does. I also love to put f_ at the start of the name. In our database, one example is – f_east_from_long.
功能。 函数通常执行简单的计算并返回值。 因此,命名它们的最佳方法是描述函数的作用。 我也喜欢将f_放在名字的开头。 在我们的数据库中,一个示例是– f_east_from_long 。
Views. Most of the rules that are applied to naming stored procedures should be applied to views. I usually don’t use views, but when I do, I place v_ at the start of their name.
意见。 适用于命名存储过程的大多数规则都应应用于视图。 我通常不使用视图,但是当我使用视图时,我将v_放在其名称的开头。
The naming convention is not a must, but (very) nice to have. Applying the rules you’ve set for the database design will help not only you but also others who will work with the database. Therefore, I would suggest that you use it and keep the database as organized as it could be.
命名约定不是必须的,但是(非常)不错。 应用您为数据库设计设置的规则不仅对您有帮助,而且对使用数据库的其他人也有帮助。 因此,我建议您使用它并保持数据库尽可能的井井有条。
Learn SQL: CREATE DATABASE & CREATE TABLE Operations |
Learn SQL: INSERT INTO TABLE |
Learn SQL: Primary Key |
Learn SQL: Foreign Key |
Learn SQL: SELECT statement |
Learn SQL: INNER JOIN vs LEFT JOIN |
Learn SQL: SQL Scripts |
Learn SQL: Types of relations |
Learn SQL: Join multiple tables |
Learn SQL: Aggregate Functions |
Learn SQL: How to Write a Complex SELECT Query? |
Learn SQL: The INFORMATION_SCHEMA Database |
Learn SQL: SQL Data Types |
Learn SQL: Set Theory |
Learn SQL: User-Defined Functions |
Learn SQL: User-Defined Stored Procedures |
Learn SQL: SQL Views |
Learn SQL: SQL Triggers |
Learn SQL: Practice SQL Queries |
Learn SQL: SQL Query examples |
Learn SQL: Create a report manually using SQL queries |
Learn SQL: SQL Server date and time functions |
Learn SQL: Create SQL Server reports using date and time functions |
Learn SQL: SQL Server Pivot Tables |
Learn SQL: SQL Server export to Excel |
Learn SQL: Intro to SQL Server loops |
Learn SQL: SQL Server Cursors |
Learn SQL: SQL Best Practices for Deleting and Updating data |
Learn SQL: Naming Conventions |
学习SQL:CREATE DATABASE&CREATE TABLE操作 |
学习SQL:插入表 |
学习SQL:主键 |
学习SQL:外键 |
学习SQL:SELECT语句 |
学习SQL:INNER JOIN与LEFT JOIN |
学习SQL:SQL脚本 |
学习SQL:关系类型 |
学习SQL:联接多个表 |
学习SQL:聚合函数 |
学习SQL:如何编写复杂的SELECT查询? |
学习SQL:INFORMATION_SCHEMA数据库 |
学习SQL:SQL数据类型 |
学习SQL:集合论 |
学习SQL:用户定义的函数 |
学习SQL:用户定义的存储过程 |
学习SQL:SQL视图 |
学习SQL:SQL触发器 |
学习SQL:练习SQL查询 |
学习SQL:SQL查询示例 |
学习SQL:使用SQL查询手动创建报告 |
学习SQL:SQL Server日期和时间函数 |
学习SQL:使用日期和时间函数创建SQL Server报表 |
学习SQL:SQL Server数据透视表 |
学习SQL:将SQL Server导出到Excel |
学习SQL:SQL Server循环简介 |
学习SQL:SQL Server游标 |
学习SQL:删除和更新数据SQL最佳实践 |
学习SQL:命名约定 |
翻译自: https://www.sqlshack.com/learn-sql-naming-conventions/
sql:命名管道管道程序