You should not split tables in this way. Instead, consider placing all in a single table with columns for the month and index OR create a table with columns for the month and index and specify the row identifier in another table:
Option 1: unified table:
CREATE TABLE Unified ( month CHAR(3) NOT NULL, ix INT NOT NULL DEFAULT 1, [...], PRIMARY (month, ix, somethingMore), CHECK month IN ( 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec') ); SELECT * FROM Unified where month = 'jan' AND ix = 1; // select only tables with a "jan" prefix... or "1" suffix. SELECT * FROM Unified where month = 'jan' OR ix = 1
Option 2: use a foreign key:
CREATE TABLE Partitions ( id INT AUTO_INCREMENT PRIMARY, month CHAR(3) NOT NULL, ix INT NOT NULL DEFAULT 1, CHECK month IN ( 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'), INDEX (month, ix) ) CREATE TABLE Stuff ( partition INT NOT NULL, [...], PRIMARY KEY (partition, somethingMore), FOREIGN KEY fk_Stuff_Partitions (partition) REFERENCES Partitions (id) ) SELECT * FROM Stuff INNER JOIN Partitions ON Stuff.partition = Partitions.id WHERE Partition.month = 'jan' AND Partition.ix = 2; // select only tables with a "jan" prefix... or "1" suffix. SELECT DISTINCT * FROM Stuff INNER JOIN Partitions ON Stuff.partition = Partitions.id WHERE Partition.month = 'jan' OR Partition.ix = 1;
PatrikAkerstrand
source share