mysql – AD日期的数据仓库

我们正在为世界历史数据库创建历史存档,我们需要一个日期查找表来引用AD中的所有日期.如何创建此表的值 – 从1AD到2011,为YYYY / MM / DD?数据库是MySQL.

问题:

>我使用Excel预先填充日期,然后导入MySQL:YYYY / MM / DD但Excel不识别0007,0008等年份,因此我无法自动复制单元格以生成日期.我必须手动完成这个工作,从1AD到2011年需要几天时间作为YYYY / MM / DD.
>闰年是在1752年引入的.如果我以编程方式生成日期,我如何在1752年之前处理没有闰年的案件?它会生成错误的日期.

我的桌子:

    CREATE TABLE `dates` (
      `date_id` int(10) NOT NULL,
      `format` char(10) NOT NULL,
      `century` int(10) NOT NULL,
      `decade` int(10) NOT NULL,
      `year` int(10) NOT NULL,
      `month` int(10) NOT NULL,
      `week` int(10) NOT NULL,
      `day` int(10) NOT NULL,
      `month_year` int(10) NOT NULL,
      `week_year` int(10) NOT NULL,
      `week_month` int(10) NOT NULL,
      `day_year` int(10) NOT NULL,
      `day_month` int(10) NOT NULL,
      `day_week` int(10) NOT NULL,
      PRIMARY KEY (`date_id`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

解决方法:

使用类似这样的SQL(使用我自己的表结构,而不是你拥有的那个):

DROP TABLE IF EXISTS time_dimension;
CREATE TABLE time_dimension (
        id                      INTEGER PRIMARY KEY,  -- year*10000+month*100+day
        db_date                 DATE NOT NULL,
        year                    INTEGER NOT NULL,
        month                   INTEGER NOT NULL, -- 1 to 12
        day                     INTEGER NOT NULL, -- 1 to 31
        quarter                 INTEGER NOT NULL, -- 1 to 4
        week                    INTEGER NOT NULL, -- 1 to 52/53
        day_name                VARCHAR(9) NOT NULL, -- 'Monday', 'Tuesday'...
        month_name              VARCHAR(9) NOT NULL, -- 'January', 'February'...
        holiday_flag            CHAR(1) DEFAULT 'f' CHECK (holiday_flag in ('t', 'f')),
        weekend_flag            CHAR(1) DEFAULT 'f' CHECK (weekday_flag in ('t', 'f')),
        event                   VARCHAR(50),
        UNIQUE td_ymd_idx (year,month,day),
        UNIQUE td_dbdate_idx (db_date)

) Engine=MyISAM;

DROP PROCEDURE IF EXISTS fill_date_dimension;
DELIMITER //
CREATE PROCEDURE fill_date_dimension(IN startdate DATE,IN stopdate DATE)
BEGIN
    DECLARE currentdate DATE;
    SET currentdate = startdate;
    WHILE currentdate < stopdate DO
        INSERT INTO time_dimension VALUES (
                        YEAR(currentdate)*10000+MONTH(currentdate)*100 + DAY(currentdate),
                        currentdate,
                        YEAR(currentdate),
                        MONTH(currentdate),
                        DAY(currentdate),
                        QUARTER(currentdate),
                        WEEKOFYEAR(currentdate),
                        DATE_FORMAT(currentdate,'%W'),
                        DATE_FORMAT(currentdate,'%M'),
                        'f',
                        CASE DAYOFWEEK(currentdate) WHEN 1 THEN 't' WHEN 7 then 't' ELSE 'f' END,
                        NULL);
        SET currentdate = ADDDATE(currentdate,INTERVAL 1 DAY);
    END WHILE;
END
//
DELIMITER ;

TRUNCATE TABLE time_dimension;

CALL fill_date_dimension('1-01-01','2015-01-01');
OPTIMIZE TABLE time_dimension;
上一篇:UI优秀框架(库)


下一篇:android – 以编程方式设置listview标题大小