我的过程如下:
--建立主表:
CREATE TABLE `country` (
`country_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`country` varchar(50) DEFAULT NULL,
PRIMARY KEY (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1--建立从表CREATE table city
(
city_id int not null auto_increment,
city varchar(20),
country_id int,
primary key(city_id)
) engine = InnoDB;--给外键列加索引
alter table city add index idx_s(country_id);--加外键约束 ---这部出错了
alter table city add constraint fk_city_country
foreign key(country_id)
references country(country_id);
弄了半天 感觉语法没错 。求指教
--建立主表:
CREATE TABLE `country` (
`country_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`country` varchar(50) DEFAULT NULL,
PRIMARY KEY (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1--建立从表CREATE table city
(
city_id int not null auto_increment,
city varchar(20),
country_id int,
primary key(city_id)
) engine = InnoDB;--给外键列加索引
alter table city add index idx_s(country_id);--加外键约束 ---这部出错了
alter table city add constraint fk_city_country
foreign key(country_id)
references country(country_id);
弄了半天 感觉语法没错 。求指教
`city_id` int(11) NOT NULL AUTO_INCREMENT,
`city` varchar(20) DEFAULT NULL,
`country_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`city_id`),
KEY `idx_s` (`country_id`),
CONSTRAINT `fk_city_country` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`));