R
R
rumkin2011-02-07 10:28:16
MySQL
rumkin, 2011-02-07 10:28:16

BIT field

I'll try to be specific and clear.
In mysql, a field of type bit(2) is used as a flag, i.e. for example, the first bit - the recipient deleted the message, the second bit the sender deleted the message, a kind of double boolean, the question is how to change each bit separately without extracting the previous one?
I read about it somewhere, but I can't find it.
Dig towards offsets?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan, 2011-02-07
@iSage

set the rightmost bit to 1: update bitset set data=data | 1
set rightmost bit to 0: update bitset set data=data &~ 1
other bits the same (2, 4, 8, 16 ...)

E
eternals, 2011-02-07
@eternals

habrahabr.ru/blogs/webdev/41458/
There are 2 links above, well, on the author's topics, plus in the comments.
In addition, dev.mysql.com/doc/refman/5.0/en/bit-functions.html
Plus, do not forget about the peculiarity of BIT in mysql, portability issues, etc.
If you're talking about "separately each bit without extracting the previous one", then the bits are still physically extracted before your new bit is written. Therefore speech only about sql-record of change of a bit. This is where binary operations come in.
PS: In passing, I'll note that the search on the fucking dev.mysql.com does not work again. Well, this is so, for lovers of shit of any kind, so that the place is known.

J
Jazzist, 2011-02-08
@Jazzist

I use the integer field through the layer. NULL is understood as FALSE, one 1 is TRUE.
As a result:
1. Cross-border. At any time, you can move from SQLite and MySQL to PostgreSQL and Oracle, as well as exotic, and even nosql databases. Anything the layer supports. If it doesn't support it, just write a new driver for the layer.
2. There are no significant changes in performance and resources. Honestly, it's not noticeable!
3. Convenient for use, convenient for programming, very convenient for standardization.
4. At any time, you can expand the number of entity states. In reserve, there are 4 billion options ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question