使用sql,我需要将Lineno和orderline组合成一个组合键,到目前为止,我有
CREATE TABLE ORDERLINE (
ORDERNO INTEGER NOT NULL,
LINENO VARCHAR(4) NOT NULL,
NUMBERORDERED NUMERIC DEFAULT '1',
QUOTEDPRICE NUMERIC DEFAULT '0.00',
CONSTRAINT ORDERLINE_ORDERNO_PK PRIMARY KEY (ORDERNO),
CONSTRAINT ORDERLINE_ORDERNO_CC CHECK (ORDERNO BETWEEN 10000 AND 99999),
CONSTRAINT ORDERLINE_NUMBERORDERED CHECK (NUMBERORDERED BETWEEN 1 AND 100)
);
最佳答案
CREATE TABLE ORDERLINE (
ORDERNO INTEGER NOT NULL,
LINENO VARCHAR(4) NOT NULL,
NUMBERORDERED NUMERIC DEFAULT '1',
QUOTEDPRICE NUMERIC DEFAULT '0.00',
CONSTRAINT ORDERLINE_ORDERNO_PK PRIMARY KEY(ORDERNO, LINENO), -- note alteration on this line
CONSTRAINT ORDERLINE_ORDERNO_CC CHECK (ORDERNO BETWEEN 10000 AND 99999),
CONSTRAINT ORDERLINE_NUMBERORDERED CHECK (NUMBERORDERED BETWEEN 1 AND 100)
);
你离得很近。注意上面添加的列名。
确定哪个字段更可能是任何查询中的前导列,并将其放在第一位。例如,如果您经常按OrderNO访问,那么LineNo将是最好的顺序,如果您按LineNo访问,OrderNO将跟随该顺序。
关于mysql - sql COMPOSITE KEY帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6172816/