2016-04-18 6 views
1

Ich muss jeden Tag meine Tabelle partitionieren basierend auf UTC-Zeitstempel, erhalte ich Fehler als Fatal error: A PRIMARY KEY must include all columns in the table's partitioning functionMySQL - Primärschlüssel für die Partition benötigt

ich auf diese Weise tue, Spalte id ist INT PRIMARY KEY-Spalte date ist INT Speicherung UTC-Zeitstempel

Meine Tabellenstruktur DESCRIBE stats;

Field Type Null Key  Default  Extra 
id  int(11) NO  PRI  NULL auto_increment 
date int(11) YES    NULL  
. 
. 
. 
. 

Tabellenstruktur erstellen SHOW CREATE TABLE stats

CREATE TABLE `stats` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`date` int(11) DEFAULT NULL, 
. 
. 
. 
. 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=371 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 

I Skript über php

$today = strtotime("today midnight"); 
$partition = date('d_m_y',$today); 

ALTER TABLE `stats` 
PARTITION BY RANGE (date) (
PARTITION p_$partition VALUES LESS THAN ($today) 
) 

Ich habe versucht, dies auszuführen, aber Syntaxfehler

ALTER TABLE `stats` 
UNIQUE KEY (`id`) 
PARTITION BY RANGE (date) (
PARTITION p_$partition VALUES LESS THAN ($today) 
) 

ALTER TABLE `stats` 
PRIMARY KEY (`id`) 
PARTITION BY RANGE (date) (
PARTITION p_$partition VALUES LESS THAN ($today) 
) 

ALTER TABLE `stats` 
PRIMARY KEY (`id`,`date`) 
PARTITION BY RANGE (date) (
PARTITION p_$partition VALUES LESS THAN ($today) 
) 

auch versucht, mit HASH aber gleichen Fehler A PRIMARY KEY must include all columns in the table's partitioning function in

ALTER TABLE stats 
PARTITION BY HASH(date) 
PARTITIONS 1 

auch versucht, dies immer Weg aber bekam Fehler Fatal error: Partition management on a not partitioned table is not possible

ALTER TABLE `stats` 
ADD PARTITION (
PARTITION p_$partition VALUES LESS THAN ($today) 
) 

Bitte sehen und deuten darauf hin, jede mögliche Art und Weise, dies zu tun.

Dank

+0

Sie mischen PHP und SQL-Befehle. Wie führen Sie die SQL-Befehle aus? – Shadow

+0

@Shadow mit PHP-Skript –

+0

Mögliches Duplikat von [Ein Primär muss alle Spalten in der Partitionierungsortfehler der Tabelle enthalten?] (Http://stackoverflow.com/questions/11896067/a-primary-must-include-all-columns- in-the-tables-partitioning-location-error) – regulus

Antwort

1

Ich löste es durch date auf Primärschlüssel id hinzufügen.

ALTER TABLE `stats` DROP PRIMARY KEY, ADD PRIMARY KEY(`id`,`date`); 

Jetzt ist die Tabelle erfolgreich partitioniert.

Hoffe es hilft anderen auch.

Danke

Verwandte Themen