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

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 '

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.

Createdatetime columns is populating as 0000-00-00

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....!!

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 issue with altering column to add default

I have a table named Users with a column call created. Whenever a record is created I want to add the datetime.
Users Table:
CREATE TABLE `Users` (
`userId` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fullName` varchar(50) DEFAULT NULL,
`firstName` varchar(25) NOT NULL DEFAULT '',
`lastName` varchar(25) NOT NULL DEFAULT '',
`address` varchar(50) NOT NULL DEFAULT '',
`city` varchar(25) DEFAULT NULL,
`state` char(2) DEFAULT NULL,
`zipCode` varchar(25) DEFAULT NULL,
`email` varchar(50) NOT NULL DEFAULT '',
`cellPhone` varchar(15) DEFAULT NULL,
`birthDate` date NOT NULL,
`creditCard` varchar(250) NOT NULL DEFAULT '',
`subscriptionStarted` date NOT NULL,
`subscriptionEnded` date NOT NULL,
`basicPlan` tinyint(1) DEFAULT NULL,
`standardPlan` tinyint(1) DEFAULT NULL,
`premiumPlan` tinyint(1) DEFAULT NULL,
`staff` tinyint(1) DEFAULT NULL,
`admin` tinyint(1) DEFAULT NULL,
`systemAdmin` tinyint(1) DEFAULT NULL,
`edited` datetime DEFAULT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=latin1;
now i added this extra query to make my created field get the current datetime when a new record is created.
ALTER TABLE Users
ALTER COLUMN created SET DEFAULT CURRENT_TIMESTAMP
The problem is that I get the following error when running the alter table query
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'CURRENT_TIMESTAMP' at line 2
Your syntax is slightly off, I think you have to specify the column to change:
ALTER TABLE Users CHANGE created created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
See this sample SQL Fiddle for an example.

mysqldiff tool drops indexes and adds duplicates and causes errors

I'm using the mysqldiff tool (v1.5.3) from MySQL to synchronize the schema between two databases. The databases should have the same schema, but would like to produce a file that only has the ALTER TABLE differences between them so it can (after thorough testing) be run in production.
The problem is that the mysqldiff tool produces some weirdness. For example:
ALTER TABLE `database1`.`ACCOUNT_TRANSACTIONS`
DROP INDEX MATCHING_TRANSACTION_ID,
DROP INDEX PAYEE_ID,
ADD INDEX CHECK_NUMBER (ACCOUNT_ID),
ADD INDEX PAYEE_ID (PAYEE_ID),
ADD INDEX CHECK_NUMBER (PAYEE_ID,CHECK_NUMBER);
It seems to not only drop the indexes, but tries to recreate the new index through two statements that produce an error. The final schema should have CHECK_NUMBER (ACCOUNT_ID, PAYEE_ID, CHECK_NUMBER) as the index.
What might be causing this?
Final version:
CREATE TABLE `ACCOUNT_TRANSACTIONS` (
`TRANSACTION_ID` int(12) NOT NULL AUTO_INCREMENT,
`ACCOUNT_ID` int(1) DEFAULT NULL,
`REF_NUMBER` varchar(15) DEFAULT NULL,
`PAYEE_ID` int(6) DEFAULT NULL,
`ACCOUNT_CHART_ID` int(12) DEFAULT NULL,
`TRANSACTION_AMOUNT` decimal(10,2) DEFAULT NULL,
`TRANSACTION_DATE` date DEFAULT NULL,
`MEMO` varchar(100) DEFAULT NULL,
`ACCOUNT_BALANCE` decimal(10,2) DEFAULT NULL,
`DEBIT_OR_CREDIT` char(1) DEFAULT 'D',
`MATCHING_TRANSACTION_ID` int(11) DEFAULT '0',
`RECONCILED_DATE` date DEFAULT NULL,
`RECONCILED_EMPLOYEE_ID` int(12) DEFAULT NULL,
`TIME_UPDATED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`CHECK_NUMBER` bigint(20) unsigned DEFAULT NULL,
`STATEMENT_ID` int(12) DEFAULT NULL,
PRIMARY KEY (`TRANSACTION_ID`),
KEY `PAYEE_ID` (`PAYEE_ID`),
KEY `CHECK_NUMBER` (`CHECK_NUMBER`,`ACCOUNT_ID`,`PAYEE_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=xxx DEFAULT CHARSET=latin1
Original version (needs to be altered to be final version)
CREATE TABLE `ACCOUNT_TRANSACTIONS` (
`TRANSACTION_ID` int(12) NOT NULL AUTO_INCREMENT,
`ACCOUNT_ID` int(1) DEFAULT NULL,
`REF_NUMBER` varchar(15) DEFAULT NULL,
`PAYEE_ID` int(6) DEFAULT NULL,
`ACCOUNT_CHART_ID` int(12) DEFAULT NULL,
`TRANSACTION_AMOUNT` decimal(10,2) DEFAULT NULL,
`TRANSACTION_DATE` date DEFAULT NULL,
`MEMO` varchar(100) DEFAULT NULL,
`ACCOUNT_BALANCE` decimal(10,2) DEFAULT NULL,
`DEBIT_OR_CREDIT` char(1) DEFAULT 'D',
`MATCHING_TRANSACTION_ID` int(11) DEFAULT '0',
`RECONCILED_DATE` date DEFAULT NULL,
`RECONCILED_EMPLOYEE_ID` int(12) DEFAULT NULL,
`TIME_UPDATED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`CHECK_NUMBER` bigint(20) unsigned DEFAULT NULL,
`STATEMENT_ID` int(12) DEFAULT NULL,
PRIMARY KEY (`TRANSACTION_ID`),
KEY `PAYEE_ID` (`PAYEE_ID`),
KEY `MATCHING_TRANSACTION_ID` (`MATCHING_TRANSACTION_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=xxx DEFAULT CHARSET=latin1