使用“stuff.whatever”是否有必要或有益? MySQL 中的命名约定?
我正在为我正在构建的社交网站开发 MySQL 数据库,到目前为止,这是一次很棒的学习经历。然而,有一件事情一直让我很困惑。
在寻找特定问题的答案时,我看到很多示例在 MySQL 查询的命名约定中使用点。例如:
SELECT c.id, c.comment, c.user_id, u.username, u.photo
FROM (comments c)
JOIN users u ON c.user_id = u.id
WHERE c.topic_id = 9
这是另一个例子:
SELECT fb.auto_id, fb.user_id, fb.bulletin, fb.subject, fb.color, fb.submit_date, fru.disp_name, fru.pic_url
FROM friend_bulletin AS fb
LEFT JOIN friend_friend AS ff ON fb.user_id = ff.userid
LEFT JOIN friend_reg_user AS fru ON fb.user_id = fru.auto_id
WHERE (
ff.friendid =1
AND ff.status =1
)
LIMIT 0 , 30
在名称中使用点有什么特别的好处吗?作为一个做过大量 CSS 工作的人,乍一看,这些点对我来说似乎是不同事物之间的某种关联,但它们在这里有什么用呢?
我想我只是想确保我不会因为不使用这个“点”命名约定而降低我的数据库结构/查询的效率。如果有人能用外行人的话向我解释它们的用途,我将非常感激。
I'm working on a MySQL database for a social network site I'm building, and so far it's been a great learning experience. However, there has been one thing in particular that's always confused me.
When seeking answers to a particular issue, I see so many examples that use dots in their naming conventions in their MySQL queries. For example:
SELECT c.id, c.comment, c.user_id, u.username, u.photo
FROM (comments c)
JOIN users u ON c.user_id = u.id
WHERE c.topic_id = 9
and here is another example:
SELECT fb.auto_id, fb.user_id, fb.bulletin, fb.subject, fb.color, fb.submit_date, fru.disp_name, fru.pic_url
FROM friend_bulletin AS fb
LEFT JOIN friend_friend AS ff ON fb.user_id = ff.userid
LEFT JOIN friend_reg_user AS fru ON fb.user_id = fru.auto_id
WHERE (
ff.friendid =1
AND ff.status =1
)
LIMIT 0 , 30
Is there a particular benefit to using the dots in the names? As someone who comes from doing a lot of CSS work, at first glance the dots appear to me as some kind of association between different things, but what are they for here?
I suppose I just want to make sure I'm not making my database structure/queries less efficient by not using this 'dot' naming convention. If someone could explain to me in laymen's terms what they are used for, I'd really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
stuff.whatever
应被视为table_name.column_name
。您显式地将每个列引用与其所属的表关联起来,恕我直言,这是要遵循的最佳实践。stuff.whatever
should be thought of astable_name.column_name
. You're explicitly associating each column reference with the table it belongs to which, IMHO, is a best practice to follow.您不应该将这些点视为“命名约定”的一部分。该功能更类似于调用对象的属性。
对于
stuff.whatever
'stuff'代表数据库表和
whatever
表示数据库中名为“whatever”的列中的数据。如果您看到引用的列没有表部分,那是因为用户期望 mysql 找出他们所指的列。
如果查询中只有一个表具有该名称的列,mysql可以做到,没问题。
但是,例如,您有一个“facebook”表和一个“twitter”表,您通过查询将它们连接起来,因为它们都有“user_id”列或其他内容,并且它们都有“avatar_image”列,而您没有如果不指定表,mysql 会引发错误,告诉你它不知道你到底在要求什么。
You shouldn't think of the dots as being part of a "naming convention." The functionality is more similar to calling an attribute on an object.
In the case of
stuff.whatever
'stuff'represents the database table and
whatever
represents the data in a column called 'whatever' in the database.If you've seen a column referenced without the table portion, it is because the user is expecting mysql to figure out which column they mean.
If there is only one table in the query with a column of that name, mysql can do it, no problem.
But, for example, you have a "facebook" table and a "twitter" table and you join them through a query because they both have a "user_id" column or something, and they BOTH have an "avatar_image" column and you didn't specify the table, mysql would raise en error telling you it didn't know exactly what you were asking for.
使用全名约定没有任何问题,但是,如果您有很长的
表
、字段
名称,则出于可读性目的使用别名会更容易。There is nothing wrong with using full name conventions, however, if you have long
table
,field
names, it is easier to use alias for readability purpose.