Which datatype should I use for a flag (in MySQL)? I just want to check if a user is online at the moment.
I thought about a TINYINT or SET which both could be 0 or 1.
Which datatype for flag?
TINYINT
is perfect for what you are suggesting:
`status` tinyint(1) unsigned NOT NULL DEFAULT '0'
Note that an unsigned tinyint(1) can still store values from 0 – 255. –
Wideangle
Dear Tinyint is better for flag because it's very small int and it also save your bytes more than other. You can also choose char(1) but i will recommend you choose tinyint
Why would you recommend tinyint over char(1)? –
Pursuant
@JonathanClark datatypes. Tinyint is an int, char is a string. If you are pulling values from the DB and do boolean (=== vs ==) conditionals a '1' will not behave the same as a 1. –
Pikeperch
@MikePurcell Makes sense, although all the ORMs / DB Driver's I've ever used return strings irregardless of the database data type. From my research, Char(1) and TinyInt(1) are both 1 byte, so overall I agree, may as well use TinyInt... –
Pursuant
@JonathanClark ya one of the BIG downsides of php is loose data typing, on which ORMs are built. With our current ORM we added a getter/setter upstream so anything that is_numeric gets automatically cast to an int (except for floats). Ensures consistent data type from DB to presentation and back. –
Pikeperch
TINYINT is the same as a boolean type in MySQL, where 0 is false and 1 is true. You can enter in true or false and it will get evaluated to 1 or 0.
© 2022 - 2024 — McMap. All rights reserved.