Faiyaz Alam
Faiyaz Alam

Reputation: 1217

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

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.

Upvotes: 0

Views: 143

Answers (4)

Professor Abronsius
Professor Abronsius

Reputation: 33813

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;

Upvotes: 0

Alex Andrei
Alex Andrei

Reputation: 7283

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.

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.

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;

Upvotes: 1

Lionel Shrestha
Lionel Shrestha

Reputation: 39

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`);

Upvotes: 0

Rahul
Rahul

Reputation: 77896

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;

Upvotes: 1

Related Questions