If i execute my sql create table statement i get an errormessage that says:
Invalid default value for 'end'
CREATE TABLE IF NOT EXISTS `monkeybutler`.`Setuptimeslot` (
`id` INT NOT NULL,
`begin` TIMESTAMP NOT NULL,
`end` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
If I change the statement and comment out the "end"-column it works, but as soon as i try to create 2 columns of type TIMESTAMP it doesnt work anymore. How can I insert both columns without getting an error?
(This is the complete Create Table Statement and my mysql Version is 5.6.19)
Related
I need to create a database to store some logs which can occurs once per millisecond.
I've created the following table:
CREATE TABLE `log` (
`DataEvento` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`CodiceEvento` int(11) NOT NULL,
`IdApplicativo` int(11) NOT NULL,
PRIMARY KEY (`DataEvento`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
And a stored procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `ScriviEvento`(IN evt_id INT, IN app_id INT, IN evt_descr TEXT)
BEGIN
DECLARE timestamp_now TIMESTAMP(3) DEFAULT NOW(3);
INSERT INTO log (DataEvento, CodiceEvento, IdApplicativo) VALUES (timestamp_now, evt_id, app_id);
IF (LENGTH(evt_descr) > 0) THEN
INSERT INTO descrizionelog (DataEvento, DescrizioneEvento) VALUES (timestamp_now, evt_descr);
END IF;
END
Inserting manually some entries I get the correct timestamp with milliseconds but if I create a thread
with a Sleep(1) I got duplicate key error, same happens if I press execute button fast in workbench with
CALL(1, 0, '');
Is there a workaround to this (excluding using an auto-increment id), or am I doing something wrong?
You are doing something wrong by assuming that the timestamp is going to be unique for log records. That really doesn't make sense.
I'm not sure why you are opposed to an auto-increment solution. This would be the right approach:
CREATE TABLE `log` (
LogId int auto_increment primary key,
`DataEvento` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`CodiceEvento` int NOT NULL,
`IdApplicativo` int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
I'm encountering an unexpected situation with inserting/querying particular records in the following table during periods of high contention. I believe there is a race condition in the database.
CREATE TABLE `business_objects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`obj_id` varchar(255) DEFAULT NULL,
`obj_type` varchar(255) DEFAULT NULL,
`created_at` datetime(6) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_business_objects_on_obj_type_and_obj_id`
(`obj_type`,`obj_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The steps to reproduce are:
Check if record exists in table using this query
SELECT *
FROM business_objects
WHERE obj_type = 'Tip'
AND obj_id = '7616904'
If this query is null, attempt to create
INSERT INTO business_objects (obj_type, obj_id)
VALUES ('Tip', '7616904')
If another thread has already created a record with the same attributes this insert will fail and raise a uniqueness MySQL error. In this scenario, I catch the error and run the same query in step 1 to get the record.
SELECT *
FROM business_objects
WHERE obj_type = 'Tip'
AND obj_id = '7616904'
The query returns an empty result.
My expectation is that if the index uniqueness constraint is violated than the record should be committed to the table. What am I missing?
my table structure is
CREATE TABLE IF NOT EXISTS `emp` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(11) DEFAULT NULL,
`age` varchar(31) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
My query is :
INSERT INTO `emp` (`id`, `name`) VALUES ('1', 'prashant');
This is working with all the MYSQL versions below 5.7, but not working with MYSQL version 5.7.12-0ubuntu1
Getting error :
#1364 - Field 'age' doesn't have a default value
What is new in this version ??
Try it on mysql version below 5.7 ,you will see the difference.
Thanks :-)
It would be a huge surprise if this worked in any version of mysql at all. Copy paste this into sqlfiddle.com (mysql 5.6 or 5.5) and confirm for yourself.
age is defined as varchar(31) and not null. Thus your insert statement should have a value for that column. Or you should give it a default value. While you are at it, change it to a more appropriate data type.
CREATE TABLE IF NOT EXISTS `emp` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(11) DEFAULT NULL,
`age` int(3) NOT NULL default 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
Updated:
Thinking about this some more I think you have switched off Strict Mode in your older version of mysql
Strict mode controls how MySQL handles invalid or missing values in
data-change statements such as INSERT or UPDATE. A value can be
invalid for several reasons. For example, it might have the wrong data
type for the column, or it might be out of range. A value is missing
when a new row to be inserted does not contain a value for a non-NULL
column that has no explicit DEFAULT clause in its definition. (For a
NULL column, NULL is inserted if the value is missing.) Strict mode
also affects DDL statements such as CREATE TABLE.
So my original statement is wrong! With string mode off, the default for varchar is probably '' (not sure though never used strict mode off)
In your table age described as not null.
`age` varchar(31) NOT NULL
So, it is required field for insert.
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field.You have to give value for age also in your insert query because it cannot be null.For eg:-
insert into emp(`id`,`name`,`age`) values('1','rahul','26')
hope this helps!!.Comment for further query
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?
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.
In one of my tables, I have a column with some null values and some integer values. When I use MySQL Workbench to set a default value and non-null for that column, I get a truncation error.
When my colleague performs the same operation, his apply works and the null value are set to 0.
The only obvious difference is that he is running MySQL Server 5.6 and I am running 5.5.
An example table can be created with the following CREATE:
CREATE TABLE 'resource`.`test_table` (
`idtest_table` INT NOT NULL ,
`bad_column` BIGINT(20) NULL ,
PRIMARY KEY (`idtest_table`) );
Insert some values as follows:
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (1);
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (2);
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (3);
The command that is failing is:
ALTER TABLE `resource`.`test_table` CHANGE COLUMN `bad_column` `bad_column` BIGINT(20) NOT NULL DEFAULT 0 ;
with the following error:
ERROR 1265: Data truncated for column 'bad_column' at row 1
SQL Statement:
ALTER TABLE `resource`.`test_table` CHANGE COLUMN `bad_column` `bad_column` BIGINT(20) NOT NULL DEFAULT 0
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'test_table' already exists
SQL Statement:
CREATE TABLE `test_table` (
`idtest_table` int(11) NOT NULL,
`bad_column` bigint(20) DEFAULT NULL,
PRIMARY KEY (`idtest_table`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Is there a setting in the MySQL config that allows this? Is it the version?
The error is occurring because bad_columns has a value of NULL after the insert.
You are changing the column type to "not NULL", so what can the database do? It can generate an error. The default value doesn't apply to existing rows.
If you want to do this, then update the column first:
update resource.test_table
set bad_column = 0
where bad_column is null;