简体   繁体   English

为mysql表中的每个主键和外键使用唯一的列名,以避免产生歧义

[英]Using of unique column name for each primary and foreign keys in mysql table to avoid ambiguous condition

Which is better method among below:- 以下哪个是更好的方法:

  1. Using unique column name for each column, primary and foreign keys among all table. 每列使用唯一的列名称,所有表之间使用主键和外键。

Example 1: 范例1:

CREATE TABLE projects (
project_id PRIMARY KEY,
project_name VARCHAR(30)
client_id INT,
fk_project_user_id INT, ( FOREIGN KEY )
projects_created_by INT, ( FOREIGN KEY )
);


CREATE TABLE clients (
client_id  PRIMARY KEY,
client_name varchar(20),
fk_client_user_id INT,( FOREIGN KEY )
client_created_by INT, ( FOREIGN KEY )
)
  1. Don't care about the uniqueness of each column name for each primary and foreign keys among all tables. 不必担心所有表中每个主键和外键的每个列名的唯一性。

Example 2: 范例2:

CREATE TABLE projects (
id PRIMARY KEY,
project_name VARCHAR(30)
client_id INT, ( FOREIGN KEY )
fk_user_id INT, ( FOREIGN KEY )
created_by INT ( FOREIGN KEY )
);


CREATE TABLE clients (
id PRIMARY KEY,  (Same as above table)
client_id INT, 
client_name varchar(20),
fk_user_id INT,  ( FOREIGN KEY ) (Same as above table)
fk_client_id int, ( FOREIGN KEY )
created_by INT    ( FOREIGN KEY ) (Same as above table)
);

When we plan database for big ERP having multiple tables and relationships among all? 当我们为大型ERP规划数据库时,该数据库具有多个表并相互关联? I have used a different name for each key to avoiding ambiguous error when we join two tables. 我为每个键使用了不同的名称,以避免在我们连接两个表时出现模棱两可的错误。

What is the best solution? 最好的解决方案是什么?

Naming conventions are up to you. 命名约定由您决定。 Which naming convention you decide to use is not important. 您决定使用哪种命名约定并不重要。 What's important is that you follow your own convention consistently, and document it so that other developers know how to understand your code and your schema. 重要的是要始终遵循自己的约定,并对其进行记录,以便其他开发人员知道如何理解您的代码和架构。

Some people choose to give every table a primary key named id . 有些人选择给每个表一个名为id的主键。 If every one of their tables must have a column named id , then they can write reusable code for certain queries against any table. 如果他们的每个表都必须有一个名为id的列,那么他们可以针对任何表编写某些查询的可重用代码。

However, this convention does not account for compound primary keys. 但是,此约定不考虑复合主键。 Also if your query does a join, the query result set may have multiple columns named id unless you define column aliases. 同样,如果您的查询进行联接,则查询结果集可能包含名为id多个列,除非您定义列别名。

When I design a database, I name my primary key in a descriptive way. 设计数据库时,我以描述性方式命名主键。 projects.project_id for example. 例如projects.project_id This avoids the problem of duplicate column names in a result set of a join. 这避免了联接结果集中列名重复的问题。 It also makes it more clear what the column means when you see it in a query or a result set. 它还使您在查询或结果集中看到该列时更清楚该列的含义。

I like to name the foreign key the same as the primary key column it references, when I can do it without resulting in a conflict. 在不引起冲突的情况下,我喜欢将外键的名称与其引用的主键列相同。

But consider this example, where there are multiple foreign keys in the same table that reference Users.user_id . 但是考虑这个示例,在同一表中有多个引用Users.user_id外键。

CREATE TABLE Bugs (
  bug_id INT PRIMARY KEY,
  description TEXT NOT NULL,
  reported_date DATETIME NOT NULL,
  user_reported_by INT NOT NULL,
  user_assigned_to INT,
  user_verified_by INT,
  FOREIGN KEY (user_reported_by) REFERENCES Users(user_id),
  FOREIGN KEY (user_assigned_to) REFERENCES Users(user_id),
  FOREIGN KEY (user_verified_by) REFERENCES Users(user_id)
);

You can't assume you can use a common column name, because it's normal to need multiple foreign keys referencing the same table, as in the example above. 您不能假定可以使用公用列名,因为如上例所示,通常需要多个外键引用同一个表。 Therefore you must allow the FK column name to be different from the PK it references. 因此,您必须允许FK列名称与其引用的PK不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM