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
Related
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.
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, ' %');
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.
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..
I have MySQL 5.5.20 and this table:
mysql> desc country;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| id | int(255) | NO | PRI | NULL | |
| iso | varchar(2) | NO | | NULL | |
| name | varchar(80) | NO | | NULL | |
| printable_name | varchar(80) | NO | | NULL | |
| iso3 | varchar(3) | YES | | NULL | |
| numcode | smallint(6) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
If I run a query like this
SELECT country.ID, country.ISO, country.NAME,
country.PRINTABLE_NAME, country.ISO3, country.NUMCODE
FROM country;
It returns:
ERROR 2013 (HY000): Lost connection to MySQL server during query
If I change the order of the columns (ISO3 before PRINTABLE_NAME for example) like this:
SELECT country.ID, country.ISO, country.NAME,
country.PRINTABLE_NAME, country.NUMCODE, country.ISO3
FROM country;
It works fine!
Or if I rewrite the query using lower-case letters for columns, it works as well.
This issue appears from time to time (about once a month) and the only solution to fix it is to restart MySQL! i have checked error log and this what i have in it:
130310 11:01:23 [Warning] /usr/sbin/mysqld: Forcing close of thread 401108 user: 'root'
I am really confused and don't know why this happens! Any ideas on how this could be fixed?