[英]Mysql unique constraint > 16 columns
I want to use normalization as much as I can. 我想尽可能地使用标准化。 However, just can't wrap my head around this one.
但是,只是不能把我的头缠在这个上面。
Suppose I have financial data as such: 假设我有这样的财务数据:
ledger# | 分类帐#| dimension1 |
维度1 | dimension2 |
维度2 | dimension3
维度3
4000 | 4000 | 100 |
100 | 200 |
200 | 300
300
5113 | 5113 | 100 |
100 | 298 |
298 | 300
300
I need to be sure that every combination of these 4 columns is unique. 我需要确保这4列的每种组合都是唯一的。 I know about the unique constraint, so I put a 'unique' on all the mentioned columns.
我知道唯一约束,因此我在所有提到的列上都添加了“唯一”。
But...what if the number of dimension columns has to be 25, so there will be 25 dimension columns instead of the 3 mentioned? 但是...如果维度列数必须为25,那么将有25个维度列,而不是提到的3列? How would I go around as to use a unique constraint?
我将如何使用唯一约束? I know a constraint can hold only hold 16 columns or 3072 bytes, no more.
我知道一个约束只能容纳16列或3072个字节,仅此而已。
I was thinking of using a hash of some sort, but haven't quite figured out how to do that. 我当时在考虑使用某种哈希,但是还没有弄清楚该怎么做。
can anyone help me by providing an idea / info / example? 谁能提供一个想法/信息/示例来帮助我?
If there is a totally different idea of accomplishing this without a unique constraint, I'm all for hearing about it! 如果有完全不同的想法来完成此任务而没有唯一的约束,那么我将全力以赴!
Option1: 选项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);
Option2: 选项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
Option3: 选项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
(disclaimer - i wouldn't use any these alone unless hacking around) (免责声明-除非骇客,否则我不会单独使用这些工具)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.