我想尽可能多地使用标准化。然而,我就是无法理解这个问题。
假设我有这样的财务数据:
账本# |维度1 |维度2 |维度3
4000 | 100 | 100 200 | 200 300
5113 | 100 | 100 298 | 298 300
我需要确保这 4 列的每个组合都是唯一的。我知道唯一约束,因此我在所有提到的列上添加了“唯一”。
但是...如果维度列的数量必须为 25,那么将会有 25 个维度列,而不是提到的 3 个?我将如何使用唯一约束?我知道约束只能容纳 16 列或 3072 字节,不能再多了。
我正在考虑使用某种哈希,但还没有完全弄清楚如何做到这一点。
任何人都可以通过提供想法/信息/示例来帮助我吗?
如果有一个完全不同的想法可以在没有独特约束的情况下实现这一目标,我完全赞成听到它!
最佳答案
选项1:
CREATE TABLE bigboy (
ledger_sub1 int unsigned not null COMMENT 'foreign key me...',
ledger_sub2 int unsigned not null COMMENT 'forgein key me...'
PRIMARY KEY (ledger_sub1,ledger_sub2)
)
CREATE TABLE bigboy_sub1 {
ledger int unsigned auto_increment,
dim01 int unsigned not null,
...
dim15 int unsigned not null,
PRIMARY KEY (ledger),
UNIQUE KEY uk1 (dim01..dim15)
)
CREATE TABLE bigboy_sub2 {
ledger int unsigned auto_increment,
dim16 int unsigned not null,
...
dim30 int unsigned not null,
PRIMARY KEY (ledger),
UNIQUE KEY uk1 (dim16..dim30)
)
ledger_sub1 = INSERT IGNORE INTO bigboy_sub1(dim01..dim15) and return ledger;
ledger_sub2 = INSERT IGNORE INTO bigboy_sub2(dim16..dim30) and return ledger;
its_unique = INSERT IGNORE INTO bigboy(ledger_sub1,ledger_sub2);
选项2:
CREATE TABLE bigboy {
ledger int unsigned auto_increment,
dim01 int unsigned not null,
...
dim30 int unsigned not null,
bigboy_uniq VARCHAR(301) not null,
PRIMARY KEY (ledger),
UNIQUE KEY uk1 (bigboy_uniq)
);
where bigboy_uniq = concatenation of LPAD'ed dim01..dim30
选项3:
CREATE TABLE bigboy {
ledger int unsigned auto_increment,
dim01 int unsigned not null,
...
dim30 int unsigned not null,
fingers_crossed VARCHAR(32) not null,
PRIMARY KEY (ledger),
UNIQUE KEY uk1 (fingers_crossed)
);
where fingers_crossed = md5 of LPAD'ed dim01..dim30 string
(免责声明 - 除非进行黑客攻击,否则我不会单独使用这些)
关于Mysql 唯一约束 > 16 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37264568/