I am trying to create a table that references another table in my db however I am receiving this 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 'UInt32 PRIMARY KEY, name VARCHAR(20), handle VARCHAR(20), countryName VARCHAR(20' at line 1
I have had a look at error #1064 on the mySQL refrence page but its abit of a vuage error just giving the outline of its structure.
Error: 1064 SQLSTATE: 42000 (ER_PARSE_ERROR)
Message: %s near '%s' at line %d
this is the code I have tried to execute
CREATE TABLE User (userID UInt32 PRIMARY KEY, name VARCHAR(20), handle VARCHAR(20), countryName VARCHAR(20) references Countries(SID))engine=innodb;
Countries table has already been created, so I am not entirely sure what is causing the problem
any help would be greatly appreciated.
added not this is how I have defined countries
CREATE TABLE Countries (countryName VARCHAR(20) PRIMARY KEY)engine=innodb;
UPDATED response:
Moving the constraint definition out of the table definition helped isolate the problem:
CREATE TABLE User(
userID UInt32 PRIMARY KEY
,name VARCHAR(20)
,handle VARCHAR(20)
,countryName VARCHAR(20)
)engine=innodb;
ALTER TABLE user ADD CONSTRAINT user_country_name_fk FOREIGN KEY(countryName) REFERENCES Countries(SID);
Table creation failure was caused by the use of the UInt32 data type. Changed to int, table creation succeeded.
Foreign key constraint was referring to the wrong column in Countries. It was referring to SID but needed to refer to countryName.
Related
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 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));
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.
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.
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.