Ways to resolve mysql #1064 error in the following code? - mysql

I tried using EVERSQL to validate the following code but its also showing error
You have an error in your SQL syntax; it seems the error is around: 'NOT NULL, DepartmentID int NOT NULL, CategoryID int NOT NULL, CourseName V' at line 2
this is my SQL query,
CREATE TABLE Course (
CourseID varchar NOT NULL,
deptID int NOT NULL,
catID int NOT NULL,
CourseName Varchar NOT NULL,
Credit varchar NOT NULL,
PRIMARY KEY (CourseID),
FOREIGN KEY (deptID) REFERENCES department(deptID),
FOREIGN KEY (catID) REFERENCES coursecategory(catID)
);
and this is the error I'm getting
#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 'NOT NULL,
deptID int NOT NULL,
catID int NOT NULL,
CourseName ...' at line 2

You need to specify the lengths of your varchar columns. E.g.:
CREATE TABLE Course (
CourseID varchar(10) NOT NULL,
deptID int NOT NULL,
catID int NOT NULL,
CourseName varchar(10) NOT NULL,
Credit varchar(10) NOT NULL,
PRIMARY KEY (CourseID),
FOREIGN KEY (deptID) REFERENCES department(deptID),
FOREIGN KEY (catID) REFERENCES coursecategory(catID)
);

Related

Creating table within mysql db, ERROR 1064 (42000)?

**Am working with MySQL 8.0 Command Line Client
I am trying to create a general table in mysql db but keep getting the following 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 'AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am I' at line 2"
I have look through the manual but haven't found anything that helps with this issue; have also tried removing the index (id) column and assigning it as the KEY
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO),
);
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (id)
);
Primary key should be id
Updated answer as per you comment
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO),
UNIQUE KEY (id)
);
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO), <--- remove comma from here
);
and make PRIMARY KEY(id);
I had the same error message, after a long time I realized that I was trying to create a table by name "order" and that was the problem.As soon as I changed the name it worked!!!

Error when uploading table to server

