Createdatetime columns is populating as 0000-00-00 - mysql

I have created one table and inserting values in the table from a csv file using a python code .The createdatetime and updatedatetime columns are set to default CURRENT_TIMESTAMP.But when I am populating the data updatedatetime is populating the correct value but createdatetime is populating as 0000-00-00 00:00:00.
Here is my table definition:
CREATE TABLE `fico_details` (
`adf_contact_id` varchar(100) NOT NULL,
`sf_contact_id` varchar(100) DEFAULT NULL,
`Name` varchar(100) NOT NULL,
`BirthDate` date DEFAULT NULL,
`Address1` varchar(100) DEFAULT NULL,
`City` varchar(100) DEFAULT NULL,
`State` varchar(2) DEFAULT NULL,
`Zipcode` varchar(10) DEFAULT NULL,
`SSN` varchar(10) DEFAULT NULL,
`Address2` varchar(100) DEFAULT NULL,
`City2` varchar(100) DEFAULT NULL,
`State2` varchar(2) DEFAULT NULL,
`Zipcode2` varchar(10) DEFAULT NULL,
`Customerinput` varchar(10) DEFAULT NULL,
`AddrDiscrepancyFlg` varchar(10) DEFAULT NULL,
`Permid` varchar(10) DEFAULT NULL,
`score_date` varchar(10) DEFAULT NULL,
`reason_code_1` varchar(10) DEFAULT NULL,
`reason_code_2` varchar(10) DEFAULT NULL,
`reason_code_3` varchar(10) DEFAULT NULL,
`reason_code_4` varchar(10) DEFAULT NULL,
`CreateDateTime` TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,
`UpdateDateTime` TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
KEY `SSN` (`SSN`)
) ENGINE=InnoDB AUTO_INCREMENT=876800 DEFAULT CHARSET=utf8;
please help me to solve it.

I am working on same thing these days.
First of all I use timestamp as the datatype as apart from having datetime capabilities, it has MANY usable functions as a string as well as timestamp datatype.
I got the SAME issue as yours, what I did was alter my table was created, I would click on ALTER table using any Db tool (SQLYog in my case) and then Delete the 0000-00-00 00:00:00 value and uncheck the Not NULL check box.
This issue gets resolved EVERYTIME (whenever I create same type of table/columns)after this simple one step.
Hope this helps you too, let me know if anything still bothers you....!!

Related

