getting error in check constraint with modulus operator - mysql

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

Related

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.

MySQL "ALTER IGNORE TABLE" Error In Syntax

I am trying to run the query:
ALTER IGNORE TABLE test_table DROP PRIMARY KEY, ADD PRIMARY KEY(id);
test_table is just a temporary table I'm using for testing purposes, and id is a column in it.
The query works fine without the "IGNORE" key word, but when I add it I get the 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 'IGNORE TABLE test_table ADD PRIMARY KEY(id)' at line 1"
How do I fix this? Or how can I run that query and ignore any errors that might occur? Thanks!
As of MySQL 5.7.4, the IGNORE clause for ALTER TABLE is removed and its use produces an error. Please check your version

Tracking down basic 1064 Error

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

How to add named not null constraint

I am trying to add a NOT NULL constraint by the name 'NN_Grade' in existing column 'grade'.
but not able to do getting error -
My question is -
Add a constraint (NN_Grade) in table Empl that declares column Grade NOT NULL.
My Command which I tried-
ALTER TABLE students ADD CONSTRAINT NN_Grade NOT NULL(adm_no);
Error -
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 'NOT NULL(adm_no)' at line 1
Need guidance
ALTER TABLE students ADD CONSTRAINT NN_Grade CHECK (adm_no IS NOT NULL);
Why not just make the column not nullable, i.e.
ALTER TABLE Empl MODIFY Grade *TYPE* NOT NULL;
SqlFiddle here
Note that your question mentions table Empl whereas your answer uses students