I cannot change databases column - mysql

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

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())

Error updating entity - Symfony 3

There's something very very wrong with Doctrine/MySQL or I'm just completly dumb. I'm trying to execute a simple update query on Question entity:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| form_id | int(11) | YES | MUL | NULL | |
| name | varchar(255) | NO | | NULL | |
| question_type | varchar(255) | NO | | NULL | |
| validation | longtext | YES | | NULL | |
| required | tinyint(1) | YES | | NULL | |
| order | int(11) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
With:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('order', IntegerType::class)
->add('name', TextType::class)
->add('questionType', TextType::class)
->add('validation', TextType::class)
->add('required', CheckboxType::class)
;
}
and
...
$em->persist($question);
...
I'm getting this error:
Uncaught PHP Exception Doctrine\DBAL\Exception\SyntaxErrorException: "An exception occurred while executing 'UPDATE questions SET order = ? WHERE id = ?' with params [1, 12]
Every other field validates with no problem!
When I'm trying to run the same query in the console, this is the result:
mysql> UPDATE questions SET order=1 WHERE id=14;
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 'order=1 WHERE id=14' at line 1
When I run the same query in Workbench, everything is ok:
UPDATE `questions` SET `order`='2' WHERE `id`='7';
Finally, updating another column in this table (also integer!), like so:
update questions set form_id=5 where id=7;
Goes as expected.
What is going on?
EDIT:
To simplify, why this works:
UPDATE questions SET form_id=5 WHERE id=7;
And this doesn't:
UPDATE questions SET order=3 WHERE id=7;
order is a reserved word for Mysql
That's why it runs when (escaped with backticks)
It goes without saying that choosing another name would be safer
See this answer for more informations

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.

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)

MySQL INSERT INTO failing at WHERE clause

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.