MySQL Import CSV (Row import failed with error: ("Incorrect datetime value: '' for column 'Escalation_date' at row 1", 1292)

I've exported some rows from a table in order to simulate a Import from CSV into MySQL Table.
Rows are like this:
Number;Opened;Opened by;Source;Caller;Location;Category;Subcategory;Priority;Incident state;On hold reason;Assignment group;Support vendor name;ID vendor ticket;Escalation date;Parent Incident;Problem;Resolved;Resolved by;Closed;Closure CI;CI Closure Code;Sub CI;Sub Closure CI;Resolve time;Duration
INC0028837;2019-01-02 07:01:35;User1;Portal;User2;Location1;Category1;Sub_Cat1;3 - Moderate;Closed;;Assignment1;;;;;;2019-01-02 09:43;Resolved1;2019-01-05 10:00:00;;;;;9742;9742
I made some stored procedure that will update last columns when called.
My DB structure is as follows:
CREATE TABLE `incident_raw_data` (
`Number` varchar(12) NOT NULL,
`Opened` datetime DEFAULT NULL,
`Opened_by` varchar(45) DEFAULT NULL,
`Source` varchar(12) DEFAULT NULL,
`Caller` varchar(25) DEFAULT NULL,
`Location` varchar(25) DEFAULT NULL,
`Category` varchar(45) DEFAULT NULL,
`Subcategory` varchar(25) DEFAULT NULL,
`Priority` varchar(12) DEFAULT NULL,
`Incident_state` varchar(12) DEFAULT NULL,
`On_hold_reason` varchar(255) DEFAULT NULL,
`Assignment_group` varchar(45) DEFAULT NULL,
`Support_vendor_name` varchar(5) DEFAULT NULL,
`ID_vendor_ticket` varchar(25) DEFAULT NULL,
`Escalation_date` datetime DEFAULT NULL,
`Parent_Incident` varchar(12) DEFAULT NULL,
`Problem` varchar(255) DEFAULT NULL,
`Resolved` datetime DEFAULT NULL,
`Resolved_by` varchar(45) DEFAULT NULL,
`Closed` datetime DEFAULT NULL,
`Closure_CI` varchar(45) DEFAULT NULL,
`CI_Closure_Code` varchar(5) DEFAULT NULL,
`Sub_CI` varchar(12) DEFAULT NULL,
`Sub_Closure_CI` varchar(255) DEFAULT NULL,
`Resolve_time` int(11) NOT NULL,
`Duration` int(11) NOT NULL,
`Opened_weekeday` int(11) NOT NULL,
`Resolved_weekeday` int(11) NOT NULL,
`Closed_weekday` int(11) NOT NULL,
`Opened_hour` int(11) NOT NULL,
`Aging` int(11) NOT NULL,
`Gruppo_creatore` varchar(12) DEFAULT NULL,
`Gruppo_risolutore` varchar(12) DEFAULT NULL,
`Check_SSG/SSG` tinyint(1) DEFAULT NULL,
`Aging_<5` tinyint(1) DEFAULT NULL,
`Aging_hh` double DEFAULT NULL,
PRIMARY KEY (`Number`),
KEY `Resolve_time_index` (`Resolve_time`),
KEY `Duration_index` (`Duration`),
KEY `Opened_weekeday_index` (`Opened_weekeday`),
KEY `Resolved_weekeday_index` (`Resolved_weekeday`),
KEY `Closed_weekday_index` (`Closed_weekday`),
KEY `Opened_hour_index` (`Opened_hour`),
KEY `Aging_index` (`Aging`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Table B has the same structure in order not to tamper with original data and to perform some tests.
When I use the Import Wizard from Workbech, everything is correct except that when I've a EMPTY value in field Escalation_date, wizard tells me:
(Row import failed with error: ("Incorrect datetime value: '' for
column 'Escalation_date' at row 1", 1292)
I've disabled STRICT_MODE both global and session(SET ##global.sql_mode = '';), but still import fails.
I've also tried to modify the csv, putting '0000-00-00 00:00:00' in the corresponding column, but i get the same error.
So i failed to understand which is the correct way to import this csv file.
Where am i wrong?
I had this similar problem, discovering that MySQL DATETIME fields in particular don't like CSV empty values. I ended up working around it by replacing ,, with ,NULL, within the CSV file.

Not getting expected Output while trying to alter a column default value

Problem:
I have created a table with a given structure:
CREATE TABLE `X` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`MSISDN` varchar(25) DEFAULT NULL,
`subservice_id` varchar(25) DEFAULT NULL,
`SUB_START_DATE` datetime DEFAULT NULL,
`START_DATE` datetime DEFAULT NULL,
`END_DATE` datetime DEFAULT NULL,
`scheduler_renewal_status` varchar(25) NOT NULL DEFAULT 'scheduled',
`STATUS` varchar(20) DEFAULT NULL,
`LAST_RENEW_DATE` datetime DEFAULT NULL,
`LAST_CALL_DATE` datetime DEFAULT NULL,
`TRANSACTION_STATUS` varchar(15) DEFAULT NULL,
`CIRCLE` varchar(3) DEFAULT NULL,
`COUNTRY` varchar(3) DEFAULT NULL,
`LANGUAGE` varchar(3) DEFAULT NULL,
`price` float DEFAULT '0',
`CALL_ATTEMPTS` int(3) DEFAULT NULL,
`PRIMARY_ACT_MODE` varchar(25) DEFAULT NULL,
`SECONDARY_ACT_MODE` varchar(25) DEFAULT NULL,
`ERROR_MSG` varchar(25) DEFAULT NULL,
`SUB_TYPE` varchar(10) DEFAULT NULL,
`SUB_TIME_LEFT` int(3) DEFAULT '0',
`SUB_SERVICE_NAME` varchar(25) DEFAULT NULL,
`OPERATOR` varchar(25) DEFAULT NULL,
`GIFT_ID` varchar(25) DEFAULT NULL,
`service_id` varchar(25) DEFAULT NULL,
`RETRY_COUNT` int(2) NOT NULL DEFAULT '0',
`NEXT_RETRY_DATE` datetime DEFAULT NULL,
`CPID` int(11) DEFAULT NULL,
`SCHEDULER_RETRY_STATUS` varchar(25) DEFAULT 'scheduled',
`scheduler_statechanger_status` varchar(25) DEFAULT 'scheduled',
`scheduler_subs_msgs_status` varchar(25) DEFAULT 'scheduled',
`serviceName` varchar(25) DEFAULT NULL,
`transactionId` varchar(25) DEFAULT NULL,
`nextAction` varchar(25) DEFAULT NULL,
`topUpPrice` int(3) DEFAULT NULL,
`topUp_time_left` int(5) NOT NULL DEFAULT '0',
`autoRenew` varchar(10) DEFAULT NULL,
`option1` varchar(25) DEFAULT NULL,
`callBackRecvFlag` varchar(10) DEFAULT NULL,
`last_msg_date` date DEFAULT NULL,
`accountType` varchar(10) DEFAULT 'prepaid',
`optkey` varchar(20) DEFAULT NULL,
`successCallCount` int(3) DEFAULT '0',
PRIMARY KEY (`ID`),
UNIQUE KEY `UK_3` (`MSISDN`,`subservice_id`)
) ENGINE=InnoDB AUTO_INCREMENT=27793 DEFAULT CHARSET=utf8
I want to change the column SUB_START_DATE default type from NULL to TIMESTAMP.
Can anyone guide me the proper syntax for this
Solution:
I am running the following Command:
ALTER TABLE `X` MODIFY COLUMN SUB_START_DATE TIMESTAMP NOT NULL DEFAULT current_timestamp;
It is affecting the rows of the table.
But,if I want to have column datatype datetime only and change the default value to current_timestamp.
I ran the following command:
ALTER TABLE `X` MODIFY COLUMN SUB_START_DATE datetime DEFAULT current_timestamp;
OutputShown:
Error Code : 1067
Invalid default value for 'SUB_START_DATE'
In this case my SQL server does not allow to modify existing default constraint value. So to change the default value we need to delete the existing system generated or user generated default constraint. And after that default value can be set for a particular column.
Follow some steps :
List all existing default value constraints for columns.
Execute this system database procedure, it takes table name as a parameter. It returns list of all constrains for all columns within table.
execute [dbo].[sp_helpconstraint] 'table_name'
Drop existing default constraint for a column.
Syntax:
alter table 'table_name' drop constraint 'constraint_name'
Add new default value constraint for that column:
Syntax:
alter table 'table_name' add default 'current_timestamp' for 'SUB_START_DATE '

New MySQL version breaking queries

I moved a MySQL database from a server using MySQL 5.1.36 to a server using MySQL 5.7.11 and most of the INSERT statements now don't work. A previous developer created this app, and I'm trying to fix this query.
Here is an example:
INSERT INTO arts.tblmainrequest
select UUID(),'Aetna','Test','Test','555555','20160921','\\Base Folder\',
NULL,'testdoc.doc',NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
'20160920','test record','Y','400889', now()
Here is the create table script:
CREATE TABLE `tblmainrequest` (
`reqUUID` varchar(100) NOT NULL,
`company` varchar(200) DEFAULT NULL,
`patientlname` varchar(200) DEFAULT NULL,
`patientfname` varchar(200) DEFAULT NULL,
`MRN` char(6) DEFAULT NULL,
`DischargeDate` date DEFAULT NULL,
`BaseFolder` varchar(100) DEFAULT NULL,
`RecordsMailed` date DEFAULT NULL,
`OriginalReqFileName` varchar(100) DEFAULT NULL,
`SHHURCommitteeLetter` date DEFAULT NULL,
`SHHURCommitteeLetterFileName` varchar(100) DEFAULT NULL,
`MedRecsDeliveryMethod` varchar(50) DEFAULT NULL,
`TrackingNumber` varchar(50) DEFAULT NULL,
`PrelimFindings` date DEFAULT NULL,
`PrelimFindingsFileName` varchar(100) DEFAULT NULL,
`SHHPrelimResponseUM` date DEFAULT NULL,
`SHHPrelimResponseUMFileName` varchar(100) DEFAULT NULL,
`SHHPrelimResponseMR` date DEFAULT NULL,
`SHHPrelimResponseMRFileName` varchar(100) DEFAULT NULL,
`FinalReviewFindings` date DEFAULT NULL,
`FinalReviewFindingsFileName` varchar(100) DEFAULT NULL,
`FinalResponseSHH` date DEFAULT NULL,
`FinalResponseSHHFileName` varchar(100) DEFAULT NULL,
`HearingRequestSHH` date DEFAULT NULL,
`HearingRequestSHHFileName` varchar(100) DEFAULT NULL,
`HearingCorrespondenceUnnamed` date DEFAULT NULL,
`HearingCorrespondenceUnnamedFileName` varchar(100) DEFAULT NULL,
`HearingDateScheduled` date DEFAULT NULL,
`HearingDateScheduledFileName` varchar(100) DEFAULT NULL,
`HearingDecision` date DEFAULT NULL,
`HearingDecisionFileName` varchar(100) DEFAULT NULL,
`EmailedBusOffice` date DEFAULT NULL,
`Comments` longtext,
`Active` char(1) DEFAULT NULL,
`UpdatedBy` int(11) DEFAULT NULL,
`UpdatedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`reqUUID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You need to escape the second \ for the folder, \Base Folder****
Otherwise, mysql takes **** to as escape char and the following ' becomes the value of the field, and cause the following syntax are wrong.

MySQL - Querying Large table in MySQL -

This surely seems like poor design of the college_major table.
CREATE TABLE `college_majors` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date_time` datetime DEFAULT NULL,
`UNITID` varchar(255) DEFAULT NULL,
`CIPCODE` varchar(255) DEFAULT NULL,
`AWLEVEL` varchar(255) DEFAULT NULL,
`CTOTALT` varchar(255) DEFAULT NULL,
`CTOTALM` varchar(255) DEFAULT NULL,
`CTOTALW` varchar(255) DEFAULT NULL,
`CAIANT` varchar(255) DEFAULT NULL,
`CASIAT` varchar(255) DEFAULT NULL,
`CBKAAT` varchar(255) DEFAULT NULL,
`CHISPT` varchar(255) DEFAULT NULL,
`CNHPIT` varchar(255) DEFAULT NULL,
`CWHITT` varchar(255) DEFAULT NULL,
`C2MORT` varchar(255) DEFAULT NULL,
`CUNKNT` varchar(255) DEFAULT NULL,
`CNRALT` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=270167 DEFAULT CHARSET=utf8;
I can reduce this table to three columns - id, CIPCODE and UNITID. But the problem is even simple queries like *select * FROM college_majors* is taking too long to execute and sometime not even executing.
I increased the query execution to 6000.00 sec, but still the query won't run.
Any suggestion on how to improve the design, create a new table and insert the data from this table (college_majors).
Thanks,
A
If field 'codevalue' in college_majors_mapping is unique you can indexed it and increase join performance.

MySQL delete data without query?

I have a client that is describing an occurrence when they are adding new data from a web form. If the submitted data matches any existing database entries, the system deletes the existing entry.
I've checked the scripts and I don't see a DELETE query that would affect the related table. There are UPDATE queries, none of which affect a field in the DB indicating a deletion or marked as deleted.
Are there ways data could be lost without the execution of a DELETE query?
As asked for, here's the "create table" statement:
CREATE TABLE `msp_zip_codes`
(
`zip_code` varchar(5) DEFAULT NULL,
`city` varchar(35) DEFAULT NULL,
`state_prefix` varchar(2) DEFAULT NULL,
`county` varchar(45) DEFAULT NULL,
`area_code` varchar(45) DEFAULT NULL,
`CityType` varchar(1) DEFAULT NULL,
`CityAliasAbbreviation` varchar(13) DEFAULT NULL,
`CityAliasName` varchar(35) DEFAULT NULL,
`lat` decimal(18,6) DEFAULT NULL,
`lon` decimal(18,6) DEFAULT NULL,
`time_zone` varchar(7) DEFAULT NULL,
`Elevation` int(10) DEFAULT NULL,
`CountyFIPS` varchar(5) DEFAULT NULL,
`DayLightSaving` varchar(1) DEFAULT NULL,
`PreferredLastLineKey` varchar(25) DEFAULT NULL,
`ClassificationCode` varchar(2) DEFAULT NULL,
`MultiCounty` varchar(1) DEFAULT NULL,
`StateFIPS` varchar(2) DEFAULT NULL,
`CityStateKey` varchar(6) DEFAULT NULL,
`CityAliasCode` varchar(5) DEFAULT NULL,
`PrimaryRecord` varchar(50) DEFAULT NULL,
`CityMixedCase` varchar(50) DEFAULT NULL,
`CityAliasMixedCase` varchar(50) DEFAULT NULL,
KEY `AreaCode` (`area_code`),
KEY `CityAliasCode` (`CityAliasCode`),
KEY `CityStateKey` (`CityStateKey`),
KEY `ClassificationCode` (`ClassificationCode`),
KEY `PreferredLastLineKey` (`PreferredLastLineKey`),
KEY `ZipCode` (`zip_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
One of the possibilities is that the table has an UPDATE TRIGGER set which fires every time there's an update. Or the other one, there's an Event Scheduler running on the server.
Code to show all triggers on the database:
SELECT *
FROM INFORMATION_SCHEMA.TRIGGERS a
WHERE a.TRIGGER_SCHEMA LIKE CONCAT('%', 'databaseNameHERE', '%')
Code to show all events on the database:
SHOW EVENTS FROM databaseNameHERE;