我有这些 SQL 指令:
CREATE TABLE discipline (
did INT PRIMARY KEY,
code VARCHAR(127) UNIQUE,
university VARCHAR(127),
number_students INT
) ENGINE=INNODB;
CREATE TABLE precedence(
type VARCHAR(127),
basic INT,
advanced INT,
PRIMARY KEY (basic, advanced),
FOREIGN KEY (basic) REFERENCES discipline(did) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (advanced) REFERENCES discipline(did)
)ENGINE=INNODB;
INSERT INTO discipline VALUES (1,'BD','U1',30);
INSERT INTO discipline VALUES (2,'IBD','U2',30);
INSERT INTO discipline VALUES (3,'SIBD','U3',30);
INSERT INTO precedence VALUES ('optional',1,2);
INSERT INTO precedence VALUES ('mandatory',2,3);
UPDATE discipline SET did=did+did WHERE did=2;
我不明白为什么指令的结果SELECT SUM (DISTINCT did)
是 6 而不是 8 (1+4+3)。更新指令的值2
(did)不应该是4
?你能给我解释一下吗?谢谢。