Sql syntax error caused by reserved names - mysql

i have an syntax error in my sql
it has to do with the use of reserved names(user security). i cant seem to figure out how to put the quotes can someone please help me fix this.
ALTER TABLE `user` ADD FOREIGN KEY `security_check_id` REFERENCES `security_check`(security_check_id)
#1064 - Er is iets fout in de gebruikte syntax bij 'REFERENCES security_check(security_check_id)' in regel 1
Translated sorry for the bad translation
#1064 - There is an mistake in the used syntax at 'REFERENCES security_check(security_check_id)' at line 1

Pease check this answer, it might be helpful -
Selecting a column that is also a keyword in MySQL
In your case try -
ALTER TABLE `user` ADD FOREIGN KEY (`security_id`) REFERENCES `security`(`security_id`)

ADD FOREIGN KEY security_check_id is incorrect if security_check_id is a column you wish to reference then it must be enclosed in parenthesis ie ADD FOREIGN KEY (security_check_id) if security_check_id is a name you intend to give the FK then it should be followed by the column in parenthesis that you wish to check..
BTW none of the column or table names are reserved words so they don't need to be escaped

Related

Implementing ER Diagram in MySQL, receiving Error 1064 during altering

I'm trying to establish an ER diagram for my database. There are 5 tables in my database.
When I want to make the relationship between industry_number and company_inustry, it always said there is a 1064 error.
my code is:
ALTER TABLE company_inustry
ADD FOREIGN KEY (Detailed Industry)
REFERENCES Industry_number(Detailed Industry)
Error is #1064 - 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 'Industry)
REFERENCES Industry_number(Detailed Industry)' at line 2.
I'm a new learner. Please help me... Thank you!
You have spaced in the column names so you need quote them.
It would be a better idea to use an underscore Detailed_Industry as you have done in the table names. If you do not you will have the same problem in every query that refers to these columns.
ALTER TABLE company_inustry ADD FOREIGN KEY ("Detailed Industry") REFERENCES Industry_number("Detailed Industry")
You need to use double quotes (" ") if you have space in name of the tables. use underscore as a good industry practice.
Use this query for correct syntax:
ALTER TABLE company_inustry ADD FOREIGN KEY ("Detailed Industry") REFERENCES Industry_number("Detailed Industry")

Syntax error 1064 (42000) when adding foreign key in SQL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to add some foreign keys to some tables.
First, I added a foreign key to table "Room" so that column roomtype was referencing table Roomtype, column ID.
ALTER TABLE room ADD FOREIGN KEY (roomtype) REFERENCES roomtype(ID);
This worked fine. However, when I used the exact same formatting to add a foreign key to another table so that column "position" in table Nurse was referencing column ID in table Position:
ALTER TABLE nurse ADD FOREIGN KEY (position) REFERENCES position(ID);
I get an error:
ERROR 1064 (42000) 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 'Position(ID)' at line 2
I don't know how to transfer the result onto here, but I did a SHOW CREATE TABLE query for tables nurse and position, and it doesn't seem to be due to a spelling error.
I know the names are confusing, but I was required to use these names.
I changed your tags: the error message clearly says you're using mySql (not MSSQL).
I suspect the problem is with the column name, "position". Try this syntax instead:
ALTER TABLE nurse ADD FOREIGN KEY (`position`) REFERENCES `position`(ID);

Add a field in a table and check condition with another column

I need to add a field – DOC with Date type to the Registration table also ensure DOC greater than DOJ.I tried this
ALTER TABLE Registration
ADD DOC date
CHECK (DOC>DOJ);
It shows error.Where i went wrong?
Firstly, prior to MySQL version 8.0.16, MySQL permitted only a very limited version of table CHECK constraint syntax, which was parsed and ignored. So if your MySQL version < 8.0.16, then your ALTER TABLE statement would not fulfill any purpose; and possibly the reason of syntax error as well.
Now, if your version is indeed correct; then you are defining Table Constraint incorrectly. As per Documentation:
A CHECK constraint is specified as either a table constraint or column
constraint:
A table constraint does not appear within a column definition and can refer to any table column or columns. Forward references are
permitted to columns appearing later in the table definition.
A column constraint appears within a column definition and can refer only to that column.
Since your CHECK constraint involves more than one column, it is a Table constraint. Also, ALTER TABLE syntax to add a new CHECK constraint is as follows:
ALTER TABLE tbl_name
ADD CONSTRAINT [symbol] CHECK (expr) [[NOT] ENFORCED];
Try the following instead:
ALTER TABLE Registration
ADD DOC date,
ADD CONSTRAINT chk_doc_greater_doj CHECK (DOC>DOJ);
ALTER TABLE REGISTRATION
ADD DOC DATE
ADD CONSTRAINT DATE_OF_COMPLETION CHECK(DOC>DOJ);
Hey I encountered same problem, I solved it by writing in two seperate queries
ALTER TABLE Registration ADD DOC date;
ALTER TABLE Registration ADD CONSTRAINT check_date CHECK (DOC>DOJ);

Can't add column into table - mysql

I have a table called 'messages'. I wan't to add a column called 'key'.
When I try
ALTER TABLE messages ADD key BIGINT(20);
I get this error:
ERROR 1064 (42000): 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 'bigint(20)' at line 1
What am I doing wrong?
Because key is a keyword in mysql. Reserved keywords
You can use
ALTER TABLE messages ADD `key` BIGINT(20);
by escaping the word key you should be fine with this query.
Syntax highlighting already shows the problem: key is a keyword (yeah I know that sounds funny). You can use backquotes to specify the column name:
ALTER TABLE messages ADD `key` BIGINT(20);
-- ^ ^ backquotes
Mind that the backquotes are not part of the name of the column: the name of the column will be key. By using a backquote you state explicitly that you write a column name, not the keyword key.
Always add ` backquotes around keywords.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
Problem is key is a reserve word in MySQL and thus needs to be escaped using backtique like
ALTER TABLE messages ADD `key`
(OR) using double quotes "" (ANSI92 standard) like
ALTER TABLE messages ADD "key"
Better yet, stop using reservewords/keywords for table/column names (in fact for any DB object names)

Adding foreign key - Error Code 1064

I'm trying to add a foreign key to my table by the following code:
ALTER TABLE scenes ADD CONSTRAINT fk_dancer_charachter FOREIGN KEY (dancer_pick) references characters(character);
But it keeps saying: Error Code 1064: You have an error in your sql syntax: check the manual that corresponds to your MySQL sever version for the right syntax to use near 'charachter)' at line 1.
Why is that? The syntax seems correct! Isn't it? What am I doing wrong?
Thanks
You are using a reserved word - CHARACTER.
More information in Reserved Words in MySQL 5.5