mySQL Syntax Error on Constraint - mysql

CREATE TABLE MARINA_SLIP
(
SLIP_ID VARCHAR(4),
MARINA_NUM VARCHAR(4),
SLIP_NUM VARCHAR(4),
LENGTH INT,
RENTAL_FEE DECIMAL(8,2),
BOAT_NAME VARCHAR(50),
BOAT_TYPE VARCHAR(50),
OWNER_NUM VARCHAR(4),
CONSTRAINT MARINA_SLIP_SLIP_ID_PK PRIMARY KEY (SLIP_ID),
CONSTRAINT MARINA_SLIP_MARINA_NUM_FK FOREIGN KEY (MARINA_NUM),
CONSTRAINT MARINA_SLIP_OWNER_NUM_FK FOREIGN KEY (OWNER_NUM) REFERENCES OWNER (OWNER_NUM)
);
The error states:
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 '
CONSTRAINT MARINA_SLIP_OWNER_NUM_FK FOREIGN KEY (OWNER_NUM) REFERENCES OWNER (O' at line 12
Does anyone know why this won't work?
Edit: this was figured out for me, small mistake on not including a foreign key reference.

add REFERENCES to CONSTRAINT MARINA_SLIP_MARINA_NUM_FK FOREIGN KEY (MARINA_NUM)
for example
CONSTRAINT MARINA_SLIP_MARINA_NUM_FK FOREIGN KEY (MARINA_NUM) REFERENCES OWNER (MARINA_NUM)

Related

Is there is any method to sove this error of MYSQL workbench when working on database?

Apply changes to exams Error 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 '
CONSTRAINT `reg`
FOREIGN KEY (`reg`)
REFERENCES `students`.`stdinf...
at line 6 SQL Statement:
CREATE TABLE `students`.`exams`
(
`reg`VARCHAR(25) NOT NULL,
`code`VARCHAR(10) NOT NULL,
`marks` INT NULL,
PRIMARY KEY (`reg`, `code`),
INDEX `code_idx` (`code`ASC) VISIBLE,
CONSTRAINT`reg`
FOREIGN KEY (`reg`)
REFERENCES `students`.`stdinfo` (`reg`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `code`
FOREIGN KEY (`code`)
REFERENCES `students`.`courses` (`code\`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
I wanted to make foreign key in third table for connecting my other two tables but it showing this error.

Trouble with mysql, does not allow multiple foreign keys

I have three tables in my database
Books(table)
create table books (book_id int auto_increment, bookName varchar(10), qty int,primary key(book_id));
Students(table)
create table students (student_id int auto_increment,studentName varhchar(10),primary key(student_id));
issuedBooks(table)
create table issuedBooks(book_id int ,student_id int ,issued_date date, foreign key (book_id) references books(book_id), foreign key (students) references students(student_id));
mysql report an error that
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 'references students(student_id))' at line 1
When I remove second foreign key, It works. I don't understand what mistake did I do?
Works(without any error, I just removed second foreign key)
create table issuedBooks(book_id int ,student_id int,issued_date date, foreign key (book_id) references books(book_id));

I am trying to create a table with only two foreign key

I have searched a lot but I could get only the concept that this is used for many to many linking. Or separate syntax for foreign key and primary key. But could not correct syntax as a whole.
CREATE TABLE cart
(
Customer varchar(40) FOREIGN KEY REFERENCES users(UserName),
Product varchar(40) FOREIGN KEY REFERENCES products(PID),
CONSTRAINT combination PRIMARY KEY (Customer,Product)
);
I am getting the error
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 'FOREIGN KEY REFERENCES users(UserName), Product varchar(40) FOREIGN KEY RE' at line 3
Foreign key definitions go after all of the column definitions, they aren't defined inside the column definition. In your case, it would rather be:
CREATE TABLE cart
(
Customer varchar(40),
Product varchar(40),
FOREIGN KEY (Customer) REFERENCES users(UserName),
FOREIGN KEY (Product) REFERENCES products(PID),
PRIMARY KEY (Customer,Product)
);

mysql- cannot add foreign key constraint

When I am trying to add foreign key constraint to the course table mysql said:
1215 - Cannot add foreign key constraint
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department
on delete set null
);
The ddl statement I used to create the table department is:
create table department
(dept_name varchar(20),
building varchar(15),
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);
I have used set foreign_key_checks=0 before creating any table.
The LATEST FOREIGN KEY ERROR says:
Syntax error close to:
on delete set null
Try including the column name in the foreign key definition:
foreign key (dept_name) references department(dept_name)
Here is a SQL Fiddle.
Note that the check constraint is parsed, but not enforced. MySQL accepts the syntax but does nothing.

MySQL 1064 Error Creating a Linking Table (Many-to-Many)

I'm trying to understand why a parsing error occurred (1064) with the following 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 '' at line 3>
CREATE TABLE Party_Library
(
Party INT(11)
Library varchar(40)
PRIMARY KEY (Library,Party)
FOREIGN KEY (Party) REFERENCES Party(PartyKey) ON DELETE CASCADE
FOREIGN KEY (Library) REFERENCES MusicLibraries(MusicSource) ON DELETE CASCADE
)
You are missing commas after each declaration:
CREATE TABLE Party_Library
(
Party INT(11),
Library varchar(40),
PRIMARY KEY (Library,Party),
FOREIGN KEY (Party) REFERENCES Party(PartyKey) ON DELETE CASCADE,
FOREIGN KEY (Library) REFERENCES MusicLibraries(MusicSource) ON DELETE CASCADE
);