I have problems with this MySQL query:
CREATE TABLE profili(
id_profilo INT AUTO_INCREMENT PRIMARY KEY,
id_utente INT,
id_libro INT,
categoria VARCHAR(50) NOT NULL,
FOREIGN KEY(id_utente) REFERENCES utenti(id)
ON DELETE CASCADE,
FOREIGN KEY(id_libro) REFERENCES libri(id_libro)
ON DELETE UPDATE
)
PhpMyAdmin gives me a generic "syntax error" but I don't understand
First to make your question more appealing to answer be so specific as possible.
Just a few things to remember
Always include error messages
Explain what you want to accomplish and why
And much more. This will increase the chance people can understand and want to help you.
The issue in this query is that the last FOREIGN KEY definition is invalid.
ON DELETE UPDATE is nothing and incorrect syntax.
If I execute this code I get the 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 'UPDATE
For more information about FOREIGN KEY syntax see:
https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html
Related
I am implementing a check constraint on a column such that the inserted value is always a multiple of 11, which is below:
create table multinum(empid int(4) primary key, check (empid%11==0));
But I am getting the 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 '==0))' at line 1
So I removed one equal operator but now it is accepting those values as well that are not multiple of 11, like 12.
What is the problem in my statement? Is there any other way of doing this?
EDIT
I also tried below statement, but to no effect:
create table multinum(empid int(4) primary key check (empid%11==0));
IT works just fine.
Copy the text from the example and try it in your database
CHECK constraints only work with mysql 8 prior versions didn't implement it completely.
create table multinum(empid int(4) primary key, check (empid%11=0));
✓
INSERT INTO multinum VALUES(10);
Check constraint 'multinum_chk_1' is violated.
INSERT INTO multinum VALUES(11);
✓
INSERT INTO multinum VALUES(22);
✓
INSERT INTO multinum VALUES(12);
Check constraint 'multinum_chk_1' is violated.
db<>fiddle here
Could someone please tell me why I am getting this error and what I should change!
mysql_exceptions.ProgrammingError: (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 'ORDER int,\n
FOREIGN KEY (ID) REFERENCES papers(ID),\n PRIMARY K' at line
4")
This is my statement:
""" CREATE TABLE Authors (
ID int NOT NULL,
AUTHOR varchar(255),
ORDER int,
FOREIGN KEY (ID) REFERENCES papers(ID),
PRIMARY KEY (ID, ORDER)
) """
Thank you I am new to sql and simply cannot spot where I have gone wrong!
"ORDER" is a reserved word and cannot be used as a field name...
Select * from Author order by order;
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
If you absolutely want to use order as a field name, you must quote it, but it is better to avoid it, since you also would need to quote it in any sql sentence refering it.
I'm working through the head first SQL book using MySQL and am encountering an error when trying to run the code in the book. I'm sure the error is very obvious but it has me stumped. The idea is to add a primary key to the table project_list by changing the name of a current column and setting it as a primary key.
ALTER TABLE project_list
CHANGE COLUMN number proj_id INT NOT NULL AUTO_INCREMENT,
ADD PRIMARY KEY ('proj_id');
Error message:
Error Code: 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 ''proj_id')' at line 3.
ADD PRIMARY KEY ('proj_id')
is attempting to set the primary key to a literal string rather than a column. You should either use proj_id on it's own:
... ADD PRIMARY KEY (proj_id);
or the back-tick version (with ` rather than '):
... ADD PRIMARY KEY (`proj_id`);
CREATE TABLE Exhibitor_Info
(Ex_id int AUTO_INCREMENT,User_id int,Category varchar(150),Description varchar(400), PRIMARY KEY(Ex_id),FOREIGN KEY(User_id));
while executing this sql I got the following 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 2
Can anyone help me to fix the problem
thanks
Add reference to foreign key by replacing
FOREIGN KEY(User_id)
with
foreign key(user_id) references referred_table(referred_col)
You have to add the reference to a foreign key
CREATE TABLE Exhibitor_Info
(Ex_id int AUTO_INCREMENT,User_id int,Category varchar(150),Description varchar(400), PRIMARY KEY(Ex_id),FOREIGN KEY(User_id)
REFERENCES referred_parent_table(referred_col) ON DELETE CASCADE);
You can have a good example here
I keep getting this sql 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 'Option (OptionId)' at line 1"
when I try and add a foreign key to the OptionId field from the Question Table to the OptionId(pk) field in the Option field. I don't get wy I keep getting the error because I don't see what is wrong with it.
Below is the foreign key constraint using ALTER TABLE:
ALTER TABLE Question ADD CONSTRAINT FK_OptionId FOREIGN KEY (OptionId) REFERENCES Option (OptionId)
Table names and syntax are correct, I made sure by double checking.
Why is it not working?
option is a reserved word in MySQL and must be surrounded by backticks.
ALTER TABLE Question
ADD CONSTRAINT FK_OptionId FOREIGN KEY (OptionId)
REFERENCES `Option` (OptionId)