Trouble with mysql, does not allow multiple foreign keys - mysql

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

Related

why does my foreign key code show a syntax error?

I'm attempting to create a table in a relational database that has a foreign key and a SQL syntax error consistently occurs.
I am using PHP and mySQL (i've removed any non interacting tables that only hold data and don't contribute to the relations)
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
buildingID int FOREIGN KEY REFERENCES buildings(buildingID))
buildings is another table, which has the primary key "buildingID"
the error that shows is
Fatal error: Uncaught mysqli_sql_exception: 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 buildings(buildingID), login varchar(255)...'
this error doesn't happen when the foreign key is removed, the table creates with no errors; and I create this table after i create the table that the foreign key references.
i have tried using the other syntax:
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT,
buildingID int,
PRIMARY KEY(loginID),
FOREIGN KEY (buildingID) REFERENCES buildings(buildingID))
to no avail: the same error happens
what is my mistake here, and how can I change my code to fix it? Thanks.
the error is that primary keys are not permitted to be unsigned, i changed this one thing in my code and the error went away
Few typos.
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT ,
buildingID int,
PRIMARY KEY(loginID),
FOREIGN KEY (buildingID) REFERENCES buildings(buildingID))
this works fine

I am unable to make a foreign key in the table. An error message keeps appearing

image of the code and error
CREATE TABLE Ticket(
-> Ticket_No INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
-> BusID INT,
-> Registration_No INT,
-> Seat_No INT NOT NULL,
-> FOREIGN KEY BusID REFERENCES Bus(BusID),
-> FOREIGN KEY Registration_No REFERENCES Passenger(Registration_No));
Error message:
ERROR 1064 (42000): 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 'REFERENCES Bus(BusID),
FOREIGN KEY Registration_No REFERENCES Passenger(Registra' at line 6
I am trying to add foreign keys to this table but this error message keeps appearing. The referenced table and column name is correct. I have also tried changing the column name. Please help.
You need to put parenthesis around the referencing column(s).
...
FOREIGN KEY (BusID) REFERENCES Bus(BusID),
...
And analog for the other one.

How to fix this it is showing Syntax error, ERROR 1064 (42000):

I'm trying to create a table with 2 foreign keys but unable to find the syntax error. Where do i need to make the changes?
create table Leave(
leaveId int primary key, noOfDays int, approverId varchar(50), requestorId varchar(50),
Foreign key (approverId) References Approver (approverId),
Foreign key(requestorId) References Requestor(requestorId)
);
These are the 2 tables create before creating leave table:
create table Approver(approverId int primary key, approverName varchar(50));
create table Requestor(requestorId int primary key, requestorName varchar(50), noOfLeavesApproved int, approverId int, dateOfApplication date, Foreign key(approverId) References Approver(approverId));
where can I make the changes for this syntax error?
It is showing the below error for the leave table
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 'Leave(leaveId int primary key, noOfDays int,
approverId varchar(50), requestorId' at line 1
Your problem is that Leave is a reserved word in MySQL (documentation). Therefore, it cannot be used by your objects
Change your table name to something else (example: leave_request) and it should work.

MySQL CONSTRAINT FOREIGN KEY syntax

I am new to MySQL and am messing with a database I created just to learn.
I created my first table and it worked OK:
mysql> CREATE TABLE Pizzas(ItemID INTEGER PRIMARY KEY, Size INTEGER, Price DECIMAL);
Query OK, 0 rows affected (0.90 sec)
then I tried to create another table using CONSTRAINT (I was simply copying the way foreign keys were created in a class I am taking) and I got an error:
mysql> CREATE TABLE Customers(CustomerID PRIMARY KEY, Name CHAR(20), ItemID INTEGER, CONSTRAINT FOREIGN KEY (ItemID) REFERENCES Pizzas (ItemID));
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 'PRIMARY KEY, Name CHAR(20), ItemID INTEGER, CONSTRAINT FOREIGN KEY (ItemID) REFE' at line 1
I'm using a linux computer and my MySQL version is 5.5. Why am I getting this problem?
your table definition is not correct. you are missing the datatype in CustomerID PRIMARY KEY. It should be
CREATE TABLE Customers(CustomerID int not null PRIMARY KEY,
<-- Here
Name CHAR(20), ItemID INTEGER,
CONSTRAINT FOREIGN KEY (ItemID) REFERENCES Pizzas (ItemID));

Unable to execute a foreign key in MySQL 5.5 (using XAMPP)

I am unable to execute a foreign key in MySQL 5.5 (using XAMPP).
Here is the code I am trying to execute:
create table Category (
Category_ID int,
CategoryName varchar(50),
Primary Key (Category_ID)
);
create table SubCategory (
SubCategory_ID int,
Category_ID int,
SubCategoryName varchar(50),
Primary Key (SubCategory_ID),
Foreign Key Category_ID references Category(Category_ID)
);
I tried replacing int with int(10) but it did not help.
Also, I tried adding ON CASCADE suff but it did not work.
Even adding CONSTRAINT inside and outside the table did not work.
Error I keep on 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 'references Category(Category_ID))'
Please help.
you need to wrap your foreign key with parenthesis like:
create table SubCategory (
SubCategory_ID int,
Category_ID int,
SubCategoryName varchar(50),
Primary Key (SubCategory_ID),
Foreign Key (Category_ID) references Category(Category_ID)
------------^-----------^
);