Create Table in SQL where table name is from digits - mysql

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.

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.

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

sql syntax error with VERY basic command? why?

I am trying just create a table using MySql Workbench. Here's the sql command:
CREATE TABLE `bmxData`.`new_table` ();
yet I get this error while executing this :-
Executing:
CREATE TABLE bmxData.new_table ();
Operation failed: There was an error while applying the SQL script to the database.
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 ')' at line 1
SQL Statement:
CREATE TABLE bmxData.new_table ()
Any idea why? I have no idea what I'm doing wrong...
You need to add at least one column:
CREATE TABLE `bmxData`.`new_table`(col INT);
Some RDBMS allow to define table without explicit user column like PostgreSQL:
CREATE TABLE tab();
DBFiddle Demo
There is some discussion in relational theory of the meaning of tables with zero columns. Also described as relations of zero degree.
Relational theory researcher C. J. Date refers to TABLE_DUM and TABLE_DEE. Both have no columns, but the difference is that TABLE_DUM has no columns and no rows, whereas TABLE_DEE has one row (even though that row has no columns).
See this excerpt from "Database in Depth: Relational Theory for Practitioners": https://flylib.com/books/en/2.214.1.38/1/
The following query will therefore return no rows:
SELECT * FROM TABLE_DUM CROSS JOIN MyTable;
Whereas this query will return the same rows and columns as MyTable:
SELECT * FROM TABLE_DEE CROSS JOIN MyTable;
So there's some precedent and significance for a table with no columns. You can think of it by analogy as the role of 0 and 1 in multiplication:
0 x any number is 0
1 x any number is the same number
HOWEVER, standard SQL doesn't allow it. SQL doesn't exactly implement all of the concept of relational theory faithfully. In this case, standard SQL defines a table as having at least one column. The designers of SQL decided that a table with no columns isn't so interesting as to justify support in the SQL language.
If PostgreSQL or some other implementation of SQL decides to allow a table with no columns, they are doing it as an extension to standard SQL.

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 Creating temporary table syntax error

I am getting a syntax error when I run a MySQL statement.
I know backticks can be added to the tablename to make it work but was wondering why it happens in the first place.
Create temporary table 6514202534e1b20f0d6331 (user_id INT (10)) ENGINE=HEAP;
If I put this in Mysql Query Browser it treats the table name as two seperate words - 6514202534e1 and b20f0d6331.
The table name is generated dynamically and I haven't had a problem with this before, so I was wondering why it stopped working all of a sudden.
I think this is because the server (mysql) understands it in this case as 6514202534*e^1 INT.
Using the ` character:
CREATE TEMPORARY TABLE `6514202534e1b20f0d6331` (user_id INT (10)) ENGINE=HEAP;
In this way the MySQL Server understands (explicitly) that the whole phrase is database, table or field not a value or function, etc.
For example `order` is legal while just order will rise an error for invalid order clause.