I am trying to create a mysql table and am getting the following error:
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 19
here is the create table script:
CREATE TABLE User(
userID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (userID),
username char(30) NOT NULL UNIQUE,
lastName char(30) NOT NULL,
firstName char(30) NOT NULL,
birthDay int NOT NULL CHECK(birthDay BETWEEN 1 AND 31),
birthMonth int NOT NULL CHECK(birthMonth BETWEEN 1 AND 13),
birthYear int NOT NULL CHECK(birthYear > 1900),
password char(30) NOT NULL,
geschlecht char(30) NOT NULL,
profilPicURL char(255) NOT NULL,
CONSTRAINT fk_lives_in
FOREIGN KEY (lives_in)
REFERENCES Location(LocationID),
CONSTRAINT fk_comes_from
FOREIGN KEY (comes_from)
REFERENCES Location(LocationID),
);
i appreciate any help,
thanks!
CREATE TABLE User(
userID int NOT NULL AUTO_INCREMENT Primary Key,
username char(30) NOT NULL UNIQUE,
lastName char(30) NOT NULL,
firstName char(30) NOT NULL,
birthDay int NOT NULL CHECK(birthDay BETWEEN 1 AND 31),
birthMonth int NOT NULL CHECK(birthMonth BETWEEN 1 AND 13),
birthYear int NOT NULL CHECK(birthYear > 1900),
password char(30) NOT NULL,
geschlecht char(30) NOT NULL,
profilPicURL char(255) NOT NULL,
CONSTRAINT fk_lives_in
FOREIGN KEY (lives_in)
REFERENCES Location(LocationID),
CONSTRAINT fk_comes_from
FOREIGN KEY (comes_from)
REFERENCES Location(LocationID)
);
Try it now
Related
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)
);
I tried looking up the reasons why it's failing but I can't find the different data types issues mentioned in the solutions in my tables.
I'm getting the error when I'm trying to run the following:
comments (id int NOT NULL AUTO_INCREMENT,
user_id varchar(10) NOT NULL,
blog_id int NOT NULL,
comment varchar(150) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES user_details(id),
FOREIGN KEY(blog_id) REFERENCES blog(id));
existing data bases for primary keys:
user_details (id varchar(10) NOT NULL ,
name varchar(70) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(40) NOT NULL,
masked_password varchar(40) NOT NULL,
is_active varchar(10) DEFAULT 'False');
blog (id int NOT NULL AUTO_INCREMENT,
category_id int NOT NULL,
title varchar(240) NOT NULL,
body longtext NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(category_id) REFERENCES blog_catagories(id));
Let me know where I'm going wrong
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 have created the following script to set up my MySQL database:
CREATE DATABASE IF NOT EXISTS magicc_hat;
USE magicc_hat;
CREATE TABLE people (
personID INT NOT NULL AUTO_INCREMENT,
firstName VARCHAR(45) NOT NULL,
lastName VARCHAR(45),
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (personID)
);
CREATE TABLE categories (
categoryID INT NOT NULL AUTO_INCREMENT,
categoryName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (categoryID)
);
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
};
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
};
CREATE TABLE projects (
projectID INT NOT NULL AUTO_INCREMENT,
projectName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (projectID)
);
CREATE TABLE checkouts (
checkoutID INT NOT NULL AUTO_INCREMENT,
itemID INT NOT NULL,
personID INT NOT NULL,
projectID INT,
quantity INT NOT NULL,
outDateTime DATETIME,
inDateTime DATETIME,
outNotes TEXT,
inNotes TEXT,
checkedIn BOOL,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (checkoutID),
FOREIGN KEY (itemID) REFERENCES items(itemID),
FOREIGN KEY (personID) REFERENCES people(personID),
FOREIGN KEY (projectID) REFERENCES projects(projectID)
);
However, when I run this script I get the following error messages:
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 '}' at line 8
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 '}' at line 17
ERROR 1215 (HY000): Cannot add foreign key constraint
I suspect it has something to do with using the bool datatype since that is what is happening at lines 8 and 17; however I have not been able to find any solutions to this issue online. Is someone able to see what might be causing the problem? I'm running MySQL 5.6.11 on my local Windows machine.
Thanks!
The characters need to be the same. Use parentheses for both
CREATE TABLE homes (
};
In this case: It's showing you the incorrect character in the error message:
check the manual that corresponds to your MySQL server version for the right
syntax to use near '}' at line 8
Get rid of the curly braces and replace with (
Works great : http://sqlfiddle.com/#!8/4ad37
Try this one, I tested it and it created the database successfully. The issues were caused due to the opening and closing brackets not matching each other.
CREATE DATABASE IF NOT EXISTS magicc_hat;
USE magicc_hat;
CREATE TABLE people (
personID INT NOT NULL AUTO_INCREMENT,
firstName VARCHAR(45) NOT NULL,
lastName VARCHAR(45),
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (personID)
);
CREATE TABLE categories (
categoryID INT NOT NULL AUTO_INCREMENT,
categoryName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (categoryID)
);
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
);
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
);
CREATE TABLE projects (
projectID INT NOT NULL AUTO_INCREMENT,
projectName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (projectID)
);
CREATE TABLE checkouts (
checkoutID INT NOT NULL AUTO_INCREMENT,
itemID INT NOT NULL,
personID INT NOT NULL,
projectID INT,
quantity INT NOT NULL,
outDateTime DATETIME,
inDateTime DATETIME,
outNotes TEXT,
inNotes TEXT,
checkedIn BOOL,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (checkoutID),
FOREIGN KEY (itemID) REFERENCES items(itemID),
FOREIGN KEY (personID) REFERENCES people(personID),
FOREIGN KEY (projectID) REFERENCES projects(projectID)
);
you have 2 syntax errors as below...
And since below tables are not created, you are facing foreign key error..
run below sqls your problem will be solved :)
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
); /* previously }; */
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
); /* previously }; */
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
),
I have this as part of a database I'm setting up, yet whenever I try to run the .sql file that this is included in, I get 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 '
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(' at line 8
Any ideas what's wrong? Thanks.
Remove the , from the end CREATE TABLE statement
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)
Or use semicolon instead comma:
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
);
You have comma at the end if you remove that it is working fin for me.
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)
We don't use "," at the end to separate two sql queries, you have to use ";" and if you are running a single query then you don't need.
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)