News for hackers. Stuff that matters.
Re:SQL style guide by Dowwie on Sunday October 09, 2016 @01:27PM (#12672194)
"Where possible avoid simply using id as the primary identifier for the table."

Has anyone had trouble by using surrogate primary keys? I've found the opposite of what the author said could be more true: composite keys should be avoided instead.

Re:SQL style guide by andreareina on Sunday October 09, 2016 @01:39PM (#12672261)
I think the author was talking about (not) using `id` as the column name, e.g. `user.user_id` instead of `user.id`
Re:SQL style guide by webbore on Sunday October 09, 2016 @02:50PM (#12672713)
ON second reading I think you are correct. I agree on this point then, as it helps when joining through a denormalized table that references multiple PK ids, ex: I think: user_id = role_id is more readable than the likely alternative: u.id = r.id
Re:SQL style guide by NickNameNick on Monday October 10, 2016 @01:32AM (#12675249)
it also lets you use the join foo using(foo_id) syntax, which I quite like
Re:SQL style guide by echelon on Sunday October 09, 2016 @02:29PM (#12672563)
Generated unique tokens are better than auto increment IDs. They can be created in a distributed multi-master setup, and they don't expose how many of a given record type you have to 3rd parties when exposing primary keys or feeds via APIs.
Re:SQL style guide by dpcx on Sunday October 09, 2016 @04:49PM (#12673388)
But also because they won't join incorrectly. That was the biggest win in explaining it to other devs.
Re:SQL style guide by calpaterson on Sunday October 09, 2016 @05:03PM (#12673451)
I've had lots of problems with surrogate primary keys where people don't put enough constraints on the natural key and then duplicates (sometimes slightly different) get in.

Unless you're using innodb, primary key is not special, it's just another index. If you decide to use surrogate keys you also need to enforce the natural key too (if possible).

There are also some nice performance advantages you can get from natural keys with covering composite indices in the case where you only want the key. But in general a table can have multiple keys in all but the highest levels of normalisation.

Re:SQL style guide by Dowwie on Monday October 10, 2016 @01:17PM (#12678446)
That's a good point about manually defining constraints on a table. A composite key forces a developer to think about uniqueness whereas the surrogate key makes that thought process optional.
Re:SQL style guide by jawns on Sunday October 09, 2016 @01:37PM (#12672255)
I disagree with this style point, and I think the author implicitly acknowledges that `id` should be the standard identifier, because further down he points out that the `_id` suffix should be used for columns that make reference to that identifier.
Re:SQL style guide by jbverschoor on Sunday October 09, 2016 @02:34PM (#12672594)
No he doesn't. In an example he actually uses the following:

ON s2.mentor_id = s1.staff_num

staff_num is the primary key

To me this isan anti pattern because: - a primary should (can) never change; - composite primary keys are only useful (save space) in very large datasets

It also conflicts with his pattern: uniform suffix _id

Re:SQL style guide by forkandwait on Sunday October 09, 2016 @04:42PM (#12673357)
I am working with a 20 table db (not including lookups and session management tables) based on surrogate keys and it sucks ass for discoverability. It also subverts data checking based on SQL REFERENCES. I think natural / composite keys should used to design the db and establish relationships, surrogate keys added only if necessary, and then auto-generated, just like one waits to denormalize until there is a real need.

I suspect the love of surrogate keys is just more MySQL bullshit pretending that classic SQL design is wrong merely because it is not implemented in that particular broken database.

Re:SQL style guide by Dowwie on Monday October 10, 2016 @01:11PM (#12678408)
I'm not using mysql. Would you mind expanding what you mean by the struggles you've experienced during discoverability?
Re:SQL style guide by dahdum on Sunday October 09, 2016 @02:20PM (#12672511)
I disagree with this too, using table.id is easier to type and consistent when creating joins. It also encourages (forces) to reference the fields with tables prefix.
Re:SQL style guide by ryeguy on Monday October 10, 2016 @12:12AM (#12675000)
One advantage of using user_id on both the pk of users as well as the fk side is you can do:

    INNER JOIN comments USING (user_id)
instead of:

    INNER JOIN comments ON comments.user_id = user.user_id
Re:SQL style guide by philliphaydon on Monday October 10, 2016 @07:29AM (#12676403)
Which is fine if you are aware of the differences between the two. Since they act differently.
Re:SQL style guide by ryeguy on Monday October 10, 2016 @02:35PM (#12678965)
I don't think so, what differences are there? Could this be a database specific thing? In PG it seems equivalent.
Re:SQL style guide by philliphaydon on Monday October 10, 2016 @09:06PM (#12681198)
In MySQL you can end up with different results.
Re:SQL style guide by comboy on Sunday October 09, 2016 @02:28PM (#12672558)
Maybe what author means is that from the db design standpoint, composite keys are used much less often than they could be. Very often unique table row can be defined by some other column or a pair, on which you will probably put an index anyway.

But that's just playing devil's advocate. In general I think ids are fine because ORMs play nice with them and FKs are simple.

Re:SQL style guide by Treffynnon on Monday October 10, 2016 @12:40AM (#12675080)
Basically this is the answer - besides that bit about acquiescing to the demands of an ORM.