I have two tables. Table A and B. Table A has a column with auto increment as primary key.
id name date_created
1 Kletian 12-12-12 12:12:12
2 Vormav 12-12-12 12:12:13
3 Agrias 12-12-12 12:12:14
4 Ramza 12-12-12 12:12:15
Table B should has a foreign key referencing the id in table A. But since the id is auto increment (generated by mysql) I can't assign it using server side programming (I use PHP to insert). But, In table A, there is a column date_created with datetime attribute.
Normally, this is the table B should looks like:
id images
1 abc.jpg
1 def.png
1 ghi.jpg
2 jkl.png
3 mno.jpeg
3 pqr.png
and this is my plan for table B:
date_created images
12-12-12 12:12:12 abc.jpg
12-12-12 12:12:12 def.png
12-12-12 12:12:12
12-12-12 12:12:13
12-12-12 12:12:14
12-12-12 12:12:14 pqr.png
Note:
Sequence number: Take one column in table A which you increment when new entry is inserted ( not auto-increment ) e.g:
seq_no | name
1 | Kletian
3 | Agrias
So when you want to do new entry in a table A, just get the MAX()
of seq_no
and increment it by 1.
Also in table B, make seq_no
as like a foreign key, as:
seq_no | images
1 | abc.jpg
3 | efg.jpg
Here is what you would want for your table DDL (schema):
CREATE TABLE IF NOT EXISTS `table_a` (
`id` MEDIUMINT NOT NULL AUTO_INCREMENT,
`name` TEXT NOT NULL,
`created_date` TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `table_b` (
`id` MEDIUMINT NOT NULL AUTO_INCREMENT,
`image` TINYTEXT NOT NULL,
`created_date` TIMESTAMP NOT NULL DEFAULT NOW(),
`table_a_id` MEDIUMINT NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`table_a_id`) REFERENCES table_a(id) ON DELETE CASCADE
);
Table B will have a foreign key reference to Table A.
I am not sure if are able to make those changes, but that would be the ideal solution.