以月份创建分区表

CREATE TABLE IF NOT EXISTS `student`(`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT `主键`,`name` VARCHAR(50) NULL DEFAULT NULL COMMENT `姓名`,`create_time` DATETIME NOT NULL COMMENT `入库时间`,PRIMARY KEY (id,create_time)) ENGINE=INNODB DEFAULT CHARSET=utf8PARTITION BY RANGE (MONTH(create_time))(PARTITION p1 VALUES LESS THAN(02),PARTITION p2 VALUES LESS THAN(03),PARTITION p3 VALUES LESS THAN(04),PARTITION p4 VALUES LESS THAN(05),PARTITION p5 VALUES LESS THAN(06),PARTITION p6 VALUES LESS THAN(07),PARTITION p7 VALUES LESS THAN(08),PARTITION p8 VALUES LESS THAN(09),PARTITION p9 VALUES LESS THAN(10),PARTITION p10 VALUES LESS THAN(11),PARTITION p11 VALUES LESS THAN(12),PARTITION p12 VALUES LESS THAN(13));

以天份创建分区表

CREATE TABLE IF NOT EXISTS `student`(`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT `主键`,`name` VARCHAR(50) NULL DEFAULT NULL COMMENT `姓名`,`create_time` DATETIME NOT NULL COMMENT `入库时间`,PRIMARY KEY (id,create_time)) ENGINE=INNODB DEFAULT CHARSET=utf8PARTITION BY RANGE (DAY(create_time))(PARTITION p1 VALUES LESS THAN(02),PARTITION p2 VALUES LESS THAN(03),PARTITION p3 VALUES LESS THAN(04),PARTITION p4 VALUES LESS THAN(05),PARTITION p5 VALUES LESS THAN(06),PARTITION p6 VALUES LESS THAN(07),PARTITION p7 VALUES LESS THAN(08),PARTITION p8 VALUES LESS THAN(09),PARTITION p9 VALUES LESS THAN(10),PARTITION p10 VALUES LESS THAN(11),PARTITION p11 VALUES LESS THAN(12),PARTITION p12 VALUES LESS THAN(13),PARTITION p13 VALUES LESS THAN(14),PARTITION p14 VALUES LESS THAN(15),PARTITION p15 VALUES LESS THAN(16),PARTITION p16 VALUES LESS THAN(17),PARTITION p17 VALUES LESS THAN(18),PARTITION p18 VALUES LESS THAN(19),PARTITION p19 VALUES LESS THAN(20),PARTITION p20 VALUES LESS THAN(21),PARTITION p21 VALUES LESS THAN(22),PARTITION p22 VALUES LESS THAN(23),PARTITION p23 VALUES LESS THAN(24),PARTITION p24 VALUES LESS THAN(25),PARTITION p25 VALUES LESS THAN(26),PARTITION p26 VALUES LESS THAN(27),PARTITION p27 VALUES LESS THAN(28),PARTITION p28 VALUES LESS THAN(29),PARTITION p29 VALUES LESS THAN(30),PARTITION p30 VALUES LESS THAN(31),PARTITION p31 VALUES LESS THAN(32),);

查询分区表数据

SELECT * FROM `student` PARTITION(p3);

清空分区数据,不删除对应分区

ALTER TABLE `student` TRUNCATE PARTITION(p5);