Can't add column into table - mysql - mysql

I have a table called 'messages'. I wan't to add a column called 'key'.
When I try
ALTER TABLE messages ADD key BIGINT(20);
I get this 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 'bigint(20)' at line 1
What am I doing wrong?

Because key is a keyword in mysql. Reserved keywords
You can use
ALTER TABLE messages ADD `key` BIGINT(20);
by escaping the word key you should be fine with this query.

Syntax highlighting already shows the problem: key is a keyword (yeah I know that sounds funny). You can use backquotes to specify the column name:
ALTER TABLE messages ADD `key` BIGINT(20);
-- ^ ^ backquotes
Mind that the backquotes are not part of the name of the column: the name of the column will be key. By using a backquote you state explicitly that you write a column name, not the keyword key.

Always add ` backquotes around keywords.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html

Problem is key is a reserve word in MySQL and thus needs to be escaped using backtique like
ALTER TABLE messages ADD `key`
(OR) using double quotes "" (ANSI92 standard) like
ALTER TABLE messages ADD "key"
Better yet, stop using reservewords/keywords for table/column names (in fact for any DB object names)

Related

Implementing ER Diagram in MySQL, receiving Error 1064 during altering

I'm trying to establish an ER diagram for my database. There are 5 tables in my database.
When I want to make the relationship between industry_number and company_inustry, it always said there is a 1064 error.
my code is:
ALTER TABLE company_inustry
ADD FOREIGN KEY (Detailed Industry)
REFERENCES Industry_number(Detailed Industry)
Error is #1064 - 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 'Industry)
REFERENCES Industry_number(Detailed Industry)' at line 2.
I'm a new learner. Please help me... Thank you!
You have spaced in the column names so you need quote them.
It would be a better idea to use an underscore Detailed_Industry as you have done in the table names. If you do not you will have the same problem in every query that refers to these columns.
ALTER TABLE company_inustry ADD FOREIGN KEY ("Detailed Industry") REFERENCES Industry_number("Detailed Industry")
You need to use double quotes (" ") if you have space in name of the tables. use underscore as a good industry practice.
Use this query for correct syntax:
ALTER TABLE company_inustry ADD FOREIGN KEY ("Detailed Industry") REFERENCES Industry_number("Detailed Industry")

Maria DB : Alter a field to a PERSISTENT Calculated

I have created a table and I wish to make a Computed Column from the concatenated values of three other fields in the table.
I want this Computed Field to take place at INSERT or UPDATE, so I am specifying PERSISTENT
I have tried the following code (in various ways) in phpMyAdmin but always get errors, which seem to be referencing immediately after ALTER table
I did not see a way of doing this when adding the field in phpMyAdmin, so I hoped I could ALTER it.
Alter TABLE 'tlImages'
CHANGE COLUMN tlImageQuery
AS CONCAT(tlImgTitle,"~",tlImgDescrip,"~",tlImgWhereWhen) PERSISTENT;
MariaDB version 10.0.29-MariaDB-cll-lve - MariaDB Server
phpMyAdmin . Version information: 4.0.10.18
First, lose single quotes around the table name, they are not suitable for this purpose. Use backticks or nothing.
You will still get a syntax error further in the statement, because AS clause should be in brackets. Add them.
You will still get a syntax error because you are missing column type before the AS (...) clause, add it.
You will still get a syntax error because CHANGE COLUMN needs two column names, old and new, use MODIFY instead.
Alter TABLE `tlImages`
MODIFY COLUMN tlImageQuery VARCHAR(128)
AS (CONCAT(tlImgTitle,"~",tlImgDescrip,"~",tlImgWhereWhen)) PERSISTENT
;
(Type VARCHAR(128) is given just as an example).

syntax error when trying to alter table column name mysql

I am working on a homework assignment. my instructions are this:
In the MATCHES table, change the column name MATCHNO to MATCH.
my syntax i wrote will be below, the problem I am having is with the word "match"
its giving me a syntax error of unexpected syntax error for it. I am assuming
that match is a keyword of some sort but after googling I'm not sure exactly.
the database I am using is the popular "tennis" one for teachings.
here is my code:
sql
ALTER TABLE MATCHES
CHANGE MATCHNO MATCH INT;
what can I do to fix this? If i change the name of "match" to something else it works but my instructions say to use "match"... is this possible?
The problem is that your query contains MySQL keywords (MATCH, cf. https://dev.mysql.com/doc/refman/5.5/en/keywords.html).
Put the table and column names in backticks (`) in order to prevent MySQL from interpreting those as keywords.
ALTER TABLE `MATCHES` CHANGE `MATCHNO` `MATCH` INT;

Why does this ALTER TABLE query throw syntax error?

This is the query I'm using:
ALTER TABLE apartment ADD technical TEXT NOT NULL AFTER is_sale
Why does it produce a syntax error?
EDIT:
ALTER TABLE apartment ADD COLUMN technical TEXT NOT NULL AFTER is_sale still produces an error:
Error: (1064) 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 'ALTER TABLE andreevka_apartment ADD COLUMN technincal TEXT NOT NULL AFTER' at line 1
Also doesn't validate: http://developer.mimer.com/validator/parser200x/index.tml#parser
It actually turned out that my query was copy-pasted from PhpMyAdmin and had some encoding issues. I pasted it into Notepad++, converted it to ANSI and it worked. Weird.
The syntax was perfectly fine.
You have a syntax error, to add column you should use COLUMN in sentence
ALTER TABLE apartment ADD COLUMN technical TEXT NOT NULL
EDITED
I think you cannot use AFTER with ALTER because it won't care of column order, it does not matter for storage or querying.
ALTER TABLE apartment ADD technical TEXT NOT NULL AFTER is_sale is working fine and I tested it.I use HeidiSQL as client.Here no need of ADD COLUMN.check whether is_sale name is correct or apartment name is correct
use ADD COLUMN for adding new column
ALTER TABLE apartment ADD COLUMN technical TEXT NOT NULL AFTER is_sale
Not sure it's allowed in MySQL, but in Oracle there's another syntax as well:
ALTER TABLE apartment ADD (technical TEXT NOT NULL AFTER is_sale);

MySQL: Can't update a single record based on an autoincremented key

I have a table called "data" that is very simple. It has 2 columns: "key"and "tagged". "Key" is an autoincremented id.
I am trying to update the "tagged" column depending on the "key". I am using this very simple query:
UPDATE data SET tagged='Blahblahblah' WHERE key='1';
However, MySQL gives me the following error: #1064 - 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 'key='1'' at line 1
I don't understand what the problem is with my query. Would you have any idea?
Jean-Nicolas
key is a reserved word and must be escaped with back ticks.
UPDATE data SET tagged='Blahblahblah' WHERE `key`='1';
Since the key column is an auto increment column. I am sure it is either of type int or bigint.
So you should not use in the WHERE clause also the word key is a reserved keyword.
Try -
UPDATE data SET tagged='Blahblahblah' WHERE `key`=1;
Remove the single quotes around the 1 and put it around key, since its a keyword. Make it 'key' = 1
That should do the trick.