Coming from my question here, the answers were valid but I tried to transform MySQL to PostgreSQL. This is not the best way to do what I need.
I have a file with more than 600 000 objects:
paper
pen
whatever
...
All those objects are listed in a "cyclic" colors, and I know what the cycle look like but it's not in the file itself, so, to give you an example (it's much longer than the example, but I hope you get the idea):
- black + red
- black + blue
- black + yellow
- white + red
- white + blue
- white + yellow
- black + red
- black + blue
- ....
I've separated those colors into a table wisely named "colors", and I've created another table named "group_colors", which is a many_to_many relationship with colors. The objects will have a one_to_one relationship with group_colors
I want to generate my own script, and only one script that:
- creates tables
colors,group_colorsandobjects - creates
INSERTstatements forcolors - based on those previous
INSERTstatements use theID's ofcolorsto createsINSERTstatements forgroup_colors - from now on, creates
INSERTstatements forobjects - based on the
ID's ofgroup_colorsandID's of just insertedobject, createsINSERTstatements forobjects_group_colors
This script should starts like that:
DROP TABLE IF EXISTS color;
CREATE TABLE color (
id AUTOINC,
description VARCHAR(50)
)
DROP TABLE IF EXISTS group_colors;
CREATE TABLE group_colors(
id AUTOINC,
description VARCHAR(50)
)
DROP TABLE IF EXISTS group_colors_color;
CREATE TABLE group_colors_color(
group_colors_id INT,
color_id INT,
group_colors_id INT,
CONSTRAINT color_id NOT NULL FOREIGN KEY REFERENCES color(id),
CONSTRAINT group_colors_id NOT NULL FOREIGN KEY REFERENCES group_colors(id)
)
DROP TABLE IF EXISTS obj;
CREATE TABLE obj(
id AUTOINC,
description VARCHAR(50),
group_colors_id INT,
CONSTRAINT group_colors_id NOT NULL FOREIGN KEY REFERENCES group_colors(id)
)
And so on. The problem is not in creating table, it's in how to INSERT VALUES and re-use the ID's of those values later on (very very late (>600 000 rows)), and more than once.
How would you do?