How to fix "A comma or a closing bracket was expected"? - mysql

I get "A comma or a closing bracket was expected" error when trying to execute this SQL code:
CREATE TABLE `player_vehicles` (
`#` int(11) NOT NULL,
`steam` varchar(50) DEFAULT NULL,
`citizenid` varchar(50) DEFAULT NULL,
`vehicle` varchar(50) DEFAULT NULL,
`hash` varchar(50) DEFAULT NULL,
`mods` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
`plate` varchar(50) NOT NULL,
`fakeplate` varchar(50) DEFAULT NULL,
`garage` varchar(50) DEFAULT NULL,
`fuel` int(11) DEFAULT 100,
`engine` float DEFAULT 1000,
`body` float DEFAULT 1000,
`state` int(11) DEFAULT 1,
`depotprice` int(11) NOT NULL DEFAULT 0,
`drivingdistance` int(50) DEFAULT NULL,
`status` text DEFAULT NULL,
`health` longtext NOT NULL '[{"value":100,"part":"electronics"},{"value":100,"part":"fuelinjector"},{"value":100,"part":"brakes"},
{"value":100,"part":"radiator"},{"value":100,"part":"driveshaft"},{"value":100,"part":"transmission"},{"value":100,"part":"clutch"}]'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Error message:
A comma or a closing bracket was expected. (near "'[{"value":100,"part":"electronics"},{"value":100,"part":"fuelinjector"},{"value":100,"part":"brakes"},{"value":100,"part":"radiator"},{"value":100,"part":"driveshaft"},{"value":100,"part":"transmission"},{"value":100,"part":"clutch"}]'" at position 816)
Can someone please help, I can't figure this out?

It seems like you want to use a string as a default for your longtext column, but you are missing the DEFAULT keyword.
But it won't work anyway. https://dev.mysql.com/doc/refman/8.0/en/blob.html says:
BLOB and TEXT columns cannot have DEFAULT values.
If I add the DEFAULT keyword to your example, and try it in the MySQL client, I get this error:
ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'health' can't have a default value
MariaDB, a fork of MySQL, added the capability to add a DEFAULT value to a BLOB or TEXT column in MariaDB 10.2.1, but this is not supported in MySQL.

If you need to assign some default value to LONGTEXT column then use BEFORE INSERT trigger.
CREATE TABLE test (id INT, txt LONGTEXT NOT NULL);
CREATE TRIGGER tr_bi_setdef
BEFORE INSERT ON test
FOR EACH ROW
SET NEW.txt = COALESCE(NEW.txt, '["default value"]');
INSERT INTO test VALUES (1, '["specified value"]'), (2, NULL);
INSERT INTO test (id) VALUES (3);
SELECT * FROM test;
id | txt
-: | :------------------
1 | ["specified value"]
2 | ["default value"]
3 | ["default value"]
db<>fiddle here

Related

Unable to change table column type in MySQL 5.7

I have this table in MySQL 5.7 database and rest of SQL statements:
create table m_workflow
(
id bigint(10) auto_increment primary key,
course bigint(10) default 0 not null,
name varchar(255) default '' not null,
intro longtext not null,
introformat smallint(4) default 0 not null,
recommendationstitle varchar(255) null,
recommendintro longtext null,
recommendintroformat bigint(10) default 1 null,
timeopen bigint(10) default 0 not null,
timeclose bigint(10) default 0 not null,
timelimit bigint(10) default 0 not null,
timecreated bigint(10) default 0 not null,
timemodified bigint(10) default 0 not null
)
comment 'The settings for each workflow.';
create index m_work_cou_ix on m_workflow (course);
INSERT INTO m_workflow (course, name, intro, introformat) VALUES (1, 'aaa', 'aaaa', 1);
INSERT INTO m_workflow (course, name, intro, introformat) VALUES (1, 'aaa', 'aaaa', 1);
ALTER TABLE m_workflow MODIFY COLUMN recommendationstitle LONGTEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL after introformat;
The poblem here is that it fails on ALTER column statement with error:
Data truncated for column 'recommendationstitle' at row 1
If I add some text into recommendationstitle fields than conversion goes OK.
Is this to be expected or am I doing something wrong?
Here is the SQL fiddle - http://www.sqlfiddle.com/#!9/10934c/2
You are trying to modify it from NULL to NOT NULL.
Currently existing records with NULL values causing the issue.

