Adding a calculated date column to a MySQL dataset - mysql

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

Related

Error when adding column with numeric name

I currently have a MariaDB database with columns named after dates : 20200105, 20200914 etc.
If I try to add a column using ALTER TABLE dates ADD COLUMN IF NOT EXISTS (test VARCHAR(255));, it works and the test column is created.
If I type ALTER TABLE dates ADD COLUMN IF NOT EXISTS (20201205 VARCHAR(255));, though (so, with a number replacing "test"), the creation does not work anymore and MariaDB tells me that there is an error with my SQL syntax.
I have tried to put quotes around the column name, but that does not work (not even with "test").
Is there something obvious I am missing ?
Use backticks to escape the column name:
ALTER TABLE dates ADD COLUMN IF NOT EXISTS (`20201205` VARCHAR(255));
But really best practice frowns upon the use of naming your database objects with mandatory backticks. The reason for using a name like 20201205 as a column name is that you will forever be needing to escape it using backticks. Also, from a data design point of view, your data should grow with new dates in terms of increasing the number of records, not columns.

Create Table in SQL where table name is from digits

When I try do next
mysql> CREATE TABLE '20181020';
sql return an error:
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 ''20181020'' at line 1
How can I solve it?
You needt to wrap identifier with backticks:
CREATE TABLE `20181020`(id INT);
I propose not to do so and use proper meaningful naming. Using date as name of table suggest that it could be table-per-date antipattern.
Related article: SELECT * FROM sales + #yymm
your can also use double quote for this type of table name
CREATE TABLE "20181020" (id INT);
insert into "20181020" values(1)
But this type of naming is not standard practice
The other answers cover the solution, which is to use backticks.
Let me add that using non-standard identifiers is a bad idea. If you start naming columns as number as well, who wants to figure out the differences between 20181020.1, 20181020.1, and 20181020.1. Is it so hard to use a prefix: t_20181020?
But you don't want to do that either. Let me explain. Presumably, if you have one table called 20181020, then tomorrow you will want another table 20181021. This is a really bad database design.
You should be putting all the data into a single table, with a date column specifying the date. You can then run queries over one table to see changes over time -- for instance.
If the tables are large and you want the ability to manipulate each day separately, then you use table partitioning to store each day's worth separately. This is handy, because partitions can be archived, restored, and dropped independently of other partitions.

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

How do you change an autoincremented columns starting value through liquibase?

I am using MySql for my database. I have found how to set a column's starting autoincrement value when creating a table, but I need to know how to set a new starting value for an existing column. What does the liquibase script look like to do that?
The MySQL syntax is pretty straightforward:
ALTER TABLE mytable AUTO_INCREMENT = val ;
(Note that this is really a table attribute, not a column attribute. There can be only one column in a table declared to be AUTO_INCREMENT.)
This syntax isn't supported in SQL Server or Oracle; Oracle doesn't even have a concept of an "auto_increment" column, apart from a SEQUENCE object and a TRIGGER. SQL Server calls it an IDENTITY property. So I don't know how this statement would be represented in "liquibase" syntax, other than specifying that this statement is native MySQL syntax.
You can use addAutoIncrement (http://www.liquibase.org/documentation/changes/add_auto_increment.html) to change your existing AUTO_INCREMENT column.
Don't forget to specify columnDataType in the addAutoIncrement.
I used this yesterday for our project and it worked (for MySQL).