Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

split a database into related tables based on their structure in MySQL

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)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #split #database #related #tables #based #structure #MySQL
ADD COMMENT
Topic
Name
6+2 =