Why I can not create a table in this way?

Why I can not create a table in this way, I get an error?
CREATE TABLE `dynamusic_album` (
`id` VARCHAR(32) NOT NULL,
`title` VARCHAR(100) NULL DEFAULT NULL,
`cover` VARCHAR(100) NULL DEFAULT NULL,
`artist` VARCHAR(32) NULL DEFAULT NULL,
`published` DATETIME NULL DEFAULT NULL,
`description` MEDIUMTEXT NULL,
`genre` INT(11) NULL DEFAULT NULL,
`rating` DOUBLE(11) NOT NULL DEFAULT '1.0',
PRIMARY KEY (`id`)
)
My Error:
ERROR 1064: 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 ') NOT NULL DEFAULT '1.0',
PRIMARY KEY (`id`)
)' at line 9
SQL version = 5.1.15
While specifying numeric datatypes, especially handling decimal point values, you need to specify two arguments. So, DOUBLE(11) needs to be changed.
CREATE TABLE `dynamusic_album` (
`id` VARCHAR(32) NOT NULL,
`title` VARCHAR(100) NULL DEFAULT NULL,
`cover` VARCHAR(100) NULL DEFAULT NULL,
`artist` VARCHAR(32) NULL DEFAULT NULL,
`published` DATETIME NULL DEFAULT NULL,
`description` MEDIUMTEXT NULL,
`genre` INT(11) NULL DEFAULT NULL,
-- assuming you need at most 2 decimal places for rating
`rating` DOUBLE(11,2) NOT NULL DEFAULT '1.0',
PRIMARY KEY (`id`)
)
Refer: https://dev.mysql.com/doc/refman/8.0/en/floating-point-types.html
MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE
PRECISION(M,D). Here, (M,D) means than values can be stored with up to
M digits in total, of which D digits may be after the decimal point.
For example, a column defined as FLOAT(7,4) will look like -999.9999
when displayed. MySQL performs rounding when storing values, so if you
insert 999.00009 into a FLOAT(7,4) column, the approximate result is
999.0001.
Just use DOUBLE instead of DOUBLE(11). AFAICS, MySQL does not know a DOUBLE(x), only DOUBLE(M,D) (see MySQL docs)

error in trigger creation in phpmyadmin

