Reputation: 23
I want to create a table with a PK column with the current year
CREATE TABLE IF NOT EXISTS `example` (
`yearId` YEAR NOT NULL DEFAULT year(curdate()),
PRIMARY KEY (`yearId`))
ENGINE = InnoDB
But it is not correct.
I know that I can save the CURRENT_TIMESTAMP
CREATE TABLE IF NOT EXISTS `example` (
`yearId` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`yearId`))
ENGINE = InnoDB
But I only want the year. Is there any way to do it?
Upvotes: 1
Views: 3447
Reputation: 14859
Just call the YEAR()
function and pass that value to it.
CREATE TABLE IF NOT EXISTS `example` (
`yearId` TIMESTAMP NOT NULL DEFAULT YEAR(CURRENT_TIMESTAMP),
PRIMARY KEY (`yearId`))
ENGINE = InnoDB
Click here for more date and time functions.
P.S. You should probably set the data type for that column to YEAR(4)
to ensure that only valid values for a year are entered.
Upvotes: 1