Tracking down basic 1064 Error - mysql

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`);

Related

getting error in check constraint with modulus operator

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

Unknown error in my MySQL query. Could you help me?

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

SQL (MySql) foreign key and composite key syntax error

I am new to SQL and I am trying to figure out what am I doing wrong here. The error that I am getting is:
#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 'Cast( movieId int, actorId int, salary decimal(7,2) default '77777.77', fore' at line 1
create table Cast(
movieId int,
actorId int,
salary decimal(7,2) default '77777.77',
primary key(movieId,actorId)
foreign key(actorId) references Actor(actorId),
foreign key(movieId) references Movie(movieId)
);
When you use Cast(... with no space between Cast and the (, MySQL thinks you are trying to use the CAST() builtin function. That makes no sense following CREATE TABLE, so MySQL is confused.
See https://dev.mysql.com/doc/refman/5.7/en/function-resolution.html
If you put a space before the paren, that fixes that problem.
create table Cast (
^
You also have another minor mistake, you forgot a comma after your PRIMARY KEY.

Appending primary key index to mysql (Using Mysql workbench) causing "GENERATED ALWAYS AS" error

I have an existing MySQL database table running on AWS, and that I'm connected through using MySQL Workbench.
I just tried to append an auto-generated primary key to the table using the GUI, and the script that gets generated is the following:
ALTER TABLE `participantData`.`resources`
ADD COLUMN `id` INT GENERATED ALWAYS AS () FIRST,
ADD PRIMARY KEY (`id`),
ADD UNIQUE INDEX `id_UNIQUE` (`id` ASC);
However, upon execution, I get the following error:
Executing:
ALTER TABLE `participantData`.`resources`
ADD COLUMN `id` INT GENERATED ALWAYS AS () FIRST,
ADD PRIMARY KEY (`id`),
ADD UNIQUE INDEX `id_UNIQUE` (`id` ASC);
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 'GENERATED ALWAYS AS () FIRST,
ADD PRIMARY KEY (`id`),
ADD UNIQUE INDEX `id_UNIQU' at line 2
I'm not the most eloquent with MySQL, so I'm relying entirely on the GUI for this. Any help is appreciated!
Have you tried checking what is the current primary key, and dropping that first?
(Sorry this is posted as an answer and not a comment, I do not have enough reputation yet)

I get an MYSQL error #1064 when add foreign key constraint

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)