表一:
mysql> select * from employee;
+----+--------+---------+
| id | name | manager |
+----+--------+---------+
| 1 | 张三 | 李红 |
| 2 | 张三 | 李红 |
| 3 | 王五 | 刘倩 |
| 4 | 马六 | 马芳 |
| 5 | 孙杨 | 明德 |
| 6 | 郭德纲 | 华北 |
+----+--------+---------+
表二:
表二的数据填充有所不同,如下:
sql:insert into manager(name) select manager from employee group by manager;
mysql> select * from manager;
+----+------+
| id | name |
+----+------+
| 1 | 刘倩 |
| 2 | 华北 |
| 3 | 明德 |
| 4 | 李红 |
| 5 | 马芳 |
+----+------+
如何把两张表连接起来?并且将employee表里面的manager存储的数据变为manager表的id?
sql: update employee as e inner join manager as m on e.manager=m.name set e.ma
nager=m.id;
此时的表:mysql> select * from employee;
+----+--------+---------+
| id | name | manager |
+----+--------+---------+
| 1 | 张三 | 4 |
| 2 | 张三 | 4 |
| 3 | 王五 | 1 |
| 4 | 马六 | 5 |
| 5 | 孙杨 | 3 |
| 6 | 郭德纲 | 2 |
+----+--------+---------+
mysql> desc employee;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | smallint(6) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| manager | varchar(20) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
此时manager的数据类型还是varchar类型,同时manager这个名字也不合适。那么如何修改呢?
sql: alter table employee change manager manager_id smallint not null;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | smallint(6) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| manager_id | smallint(6) | NO | | NULL | |
+------------+-------------+------+-----+---------+----------------+
同时应该键入外键,与父表联系起来。
sql:alter table employee add constraint FK_ma_em foreign key (manager_id) ref
erences manager(id) on update cascade on delete cascade;
mysql> desc employee;
+------------+-------------+------+-----+---------+----------------+
| Field | Type| Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | smallint(6) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| manager_id | smallint(6) | NO | MUL | NULL | |
+------------+-------------+------+-----+---------+----------------+
上面的操作流程如下:创建manager-->插入数据-->更新employee-->插入外键;
那么有没有方法将上面的步骤省略一两步呢?比如说在创建数据表的同时就将数据插入进去。
sql: create table employee2
(
id smallint unsigned auto_increment primary key,
employee_name varchar(20) not null
)
select name from employee group by name;
你是不是认为这样是正确的呢?让我们来看看现在建立的表的结构吧!
mysql> select * from employee2;
+----+---------------+--------+
| id | employee_name | name |
+----+---------------+--------+
| 1 | | 孙杨 |
| 2 | | 张三 |
| 3 | | 王五 |
| 4 | | 郭德纲 |
| 5 | | 马六 |
+----+---------------+--------+
那么正确的解决方案应该是:
sql: create table employee2
(
id smallint unsigned auto_increment primary key,
name varchar(20) not null
)
select name from employee group by name;
mysql> select * from employee2;
+----+--------+
| id | name |
+----+--------+
| 1 | 孙杨 |
| 2 | 张三 |
| 3 | 王五 |
| 4 | 郭德纲 |
| 5 | 马六 |
+----+--------+
注意:你创建新表的字段的名字要和查询到的想要插入的字段的名字应该保持一致。