如何根据MySQL中的列将数据库表分成两个表

时间:2012-08-31 04:21:43

标签: mysql database database-design

我有一个包含十列的表格:

name, id, colg, schol, add, no, subject, marks, surname, lectures

我可以将此表拆分为两个单独的表,而不会像这样丢失数据吗?

表一:

name, id, colg, schol, add, no

表二:

subject, marks, surname, lectures

2 个答案:

答案 0 :(得分:2)

是的,您只需要在表之间创建一对一的关系。您可以通过在每个表上创建唯一主键来执行此操作,其中第二个表的主键也是第一个表的主键的外键。

表#1

PK_ID      (primary key)
name
id
colg
schol
add
no

表#2

PK_ID      (primary key, foreign key to Table #1)
subject
marks
surname
lectures

所以步骤很简单:

  1. 使用上述结构创建两个新表
  2. 将现有表中的所有记录插入表#1
  3. 将现有表格中的所有记录插入表格#2,并在表格#1上加入以获取外键
  4. (请注意,您也可以使用现有表中的一个列作为主键,例如 id ,只要它是唯一的。)

答案 1 :(得分:0)

CREATE TABLE old_table (name VARCHAR(255), id BIGINT, colg VARCHAR(255), schol VARCHAR(255), addit VARCHAR(255), no VARCHAR(255), subject VARCHAR(255), marks VARCHAR(255), surname VARCHAR(255), lectures VARCHAR(255));

INSERT INTO old_table VALUES("shaun", 1234, "DePaul University", "Computing and Digital Media", "something", "something", "some subject", "A", "Husain","no thank you");

mysql> SELECT * FROM old_table;
+-------+------+-------------------+-----------------------------+-----------+-----------+--------------+-------+---------+--------------+
| name  | id   | colg              | schol                       | addit     | no        | subject      | marks | surname | lectures     |
+-------+------+-------------------+-----------------------------+-----------+-----------+--------------+-------+---------+--------------+
| shaun | 1234 | DePaul University | Computing and Digital Media | something | something | some subject | A     | Husain  | no thank you |
+-------+------+-------------------+-----------------------------+-----------+-----------+--------------+-------+---------+--------------+
1 row in set (0.00 sec)

CREATE TABLE table1 (name VARCHAR(255), id BIGINT PRIMARY KEY, colg VARCHAR(255), schol VARCHAR(255), addit VARCHAR(255), no VARCHAR(255));
CREATE TABLE table2 (id BIGINT PRIMARY KEY, subject VARCHAR(255), marks VARCHAR(255), surname VARCHAR(255), lectures VARCHAR(255));

INSERT INTO table1 (name,id,colg,schol,addit,no) SELECT name,id,colg,schol,addit,no from old_table;
INSERT INTO table2 (id,subject,marks,surname,lectures) SELECT id,subject,marks,surname,lectures from old_table;

mysql> select * from table1;
+-------+------+-------------------+-----------------------------+-----------+-----------+
| name  | id   | colg              | schol                       | addit     | no        |
+-------+------+-------------------+-----------------------------+-----------+-----------+
| shaun | 1234 | DePaul University | Computing and Digital Media | something | something |
+-------+------+-------------------+-----------------------------+-----------+-----------+
1 row in set (0.00 sec)

mysql> select * from table2;
+------+--------------+-------+---------+--------------+
| id   | subject      | marks | surname | lectures     |
+------+--------------+-------+---------+--------------+
| 1234 | some subject | A     | Husain  | no thank you |
+------+--------------+-------+---------+--------------+
1 row in set (0.00 sec)

mysql> select * from table1 LEFT JOIN table2 on (table1.id=table2.id);
+-------+------+-------------------+-----------------------------+-----------+-----------+------+--------------+-------+---------+--------------+
| name  | id   | colg              | schol                       | addit     | no        | id   | subject      | marks | surname | lectures     |
+-------+------+-------------------+-----------------------------+-----------+-----------+------+--------------+-------+---------+--------------+
| shaun | 1234 | DePaul University | Computing and Digital Media | something | something | 1234 | some subject | A     | Husain  | no thank you |
+-------+------+-------------------+-----------------------------+-----------+-----------+------+--------------+-------+---------+--------------+
1 row in set (0.00 sec)