Syntax error creating a table in my database - mysql

I'm getting error after trying to create a table for my data base
This is my script
CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
`user_role_id` int NOT NULL,
`first_name` varchar(250) NOT NULL,
`last_name` varchar(250) NOT NULL,
`name` varchar(250),
`email` varchar(250) NOT NULL,
`dni_type` varchar(32) NOT NULL,
`dni` int NOT NULL,
`phone` int,
`address` varchar(250),
`city` varchar(125),
`state` varchar(125),
`country` varchar(32),
`zip` varchar(32),
`created_at` datetime,
`created_by` int,
`updated_at` datetime,
`updated_by` int,
`deleted_at` datetime,
`deleted_by` int
);
I'm getting
#1064 - Something is wrong in your syntax near 'CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
`u...' on line 2
Why is the error generated, and how can I avoid it in the future?

CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT
Don't use `
CREATE TABLE `users` (
id int PRIMARY KEY NOT NULL AUTO_INCREMENT

Related

MySQL issue with CREATE TABLE film database

I am trying to create a table with multiple foreign keys which reference other parent tables. 1 of my foreign keys is being accepted 'fk_film_id' however the second is not due to a syntax error. how can I get this to pass?
the body of my tables is below:
create table `Actor_Role` (
`Actor_Role_id` INT AUTO_INCREMENT,
`Actor_Role` VARCHAR(20) NOT NULL,
`Character_Name` VARCHAR(50) NOT NULL,
`Alias_Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`Actor_Role_id`)
);
create table `Actor` (
`Actor_id` INT AUTO_INCREMENT,
`First_Name` VARCHAR(20) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`Date_of_Birth` DATE NOT NULL,
`Nationality` VARCHAR(20) NOT NULL,
`Gender` VARCHAR(10) NOT NULL,
`Residence` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Actor_id`)
);
create table `Film` (
`Film_id` INT AUTO_INCREMENT,
`Title` VARCHAR(50) NOT NULL,
`Release_Date` DATE NOT NULL,
`Running_Time` INT NOT NULL,
`Budget` BIGINT NOT NULL,
`Box_Office` BIGINT NOT NULL,
`Rating` INT NOT NULL,
`Language` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Film_id`)
);
create table `Film_Staff` (
`Staff_id` INT AUTO_INCREMENT,
`First_Name` VARCHAR(20) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`Nationality` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Staff_id`)
);
create table `Staff_Role` (
`Staff_Role_id` INT AUTO_INCREMENT,
`Staff_Role` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Staff_Role_id`)
);
create table `Infinity_Stones` (
`Stone_id` INT AUTO_INCREMENT,
`Colour` VARCHAR(10) NOT NULL,
`Power` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Stone_id`)
);
The table I am trying to make looks like this:
CREATE TABLE Film_Staff_Role(
-> `Film_id` INT NOT NULL,
-> `Role_id` INT NOT NULL,
-> `Staff_id` INT NOT NULL,
-> FOREIGN KEY fk_film_id(Film_id)
-> REFERENCES film(Film_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> FOREIGN KEY fk_Role_id(Role_id)
-> REFERENCES Staff_Role(Staff_Role_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> FOREIGN KEY fk_Staff_id(Staff_id)
-> REFERENCES Film_Staff(Staff_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> )ENGINE=InnoDB;
However I receive the following issue when I attempt to execute:
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 fk_Staff_Role_id(Staff_Role_id)
REFERENCES Staff_Role(Staff_Role_id' at line 9
How can this be overcome?

MySQL assigning primary key for id is not working while creating table

Hi i am trying to create table like this but it is not working:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(70) NOT NULL,
`email` varchar(70) NOT NULL,
);
Where as when i create like this it is working fine
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(70) NOT NULL,
`email` varchar(70) NOT NULL,
PRIMARY KEY (`id`)
);
I'm confused what is wrong in my first query.?
It's not the primary key what is causing your trouble the syntax is correct.
The last line has a syntax error, the comma on the last line is wrong.
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(70) NOT NULL,
`email` varchar(70) NOT NULL,
)
should be
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(70) NOT NULL,
`email` varchar(70) NOT NULL
)

MySQL error while trying to "translate" MsSQL to MySQL

