Invalid default value null for timestamp - mysql

my database is mySql 5.6.48. Invalid default value for 'modify_time' Error occur during I excute the following SQL:
CREATE TABLE `check_wave_status` (
`com_uid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`modify_time` timestamp(0) DEFAULT NULL,
PRIMARY KEY (`com_uid`, `wave_uid`, `trade_uid`) USING BTREE,
INDEX `idx_modify_time`(`com_uid`, `modify_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
,My sql_mode = NO_ENGINE_SUBSTITUTION,How can I correct this error?

I think you mean NOT NULL instead of DEFAULT NULL. The first TIMESTAMP column is auto-populated.

Related

Mysql primary key association does not use index

I have a SQL problem.
When the user, organization, and organization are associated with the table, if the user status is used to filter the table, the index user_id cannot be used. If the condition is removed, the index user_id will be used.
Why is that?
MSYQL VERSION:5.7.32-log
Below is the specific SQL and table structure.
sql 1 :
SELECT DISTINCT USER
.user_id,
USER.NAME,
USER.nickname,
USER.position,
USER.first_line_id,
USER.second_line_id,
USER.org_id,
user.state
FROM
USER INNER JOIN user_org ON USER.user_id = user_org.user_id
INNER JOIN org ON user_org.org_id = org.id
WHERE
( org.end_time IS NULL OR org.end_time > NOW( ) )
AND USER.state = 1
AND ( full_id LIKE 'H_ROOT.00000001.00000002.50060182.50091585.50095679.50092012.10148706.50092333.10161139%' )
explain:user_id index not sufficient
sql2 :
SELECT DISTINCT USER
.user_id,
USER.NAME,
USER.nickname,
USER.position,
USER.first_line_id,
USER.second_line_id,
USER.org_id,
user.state
FROM
USER INNER JOIN user_org ON USER.user_id = user_org.user_id
INNER JOIN org ON user_org.org_id = org.id
WHERE
( org.end_time IS NULL OR org.end_time > NOW( ) )
-- AND USER.state = 1
AND ( full_id LIKE 'H_ROOT.00000001.00000002.50060182.50091585.50095679.50092012.10148706.50092333.10161139%' )
explain:user_id index sufficient
table count
USER:356007
ORG:142713
USER_ORG:353088
table schema
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `user_org`;
CREATE TABLE `user_org` (
`user_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`org_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`created_at` datetime(0) NULL DEFAULT NULL,
`updated_at` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`, `org_id`) USING BTREE,
INDEX `org_id`(`org_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '工号',
`name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '姓名',
`email` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`email_private` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '个人邮箱',
`mobile` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号',
`position` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '岗位',
`state` tinyint(4) NOT NULL DEFAULT 1 COMMENT '状态(1:启用;0:禁用)',
`org_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '部门编码',
PRIMARY KEY (`user_id`) USING BTREE,
INDEX `user_email_index`(`email`) USING BTREE,
INDEX `user_mobile_index`(`mobile`) USING BTREE,
INDEX `user_name_index`(`name`) USING BTREE,
INDEX `user_org_id_index`(`org_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `org`;
CREATE TABLE `org` (
`id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`name` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`parent_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`full_id` varchar(512) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`end_time` datetime(0) NULL DEFAULT NULL COMMENT '部门过期时间',
`created_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`updated_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '更新时间',
`customer_code` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`org_type` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '组织类别',
`state` tinyint(4) NULL DEFAULT NULL COMMENT ' 1 正常 2 停用\r\n冗余目前还是用endtime来识别有效性',
PRIMARY KEY (`id`) USING BTREE,
INDEX `org_full_id_index`(`full_id`(255)) USING BTREE,
INDEX `org_name_index`(`name`(255)) USING BTREE,
INDEX `org_parent_id_index`(`parent_id`) USING BTREE,
INDEX `end_time`(`end_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '组织表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
STRAIGHT_JOIN not sufficient
STRAIGHT_JOIN not sufficien v2
FORCE INDEX not sufficient
FORCE INDEX not sufficient v2
What version of MySQL are you using? There have been Optimization and Index-limit changes that are relevant to your query and schema.
If you set end_time to some date in the distant future, you could avoid the OR by changing to simply end_time > NOW(). (OR used to be bad for performance.)
The indexes you have for the many-to-many table (user_org) are optimal.
Index "prefixing" (full_id(255)) is problematic. It can be eliminated in newer versions. INDEX(full_id) would let the query start with `full_id LIKE '...%' be much more usable.
Perhaps you should change to utf8mb4? It is needed for the more obscure Chinese characters, plus some Emoji.
This index may be picked by the Optimizer; suggest you add it:
USER: INDEX(state, user_id)
If you don't actually need user.name to be a full 256 characters, lower it to 255. That way you can eliminate the prefixing:
USER: INDEX(name)
See other options here: http://mysql.rjweb.org/doc.php/limits#767_limit_in_innodb_indexes

Sql constraint error references other column

I don't know a lot of mysql and have an error in my sql script. Currently running mysql 8.0.24. Does anyone know what might be the problem?
Error:
https://prnt.sc/226xk5x
Sql:
-- Dumping structure for table gtav_rp2._vehicle
CREATE TABLE IF NOT EXISTS `_vehicle` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cid` int(11) unsigned NOT NULL,
`vin` varchar(50) NOT NULL DEFAULT '',
`type` varchar(50) NOT NULL DEFAULT '',
`size` int(11) NOT NULL,
`plate` varchar(50) NOT NULL DEFAULT '',
`model` varchar(50) NOT NULL DEFAULT '',
`name` varchar(50) DEFAULT NULL,
`garage` varchar(59) DEFAULT NULL,
`state` varchar(50) DEFAULT NULL,
`appearance` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('appearance')),
`mods` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('mods')),
`data` longtext DEFAULT NULL,
`damage` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('damage')),
`degredation` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('degredation')),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Dumping data for table gtav_rp2._vehicle: ~0 rows (approximately)
/*!40000 ALTER TABLE `_vehicle` DISABLE KEYS */;
/*!40000 ALTER TABLE `_vehicle` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(#OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(#OLD_FOREIGN_KEY_CHECKS IS NULL, 1, #OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER
_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
Your CHECK constraint does not reference the column. Or any column, actually. By using single-quotes, you're using a string literal, not a column name.
`appearance` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL
CHECK (json_valid('appearance')),
I assume this is meant to be:
`appearance` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL
CHECK (json_valid(`appearance`)),
Use the right type of quotes for SQL identifiers, not string literals.
In addition, this CHECK constraint would be unnecessary if you used the JSON data type instead of LONGTEXT. The JSON data type already enforces that the content of the column must be valid JSON format.
MySQL's JSON data type already uses utf8mb4 character set and utf8mb4_bin collation.
So your column definition could be simply as follows:
`appearance` JSON

MySQL 8.0.15 Dam slow select query

Motto of the query is very simple, to find out the last entry on a foreign key column.
the pseudo code I can say is
select vehicleid , last_journey_point , last_journey_time from journeyTable.
here is my SQL statement
-- loconumber is a indexed column
-- journeyserla is a autonumber primary key int(11)
-- the table locojourney contains 400,000 records
-- the below block of code executes in 19 secs
with LocomotiveLastRun AS(
-- this block of code runs in 0.016 sec
SELECT locojourney.loconumber , MAX(locojourney.journeyserla) as lastrunid
FROM locojourney GROUP BY loconumber)
SELECT locojourney.CurrentCombiners , locojourney.JourneySerla ,
locojourney.From_RunPoint , locojourney.NEXT_RunPoint
FROM LocomotiveLastRun FORCE INDEX(lastrunid)
JOIN locojourney FORCE INDEX(PRIMARY) ON x.lastrunid = locojourney.journeyserla
WHERE locojourney.ishoc = 'n'
the EXPLAIN command shows a derived table which is using no index and using where and type ALL
This is the table definition:
-- SHOW CREATE TABLE locojourney
CREATE TABLE `locojourney` (
`trainID` smallint(5) NOT NULL,
`LocoNumber` varchar(5) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`CurrentLocoBase` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL,
`CurrentDuedate` date DEFAULT NULL,
`LocoConsist` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`CurrentLocoDomain` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL,
`DomainChange` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`FEDR` enum('N','Y') CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT 'N',
`LADR` enum('N','Y') CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT 'N',
`ISBANKER` enum('N','Y') CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT 'N',
`TrainName` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`WithOutLoad` enum('N','Y') CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'N',
`runRoute` varchar(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`From_RunPoint` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`From_RunTime` datetime NOT NULL,
`NEXT_RunPoint` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`NEXT_RunTime` datetime NOT NULL,
`Affects_Outage` enum('N','Y') CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT 'N',
`Affects_Mileage` enum('N','Y') CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT 'N',
`GroundDistance` double(5,2) DEFAULT '0.00',
`SHGallowance` int(11) DEFAULT '0',
`Outage` double(5,4) DEFAULT '0.0000',
`UnderServiceType` enum('FHT','CHG','DEP','MIX','DETN') CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'FHT',
`SubServiceHead` varchar(25) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'RUN',
`IShoc` enum('N','Y') CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT 'N',
`CurrentCombiners` varchar(28) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL,
`RunSetSerla` varchar(25) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL,
`JourneySerla` int(11) NOT NULL AUTO_INCREMENT,
`NominationSerla` varchar(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL,
`Traction` enum('DSL','AC') CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'DSL',
`Trainload` smallint(4) NOT NULL DEFAULT '0',
`LeadAssist` enum('Y','N') CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'N',
`DEO` varchar(100) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL,
`DEOtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`JourneySerla`),
KEY `trainID` (`trainID`) USING BTREE,
KEY `routesection_idx` (`runRoute`) USING BTREE,
KEY `loconumber_idx` (`LocoNumber`) USING BTREE,
KEY `runsetserla_idx` (`RunSetSerla`) USING BTREE,
KEY `subservicehead_idx` (`SubServiceHead`) USING BTREE,
CONSTRAINT `locojourney_ibfk_1` FOREIGN KEY (`SubServiceHead`) REFERENCES `ineffective` (`IneffectiveHead`) ON UPDATE CASCADE,
CONSTRAINT `locojourney_ibfk_3` FOREIGN KEY (`runRoute`) REFERENCES `routesections` (`Sectionname`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `loconumber_fk` FOREIGN KEY (`LocoNumber`) REFERENCES `lococontainer` (`LocoNumber`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=345719 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
with LocomotiveLastRun AS(
-- this block of code runs in 0.016 sec
SELECT locojourney.loconumber , MAX(locojourney.journeyserla) as lastrunid
FROM locojourney
GROUP BY loconumber)
Why is this CTE subquery fast? Because your table already has an index on (loconumber, journeyserla). (InnoDb automatically appends the primary key to every index.) This query can be satisfied with a loose index scan on that index, and those are fast.
Now for your main query:
Get rid of FORCE INDEX(). Don't even dream of using that unless you have at least a decade of SQL experience or you have read the source code for the InnoDB indexing stuff in MySQL. Notably, it's completely useless on the CTE because CTEs don't have indexes.
For clarity put your main (detail) table first and your CTE second.
For clarity recast the JOIN as a WHERE...IN...
Those three suggestions give us this:
WITH LocomotiveLastRun AS (...)
SELECT locojourney.CurrentCombiners , locojourney.JourneySerla ,
locojourney.From_RunPoint , locojourney.NEXT_RunPoint
FROM locojourney
WHERE journeyserla IN (SELECT lastrunid FROM LocomotiveLastRun)
AND locojourney.ishoc = 'n'
Now, it's plain what index can help this query.
An index on (ishoc) will help a bit. (It's actually an index, because InnoDB, on (ishoc, journeyserla) so it helps with both WHERE conditions.) The query planner uses BTREE random access to find the first index row with the ishoc value 'n', then scans the values of the primary key to match them with the IN clause.
Instead of that index, a compound index that covers the query will help even more. Such a covering index helps especially because each row of your table is large, with many columns. That index mentions the columns in the WHERE clause and those you want to select, like this:
(ishoc, journeyserla, CurrentCombiners, From_RunPoint, NEXT_RunPoint)
The query planner can satisfy your query entirely from the index, which saves on disk reading time to satisfy the query. If you use your query a lot, this index is a good idea. But, it does consume disk space and slow down INSERT and UPDATE operations a bit.
Read https://use-the-index-luke.com/
Give this a try:
SELECT lj.CurrentCombiners , lj.JourneySerla , lj.From_RunPoint , lj.NEXT_RunPoint
FROM ( SELECT MAX(journeyserla) as lastrunid
FROM locojourney
GROUP BY loconumber
) AS llr
JOIN locojourney AS lj ON llr.lastrunid = lj.journeyserla
WHERE lj.ishoc = 'n'
(time it and provide EXPLAIN for it)

#1075 MySQL Error

So i am just a beginner in all this php stuff. I know just the basics, and when i setting up the settings for my new table, I met the problem #1075. Before, i created one, almost similar to this one, and i don't see the differenc. Can you say me where is the problem and explain what is happening?
CREATE TABLE `try`.`testing` ( `id` INT NOT NULL AUTO_INCREMENT , `date` DATE NOT NULL , `text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ) ENGINE = MyISAM;
here is the code of my SQL Preview. I use phpMyAdmin, obviously.
Please, help me.
Thank, you)
Try this
CREATE TABLE `testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MYISAM ;
You have to declare your AUTO_INCREMENT field as a primary key or a key. So you have to add PRIMARY KEY (id) or KEY (id) to your CREATE TABLE statement:
CREATE TABLE `try`.`testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL ,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) -- as primary key
KEY (`id`) -- or as key
) ENGINE = MyISAM;
Please also check:
https://stackoverflow.com/a/8114994/3647441
https://stackoverflow.com/a/14087703/3647441
For an autoincrement field you should have some sort of index associated with it. eg: primary key which is missing
Try This.
CREATE TABLE `try`.`testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL ,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
KEY (`id`)
) ENGINE = MyISAM;
https://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html

Is there any way to make a UNIQUE index case insensitive in Mysql 5.1.x ?

If so - What must change in this table ?
CREATE TABLE contestants
(
idContestants int(10) unsigned NOT NULL AUTO_INCREMENT,
idEvent int(10) unsigned NOT NULL,
ContestantName varchar(50) DEFAULT NULL,
PRIMARY KEY (idContestants),
UNIQUE KEY Index_UniqueName (idEvent,ContestantName),
)
ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
If you mean case sensitive then:
ALTER TABLE `contestants` CHANGE `ContestantName` `ContestantName` VARCHAR( 50 )
CHARACTER SET latin1 COLLATE latin1_bin NULL DEFAULT NULL
If you mean case insensitive then:
ALTER TABLE `contestants` CHANGE `ContestantName` `ContestantName` VARCHAR( 50 )
CHARACTER SET latin1 COLLATE latin1_general_ci NULL DEFAULT NULL
For table level do (for case insensitive):
ALTER TABLE `contestants` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci
Note that table level only affects new columns.
For database level do (for case insensitive):
ALTER DATABASE `database_name` CHARACTER SET latin1 COLLATE latin1_general_ci
Note that database level only affect new tables.
Yes, use a case-insensitive collation on the columns involved.
MySQL Manual :: Column Character Set and Collation
This worked for me in Mysql 5.5
ALTER TABLE `contestants` MODIFY
`ContestantName` VARCHAR(50)
CHARACTER SET latin1
COLLATE latin1_bin;