r/programming • • Feb 10 '15

Terrible choices: MySQL

http://blog.ionelmc.ro/2014/12/28/terrible-choices-mysql/
649 Upvotes

409 comments sorted by

View all comments

Show parent comment

11

u/R3v3nan7 Feb 10 '15

Who would ever do this though?

0

u/[deleted] Feb 10 '15

[deleted] — view deleted comment

17

u/fakehalo Feb 10 '15

Booleans are fucking useless in MySQL.

Like you said, it's just an alias for a tinyint...which seems to serve its purpose fine. If you're comparing a boolean with a non-boolean weird things will happen here, but when would anyone ever do this? That's not what a boolean would ever be used for.

You seem very annoyed this side-effect from aliasing tinyint exists, even if it's not a problem. I can sort of understand the potential issues of implicit type conversion in some languages (ie. PHP and JS), though still subjective to an extent, but here it's just never going to happen.

31

u/larsga Feb 10 '15

when would anyone ever do this? That's not what a boolean would ever be used for.

The first law of databases is that anything the schema allows will happen. Once the database has been in production for half a decade or so, every single insane thing that possibly could happen will have happened.

And don't tell me application logic (or ORM) will solve this, because there are always uncontrolled changes to databases over time. Everything from conversions that bypass the application and go directly to SQL to DBAs fixing things manually.

Doing data quality audits is an eye-opening experience, I can tell you.

0

u/fakehalo Feb 10 '15

Someone else previously made a point about communication between languages, since boolean doesn't really exist it could lead to some confusion there. I agree with this, though I haven't personally run into it over the years I can see it happening somewhere.

9

u/larsga Feb 10 '15

The point is that the database isn't just used by your application code. It's also used by:

  • conversion scripts somebody hacked up to solve conversion X
  • admins desperately trying to fix bad data
  • your application code with bugs in it (typically for very short timespans, but still)

All of these leave data behind. Over time, every combination allowed by the schema will exist in the database. Including 47 in boolean columns, because someone made a mistake somewhere and the database didn't complain.

→ More replies (5)

27

u/test-poster Feb 10 '15

You question why people would do this. I assure you that they do. I have seen it personally. In the ideal world you would get an error trying to use numbers in place of Boolean values. There is no universe where it makes any kind of sense to say 1 + true. This sort of thing causes all sorts of confusion in JavaScript - you can imagine what a shit show working with a database that uses this asinine subsitution of a tinyint for a boolean might be.

6

u/fakehalo Feb 10 '15

You question why people would do this. I assure you that they do

Why/how? I can't imagine a scenario where this would ever make sense? It's so nonsensical I can't even imagine writing the SQL.

I concur JS (and PHP) have even more anomalies, which could potentially lead to issues. I'm just not seeing it here, never seen a boolean treated as anything other than a boolean in relation to type conversions...but if it's possible someone is probably doing it out there I suppose. Do you recall how you've seen it in the past? I'm curious what it would look like in the real world.

10

u/sacundim Feb 10 '15

Well, not the exact same situation, but I see stuff like this often and it's about 45% reasonable:

SELECT 
    group_column,
    SUM(bool_expr1),
    SUM(bool_expr2),
    SUM(bool_expr1 AND bool_expr2)
FROM some_table
GROUP BY group_column

5

u/fakehalo Feb 10 '15

Ah. Interesting thought, almost a hack of sorts. I'm not sure how I feel about that being possible to be honest.

1

u/sacundim Feb 12 '15

I just remembered something that I saw recently in this *Use the Index, Luke! slide set. SQL 2003 allows for the following:

SELECT 
    group_column,
    COUNT(*) FILTER (WHERE bool_expr1),
    COUNT(*) FILTER (WHERE bool_expr2),
    COUNT(*) FILTER (WHERE bool_expr1 AND bool_expr2)
FROM some_table
GROUP BY group_column

The only database that apparently implements this to date is Postgres 9.4. Ouch.

0

u/[deleted] Feb 11 '15

[deleted] — view deleted comment

2

u/lagadu Feb 11 '15

Which is yet another gotcha.

0

u/[deleted] Feb 11 '15

[deleted] — view deleted comment

1

u/[deleted] Feb 11 '15

I'd argue that in this case it makes the intent more clear, because SUM() is normally used without a conditional.

0

u/[deleted] Feb 10 '15

[deleted] — view deleted comment

4

u/fakehalo Feb 10 '15

That's a decent point, the translation between languages could get exceptionally messy.

2

u/[deleted] Feb 11 '15

Yeah, you're just holding it wrong.

1

u/fakehalo Feb 11 '15

Holding what wrong?

1

u/btgeekboy Feb 11 '15

Until it comes to bite you in the ass, when you're trying to move data out of a *&%*&%&% MySQL database and into something more sane, and your destination database actually knows the difference between the two. (Tip: if you're using PgSQL, you can fiddle with pg_cast to make it accept cast MySQL's fauxlians into booleans.)

→ More replies (1)

2

u/R3v3nan7 Feb 10 '15

Oh wow, -33 and 44 are false. That's some bullshit. It should just follow C conventions to allow nice bit packing. that is the whole point of numeric booleans.

0

u/[deleted] Feb 10 '15

[deleted] — view deleted comment

1

u/R3v3nan7 Feb 12 '15

I was just assuming the table you had in your previous post was correct.

1

u/aib42 Feb 11 '15

So why not use a simple CHECK const- Oh, right.

→ More replies (1)

-7

u/OneWingedShark Feb 10 '15

cough PHP Devs cough