我的过程如下:
--建立主表:
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);
弄了半天 感觉语法没错 。求指教
解决方案 »
- 怎么用sql语句根据ID逐条替换内容?
- mysql数据库的导出和导入
- mysql数据库存取中文乱码
- Can't connect to local MySQL server through socket 求助!
- 这样一个sql语句,该如何优化呢?谢谢~~~解决立刻给分
- dreamwear如何连接mysql?
- 有没有基于mysql的数据仓库?
- 如何从sql文件中创建数据库?
- MYSQL 的存储过程总是错.新手问题.求解答
- 求助大神!windows下批处理命令调用.sql文件时能否传参数?
- 有没有大侠能通俗易懂讲解一下MYSQL从安装到界面化操作读取数据之间要经历那些步骤
- 问个小问题,insert插入语句也错了
`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`));