简体   繁体   English

如何将表位置分为两个表状态,即在MySQL中具有外键state_id的城市?

[英]How to split a table locations into two tables states, cities with foreign key state_id in mysql?

I have a table " locations " with following structure 我有一个具有以下结构的表“ locations

CREATE TABLE IF NOT EXISTS `locations` (
`id` int(11) NOT NULL,
  `city_code` int(11) NOT NULL,
  `city_name` varchar(100) NOT NULL,
  `state_name` varchar(100) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=154809 ;

I want to split this into two tables : 我想将其分为两个表:

states with following fields 具有以下字段的

id,
state_name

and cities with following fields 和具有以下领域的城市

id,
state_id,
city_name,
city_code

How can I do this in mysql. 我该如何在mysql中做到这一点。

Create the required tables like 创建所需的表,例如

create table states (id int not null auto_increment primary key,
                     state_name varchar(100) not null);

create table cities (id int not null auto_increment primary key,
                     state_id int not null,
                       `city_code` int(11) NOT NULL,
                       `city_name` varchar(100) NOT NULL,
                      foreign key (state_id) references states(id));

Insert the data accordingly 相应地插入数据

insert into states (state_name)
select state_name from locations;

insert into cities (state_id, `city_code`, `city_name`)
select s.state_id, l.`city_code`, l.`city_name`
from locations l
join states s on s.state_name = l.state_name;

Assuming a simple structure for the two resulting tables 假设两个结果表的结构简单

CREATE TABLE IF NOT EXISTS `cities` (
  `id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
  `state_id` MEDIUMINT(8) UNSIGNED NOT NULL,
  `city_code` VARCHAR(10) DEFAULT NULL,
  `city_name` VARCHAR(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `states` (
  `id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
  `state_name` VARCHAR(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

Populate the states table making sure we get unique values. 填充states表,确保获得唯一值。

INSERT INTO states (state_name) SELECT DISTINCT(state_name) FROM locations;

Then populate the cities table joining the initial locations table and the states table we previously created. 然后填充cities表连接的初始locations表和states ,我们以前创建的表。

INSERT INTO cities (state_id, `city_code`, `city_name`)
    SELECT states.id, locations.city_code, locations.city_name 
    FROM `locations`
    JOIN states 
    ON states.state_name = locations.state_name;

Try it 试试吧

--
-- Table structure for table `cities`
--

CREATE TABLE IF NOT EXISTS `cities` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `state_id` int(11) NOT NULL,
  `city_name` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `city_code` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `state_id` (`state_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `states`
--

CREATE TABLE IF NOT EXISTS `states` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `state_name` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;


ALTER TABLE `cities`
  ADD CONSTRAINT `fk_state_id` FOREIGN KEY (`state_id`) REFERENCES `states` (`id`);

I think the following ought to achieve your goal. 我认为以下应该可以实现您的目标。

create table `states` (
    select `id`,`state_name` from `locations`
);
alter table `states` engine=innodb;
alter table `states` change column `id` `id` int(11) unsigned not null auto_increment first, add primary key (`id`);



create table `cities`(
    select `id` as `state_id`,`city_name`,`city_code` from `locations`
);
alter table `cities` engine=innodb;
alter table `cities` add column `id` int unsigned not null auto_increment first, add primary key (`id`);
alter table `cities` change column `state_id` `state_id` int(11) unsigned not null default '0' after `id`;
alter table `cities` add constraint `fk_state` foreign key (`state_id`) references `states` (`id`) on update cascade on delete cascade;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将通过外键连接的两个表中的所有行迭代到 PHP、CodeIgniter、MySQL 中的单个表中 - How do I iterate all rows from two tables connected by foreign key into a single table in PHP, CodeIgniter, MySQL Mysql - 如何获取同一个表的两个外键列的数据 - Mysql - how to get data for two foreign key columns of same table Mysql在两个表中插入其中第一个表具有外键并更新第一个表 - Mysql insert in two tables where the first table has foreign key and update the first table mysql使用第二张表中的auto_increment值作为第二张表中的外键插入到两个表中 - mysql insert into two tables using auto_increment value from the first table as a foreign key in the second 从MySQL表中选择并显示州和各个城市 - Select and show states and respective cities from MySQL table 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another 连接具有外键ID的2个表 - Joining 2 tables with foreign key id MYSQL SELECT使用或不使用外键从两个表中获取行 - MYSQL SELECT To fetch rows from two tables with or without a foreign key 如何使用外键填充子表 - MySql - How to populate the child table with foreign key - MySql mysql表有两个来自另一个表的外键 - mysql tables have two foreign keys from another same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM