可以
mysql> create table test1(id int not null auto_increment primary key ,b int);
Query OK, 0 rows affected (0.11 sec)mysql> create table test2(a int,b int);
Query OK, 0 rows affected (0.12 sec)mysql> alter table test2 add CONSTRAINT FOREIGN KEY(a) REFERENCES test1(id);
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> alter table test2 add CONSTRAINT FOREIGN KEY(b) REFERENCES test1(id);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> show create table test2;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test2 | CREATE TABLE `test2` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
KEY `a` (`a`),
KEY `b` (`b`),
CONSTRAINT `test2_ibfk_2` FOREIGN KEY (`b`) REFERENCES `test1` (`id`),
CONSTRAINT `test2_ibfk_1` FOREIGN KEY (`a`) REFERENCES `test1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)mysql>
mysql> create table test1(id int not null auto_increment primary key ,b int);
Query OK, 0 rows affected (0.11 sec)mysql> create table test2(a int,b int);
Query OK, 0 rows affected (0.12 sec)mysql> alter table test2 add CONSTRAINT FOREIGN KEY(a) REFERENCES test1(id);
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> alter table test2 add CONSTRAINT FOREIGN KEY(b) REFERENCES test1(id);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> show create table test2;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test2 | CREATE TABLE `test2` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
KEY `a` (`a`),
KEY `b` (`b`),
CONSTRAINT `test2_ibfk_2` FOREIGN KEY (`b`) REFERENCES `test1` (`id`),
CONSTRAINT `test2_ibfk_1` FOREIGN KEY (`a`) REFERENCES `test1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)mysql>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货