DROP TABLE IF EXISTS userRole;
DROP TABLE IF EXISTS account;
CREATE TABLE account (
lionID VARCHAR(50) NOT NULL,
firstName VARCHAR(50) NOT NULL,
hashedpass VARCHAR(255) NOT NULL,
PRIMARY KEY (lionID)
);
CREATE TABLE roles (
roleID INT UNSIGNED AUTO_INCREMENT NOT NULL,
lionID VARCHAR(50) NOT NULL,
administrator BOOLEAN NOT NULL DEFAULT False,
qRole VARCHAR(255) NOT NULL,
PRIMARY KEY (roleID),
FOREIGN KEY (lionID) REFERENCES account(lionID)
);
I am having trouble understanding why I am getting an error.. the foreign key lionID in roles is referencing the primary key lionID in account but it doesn't seem to like it. Any help is greatly appreciated.
ERROR 1064 (42000) at line 76: 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 lionID REFERENCES account lionID'
)' at line 8
I think the problem is in create statement:
You need to use Syntax:
CONSTRAINT `fk_account`
FOREIGN KEY (lionID) REFERENCES account(lionID)
Related
I'm trying to create mysql db table as below, but it got a syntax error at line 4.
CREATE TABLE findable_drivers (
id BIGINT NOT NULL AUTO_INCREMENT,
driver_id BIGINT NOT NULL,
current_role VARCHAR(100) NOT NULL, #here
lat DOUBLE NOT NULL,
lng DOUBLE NOT NULL,
findable BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT findable_drivers_PK PRIMARY KEY (id),
CONSTRAINT findable_drivers_UN UNIQUE KEY (driver_id),
CONSTRAINT findable_drivers_driver_FK FOREIGN KEY (driver_id) REFERENCES driver(id)
)
ENGINE=InnoDB
DEFAULT CHARSET=latin1
COLLATE=latin1_swedish_ci ;
the error description:
#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 'current_role VARCHAR(100) NOT NULL,
lat DOUBLE NOT NULL,
lng DOUBLE NOT N...' at line 4
CURRENT_ROLE() is actually a MySQL/MariaDB system function which returns active roles for the current session. So in your CREATE TABLE statement, the database thinks you are trying to call this function. You should call the current_role column something else. If you must stick with this name, then will have to forever refer to it using backticks:
CREATE TABLE findable_drivers (
id BIGINT NOT NULL AUTO_INCREMENT,
driver_id BIGINT NOT NULL,
`current_role` VARCHAR(100) NOT NULL, -- must use backticks here
lat DOUBLE NOT NULL,
lng DOUBLE NOT NULL,
findable BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT findable_drivers_PK PRIMARY KEY (id),
CONSTRAINT findable_drivers_UN UNIQUE KEY (driver_id),
CONSTRAINT findable_drivers_driver_FK FOREIGN KEY (driver_id) REFERENCES driver(id)
) ENGINE=InnoDB
DEFAULT CHARSET=latin1
COLLATE=latin1_swedish_ci;
Tim is correct
It would be good if you use different column name than the defined keywords.
I am currently struggling to comprehend why the first MySQL code does not work for the position foreign key. Meanwhile, when I add the backtick to Position it allows the creation of the table.
I had searched if Position is a reserved word for MySQL and could not find any mention of it being a reserved word.
So I was wondering if anybody knows why it does not work.
CREATE TABLE Employee (
IDEmployee INT AUTO_INCREMENT UNIQUE NOT NULL ,
IDPosition INT NOT NULL,
IDPerson INT NOT NULL,
EmployeeNumber VARCHAR(25) NOT NULL,
Username VARCHAR(25) NOT NULL,
Password VARCHAR(256) NOT NULL,
PRIMARY KEY (IDEmployee),
FOREIGN KEY (IDPosition) REFERENCES Position(IDPosition),
FOREIGN KEY (IDPerson) REFERENCES Person(IDPerson)
);
CREATE TABLE Employee (
IDEmployee INT AUTO_INCREMENT UNIQUE NOT NULL ,
IDPosition INT NOT NULL,
IDPerson INT NOT NULL,
EmployeeNumber VARCHAR(25) NOT NULL,
Username VARCHAR(25) NOT NULL,
Password VARCHAR(256) NOT NULL,
PRIMARY KEY (IDEmployee),
FOREIGN KEY (IDPosition) REFERENCES `Position`(IDPosition),
FOREIGN KEY (IDPerson) REFERENCES Person(IDPerson)
);
mysql> CREATE TABLE Employee (
-> IDEmployee INT AUTO_INCREMENT UNIQUE NOT NULL ,
-> IDPosition INT NOT NULL,
-> IDPerson INT NOT NULL,
-> EmployeeNumber VARCHAR(25) NOT NULL,
-> Username VARCHAR(25) NOT NULL,
-> Password VARCHAR(256) NOT NULL,
-> PRIMARY KEY (IDEmployee),
-> FOREIGN KEY (IDPosition) REFERENCES Position(IDPosition),
-> FOREIGN KEY (IDPerson) REFERENCES Person(IDPerson)
-> );
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 'Position(IDPosition),
FOREIGN KEY (IDPerson) REFERENCES Person(IDPerson)
)' at line 9
This error is because POSITION() is a function.
mysql> help position;
Name: 'POSITION'
Description:
Syntax:
POSITION(substr IN str)
POSITION(substr IN str) is a synonym for LOCATE(substr,str).
URL: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html
I have some doubt in my table
CREATE TABLE user_roles(
user_role_id int(10) unsigned not null,
user_id int(10) unsigned not null,
authority varchar(45) not null,
PRIMARY KEY(user_role_id),
KEY FK_user_roles (user_id),
CONSTRAINT FK_user_roles FOREING KEY (user_id) REFERENCES users (user_id))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
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
'FOREING KEY (user_id) REFERENCES users (user_id)) ENGINE=InnoDB DEFAULT CHARSET=' at line 7
Simple mistake
FOREIGN not FOREING
Hi all I can't find the error in this table creation bit, seems really straight forward to be, here's what it's giving me:
ERROR 1064 at line 3: 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 'FOREIGN KEY(courses_courseDepartmentAbbv))' at
line 8
DROP TABLE IF EXISTS courses;
CREATE TABLE courses(
courses_courseNumber INT NOT NULL AUTO_INCREMENT,
courses_courseTitle VARCHAR(25) NOT NULL,
courses_courseTeacher VARCHAR(30) NOT NULL,
courses_courseCostOfBooks DECIMAL(5,2) NOT NULL,
courses_courseDepartmentAbbv CHAR(4) NOT NULL,
PRIMARY KEY (courses_courseNumber),
FOREIGN KEY (courses_courseTeacher),
FOREIGN KEY (courses_courseDepartmentAbbv)
);
DROP TABLE IF EXISTS departments;
CREATE TABLE departments(
departments_departmentAbbv CHAR(4) NOT NULL,
departments_departmentFullName VARCHAR(15) NOT NULL,
PRIMARY KEY (departments_departmentAbbv),
FOREIGN KEY (departments_departmentAbbv) REFERENCES (courses_courseDepartmentAbbv)
);
DROP TABLE IF EXISTS teachers;
CREATE TABLE teachers(
teachers_teacherName VARCHAR(20) NOT NULL,
teachers_teacherHomeroom SMALLINT(3) NOT NULL,
teachers_teacherHomeroomGrade SMALLINT(1) NOT NULL,
teachers_teacherFullTime BOOL NOT NULL,
PRIMARY KEY (teachers_teacherName),
FOREIGN KEY (teachers_teacherName) REFERENCES (courses_courseTeacher)
);
You need to have a References after each Foreign key. You are missing that in the beginning setup of courses. Here is the documentation
I think this is more of what you want. Your order of creation was not correct. You had foreign keys in the wrong location due to this. You only set up foreign key mappings on the tables with the relations to the PK. You only need to set up the PK on your other tables. As long as you create the tables in the right order, then you can do this, as below.
So, teachers and departments have primary keys that are foreign keys within the courses table. teachers and departments does not need to worry about the foreign key. You leave that up to the table that actually has the reference (courses)
DROP TABLE IF EXISTS teachers;
CREATE TABLE teachers(
teachers_teacherName VARCHAR(20) NOT NULL,
teachers_teacherHomeroom SMALLINT(3) NOT NULL,
teachers_teacherHomeroomGrade SMALLINT(1) NOT NULL,
teachers_teacherFullTime BOOL NOT NULL,
PRIMARY KEY (teachers_teacherName)
--FOREIGN KEY (teachers_teacherName) REFERENCES courses (courses_courseTeacher)
--This is not where you set up the FK for courses
);
DROP TABLE IF EXISTS departments;
CREATE TABLE departments(
departments_departmentAbbv CHAR(4) NOT NULL,
departments_departmentFullName VARCHAR(15) NOT NULL,
PRIMARY KEY (departments_departmentAbbv)
--FOREIGN KEY (departments_departmentAbbv) REFERENCES courses (courses_courseDepartmentAbbv)
--This is not where you set up the FK for courses
);
CREATE TABLE courses(
courses_courseNumber INT NOT NULL AUTO_INCREMENT,
courses_courseTitle VARCHAR(25) NOT NULL,
courses_courseTeacher VARCHAR(30) NOT NULL,
courses_courseCostOfBooks DECIMAL(5,2) NOT NULL,
courses_courseDepartmentAbbv CHAR(4) NOT NULL,
PRIMARY KEY (courses_courseNumber),
FOREIGN KEY (courses_courseTeacher)
REFERENCES teachers (teachers_teacherName)
FOREIGN KEY (courses_courseDepartmentAbbv)
REFERENCES departments(departments_departmentAbbv)
);
You need to give a REFERENCES clause for your FOREIGN KEY clauses
FOREIGN KEY (courses_courseTeacher)
FOREIGN KEY (courses_courseDepartmentAbbv)
Define engine type that should be innodb which supports FOREIGN KEY constraints.
Follow Syntax defined here
I'm experiencing problems with this MySQL statement:
CREATE TABLE Articoli (Cod_Articolo char(10) NOT NULL,
Des_Articolo varchar(50) NOT NULL,
Cat_Articolo char(2) NOT NULL,
Ubi_Articolo char(6) NOT NULL,
PRIMARY KEY (Cod_Articolo)
FOREIGN KEY (Cat_Articolo) REFERENCES Categorie(Cod_Categoria) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
I get this 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 'FOREIGN KEY
(Cat_Articolo) REFERENCES Categorie(Cod_Categoria) ON DELETE NO ACTI' at line 6
I don't see why...
Thanks, Mauro
You are missing a comma here:
PRIMARY KEY (Cod_Articolo)
Try this:
CREATE TABLE Articoli (Cod_Articolo char(10) NOT NULL,
Des_Articolo varchar(50) NOT NULL,
Cat_Articolo char(2) NOT NULL,
Ubi_Articolo char(6) NOT NULL,
PRIMARY KEY (Cod_Articolo),
FOREIGN KEY (Cat_Articolo) REFERENCES Categorie(Cod_Categoria) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;