实在是很强大的功能
可以通过Check的制约把结构相同的表合并起来,或者反过来说,可以在一个表名下数据库自动的根据Check条件往对应的分表里存储数据
[USER_DATA表]
CREATE TABLE test."USER_DATA"
(
"MAC" character varying(12) NOT NULL,
"ID" character(3) NOT NULL,
"YEARS" numeric(6,0) NOT NULL,
"DAY" numeric(2,0) NOT NULL,
"HOUR" numeric(2,0) NOT NULL,
CONSTRAINT "USER_DATA_pkey" PRIMARY KEY ("MAC", "ID", "YEARS", "DAY", "HOUR")
)
[USER_DATA]的分表1[USER_DATA_201310]
CREATE TABLE test."USER_DATA_201310"
(
-- Inherit from table test."USER_DATA": "MAC" character varying(12) NOT NULL,
-- Inherit from table test."USER_DATA": "ID" character(3) NOT NULL,
-- Inherit from table test."USER_DATA": "YEARS" numeric(6,0) NOT NULL,
-- Inherit from table test."USER_DATA": "DAY" numeric(2,0) NOT NULL,
-- Inherit from table test."USER_DATA": "HOUR" numeric(2,0) NOT NULL,
CONSTRAINT "USER_DATA_201210_pkey" PRIMARY KEY ("MAC", "ID", "YEARS", "DAY", "HOUR"),
CONSTRAINT "USER_DATA_YEARS_check" CHECK ("YEARS" = 201310::numeric)
)
INHERITS (test."USER_DATA")
[USER_DATA]的分表2[USER_DATA_201311]
CREATE TABLE test."USER_DATA_201311"
(
-- Inherit from table test."USER_DATA": "MAC" character varying(12) NOT NULL,
-- Inherit from table test."USER_DATA": "ID" character(3) NOT NULL,
-- Inherit from table test."USER_DATA": "YEARS" numeric(6,0) NOT NULL,
-- Inherit from table test."USER_DATA": "DAY" numeric(2,0) NOT NULL,
-- Inherit from table test."USER_DATA": "HOUR" numeric(2,0) NOT NULL,
CONSTRAINT "USER_DATA_201210_pkey" PRIMARY KEY ("MAC", "ID", "YEARS", "DAY", "HOUR"),
CONSTRAINT "USER_DATA_YEARS_check" CHECK ("YEARS" = 201311::numeric)
)
INHERITS (test."USER_DATA")
通过CHECK ("YEARS" = 2013xx::numeric)来把往表[USER_DATA]上存储的数据自动的存储到分表1[USER_DATA_201310]或者分表2[USER_DATA_201311]上
这样做不仅可以降低单表的数据量,而且可以方便的拆分,组合,更方便的是,取数据的时候只用去访问[USER_DATA]表,就可以查询所有分表里的数据
组合
ALTER TABLE test."USER_DATA_201312" INHERIT test."USER_DATA"
拆分
ALTER TABLE test."USER_DATA_201310" NO INHERIT test."USER_DATA"