mysql insert statements with '(' fail - mysql

I have a file with more than 86000 insert statements . some of the insert statements are having '(', ',', '\' type of data in column data. Mysql is throwing an error and does not recognize the data as column data.
Is there any setting in MySQL like set define off in oracle ?
error
ERROR 1064 (42000): 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 'guntur
district, ap','08644 - 285237','chiluvuru','guntur','and pradesh'); inser' at line 1
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| bank_name | varchar(200) | YES | | NULL | |
| ifsc_code | varchar(200) | YES | | NULL | |
| micr_code | varchar(200) | YES | | NULL | |
| branch_name | varchar(200) | YES | | NULL | |
| address | varchar(200) | YES | | NULL | |
| phone_number | varchar(200) | YES | | NULL | |
| city | varchar(200) | YES | | NULL | |
| district | varchar(200) | YES | | NULL | |
| state | varchar(200) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+

When I try to add '\', ')', '(', ',' in my table, I am able to insert it..
Below is how I created table
create table mytab4 (myChar char) and create table mytab4 (myChar varchar(20)) both ways..
Could you please provide your structure of table and query trying to execute...
Table structure can be seen by executing query Describe myTable
Good Luck...

You might try:
shell> mysqlimport --fields-enclosed-by="'" db_name textfile1.sql
Check out: http://dev.mysql.com/doc/refman/5.6/en/mysqlimport.html
If all your fields are enclosed by a single quote and your single quotes within are escaped out (eg. values ('you're', 'no you\'re'));.
There is also an
--fields-optionally-enclosed-by=string option too

If you want to insert a '\' char, you should use '\\' else it consider the \' as an escape char and the query fail with the syntax error

Related

Can anybody help me identify the error in this statement?

The table looks like this
mysql> DESCRIBE tenants;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| domain | varchar(255) | NO | UNI | NULL | |
| database | varchar(255) | NO | UNI | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> INSERT INTO tenants(name,domain,database) VALUES ('varun','varun.localhost','sms_varun');
ERROR 1064 (42000): 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 'database) VALUES
('varun','varun.localhost','sms_varun')' at line 1
I am using mysql Ver 8.0.30 for Linux on x86_64 (MySQL Community Server - GPL)
DATABASE is a reserved MySQL keyword (see here), so you will have to escape it in your insert statement (and forever) to make it work:
INSERT INTO tenants (name, domain, `database`)
VALUES ('varun', 'varun.localhost', 'sms_varun');
You should avoid using reserved MySQL keywords for your column and table names.

MYSQL: one table entries containing matching entries in second table. Any query ideas

I am using two MYSQL tables on has big log strings for example: "this is a sample log entry with 123.456.789 IP address". Also, there is second table that contains list of Ip addresses in each row. I want to check for all the matching Ip addresses in the log entries and get the result as all the entries in log tables with matching IPs.
I have installed Mysql community version 5.7.22 on RHEL server.
Table 1 : log table
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| log_id | int(11) | NO | PRI | NULL | auto_increment |
| Id | varchar(30) | NO | | NULL | |
| host | varchar(50) | YES | | NULL | |
| external_id | varchar(40) | NO | | NULL | |
| message | varchar(8000) | YES | | NULL | |
| timestamp | varchar(30) | NO | | NULL | |
+-------------+---------------+------+-----+---------+----------------+
Table 2 : IP table
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| ip | varchar(30) | NO | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+-----------+-------------+------+-----+---------+----------------+
I am using below query :
select * from logs where message like '%'ip_table.ip'%';
which is giving a syntax error.
Any other ideas to work on this?
You can fix the syntax error by using concat():
select *
from logs l join
ip_table it
on l.message like concat('%', it.ip, '%');
However, this would match, say, '1.1.1.1' and '1.1.1.10'.
To fix this, you need to take delimiters into account. Assuming this is always a space:
select *
from logs l join
ip_table it
on concat(' ', l.message, ' ') like concat('% ', it.ip, ' %');

Changing value in MySQL table gives syntax error

Apologies if this is a stupid question, but this is my first time using MySQL and I can't seem to get this to work:
I have this table:
+------------------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| policy_name | varchar(255) | NO | UNI | | |
| virus_lover | char(1) | YES | | NULL | |
| spam_lover | char(1) | YES | | NULL | |
| unchecked_lover | char(1) | YES | | NULL | |
| banned_files_lover | char(1) | YES | | NULL | |
| bad_header_lover | char(1) | YES | | NULL | |
| bypass_virus_checks | char(1) | YES | | NULL | |
and I would like to change virus_lover to NO
I have tried using
update policy set Null='N' where Field='virus_lover';
But this gives me a syntax error response. I have checked online and everyone seems to be suggesting this exact same command that doesn't seem to work for me.
Any help would be greatly appreciated.
MySQL Ver 14.14 Distrib 5.7.25
The correct way to modify column attributes is with the ALTER TABLE command:
ALTER TABLE policy MODIFY virus_lover CHAR(1) NOT NULL
Demo on dbfiddle
Since NULL is a reserved keyword and it's being used as a column name in the table, it must be quoted with backticks or brackets:
update policy set `Null`='N' where Field='virus_lover';
or
update policy set [Null]='N' where Field='virus_lover';
Changing the column name to a non-reserved keyword instead of NULL would make things easier as well.

Using Alter and ignore in mysql to remove duplicates

I am using MYSQL and have a table 'bid' which has duplicates entries into it
My table schema is
ITEM_CODE | int(11) | YES | | NULL | |
| Max_BidP | int(11) | YES | | NULL | |
| Seller_Name | varchar(45) | YES | | NULL | |
| Buyer_Name | varchar(45) | YES | | NULL | |
| ITEM_NAME | varchar(45) | YES | | NULL | |
| Qty | int(11) | YES | | 1 | |
+-------------+-------------+------+-----+---------+-------+
One of the entries in the table
16 | 30 | sahraw | sahraw | J.K Rowling | 1 |
16 | 30 | sahraw | sahraw | J.K Rowling | 1 |
I am trying to remove the dulicates and the query I am specifying is
ALTER IGNORE TABLE bid ADD UNIQUE INDEX (ITEM_CODE , Max_BidP ,Seller_Name , Buyer_Name , ITEM_NAME , Qty);
But its giving me an error
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 'IGNORE TABLE bid ADD UNIQUE INDEX (ITEM_CODE , Max_BidP ,Seller_Nam' at line 1
Any suggestions where I am going wrong.
Thanks
Please check the MySQL version you are using.
As of MySQL 5.7.4, the IGNORE clause for ALTER TABLE is removed and its use produces an error.
http://dev.mysql.com/doc/refman/5.7/en/alter-table.html
If you're using MySql 5.7.4 or later, IGNORE is no longer available. See MySQL “ALTER IGNORE TABLE” Error In Syntax

Cant drop column named condition

I have a column named condition VARCHAR(255) in one of my tables. I would like to drop the column from the table and seems like it doesn't work.
I tried
alter table vehicle_new_full drop column condition;
It gives me this error.
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 'condition hello varchar(255)' at line 1
Tried altering the column name which also doesn't work.I was able to drop other columns from the tables but not this one and this doesn't have any constraints as well. Any ideas?
Structure of tables
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| styleId | bigint(11) | YES | | NULL | |
| year | int(11) | YES | | NULL | |
| make | varchar(255) | YES | | NULL | |
| model | varchar(255) | YES | | NULL | |
| trim | varchar(255) | YES | | NULL | |
| condition | varchar(255) | YES | | NULL | |
| vehicleStyle | varchar(255) | YES | | NULL | |
| engineCylinder | int(11) | YES | | NULL | |
| engineFuelType | varchar(255) | YES | | NULL | |
| drivenWheels | varchar(255) | YES | | NULL | |
| transmissionType | varchar(255) | YES | | NULL | |
| numberOfDoors | int(11) | YES | | NULL | |
+------------------+--------------+------+-----+---------+-------+
Try this:
alter table vehicle_new_full drop column `condition`;
CONDITION is a reserved keyword for mysql.
Reference:
http://dev.mysql.com/doc/refman/5.7/en/keywords.html
Your SQL ALTER TABLE command looks different than what MySQL receive. You said you drop condition field but MySQl reported syntax to use near 'condition hello varchar(255)'. Maybe there's part of your code that accidentally add this hello varchar(255) line to your SQL. See your query log to better understand.
It is better not to use any mysql reserved words when declaring column name as the name itself "reserved".
Put additional character if you still want to use the reserved word such as condition1, conditions, etc..