syntax error when trying to alter table column name mysql - 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;

Related

SOLVED - Unable to update mySQL column name via mysql command line

I cannot change the column name in a mysql database I created.
I have tried the following commands, and none of them appear to work.
alter table (mytablename) CHANGE COLUMN (oldcolumnname) (newcolumnname) varchar(120);
alter table (mytablename) RENAME COLUMN (oldcolumnname) (newcolumnname) varchar(120);
ALTER TABLE (mytablename) CHANGE (oldcolumnname) (newcolumnname) varchar(120);
Where (mytablename) is the name of the table that I created, (oldcolumnname) is the original column name, and (newcolumnname) is the new column name.
This is a simple to-do list I created to learn MySQL with the following items:
id
todo
completed
1
Prepare for Take Off
Yes
2
Learn some MySQL
Yes
3
Remember that damn semicolon
No
In this case, I am trying to alter the column 'todo' to say either 'To Do' or 'To-Do' but every time I try these commands. I keep getting the famous "Check your SQL version manual".
Any hints as to what I might be doing wrong? TIA!
I have reviewed multiple tutorial websites and even reviewed another StackOverflow question
UPDATE
The ultimate solution was two-fold. First, I needed to use the TO phrase between the column names. Second, the column names do not like special characters.
The query that ultimately worked was:
ALTER TABLE mytablename CHANGE COLUMN todo TO ToDo
You should keep the column named todo; you can always change the output when you select like:
select id, todo as 'To Do', completed from ...
If you feel you really must include a space or - in the column name, in mysql you can use arbitrary identifiers that have not-usually allowed characters by enclosing them in backticks:
alter table ... rename column todo to `To Do`
but then every time you reference the column in sql you will need to enclose it in backticks:
select id,`To Do`,completed from ... where `To Do` like '%learn%'

MYSQL error: A new statement was found but no delimiter between it and the previous one

I'm trying to update my column with that simple query:
UPDATE products SET desc='1' WHERE 1
But i get this error message : A new statement was found but no delimiter between it and the previous one. If I change desc column to another column than the query is working properly.
I saw THERE that this is a bug, I updated my phpmyadmin to the latest version 5.1, but I still get this error message, what could be the problem?
DESC is a reserved word in MySQL used in ordering the results of a query.
Keywords are words that have significance in SQL. Certain keywords,
such as SELECT, DELETE, or BIGINT, are reserved and require special
treatment for use as identifiers such as table and column names. This
may also be true for the names of built-in functions.
Nonreserved keywords are permitted as identifiers without quoting.
Reserved words are permitted as identifiers if you quote them as
described in Section 9.2, “Schema Object Names”:
As I have highlighted above, you can quote a reserved word if you want to use it as identifier, using
The identifier quote character is the backtick (`)
So you just need to change your query a bit adding the backticks.
UPDATE products SET `desc`='1' WHERE 1;
You can see a little example here.

Can't add column into table - 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)

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

SQL: Unsual names

I'm trying to select some information which is inside a table with the name online-players using Left join in SQL however, ever time I run the code it just returns the following error.
"Unknown column 'ontime' in 'field list'"
I am assuming its because of the - in the name of the table that course the error.
Is there anyway of going around it so it still gets the information?
That's not true, that error will be generated only when the corresponding field name doesn't exist. However you can enclose field and table names with backticks to be sure
Backticks are required, that's all.
You can use backticks (`) to refer to the table.
select * from `online-players`
This is provided that you don't have a typo in your query and such.