Cant drop column named condition - mysql

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

Related

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.

MySql; proper way to update fields of a table

When trying to describe a table I get a table with missing information (see first table); what would be the best way to update that table in a way that looks like the second one? My SQL background is not so strong, so I'd love to hear ideas on how to do this
First table (how it is actually)
+-----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| config_id | int(10) unsigned | NO | | 0 | |
| scope | varchar(8) | NO | | default | |
| scope_id | int(11) | NO | | 0 | |
| path | varchar(255) | NO | | general | |
| value | text | YES | | NULL | |
+-----------+------------------+------+-----+---------+-------+
Second table (how it should be)
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| config_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| scope | varchar(8) | NO | MUL | default | |
| scope_id | int(11) | NO | | 0 | |
| path | varchar(255) | NO | | general | |
| value | text | YES | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
I see following differences between the two tables :
column config_id should be auto_increment and also primary key of the table
a (non-unique) index is missing on column scope
The following statement should change the table as required :
ALTER TABLE mytable
MODIFY COLUMN config_id INT auto_increment,
ADD PRIMARY KEY (config_id),
ADD INDEX idx_scope(scope)
;
PS : DEFAULT NULL does not make sense for config_id : since it is a primary key, your RDBMS will never allow it to be set to NULL.
Please note that this answer is based on the information you provided only. Running this statement will not necessarily make the table structures strictly equivalent, since there could be other differences that can not be seen in the representation that you provided. You can get a complete DDL statement describing the table using the SHOW CREATE TABLE mytable syntax.

MySQL - 'Updated By' column to automatically update with user who modified the row?

Here's my table.
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Postcode | varchar(255) | YES | | NULL | |
| Town | varchar(255) | YES | | NULL | |
| Region | varchar(255) | YES | | NULL | |
| Company Name | varchar(255) | YES | | NULL | |
| Fee | double | YES | | NULL | |
| Company Benefits | varchar(255) | YES | | NULL | |
| Date Updated | date | YES | | NULL | |
| Website | mediumtext | YES | | NULL | |
| Updated By | varchar(255) | YES | | NULL | |
| Notes | varchar(255) | YES | | NULL | |
| LNG | varchar(255) | YES | | NULL | |
| LAT | varchar(255) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
You can see we have an "Updated by" column.
How can I make it so that, when a user updates the row, the "Updated By" column automatically updates (or inserts if it's a new row they're adding) with the currently logged-in users name?
Many Thanks
You will have to explicitly make sure about that and whenever an UPDATE is happening then you need to update that column as well saying below. Best way to assure it, have your application logic fill in the column whenever an UPDATE to the record is happening from current logged-in user principle or claim
update tbl1
set ...,
Updated By = <logged in user name>
where Id = <some val>
You can use USER() or CURRENT_USER() in Update or Insert statements to achieve needed effect.
From my side - the only one secure way is to create stored procedures, providing inserts or updates to desired table.
Indeed, this problem was discussed here:
mysql Set default value of a column as the current logged in user
Something like this !
CREATE TRIGGER `updater`.`tableName_BEFORE_INSERT` BEFORE INSERT ON `tableName`
FOR EACH ROW
BEGIN
Set New.Updated_By = current_user();
END

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

Improve query performance in MySQL

I am posting this thread in order to have some advices regarding the performance of my SQL query.
I have actually 2 tables, one which called HGVS_SNP with about 44657169 rows and another on run table which has an average of 2000 rows.
When I try to update field Comment of my run table it takes lot's of time to perform the query. I was wondering if there is any method to increase my SQL query.
Structure of HGVS_SNP Table:
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| snp_id | int(11) | YES | MUL | NULL | |
| hgvs_name | text | YES | | NULL | |
| source | varchar(8) | NO | | NULL | |
| upd_time | varchar(32) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
My run table has the following structure:
+----------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+-------+
| ID | varchar(7) | YES | | NULL | |
| Reference | varchar(7) | YES | MUL | NULL | |
| HGVSvar2 | varchar(120) | YES | MUL | NULL | |
| Comment | varchar(120) | YES | | NULL | |
| Compute | varchar(20) | YES | | NULL | |
+----------------------+--------------+------+-----+---------+-------+
Here's my query:
UPDATE run
INNER JOIN SNP_HGVS
ON run.HGVSvar2=SNP_HGVS.hgvs_name
SET run.Comment=concat('rs',SNP_HGVS.snp_id) WHERE run.Compute not like 'tron'
I`m guessing since you JOIN a text column with a VARCHAR(120) column that you don`t really need a text column. Make it a VARCHAR so you can index it
ALTER TABLE `HGVS_SNP` modify hgvs_name VARCHAR(120);
ALTER TABLE `HGVS_SNP` ADD KEY idx_hgvs_name (hgvs_name);
This will take a while on large tables
Now your JOIN should be much faster,also add an index on compute column
ALTER TABLE `run` ADD KEY idx_compute (compute);
And the LIKE is unnecessary,change it to
WHERE run.Compute != 'tron'