I am trying to create two tables on a MySQL database, with the same characteristics of its MsSQL version but i get a syntax error. Any suggestions please?
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT ((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
);
CREATE TABLE locations
(
`lat` DECIMAL (10, 6) DEFAULT ((0)) NOT NULL,
`lon` DECIMAL (10, 6) DEFAULT ((0)) NOT NULL,
`dt` DATETIME NULL,
`id` INT auto_increment NOT NULL,
`owner` INT NULL
);
EDIT: The error i get is
SQL query:
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT ((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
)
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 '((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (' at line 7
Try this.
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT 1 NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE locations
(
`lat` DECIMAL (10, 6) DEFAULT 0 NOT NULL,
`lon` DECIMAL (10, 6) DEFAULT 0 NOT NULL,
`dt` DATETIME DEFAULT NULL,
`id` INT AUTO_INCREMENT NOT NULL,
`owner` INT ,
PRIMARY KEY (`id`)
);
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT 1 NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
)

Cannot add foreign key SQL fiddle

I'm having issues with the foriegn key in my table. If any can point out the error, please do, I have no idea what it is.
CREATE TABLE IF NOT EXISTS `user_account` (
`accountID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL,
`typeID` int(10) UNSIGNED NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`accountID`),
FOREIGN KEY (`typeID`) REFERENCES account_type(`typeID`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `account_type`(
`typeID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeName` VARCHAR (20) NOT NULL,
`description` VARCHAR (255),
PRIMARY KEY (`typeID`),
) ENGINE = InnoDB;
As far as I'm concerned, my datatypes are fine.
Thanks for the help.
Just do them in the opposite order after you fix the TYPO extra comma in the first table below.
CREATE TABLE IF NOT EXISTS `account_type`(
`typeID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeName` VARCHAR (20) NOT NULL,
`description` VARCHAR (255),
PRIMARY KEY (`typeID`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `user_account` (
`accountID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL,
`typeID` int(10) UNSIGNED NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`accountID`),
FOREIGN KEY (`typeID`) REFERENCES account_type(`typeID`)
) ENGINE = InnoDB;

Error 1072: Key Column `course_id` does not exist

I'm trying to make a simple table with foreign keys but it seems there's a problem with the id. I've tried everything but still comes up with an error message please help. Thanks.
CREATE TABLE `staff_info`(
`staff_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`office_add` VARCHAR(45) NOT NULL,
`email` INT NULL,
`changed` TIMESTAMP,
PRIMARY KEY (`staff_id`),
FOREIGN KEY(`course_id`)
REFERENCES `course_info`(`course_id`)
)
ENGINE = InnoDB;
CREATE TABLE `course_info`(
`course_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`module` VARCHAR(45) NOT NULL,
`StartDate` TIMESTAMP,
`EndDate` TIMESTAMP,
`course_Update` TIMESTAMP,
PRIMARY KEY(`course_id`),
FOREIGN KEY(`student_id`)
REFERENCES student(`student_id`)
)
ENGINE = InnoDB;
CREATE TABLE `student` (
`student_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`end_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`email` VARCHAR(25) NULL,
`phone` VARCHAR(20) NOT NULL,
`student_update`TIMESTAMP,
PRIMARY KEY(`student_id`)
);
Your table destruct and creation orders are invalid
CREATE TABLE `student` (
`student_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`end_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`email` VARCHAR(25) NULL,
`phone` VARCHAR(20) NOT NULL,
`student_update`TIMESTAMP,
PRIMARY KEY(`student_id`)
);
CREATE TABLE `course_info`(
`course_id` INT NOT NULL AUTO_INCREMENT,
`student_id` INT,
`name` VARCHAR(45) NOT NULL,
`module` VARCHAR(45) NOT NULL,
`StartDate` TIMESTAMP,
`EndDate` TIMESTAMP,
`course_Update` TIMESTAMP,
PRIMARY KEY(`course_id`),
FOREIGN KEY(`student_id`)
REFERENCES student(`student_id`)
)
CREATE TABLE `staff_info`(
`staff_id` INT NOT NULL AUTO_INCREMENT,
`course_id` INT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`office_add` VARCHAR(45) NOT NULL,
`email` INT NULL,
`changed` TIMESTAMP,
PRIMARY KEY (`staff_id`),
FOREIGN KEY(`course_id`)
REFERENCES `course_info`(`course_id`)
)
set CREATE TABLE student before CREATE TABLE course_info
you use student_id when does not exist studen,
and use course_id when does not exist course
In your SQL Query for TABLE student, you are missing
ENGINE = InnoDB
Because course table is creating FOREIGN KEY from table student.
Also you need to create tables in the order.
student
course_info
staff_info