SQL syntax error, closing bracket expected - mysql

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

Related

[42000][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 [duplicate]

This question already has answers here:
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Closed 3 months ago.
I have created tables like before, with the given primary and foreign keys. However I get this error when I try to create a new table with the code below.
create table Order (
oid int(255),
sid int(255),
sku int(255),
quantity int(255),
foreign key (sid) references Suppliers(sid),
foreign key (sku) references Parts(sku),
primary key(sid,sku)
)
and I have created Suppliers and Parts tables with the code below
create table Parts(
sku int(255) auto_increment primary key,
pname varchar(255),
stock_level int(255),
color varchar(255)
)
create table Suppliers (
sid int(255) auto_increment primary key,
sname varchar(255),
city varchar(255),
street varchar(255)
)
sid and sku already exist in their respective tables. I do not understand why I get such an error.
The complete output is:
[42000][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 'Order( oid int (255), sid int (255), sku int(255), quantity
i' at line 1
I solved the problem with creating the table with the name "Orders" instead of just Order. I think it got confused with the order by keyword hence the syntax error.
I suppose it chokes on INT(255).
INT is a signed four-byte integer. Its max positive value is 2147483647.
So, just use INT and not INT(255), and it will work.
Check this page on the topic, for example:
https://blog.devart.com/mysql-int-data-type.html#what_is_mysql_integer

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 7

CREATE TABLE MK_Dosen (
No INT NOT NULL,
Kode CHAR(6),
Kelas CHAR(2),
NIK CHAR(7),
FOREIGN KEY (Kode)
);
I have tried and seen the material presented by the lecturer but I still can't find the reason for this syntax error. Can anyone help me?
this is the picture
You should give the foreign key a reference column of others table
ex: table demo and field is field (type shoulde be same with MK_Dosen.Kode)
CREATE TABLE MK_Dosen (
No INT NOT NULL,
Kode CHAR(6),
Kelas CHAR(2),
NIK CHAR(7),
FOREIGN KEY (Kode) REFERENCES demo(field)
);

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.

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

MySQL Syntax Error

This is my SQL Script
CREATE TABLE account_profile
(
accnt_id int NOT NULL ,
first_name varchar(255),
last_name varchar(255),
biography varchar(255),
date_joined DATE,
country varchar(255),
gender varchar(255),
screename varchar(255),
path varchar(255),
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
)
it kept giving me this error
SQL query:
CREATE TABLE ac
MySQL said:
#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 1
What does this specifically mean? what should I do? from my point of view the script is quite okay,I just can't pin point where's the error
Make sure you're using the InnoDB engine. The other engines do not support foreign keys.
CREATE TABLE account_profile
(
...
) ENGINE = INNODB;
Also check if the column account_profile.accnt_id matches the data type of accounts.id exactly. The second column should have an index defined on it (a primary key will do.)
I have tested your query and it works with me too. Do you have a query before creating the table account_profile? because if you do, try to check if the query before has been terminated by a semi-colon. like this:
Create table TableName
(
-- fields list
); -- <== don't forget this before creating another table again.
CREATE TABLE account_profile
(
accnt_id int NOT NULL ,
first_name varchar(255),
last_name varchar(255),
biography varchar(255),
date_joined DATE,
country varchar(255),
gender varchar(255),
screename varchar(255),
path varchar(255),
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
); -- <== and also this.
the error says near line 1.
Probably your table account does not exists :
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
That's why you've got an error, the request is correct otherwise.