DATETIME Default value 0 MYSQL 5.5.25 - mysql

I'm trying to create these table:
CREATE TABLE IF NOT EXISTS `qa_discountcoupons` (
`discount_code` INT NOT NULL AUTO_INCREMENT ,
`status_code` INT NOT NULL ,
`discount_date` DATETIME NOT NULL DEFAULT 0 ,
PRIMARY KEY (`discount_code`) ,
INDEX `discounts_to_status` (`status_code` ASC) ,
CONSTRAINT `discounts_to_status`
FOREIGN KEY (`status_code` )
REFERENCES `qa_status` (`status_code` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
But i get this error:
Error Code: 1067. Invalid default value for 'discount_date'

You can use:
CREATE TABLE IF NOT EXISTS `qa_discountcoupons` (
`discount_code` INT NOT NULL AUTO_INCREMENT ,
`status_code` INT NOT NULL ,
`discount_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`discount_code`) ,
INDEX `discounts_to_status` (`status_code` ASC) ,
CONSTRAINT `discounts_to_status`
FOREIGN KEY (`status_code` )
REFERENCES `qa_status` (`status_code` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
from MySQL 5.6.5 and up.
I suggest also checking out the thread on How do you set a default value for a MySQL Datetime column? - which has a lot of comments on this.

Related

Mysql error 150 with foreigns keys

I've got this
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
`subject` VARCHAR(200) NOT NULL ,
`body` TEXT NOT NULL ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id_msg`) ,
INDEX fk_msg_messages_user (`id_user_from` ASC) ,
INDEX fk_msg_messages_msg_messages_users (`id_msg` ASC) ,
CONSTRAINT `fk_msg_messages_user`
FOREIGN KEY (`id_user_from` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_msg_messages_msg_messages_users`
FOREIGN KEY (`id_msg` )
REFERENCES `beta`.`msg_messages_users` (`id_msg` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
*And this is my error. These are my user and msg_messages_users tables too. I've reviewed all my tables and i cant see my error. Im working on MysqlWorbench and it generate this erroneous sintax.*
Error 1005: Can't create table 'beta.msg_messages' (errno: 150).
CREATE TABLE IF NOT EXISTS `beta`.`user` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`id`) ,
ENGINE = InnoDB
AUTO_INCREMENT = 87
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
`id` BIGINT UNSIGNED NOT NULL ,
`id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
`id_msg` BIGINT(20) UNSIGNED NOT NULL ,
`status` TINYINT NOT NULL DEFAULT 0 ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
CONSTRAINT `fk_msg_messages_users_user`
FOREIGN KEY (`id_usr_to` )
REFERENCES `beta`.`user` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Im a dumb... i was declaring a foreign key in the wrong table..
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
`subject` VARCHAR(200) NOT NULL ,
`body` TEXT NOT NULL ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id_msg`) ,
INDEX fk_msg_messages_user (`id_user_from` ASC) ,
CONSTRAINT `fk_msg_messages_user`
FOREIGN KEY (`id_user_from` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL ,
`id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
`status` TINYINT NOT NULL DEFAULT 0 ,
`date` DATETIME NOT NULL ,
INDEX id_msg (`id_msg` ASC) ,
INDEX fk_msg_messages_users_msg_messages (`id_msg` ASC) ,
INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
CONSTRAINT `fk_msg_messages_users_msg_messages`
FOREIGN KEY (`id_msg` )
REFERENCES `beta`.`msg_messages` (`id_msg` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_msg_messages_users_user`
FOREIGN KEY (`id_usr_to` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
thanks all

cant create table in Mysql database

I am trying to create table in Mysql database with the below sql
CREATE TABLE IF NOT EXISTS `dev_dict`.`TAMILWORD_SECONDARY` (
`ID` INT NOT NULL ,
`TAMILWORD_ID` INT NOT NULL ,
`WORDS` VARCHAR(50) NOT NULL ,
`CREATED_DTTM` TIMESTAMP NOT NULL ,
`MODIFIED_DTTM` TIMESTAMP NOT NULL ,
PRIMARY KEY (`ID`) ,
UNIQUE INDEX `WORDS_UNIQUE` (`WORDS` ASC, `TAMILWORD_ID` ASC) ,
INDEX `FK_TAMILWORD_ID_idx` (`TAMILWORD_ID` ASC) ,
CONSTRAINT `FK_TAMILWORD_ID`
FOREIGN KEY (`TAMILWORD_ID` )
REFERENCES `dev_dict`.`TAMILWORD` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
but getting Error Code: 1022. Can't write; duplicate key in table 'tamilword_secondary'.
thanks for helping me.. i have the table tamilword already , and getting same error
CREATE TABLE IF NOT EXISTS `dev_dict`.`TAMILWORD` (
`ID` INT NOT NULL ,
`WORD` VARCHAR(50) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NOT NULL ,
`SENTENCE` VARCHAR(100) NULL ,
`CREATED_DTTM` TIMESTAMP NOT NULL ,
`MODIFIED_DTTM` TIMESTAMP NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `SECONDARY` (`WORD` ASC, `SENTENCE` ASC) )
ENGINE = InnoDB
Create dev_dict.TAMILWORD table first.
Other than that your statement works.
See SQL Fiddle example

why do I get the “Error Code 1005 Cant create Table” in MYSQL with no typos in FK

With the following scripts, when i try to create NetBankingTransaction table, it fails with the following message:
Error Code: 1005. Can't create table 'wah_schema.netbankingtransaction' (errno: 150)
DB scripts:
CREATE TABLE IF NOT EXISTS `wah_schema`.`Transaction` (
`idTransaction` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`idOrder` INT UNSIGNED NOT NULL ,
`type` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idTransaction`, `idOrder`, `type`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `wah_schema`.`NetBankingTransaction` (
`idTransaction` INT NOT NULL ,
`bankCode` VARCHAR(45) NOT NULL ,
`type` VARCHAR(45) NOT NULL DEFAULT 'NETBANKING' ,
PRIMARY KEY (`idTransaction`, `type`) ,
INDEX `fk_NetBankingTransaction_Transaction1` (`idTransaction` ASC, `type` ASC) ,
CONSTRAINT `fk_NetBankingTransaction_Transaction1`
FOREIGN KEY (`idTransaction` , `type` )
REFERENCES `wah_schema`.`Transaction` (`idTransaction` , `type` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
Can someone please help me understand why am I getting this error ?
This one works for me:
/*drop table if exists `Transaction`;*/
CREATE TABLE IF NOT EXISTS `Transaction`
(
`idTransaction` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`idOrder` INT UNSIGNED NOT NULL ,
`type` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idTransaction`, `idOrder`, `type`),
KEY idx_idTransaction_type (idTransaction, `type`)
) ENGINE = InnoDB;
/*drop table if exists NetBankingTransaction;*/
CREATE TABLE IF NOT EXISTS `NetBankingTransaction`
(
`idTransaction` INT UNSIGNED NOT NULL ,
`bankCode` VARCHAR(45) NOT NULL ,
`type` VARCHAR(45) NOT NULL DEFAULT 'NETBANKING' ,
PRIMARY KEY (`idTransaction`, `type`),
CONSTRAINT `fk_NetBankingTransaction_Transaction1`
FOREIGN KEY (`idTransaction` , `type` )
REFERENCES `Transaction` (`idTransaction` , `type` )
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE = InnoDB;
idTransaction in NetBankingTransaction table has to be unsigned. And you need an index on (idTransaction, type) in the Transaction table. Your primary key does not cover the FK requires since it's over three columns.
P.S: You don't need this in NetBankingTransaction table
INDEX fk_NetBankingTransaction_Transaction1 (idTransaction ASC, type ASC)
You need a UNIQUE KEY in Transaction (idTransaction, type) to properly add that Foreign Key constraint.
idTransaction is a different type in each table. One is unsigned and the other is signed. Try making idTransaction unsigned in the second table.

Mysql Forward engineer errno 150

I'm modeling my db shema using MySQL Workbench CE EER modeler and now I'm stuck with mysql errno 150.
My sql code:
CREATE TABLE `myschema`.`Clients` (
`phone` VARCHAR(15) NOT NULL ,
`surname` VARCHAR(30) NOT NULL ,
`name` VARCHAR(30) NOT NULL ,
`middleName` VARCHAR(30) NULL ,
`discountCardNumber` BIGINT NULL ,
PRIMARY KEY (`phone`) ,
UNIQUE INDEX `discountCardNumber_UNIQUE` (`discountCardNumber` ASC) ,
CONSTRAINT `fk_Clients_DiscountCards1`
FOREIGN KEY (`discountCardNumber` )
REFERENCES `myschema`.`DiscountCards` (`cardNumber` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE `myschema`.`OrderStatuses` (
`statusID` INT NOT NULL AUTO_INCREMENT ,
`statusTitle` VARCHAR(45) NOT NULL ,
`statusDescription` VARCHAR(150) NULL ,
PRIMARY KEY (`statusID`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `myschema`.`WorkstationUsers` (
`userID` INT NOT NULL AUTO_INCREMENT ,
`login` VARCHAR(45) NOT NULL ,
`pass` VARCHAR(45) NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
`surname` VARCHAR(45) NOT NULL ,
`middleName` VARCHAR(45) NULL ,
PRIMARY KEY (`userID`) )
ENGINE = InnoDB;
CREATE TABLE `myschema`.`Orders` (
`orderID` BIGINT NOT NULL AUTO_INCREMENT ,
`registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`clientPhone` VARCHAR(15) NOT NULL ,
`shipmentAddress` VARCHAR(150) NOT NULL ,
`orderStatus` INT NOT NULL ,
`registrator` INT NOT NULL ,
PRIMARY KEY (`orderID`) ,
INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
CONSTRAINT `fk_Orders_Clients`
FOREIGN KEY (`clientPhone` )
REFERENCES `myschema`.`Clients` (`phone` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_OrderStatuses1`
FOREIGN KEY (`orderStatus` )
REFERENCES `myschema`.`OrderStatuses` (`statusID` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_WorkstationUsers1`
FOREIGN KEY (`registrator` )
REFERENCES `myschema`.`WorkstationUsers` (`userID` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
It fails in last statement (CREATE TABLE Orders).
I already checked:
Types of refferencing tables are equals
Indexes on all columns exists
Engine is InnoDB
Thanks for any help! Have a good day!
P.S. sorry for possible duplicate. I really can't find any problem in my code.
Some of fields are defined as NOT NULL, but you defined 'ON DELETE' action as 'SET NULL'.
Make these fields nullabe -
CREATE TABLE `myschema`.`Orders` (
`orderID` BIGINT NOT NULL AUTO_INCREMENT ,
`registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`clientPhone` VARCHAR(15) NULL, -- NOT NULL ,
`shipmentAddress` VARCHAR(150) NOT NULL ,
`orderStatus` INT NULL, -- NOT NULL ,
`registrator` INT NULL, -- NOT NULL ,
PRIMARY KEY (`orderID`) ,
INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
CONSTRAINT `fk_Orders_Clients`
FOREIGN KEY (`clientPhone` )
REFERENCES `myschema`.`Clients` (`phone` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_OrderStatuses1`
FOREIGN KEY (`orderStatus` )
REFERENCES `myschema`.`OrderStatuses` (`statusID` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_WorkstationUsers1`
FOREIGN KEY (`registrator` )
REFERENCES `myschema`.`WorkstationUsers` (`userID` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
In my case, the problem was solved by adding
DEFAULT CHARACTER SET = utf8
at the end of each create table
regards!

MySQL 1005 Error - What's wrong with the query?

I'm unable to see the issue with this CREATE TABLE sentences. I've double checked but can't find the error.
-- -----------------------------------------------------
-- Table `agentes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `agentes` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`codAgente` VARCHAR(8) NOT NULL ,
`clave` VARCHAR(8) NOT NULL ,
`nombre` VARCHAR(50) NOT NULL ,
`apellido1` VARCHAR(50) NOT NULL ,
`apellido2` VARCHAR(50) NOT NULL ,
`email` VARCHAR(100) NOT NULL ,
`usuarioDA` VARCHAR(45) NULL ,
`passDA` VARCHAR(100) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
COLLATE = latin1_spanish_ci;
-- -----------------------------------------------------
-- Table `cola`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `colas` (
`idCola` INT NOT NULL AUTO_INCREMENT ,
`cliente` INT NOT NULL ,
`nombre` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idCola`) ,
INDEX `fk_colas_clientes1` (`cliente` ASC) ,
CONSTRAINT `fk_colas_clientes1`
FOREIGN KEY (`cliente` )
REFERENCES `clientes` (`idCliente` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `relColaExt`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `relColaExt` (
`id` INT NOT NULL AUTO_INCREMENT ,
`codAgente` VARCHAR(8) NOT NULL ,
`cola` VARCHAR(45) NOT NULL ,
`prioridad` INT NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id` (`id` ASC) ,
INDEX `fk_relColaExt_agentes1` (`codAgente` ASC) ,
INDEX `fk_relColaExt_colas1` (`cola` ASC) ,
CONSTRAINT `fk_relColaExt_agentes1`
FOREIGN KEY (`codAgente` )
REFERENCES `agentes` (`codAgente` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_relColaExt_colas1`
FOREIGN KEY (`cola` )
REFERENCES `colas` (`nombre` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
It throws #1005 - Can't create table './centralita/relColaExt.frm' (errno: 150) creating the relColaExt table.
It's a Foreign Key issue but I'm not able to see what's wrong. Can someone help me, please?
According to this comment on MySQL manual page on Foreign keys, it may be due to different encoding of the VARCHAR columns. In table agentes you defined a collation but in relColaExt you did not.
Try using the same collation on every table (by defining it or by letting the default kick in).