Search code examples
mysqldatabasewhere-clausesql-insert

current_timestamp interval 4 day in into or other expression


Hello im haveing this code and i want to add data in the table also the current day +4 day interval, i tried with the beginig of the table creation dident work or i need an expresion thas gonna change the date value to the current_timestamp interval 4 day.

CREATE TABLE IF NOT EXISTS `Spital`.`Stoc General` (
  `ID Produs` INT ,
  `Denumire` VARCHAR(45) NOT NULL,
  `Cantitate` INT NULL,
  PRIMARY KEY (`ID Produs`))
ENGINE = InnoDB;

Select*From `Spital`.`Stoc General`;
INSERT INTO `spital`.`stoc general` (`ID Produs`, `Denumire`, `Cantitate`) VALUES ('1', 'Clabax', '20');
INSERT INTO `spital`.`stoc general` (`ID Produs`, `Denumire`, `Cantitate`) VALUES ('2', 'Betadina', '15');
INSERT INTO `spital`.`stoc general` (`ID Produs`, `Denumire`, `Cantitate`) VALUES ('3', 'Paracetamo', '4');
INSERT INTO `spital`.`stoc general` (`ID Produs`, `Denumire`, `Cantitate`) VALUES ('4', 'Oxigen', '3');


CREATE TABLE if not exists `Spital`.`Stoc URGENT` (
  `ID Produs U` INT NOT NULL AUTO_INCREMENT,
`id produs` int not null,
  `Denumire` VARCHAR(45) NOT NULL,
  `Cantitate` INT NOT NULL,
   `Data livrari` DATETIME On update CURRENT_TIMESTAMP(),
  PRIMARY KEY (`ID Produs U`))
ENGINE = MEMORY;

INSERT INTO `spital`.`stoc urgent`
            (`id produs`,
             `denumire`,
             `cantitate`)
SELECT `id produs`,
       `denumire`,
       `cantitate`
FROM   `spital`.`stoc general` 
where  `cantitate`<'5';

Solution

  • You need to set the value during the insertion, so you must set DEFAULT column's option:

    CREATE TABLE if not exists `Spital`.`Stoc URGENT` (
      `ID Produs U` INT NOT NULL AUTO_INCREMENT,
    `id produs` int not null,
      `Denumire` VARCHAR(45) NOT NULL,
      `Cantitate` INT NOT NULL,
       `Data livrari` DATETIME DEFAULT (CURRENT_TIMESTAMP + INTERVAL 4 DAY)
                               On update CURRENT_TIMESTAMP(),
      PRIMARY KEY (`ID Produs U`))
    ENGINE = MEMORY;
    
    1. Parenthesis around the expression are compulsory.
    2. You cannot set the same expression in ON UPDATE option.