我有一个十列的表:
name, id, colg, schol, add, no, subject, marks, surname, lectures
我可以将此表拆分为两个单独的表而不会丢失任何数据吗?
表一:
name, id, colg, schol, add, no
表二:
subject, marks, surname, lectures
我有一个十列的表:
name, id, colg, schol, add, no, subject, marks, surname, lectures
我可以将此表拆分为两个单独的表而不会丢失任何数据吗?
表一:
name, id, colg, schol, add, no
表二:
subject, marks, surname, lectures
是的,您只需要在表之间创建一对一的关系。您可以通过在每个表上创建一个唯一的主键来做到这一点,其中第二个表的主键也是第一个表的主键的外键。
表格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
所以步骤很简单:
(请注意,您也可以使用现有表中的一列作为主键,例如id,只要它是唯一的。)
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)