1

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 ?

1
  • 2
    Did you try using ` ? Commented Sep 7, 2021 at 10:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Totally working, thanks ! That's right, but these dates actually correspond to versions (versions named after the date they are released), and my records are the number of devices in each version :) I'll wait for the mandatory 15 minutes then mark the answer as solving
@TheCodingPenguin Then in that case disregard my rant about date column names and good design -- it sounds as though you've vetted well your table design already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.