这是我的问题:
CREATE TABLE `fisher`.`prodstock` (
`PID` VARCHAR(10) NOT NULL COMMENT 'Product Id',
`QUANTITY` DECIMAL(10,2) NOT NULL COMMENT 'Quantity in KG',
`CREATED` DATETIME NOT NULL DEFAULT NOW() COMMENT 'Invard Quantity Date',
PRIMARY KEY (`PID`),
CONSTRAINT `FK_PID`
FOREIGN KEY (`PID`)
REFERENCES `fisher`.`products` (`PID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
COMMENT = 'Stock of Product'
答案 0 :(得分:1)
将CURRENT_TIMESTAMP
设为默认值。
CREATE TABLE `fisher`.`prodstock` (
`PID` VARCHAR(10) NOT NULL COMMENT 'Product Id',
`QUANTITY` DECIMAL(10,2) NOT NULL COMMENT 'Quantity in KG',
`CREATED` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Invard Quantity Date',
PRIMARY KEY (`PID`),
CONSTRAINT `FK_PID`
FOREIGN KEY (`PID`)
REFERENCES `fisher`.`products` (`PID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
COMMENT = 'Stock of Product'
答案 1 :(得分:0)
您无法将now()
功能用于默认值。如果要保留DATETIME
数据类型,则必须创建一些触发器,并在表中插入记录时使用该触发器。默认值始终必须是常量值,而不是表达式的结果。
触发示例:
CREATE TRIGGER `yourTriggername` BEFORE INSERT ON `yourTablename`
FOR EACH ROW
SET NEW.datetimefield = NOW()
如果您可以将created
的数据类型更改为timestamp
,那么您可以使用
CREATE TABLE fisher.prodstock (
.....
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
....
)