0

I have a syntax error when adding a new column to a table. Every other column is added correct apart from this:

ALTER TABLE stock.stock_data ADD LEGAL_&_GENERAL VARCHAR(40);

Thanks for your help.

2
  • 2
    Remove the &. Use and. Commented Jul 14, 2017 at 12:35
  • 1
    Don't use & in you table name Commented Jul 14, 2017 at 12:35

2 Answers 2

2

MySQL (as with all databases) limits the characters in unquoted identifiers:

Permitted characters in unquoted identifiers:

ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)

Extended: U+0080 .. U+FFFF

Note that "&" is not one of them. The preferred way to fix this is to use "normal" characters:

ALTER TABLE stock.stock_data ADD LEGAL_AND_GENERAL VARCHAR(40);

If you really want to, you can quote the identifier:

 ALTER TABLE stock.stock_data ADD `LEGAL_&_GENERAL` VARCHAR(40);

However, you will have to quote the name wherever you use the column, just cluttering your queries and wearing out the back-tick key.

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

Comments

1

Use ` to escape &

ALTER TABLE stock.stock_data ADD `LEGAL_&_GENERAL` VARCHAR(40);

See Identifier Qualifiers

Comments

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.