I am having trouble adding 3 tables to my database (MINOR_DEGREES, STUDENT, STUDENT_RECORD).
Can anyone assist?
Relational Map has been included if there is any confusion.
CREATE TABLE PROFESSOR(
PSSN int NOT NULL,
PName varchar(255) NOT NULL,
PStreet varchar(255) NOT NULL,
PCity varchar(255) NOT NULL,
PState varchar(255) NOT NULL,
PZip int NOT NULL,
PArea int NOT NULL,
PNum int NOT NULL,
PSex ENUM ('M','F') NOT NULL,
PTitle varchar(255) NOT NULL,
PSalary int NOT NULL,
PRIMARY KEY (PSSN)
);
CREATE TABLE DEGREES(
ProfessorSSN int NOT NULL,
PDegrees varchar(255) NOT NULL,
PRIMARY KEY (ProfessorSSN, PDegrees),
FOREIGN KEY (ProfessorSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE DEPARTMENT(
DNum int NOT NULL,
DName varchar(255) NOT NULL,
DPhone varchar(255) NOT NULL,
DOffice_Location varchar(255) NOT NULL,
PChairSSN int NOT NULL,
PRIMARY KEY (DNum),
FOREIGN KEY (PChairSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE PRE_REQ(
PreReqCNum int NOT NULL,
PreReqFor varchar(255) NOT NULL,
PreReqTo varchar(255) NOT NULL,
FOREIGN KEY (PreReqCNum) REFERENCES COURSE (CNum) ON DELETE CASCADE
);
CREATE TABLE COURSE(
CNum int NOT NULL,
CTitle varchar(255) NOT NULL,
CUnits int NOT NULL,
CTextBook varchar(255) NOT NULL,
Department_Num int NOT NULL,
PRIMARY KEY (CNum),
FOREIGN KEY (Department_Num) REFERENCES DEPARTMENT (DNum)
);
CREATE TABLE MINOR_DEGREES(
MinorCWID int NOT NULL,
MinorDNum int NOT NULL,
Minor varchar(255) NOT NULL,
PRIMARY KEY (MinorCWID, MinorDNum),
FOREIGN KEY (MinorCWID) REFERENCES STUDENT (SCWID) ON DELETE CASCADE,
FOREIGN KEY (MinorDNum) REFERENCES DEPARTMENT (DNum) ON DELETE CASCADE
);
CREATE TABLE STUDENT(
SCWID int NOT NULL,
SFname varchar(255) NOT NULL,
SLname varchar(255) NOT NULL,
SAdrress varchar(255) NOT NULL,
SPhone int NOT NULL,
Major varchar(255) NOT NULL,
MajorDeptNum int NOT NULL,
PRIMARY KEY (SCWID),
FOREIGN KEY (Major, MajorDeptNum).
);
CREATE TABLE SECTION(
CourseNum int NOT NULL,
SNum int NOT NULL,
Classroom varchar(255) NOT NULL,
Meet_Dates varchar(255) NOT NULL,
Time_Start int NOT NULL,
Time_End int NOT NULL,
No_Of_Seats int NOT NULL,
ProSSN int NOT NULL,
PRIMARY KEY (CourseNum, SNum),
FOREIGN KEY (CourseNum) REFERENCES COURSE (CNum),
FOREIGN KEY (ProSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE STUDENT_RECORD(
Student_SWID int NOT NULL,
Course_Number int NOT NULL,
Section_Number int NOT NULL,
Grade varchar(255)
PRIMARY KEY (Student_SWID, Course_Number, Section_Number),
FOREIGN KEY (Student_SWID) REFERENCES STUDENT (SCWID)
);
And the errors
Error for MINOR_DEGREES:
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 STUDENT (SCWID) ON DELETE CASCADE,
UNIQUE KEY (MinorDNum) REFERENCE' at line 6
Error for STUDENT:
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 ')' at line 11
Error for STUDENT_RECORD:
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 '(Student_SWID, Course_Number, Section_Number),
FOREIGN KEY (Student_SWID) RE' at line 6.

i got error when creating table with some foreign key in MySQL

i got error when creating table with some foreign key in MySQL, here is my script :
CREATE TABLE tbl_mt_kuliah (
KD_AUTO INT (4) NOT NULL AUTO_INCREMENT,
NIM INT (10) NOT NULL,
KD_MATA_KULIAH INT (4) NOT NULL,
RUANG VARCHAR (4) NOT NULL,
KD_DOSEN INT (4) NOT NULL,
JAM TIME NOT NULL,
HARI DATE NOT NULL,
TH_AJARAN YEAR NOT NULL,
SMESTER INT (2) NOT NULL,
PRIMARY KEY KD_AUTO,
FOREIGN KEY FK_MATKUL(KD_MATA_KULIAH) REFERENCES mt_kuliah (KD_MATA_KULIAH),
FOREIGN KEY FK_MHS(NIM) REFERENCES tbl_mahasiswa (NIM),
FOREIGN KEY FK_DOSEN(KD_DOSEN) REFERENCES tbl_dosen (KD_DOSEN)
);
and here is the error :
Error Code: 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 FK_MATKUL(KD_MATA_KULIAH) references mt_kuliah (KD_MATA_KULIAH),
' at line 12
I think this will help you.
CREATE TABLE tbl_mt_kuliah (
KD_AUTO INT (4) NOT NULL AUTO_INCREMENT,
NIM INT (10) NOT NULL,
KD_MATA_KULIAH INT (4) NOT NULL,
RUANG VARCHAR (4) NOT NULL,
KD_DOSEN INT (4) NOT NULL,
JAM TIME NOT NULL,
HARI DATE NOT NULL,
TH_AJARAN YEAR NOT NULL,
SMESTER INT (2) NOT NULL,
PRIMARY KEY (KD_AUTO),
CONSTRAINT FK_MATKUL FOREIGN KEY (KD_MATA_KULIAH) REFERENCES mt_kuliah (KD_MATA_KULIAH),
CONSTRAINT FK_MHS FOREIGN KEY (NIM) REFERENCES tbl_mahasiswa (NIM),
CONSTRAINT FK_DOSEN FOREIGN KEY (KD_DOSEN) REFERENCES tbl_dosen (KD_DOSEN)
);

MySQL MariaDB syntax 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 ''Nr_analizes' int NOT NULL, 'AnPacienti_id' int NOT NULL,
This is what I get when I run SQL code in mySQL. What is happening? The sintax looks fine to me.
CREATE TABLE Analiza_laboratorike(
'Nr_analizes' int NOT NULL,
'AnPacienti_id' int NOT NULL,
'Lloji_analizes' varchar(15) NOT NULL,
'Kategoria_pacientit' varchar(15) NOT NULL,
PRIMARY KEY(Nr_analizes),
FOREIGN KEY(AnMjeku_id) REFERENCES Mjeku(Mjeku_id),
Foreign KEY(AnPacienti_id) REFERENCES Pacienti(Pacienti_id))
Column or tables names shouldn't be enclosed in apostrophes:
CREATE TABLE Analiza_laboratorike(
Nr_analizes int NOT NULL,
AnPacienti_id int NOT NULL,
Lloji_analizes varchar(15) NOT NULL,
Kategoria_pacientit varchar(15) NOT NULL,
PRIMARY KEY(Nr_analizes),
FOREIGN KEY(AnMjeku_id) REFERENCES Mjeku(Mjeku_id),
Foreign KEY(AnPacienti_id) REFERENCES Pacienti(Pacienti_id))
Here the CREATE with back Ticks:
CREATE TABLE Analiza_laboratorike(
`Nr_analizes` INT NOT NULL,
`AnPacienti_id INT NOT NULL,
`Lloji_analizes` VARCHAR(15) NOT NULL,
`Kategoria_pacientit` VARCHAR(15) NOT NULL,
PRIMARY KEY(Nr_analizes),
FOREIGN KEY(AnMjeku_id) REFERENCES Mjeku(Mjeku_id),
FOREIGN KEY(AnPacienti_id) REFERENCES Pacienti(Pacienti_id))

What is wrong with my foreign key in mySQL?

Whenever I try to run this script in mySQL I get an error that reads:
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 ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIG' at line 7
The code for the table it is referencing is on top and the code for the table with the failing foreign key constraint is below:
CREATE TABLE CATEGORY(
Category_Name varchar(35) NOT NULL,
ShippingPerPound DECIMAL(4,2),
OffersAllowed ENUM('y', 'n'),
CONSTRAINT CATEGORY_pk PRIMARY KEY(Category_Name)
);
CREATE TABLE ITEM(
Item_Number int UNSIGNED AUTO_INCREMENT,
Item_Name varchar(35) NOT NULL,
Description varchar(255),
Model varchar(50) NOT NULL,
Price DECIMAL(8,2) NOT NULL,
Category_Name,
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIGN KEY (Category_Name) REFERENCES CATEGORY(Category_Name) ON UPDATE CASCADE
);
Your Category_Name declaration in yourITEM` table is incomplete:
Try:
CREATE TABLE ITEM(
Item_Number int UNSIGNED AUTO_INCREMENT,
Item_Name varchar(35) NOT NULL,
Description varchar(255),
Model varchar(50) NOT NULL,
Price DECIMAL(8,2) NOT NULL,
Category_Name varchar(35) NOT NULL,
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIGN KEY (Category_Name) REFERENCES CATEGORY(Category_Name) ON UPDATE CASCADE
);