0

Which is better method among below:-

  1. Using unique column name for each column, primary and foreign keys among all table.

Example 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:

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? I have used a different name for each key to avoiding ambiguous error when we join two tables.

What is the best solution?

halfer
  • 19,824
  • 17
  • 99
  • 186
NASEEM FASAL
  • 429
  • 4
  • 13
  • What column lists are FKs to what column lists in what tables? Please just give actual code. What ambiguous condition? What "ambiguous error when we join two tables"? We give tables aliases in queries to diambiguate columns. PS A FK says that subrow values for a column list must appear elsewhere as subrow vales. When that is so, just declare it, unless other declarations imply it. – philipxy Mar 14 '18 at 06:08
  • 1
    When `JOINing`, qualify _all_ column names with the table name (or alias). – Rick James Mar 19 '18 at 15:53
  • (Obviously--) This is a faq. Before considering posting please always google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names; read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. – philipxy Jul 07 '19 at 22:03
  • Possible duplicate of [Naming of ID columns in database tables](https://stackoverflow.com/questions/208580/naming-of-id-columns-in-database-tables) – philipxy Jul 07 '19 at 22:04

1 Answers1

1

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. If every one of their tables must have a column named id, then they can write reusable code for certain queries against any table.

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.

When I design a database, I name my primary key in a descriptive way. projects.project_id for example. 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.

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.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828