简体   繁体   English

mysql bitmask ID字段

[英]mysql bitmask ID field

How can I create table that automatically increments a second column (BitField) on the ID autoincrement? 如何创建自动递增ID自动递增的第二列(BitField)的表?

ID: 0 ->  BitField: 2^0 = 1
ID: 1 ->  BitField: 2^1 = 2
ID: 2 ->  BitField: 2^2 = 4
ID: 3 ->  BitField: 2^3 = 8
ID: 4 ->  BitField: 2^4 = 16
ID: 5 ->  BitField: 2^5 = 32
ID: 6 ->  BitField: 2^6 = 64
//etc

This for a user table field that is a bitmask (think along the lines of "permissions"). 这是作为位掩码的用户表字段(请考虑“权限”的意思)。 So a side question would be: Do you think having a one-to-many for the options and many-to-many table for granting to each user is a better implementation (not even sure how to then format all that info into one row so usin a bitmask)? 因此,有一个附带的问题:您认为为每个用户授予一个选项的一对多选项和一个多对多的表是一个更好的实现(甚至不确定如何将所有信息格式化为一行)所以用位掩码)?

CREATE TABLE [dbo].[Test](
[Id] [int] NOT NULL,
[BitField] AS (power((2),[id]))
                        ) ON [PRIMARY]

GO

-- Testing
INSERT INTO Test(Id) VALUES(2)
INSERT INTO Test(Id) VALUES(6)
INSERT INTO Test(Id) VALUES(8)

SELECT * FROM Test


Id  BitField
2   4
6   64
8   256

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM