Can not create varchar column in mariadb - mariadb

This code does not run:
alter table States
add Key varchar(400) character set utf8 not null
The error:
Error in query (1064): Syntax error near 'varchar(400) character set utf8 not null' at line 2
What is wrong in this code?

Key is a reserved keyword in MySql (hence in MariaDB too), so to use it as an identifier you must enclose it in backticks. This will do:
alter table `States` add `Key` varchar(400) character set utf8 not null
I've also enclosed States in backticks, just for the sake of consistency, even though it's not really required.
An option would be to use another name for the new column, so to avoid the same problem in queries involving it afterwards.

Related

Pervasive database: Problem with spaces in column names

We use pervasive Database with Zen Contol-Center And want to perform the following SQl-Statenemt:
insert into MWlog
(Signatur,Datum,Uhrzeit,Auftrag,Probe,Parameter,Matrix,`Messwert neu`,Zugriff,`Messwert alt`,Aenderungsgrund)
values
('ML','2020-12-01','15:04:50','230176','230176','Bas. wirk.','TM','5.62','5','5.62','Neuer Import')
We get the following error-message:
*<<<<<<<<<<<<<<<<<<<<<<<<
insert into MWlog(Signatur,Datum,Uhrzeit,Auftrag,Probe,Parameter,Matrix,`Messwert neu`,Zugriff,`Messwert alt`,Aenderungsgrund) values ('ML','2020-12-01','15:04:50','230176','230176','Bas. wirk.','TM','5.62','5','5.62','Neuer Import')
[Zen][SQL Engine]
Syntax Error: insert into MWlog(Signatur,Datum,Uhrzeit,Auftrag,Probe,Parameter,Matrix,<< ??? >>`Messwert neu`,Zugriff,`Messwert alt`,Aenderungsgrund) values ('ML','2020-12-01','15:04:50','230176','230176','Bas. wirk.','TM','5.62','5',
>>>>>>>>>>>>>>>>>>>>>>>>*
What is wrong in the SQL Statement?
(We found out that the problem are the spaces in the field-Names)
We are using
Zen Control Center
Zen Install Version 14.10.035.
Java Version 1.8.0_222.
Gord Thompson is correct. You need to use double quotes around field names with spaces or field names that are reserved key words. You statements should be:
insert into MWlog
(Signatur,Datum,Uhrzeit,Auftrag,Probe,Parameter,Matrix,"Messwert neu",Zugriff,"Messwert alt",Aenderungsgrund)
values
('ML','2020-12-01','15:04:50','230176','230176','Bas. wirk.','TM','5.62','5','5.62','Neuer Import')

Teradata: expected something like ',' between an integer and the word

I'm trying to create a table and I get the error. Could someone please let me know how to add a column which has an integer starting in its name. Find below the statement and error
Create table mutablecode
(
4th_Procedure_Code varchar(20)
);
Syntax error, expected something like ','
between an integer and the word 'th_Procedure_Code'
A valid object name in Teradata consists of a-z,A-Z,0-9,#,_,$ but must not start with a digit.
If you really need this column name you must double quote it (then almost any character is allowed):
"4th_Procedure_Code" varchar(20)
Remark: According to Standard SQL a double quoted name is case-sensitive, but in Teradata it's still case-insensitive.

tdstats.UDFCONCAT parameter varchar limit

I was using tdstats.UDFCONCAT to aggregate result of the query,
This function trims the resulting output, upon looked at the definition of this function, it accepts VARCHAR(128) .
REPLACE FUNCTION tdstats.UDFCONCAT
(aVarchar VARCHAR(128) CHARACTER SET UNICODE)
RETURNS VARCHAR(10000) CHARACTER SET UNICODE
CLASS AGGREGATE (20000)
SPECIFIC udfConcat
LANGUAGE C
NO SQL
NO EXTERNAL DATA
PARAMETER STYLE SQL
NOT DETERMINISTIC
CALLED ON NULL INPUT
EXTERNAL NAME 'SL!staudf!F!udf_concatvarchar'
Can anybody tell if there is any specific reason to keep it limited to 128?
Note: I have duplicated the function and increased the size from 128 to 256 and it worked in my case. If anybody wants to know my use case then I can update here but question is regarding the default character limit mentioned by TD in the in-built function, so have not added my use case here.

R RMySQL not escaping table name, conflicts with MySQL reserved keyword

Code:
dbWriteTable(con, name=symbol, value=df, row.names=FALSE, append=TRUE)
Error in mysqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not run statement: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SSL
(
Date text,
Open double,
High double,
Low double,
Close double,
Volum' at line 1)
The issue is the ticker symbol "SSL" is also a MySQL keyword. It seems RMySQL is not escaping the table name?
Just to add, same problem with tickers "ALL" and "KEY", also reserved words. So I just need to figure out how to properly escape the table name with RMySQL, but so far am coming up empty.
Edit to add: I realize it is possible to use dbSendQuery but really want to make use of dbWriteTable instead. I am really surprised that the table name is not properly backticked automatically?
Edit to add more: I also tried name=paste0(dbname,".",symbol) for example, which works with MySQL to get past the reserved words, but what I found is that the dbWriteTable function uses the dbExistsTable() function built-in, and that function does not work with the change.
I could rewrite the dbExistsTable function to overwrite built-in as a workaround, but I do not know how to get the dbname from the dbConnect() function to become inherited. Still looking into this...

RS-DBI driver warning: (unrecognized MySQL field type 7 in column 1 imported as character)

I'm trying to run a simple query that works with MySQL or other MySQL connector API's,
SELECT * FROM `table` WHERE type = 'farmer'
I've tried various methods using the RMySQL package and they all get the same error
RS-DBI driver warning: (unrecognized MySQL field type 7 in column 1 imported as character)
Type = 'farmer'
(Query<-paste0("SELECT * FROM `table` WHERE type = '%",Type,"%'"))
res<-dbGetQuery(con, Query)
Query<-paste("SELECT * FROM `table` WHERE type = \'farmer\'")
Query<-paste("SELECT * FROM `table` WHERE type = 'farmer'")
What am I doing wrong?
"type" is a keyword in MYSQL. Surround the it with backticks to escape field names.
SELECT * FROM `table` WHERE `type` = 'farmer'
Also you probably have a time stamp column in your table. R is known to not recognize that column type. Convert it to a unix time stamp in the portion of the SQL statement.
Looks like the db schema has something in column which is of type 7 -- and that type appears to be unknown to the RMySQL driver.
I try to exclude column one in the query, or cast it at the select * ... level eg via something like
select foo as character, bar, bim, bom from 'table' where ...
To be clear, when I encountered this error message, it was because my data field was a time stamp.
I verified this by changing my query to SELECT created_at FROM ... which caused the error. I also verified this by changing the query not to include the column names that were timestamps, then I had no errors.
Note too, that the error message counts columns starting from 0 (instead of 1 as R does)
IMHO, the answer is you aren't doing anything wrong, but it's something that needs to be fixed in RMySQL.
The workaround is after you read in your data, you need to call one of the several possible character to datetime conversion functions. (Which one depends on what you want to do with the time stamp exactly.)

Resources