1
    CREATE TABLE `tmp` (
    `CallID` bigint(8) unsigned NOT NULL,
    `InfoID` bigint(8) unsigned NOT NULL,
   `CallStartTime` datetime NOT NULL,
    KEY `CallStartTime` (`CallStartTime`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1

在这个表中我想再添加一列PartitionIDint(4) unsigned NOT NULL,这里我想插入 PartitionID 列的数据,这意味着 PartitionID=(DAYOFYEAR('2009-01-01 00:00:00')) = 1我想自动插入数据... PartitionID=(DAYOFYEAR( CallStartTime))

我的表旧数据是这样的

        (144,   56, '2011-06-19 1:00:00'),
        (100,   57, '2011-09-01 10:24:00'),
        (168,   68, '2012-07-13 12:10:06'),
        (207,   30,         '12012-12-05 12:12:00'),
        (112,   14, '2013-05-19 1:00:00');
        (177,   90, '2013-06-05 12:12:00'),
        (144,   56, '2013-06-19 1:00:00'),
        (168,   68,     '2013-07-13 12:10:06'),
        (119,   17, '2013-08-01 10:24:00'),
        (110,   10, '2013-08-05 12:12:00'),
        (113,   11, '2013-08-13 12:10:06'),
        (100,   57, '2013-09-01 10:24:00'),

添加新列后...我想要这样

PartitionID 自动插入数据而不是手动插入

(177,   90, '2010-06-05 12:12:00',156),
(144,   56, '2011-06-19 01:00:00',170),
(100,   57, '2011-09-01 10:24:00',244),
(168,   68, '2012-07-13 12:10:06',194),
(207, 30, '2012-12-05 12:12:00',340),
(112,   14, '2013-05-19 01:00:00',139),
(177,   90, '2013-06-05 12:12:00',156),
(144,   56, '2013-06-19 01:00:00',170),
(168,   68, '2013-07-13 12:10:06',194),
(119,   17, '2013-08-01 10:24:00',213),
(110,   10, '2013-08-05 12:12:00',217),
(113,   11, '2013-08-13 12:10:06',225),
(100,   57, '2013-09-01 10:24:00',244)
4

2 回答 2

0

您不能为该列设置默认值,因为它是一个计算值并且默认值必须是常量。但是您可以创建一个触发器,在插入时计算该值

delimiter |
CREATE TRIGGER PartitionIDtrigger BEFORE INSERT ON tmp
FOR EACH ROW 
BEGIN
    SET NEW.PartitionID = DAYOFYEAR(CURDATE());
END
|
delimiter ;
于 2013-09-17T08:24:44.227 回答
0
ALTER TABLE TestTable
ADD NewCol int
CONSTRAINT DF_TestTable_NewCol DEFAULT '1' NOT NULL
GO

我不确定这是否是您要查找的内容,但这用于添加默认值为 1 的新列。

然后,您可以像@juergen d 所说的那样创建一个触发器,然后更改值。

于 2013-09-17T08:32:42.393 回答