-2

Say I have a JOB and COMPANY table and a third JOB_COMPANY table that relates the two in a many-to-many cardinality.

I've learnt that it's an anti-pattern for the JOB_COMPANY to have a surrogate id PK. The PK should probably be a composite (job_id,company_id).

In this case, following the best practice, how would I sort the JOB_COMPANY by insertion order (and DESCENDING also)? Would this case justify a surrogate id PK?

5
  • 3
    What exactly is "insertion order"? Tables have no row order, result sets & windows have (partial) order per an ORDER BY. What is MySQL row order for “SELECT * FROM table_name;”? SQL best practice to deal with default sort order PS 1 DBMS please. 1 question please. Commented May 26, 2022 at 6:23
  • Please before considering posting read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect research in posts. SO/SE search is poor & literal & sometimes limited to titles, but read the help. Google re googling/searching, including in Q&A at Meta Stack Overflow & Meta Stack Exchange. How much research effort is expected of Stack Overflow users? How to Ask Help center Commented May 26, 2022 at 6:23
  • 1
    I removed the conflicting DBMS tags. Please add only one tag for the database product you are really using. Commented May 26, 2022 at 8:10
  • @philipxy I have the need to sort the rows of this relational table by the order they were inserted. In this case, what's the best practice? Commented Jun 10, 2022 at 18:39
  • Please clarify via edits, not comments. Use enough words, sentences & references to parts of examples to clearly & fully say what you mean. PS As the links I gave explain, there is no record of the order in which they were inserted, tables have no row order. PS Ask 1 (specific researched non-duplicate) question. PS Why is asking a question on "best practice" a bad thing? Strategy for “Which is better” questions Commented Jun 10, 2022 at 23:02

1 Answer 1

0

So its not the end of the world having a surrogate key as a primary key, as long as it isn't a clustered primary key. You want your clustered index to be a composite key in majority of cases as it will usually be how your table is accessed most of the time so you will see performance gains. Unless however you have a table where the data is only accessed by ID, in that case the only choice for the clustered Primary key is ID. What you could have is:

Primary Key NonClustered - ID
Unique Clustered Index - job_id,company_id
OR
Primary Key Clustered - job_id,company_id
Unique NonClustered Index - ID

That way under the hood 2 indexes are created to be used. So you can seek to your data with both of the below predicates

WHERE ID = 
OR
WHERE job_id = 
AND company_id = 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.