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.
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'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 cant seem to figure out whats wrong with this after looking online and trying a few things;
CREATE TABLE PRODUCT (
PRODID NUMERIC(6),
DESCRIP CHAR(30),
CONSTRAINT PRODUCT_PRIMARY_KEY KEY)
;
i keep getting this error. Any help would be appreciated. thanks.
Static analysis:
1 errors were found during analysis.
A closing bracket was expected. (near ")" at position 127)
SQL query:
CREATE TABLE PRODUCT ( PRODID NUMERIC(6), DESCRIP CHAR(30),
CONSTRAINT PRODUCT_PRIMARY_KEY KEY)
MySQL said: Documentation
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 'KEY)' at line 4
Try below query
CREATE TABLE PRODUCT (
PRODID NUMERIC(6),
DESCRIP CHAR(30),
CONSTRAINT PRODUCT_PRIMARY_KEY PRIMARY KEY (PRODID)
);
You can try It
CREATE TABLE PRODUCT (
PRODID int NOT NULL AUTO_INCREMENT,
DESCRIP varchar(30) NOT NULL,
PRIMARY KEY (PRODID)
);
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`);
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.