复合主键为一个表中多个主键来确定数据的唯一性。
CREATE TABLE IF NOT EXISTS active_user(
logindate DATE,
uid VARCHAR(225),
new_user INT,
PRIMARY KEY(logindate,uid)
);
## 外键分开写会报错
mysql> CREATE TABLE IF NOT EXISTS receive_red_packets(
-> receive_date DATE,
-> receive_datetime DATETIME,
-> uid VARCHAR(225),
-> money FLOAT,
-> FOREIGN KEY(receive_date) REFERENCES active_user(logindate),
-> FOREIGN KEY(uid) REFERENCES active_user(uid)
-> );
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'receive_red_packets_ibfk_2' in the referenced table 'active_user'
mysql> CREATE TABLE IF NOT EXISTS receive_red_packets(
-> receive_date DATE,
-> receive_datetime DATETIME,
-> uid VARCHAR(225),
-> money FLOAT,
-> FOREIGN KEY(receive_date,uid) REFERENCES active_user(logindate,uid)
-> );
Query OK, 0 rows affected (0.05 sec)