I created the very simple triger and I think syntex is also correct:
CREATE TRIGGER trig1 after INSERT ON urlcontent for each row
BEGIN
insert into userpost(userid,url,hash) values (userid,url,hash);
END;
gives error:
#1064 - 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 '' at line 3
Structure of both table:
CREATE TABLE urlcontent (
userid text NOT NULL,
url varchar(255) NOT NULL,
`desc` varchar(2048) NOT NULL,
preview varchar(255) NOT NULL,
img_url varchar(128) NOT NULL,
title varchar(128) NOT NULL,
`hash` varchar(128) NOT NULL,
rate varchar(20) DEFAULT NULL,
`time` varchar(64) DEFAULT NULL,
sentiment varchar(32) DEFAULT NULL,
`subject` varchar(64) DEFAULT NULL,
PRIMARY KEY (`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table 'userpost'
--
CREATE TABLE userpost (
userid varchar(40) NOT NULL DEFAULT '',
url varchar(255) DEFAULT NULL,
`desc` varchar(2048) DEFAULT NULL,
preview varchar(255) DEFAULT NULL,
img_url varchar(128) DEFAULT NULL,
title varchar(128) DEFAULT NULL,
`hash` varchar(128) NOT NULL DEFAULT '',
rate varchar(16) DEFAULT NULL,
`time` varchar(64) DEFAULT NULL,
pcount varchar(16) DEFAULT NULL,
ncount varchar(16) DEFAULT NULL,
isset varchar(16) DEFAULT NULL,
sentiment varchar(32) DEFAULT NULL,
`subject` varchar(64) DEFAULT NULL,
PRIMARY KEY (userid,`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
hash and url are key in userpost table/
Structure of both table
Your specific error is likely due to the delimiter being defined as ;.
If you change the delimiter to | (in the box below the query editor) like in the following image it will work:
This helps the trigger to be created without stoping the query at the first ;.
This would work, but to insert the values from urlcontent into userpost you have to add the keywords NEW before the values. This tells the trigger that the values you want to insert in userpost are the ones that were just inserted into urlcontent:
CREATE TRIGGER trig1 AFTER INSERT ON urlcontent
FOR EACH ROW
BEGIN
insert into userpost(userid,url,hash) values (NEW.userid,NEW.url,NEW.hash);
END;
|

#1366 - Incorrect integer value:MYsql

hi every one i have a problem in mysql
my table is
CREATE TABLE IF NOT EXISTS `contactform` (
`contact_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL,
`addition` varchar(50) NOT NULL,
`surname` varchar(50) NOT NULL,
`Address` varchar(200) NOT NULL,
`postalcode` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`phone` varchar(20) NOT NULL,
`emailaddress` varchar(30) NOT NULL,
`dob` varchar(50) NOT NULL,
`howtoknow` varchar(50) NOT NULL,
`othersource` varchar(50) NOT NULL,
`orientationsession` varchar(20) NOT NULL,
`othersession` varchar(20) NOT NULL,
`organisation` int(11) NOT NULL,
`newsletter` int(2) NOT NULL,
`iscomplete` int(11) NOT NULL,
`registrationdate` date NOT NULL,
PRIMARY KEY (`contact_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=39 ;
mysql>insert into contactform values('','abhi','sir','shukla','vbxcvb','342342','asdfasd','234234234','abhi#gmail.com','1999/5/16','via vrienden of familie','','19','20','6','1','1','2010-03-29')
i get following error.
#1366 - Incorrect integer value: '' for column 'contact_id' at row 1
this query work fine on my local machine but give error on server
Had the same problem with some legacy project. I didn't want to turn off strict mode globally, but also didn't want to go through all the code to fix it, so I disabled it only within that application by executing the following query once after the connection to the db is made:
SET sql_mode = ""
Try using NULL instead of '' for contact_id in
insert into contactform values(NULL,......
Try to find following line in my.cnf/my.ini:
sql-mode=”STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”
Now comment it out with # and restart your mysql server.
'' is an empty string, you want a integer, but this integer is created by the database. Drop the columnname in the INSERT and don't insert any value.
'' is not an integer, is it?
Also, that is some seriously weird indentation.
In mysql console, try this:
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
If you are importing - make sure your CSV/TXT file is UTF-8 WITHOUT the BOM - the BOM will pass characters that are not intgers and MySQL will think you are importing that into the first field...

MySQL ucfirst doesn't work

I have this table:
CREATE TABLE IF NOT EXISTS `events` (
`evt_id` int(11) NOT NULL AUTO_INCREMENT,
`evt_name` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT 'ucfirst',
`evt_description` varchar(100) DEFAULT NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date DEFAULT NULL,
`evt_starttime` time DEFAULT NULL,
`evt_endtime` time DEFAULT NULL,
`evt_amtpersons` int(11) DEFAULT NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) DEFAULT NULL,
`evt_amtPersonsSubs` tinyint(4) NOT NULL DEFAULT '0',
`evt_photo` varchar(50) DEFAULT NULL,
`sys-mut-dt` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`sys-mut-user` varchar(20) DEFAULT NULL,
`sys-mut-id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
But when I add in data into evt_name my first character is not capitalized. Any ideas?
Further information:
MySQL client version: 5.1.37
I want to do this in the database so that I don't have to do ucfirst with php always.
...what?
If I'm reading this right, it will just set the default value of evt_name to the string ucfirst. Try inserting a blank row and see if I'm reading that right.
If you're really against using ucfirst in PHP, you'll probably have to still call ucfirst every time in the query. Or you could only use ucfirst on display, and not in the database.
ucfirst is a function...you defined it as Default-Value for the cell. Use it like this:
INSERT INTO events SET evt_name = UCFIRST($value)