Create Table mySql syntax error (working on other versions) - mysql

When creating a table in MySQL Version 5.7.21 on a Linux server I get the following error message:
" 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 arbeitstageprowocheab date DEFAULT NULL,
urlaubstageprojahr int(3) DEFAULT '"
when creating the following table:
CREATE TABLE arbeitsvertragsdaten (
personalnummer int(11) NOT NULL,
mailadresse varchar(50) DEFAULT NULL,
handynummerxenium varchar(50) DEFAULT NULL,
status varchar(20) DEFAULT NULL,
position varchar(50) DEFAULT NULL,
probezeitende date DEFAULT NULL,
wochenarbeitszeit int(11) DEFAULT NULL,
wochenarbeitszeitab date DEFAULT NULL,
teilzeitbefristet tinyint(1) DEFAULT NULL,
teilzeitende date DEFAULT NULL,
ueberstundenquotient double DEFAULT NULL,
arbeitstageprowoche int(11) DEFAULT NULL,
arbeitstageprowocheab date DEFAULT NULL,
urlaubstageprojahr int(3) DEFAULT NULL,
urlaubstageprojahrab date DEFAULT NULL,
mutterschutzbeginn date DEFAULT NULL,
mutterschutzende date DEFAULT NULL,
elternzeitbeginn date DEFAULT NULL,
elternzeitende date DEFAULT NULL);
It did work on my local machine for development on Version 8.0.16 (Community Server)
Any ideas whats wrong with my syntax?

There is syntax issue.
CREATE TABLE `arbeitsvertragsdaten` (
`personalnummer` int(11) NOT NULL,
`mailadresse` varchar(50) DEFAULT NULL,
`handynummerxenium` varchar(50) DEFAULT NULL,
`status` varchar(20) DEFAULT NULL,
`position` varchar(50) DEFAULT NULL,
`probezeitende` date DEFAULT NULL,
`wochenarbeitszeit` int(11) DEFAULT NULL,
`wochenarbeitszeitab` date DEFAULT NULL,
`teilzeitbefristet` tinyint(1) DEFAULT NULL,
`teilzeitende` date DEFAULT NULL,
`ueberstundenquotient` double DEFAULT NULL,
`arbeitstageprowoche` int(11) DEFAULT NULL,
`arbeitstageprowocheab` date DEFAULT NULL,
`urlaubstageprojahr` int(3) DEFAULT NULL,
`urlaubstageprojahrab` date DEFAULT NULL,
`mutterschutzbeginn` date DEFAULT NULL,
`mutterschutzende` date DEFAULT NULL,
`elternzeitbeginn` date DEFAULT NULL,
`elternzeitende` date DEFAULT NULL
); ----- here ) is missing.

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

#1067 - Invalid default value for 'LastUpdated'

I made my ER diagram via MySQL WorkBench and exported it as a sql. Below is code for one table.
CREATE TABLE IF NOT EXISTS `xxx`.`Agent` (
`idAgent` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(100) NOT NULL,
`RegistrationNumber` VARCHAR(45) NOT NULL,
`RegistrationDate` DATE NULL,
`DateOfDealStart` DATE NULL,
`AddressLine1` VARCHAR(100) NULL,
`AddressLine2` VARCHAR(100) NULL,
`Country` VARCHAR(45) NULL,
`CurrentStatus` TINYINT(1) NOT NULL,
`DateCreated` TIMESTAMP NOT NULL,
`LastUpdated` TIMESTAMP NOT NULL,
PRIMARY KEY (`idAgent`))
ENGINE = InnoDB;
However when I am trying to import the script into MySQL via PhpMyAdmin, it says below.
#1067 - Invalid default value for 'LastUpdated'
I have not provided any default value, so how can it could be "Invalid"?