MySQL INSERT INTO failing at WHERE clause - mysql

Ok, I'm stumped on this one:
mysql> INSERT INTO item (col1) VALUES ('testing') WHERE item_name = 'server1';
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 'WHERE item_name = 'server1'' at line 1
Here's the table desc (slightly sanitized):
mysql> desc item;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| item_name | varchar(128) | YES | | | |
| active | int(11) | NO | | 0 | |
| col1 | varchar(512) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
]I've tried some variations on the INSERT, but I've gotten nowhere. Look forward to someone revealing whatever obvious thing I'm missing!
Running: mysql Ver 14.12 Distrib 5.0.95, for redhat-linux-gnu (x86_64) using readline 5.1 (I know it's older, but I can't upgrade this box at the moment).

If you're looking to update an existing row, it's this:
UPDATE item
SET col1 = 'testing'
WHERE item_name = 'server1';

You're not using the INSERTstatement correctly, if you use VALUES clause you can't apply a WHERE condition on it.
Here is the same query with the appropriate syntax:
INSERT INTO item (col1)
SELECT 'testing'
FROM yourTable
WHERE item_name = 'server1';
Hope this will help you.

Related

MySQL throwing syntax error while trying to alter a table

I have been trying to alter a table to include a date column with default value of CURDATE() but MySQL is constantly throwing syntax error. Now, I have checked syntax for altering a table from several sources but I believe I do not have any syntax error. When I remove the default value part, the query runs fine but for some reason it cannot add a default value for the date column. I don't know why that is the case.
The code:
mysql> describe test;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| col1 | int | YES | | 0 | |
| col2 | varchar(100) | YES | | hello | |
| col3 | varchar(5) | YES | | T | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> ALTER TABLE test ADD COLUMN col4 DATE DEFAULT CURDATE();
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 'CURDATE()' at line 1
mysql>
Edit: My MySQL version: 8.0.31
I think it has to be like this now:
ALTER TABLE test ADD COLUMN col4 DATE DEFAULT (CURRENT_DATE);
Note the parenthesis, or (curdate())

I cannot change databases column

I cannot change databases column
My Env
MacOS Mojave, MySQL Server version: 10.1.39-MariaDB Source distribution
why
Making a CRUD app, but I want to change table column,
from text to desc, so I searched and used alter
command, but right SQL command returns error messages.
My table
MariaDB [cake_cms]> describe interns;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | | NULL | |
| name | varchar(64) | NO | | NULL | |
| text | varchar(255) | NO | | NULL | |
| location | varchar(64) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
MariaDB [cake_cms]> Alter Table interns Rename Column text to desc;
ERROR 1064 (42000): 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 'Column text to desc' at line 1
Refered
https://www.dbonline.jp/mysql/table/index18.html
says to use
ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;
Rename a column in MySQL
This site says:
ALTER TABLE tableName RENAME COLUMN "oldcolname" TO "newcolname" datatype(length);
So I write
alter table interns rename column "name" to "newname" varchar(255);
But returned syntax error message....
I do not know what to do. Please help me!
desc is a sql command so you can't name your table like this

MySQL show columns query to string

I am looking for a way to convert the result of a query to a string.
The query can be
DESC entries;
or
SHOW COLUMNS FROM entires;
For example, both yield the same result if I execute them in the CLI which is:
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| content | text | YES | | NULL | |
| time | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
However, I need this table as one string. It is NOT an option for me to get his information out of the information_schema.columns table because this information is not present there. It is further necessary to achieve this on the same query. Using PHP or another language is also not an option.
I have tried various things but none of them have been successful so far.
These queries all resulted in an error:
SELECT group_concat(Field) FROM (SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries LIMIT 1);
SELECT Field from (SHOW COLUMNS FROM entries);
SELECT 1 from (SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS) FROM entries;
I have tried the same with desc entries; but with the same result.
I always get an error like this:
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 'show columns
from entries)' at line 1
Neither Google nor the manual could tell me how to achieve this, maybe I was looking for the wrong phrases. Any help is highly appreciated.

Update only the time stamp in a MySql table

Given this table
mysql> describe last_user_activity;
+--------------+------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+-------------------+-------+
| customer_id | int(11) | NO | | NULL | |
| token | text | NO | | NULL | |
| time_stamp | timestamp | YES | | CURRENT_TIMESTAMP | |
| is_logged_in | tinyint(1) | NO | | 0 | |
+--------------+------------+------+-----+-------------------+-------+
4 rows in set (0.00 sec)
I want to "touch" a row of the table, setting the time_stamp to its default, CURRENT_TIMESTAMP.
I thought that I could try
UPDATE last_user_activity WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
(that is a valid token in the table), but that resulted in
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 'WHERE
token="40aed4d9-c9ac-471e-8d53-b2baa0d72523"' at line 1
Oh, well, it seemed clever, but NVM. So, then I tried
UPDATE last_user_activity SET token="40aed4d9-c9ac-471e-8d53-b2baa0d72523"
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
which said that it succeeded, BUT, the timestamp field was not updated.
What am I doing wrongly?
UPDATE last_user_activity
SET time_stamp = NULL
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
OR
UPDATE last_user_activity
SET time_stamp = NOW()
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
It looks as if you have given default value of column time_stamp, but not on update.
You may considering this change :
ALTER TABLE last_user_activity
MODIFY COLUMN time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
I hope this works.After that you could try using your update commands.

MYSQL - Insert statement with syntax Error [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
I have following table setup.
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| date | date | YES | | NULL | |
| limit | int(11) | YES | | NULL | |
| contract_id | int(11) | YES | | NULL | |
+-------------+---------+------+-----+---------+----------------+
And this insert query
INSERT INTO userlimit (date, limit, contract_id) VALUES (now(), 10, 1);
Always when I want to execute it I receive following 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 'limit, contract_id) VALUES (now(), 10, 1)' at line
1
My syntax looks perfectly fine to me. Why do I get this Error?
You need to quote field names with backticks
INSERT INTO userlimit (`date`, `limit`, `contract_id`) VALUES (now(), 10, 1)