Why does this ALTER TABLE query throw syntax error? - mysql

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

Related

MySQL vs MariaDB - ddl - setting default for time field

I've got table products, and want to add column with type time.
I've got statement as follows:
ALTER TABLE products ADD openTime1 TIME DEFAULT TIME(now());
it works on MariaDB, but it doesnt work on Mysql.
On mysql it produces Error -
[42000][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 '(now())' at line 1
Can someone tell me why? whats wrong here? I thought that this should be the same.
In MySQL the expression used in DEFAULT field attribute must be wrapped into the parenthesis:
ALTER TABLE products ADD openTime1 TIME DEFAULT (TIME(now()));
db<>fiddle here

Adding a calculated date column to a MySQL dataset

I have a column dateTime which consists of dates of the format "MM-DD-YYYY, hh-mm-ss" and I need to create a STORED column on the same table to get rid of the time element. I've tried:
ALTER TABLE table ADD COLUMN startOfDay AS(date(dateTime)) STORED;
but this gives a wrong syntax error. How do I make it work? I think the error is due to the AS part.
First when asking a question and you tell that you have a error, always show the error message in your post.
Secondly to use STORED columns you need MySQL 5.7 instance or higher.
At the moment I only have a 5.6 instance running so I can't test the query. But looking at the MySQL documentation I would suggest the following query syntax:
ALTER TABLE <table-name> ADD COLUMN <column-name> DATE GENERATED ALWAYS AS (DATE_FORMAT(<name-of-datetime-column>, `%Y-%m-%d`)) STORED COMMENT '<description>';
Just replace the placeholders with the names you have. To be sure and learn how things work, always check the MySQL reference manual on the subject.
See: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html

MySQL Generated Columns of date type

i use mysql version 5.7
i have a field devicetime which is a datetime field.
for some reason i want to add a generated column which stores only the date part of the devicetime field.
i have tried the following statement
alter table mytable
add COLUMN recorddate date generated always as date(devicetime) stored;
i get an error
[Err] 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 'date(devicetime) stored' at line 2
i have taken the inputs from MySQL documentation from this link.
my current table structure is like this
For whatever reason MySQL needs some ()'s around the expression:
ALTER TABLE `mytable `
ADD COLUMN `recorddate` DATE GENERATED ALWAYS AS (date(devicetime)) STORED;
One solution came into my mind is,you can store TIMESTAMP into table.
And when try to get anywhere in your SYSTEM you can use DATE_FORMAT() and STR_TO_DATE() function to whatever part you required from that actual data.

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

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.