1

我有一个十列的表:

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

我可以将此表拆分为两个单独的表而不会丢失任何数据吗?

表一:

name, id, colg, schol, add, no

表二:

subject, marks, surname, lectures
4

2 回答 2

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 上连接以获取外键

(请注意,您也可以使用现有表中的一列作为主键,例如id,只要它是唯一的。)

于 2012-08-31T04:25:18.937 回答
1
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)
于 2012-08-31T05:00:05.280 回答