MySql table doesn't create when I try to add "AUTO_INCREMENT" - mysql

CREATE TABLE `userinfo`.`users`
( id MEDIUMINT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR NOT NULL ,
`password` VARCHAR NOT NULL ,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
Could anyone give me an insight as to why this create table function isn't working? 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 'NOT NULL ,
password VARCHAR NOT NULL ,
PRIMARY KEY (id))
ENGINE ...' at line 3
new to mysql so i have no clue what the problem is. I just want a table that has the primary key increment when i add to it. Thanks in advance.

CREATE TABLE `userinfo`.`users`
( id MEDIUMINT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(50) NOT NULL ,
`password` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
You need to specify maximum character limit for VARCHAR type as above. You can set it as per your requirement.

Related

MYSQL Error 1064 in Primary and Unique Keys

Good day. I am trying to add a suppliers table on my database and when i try to save, it throws this error.
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 '0) NOT NULL , PRIMARY KEY (supp_id), UNIQUE supp_name (supp_name))' at line 1
I have attached my SQL statement.
CREATE TABLE `project_inv`.`suppliers` (
`supp_id` INT(11) NOT NULL AUTO_INCREMENT ,
`supp_name` VARCHAR(255) NOT NULL ,
`supp_addr` VARCHAR(300) NOT NULL ,
`supp_phone` INT(20) NOT NULL ,
`supp_email` VARCHAR(255) NOT NULL ,
`supp_notes` VARCHAR(1000) NOT NULL ,
`status` ENUM(0) NOT NULL ,
PRIMARY KEY (`supp_id`),
UNIQUE `supp_name` (`supp_name`)
) ENGINE = InnoDB;
I think its something with my UNIQUE key but i really can't figure out what the error is. Any form of help will be appreciated. Thanks
It is not about the PRIMARY or UNIQUE declaration, but about ENUM(): it expects string literals, so you would need to surround the values in the list with single quotes:
CREATE TABLE `project_inv`.`suppliers` (
`supp_id` INT(11) NOT NULL AUTO_INCREMENT ,
`supp_name` VARCHAR(255) NOT NULL ,
`supp_addr` VARCHAR(300) NOT NULL ,
`supp_phone` INT(20) NOT NULL ,
`supp_email` VARCHAR(255) NOT NULL ,
`supp_notes` VARCHAR(1000) NOT NULL ,
`status` ENUM('0') NOT NULL ,
PRIMARY KEY (`supp_id`),
UNIQUE `supp_name` (`supp_name`)
) ENGINE = InnoDB;
However having a non-nullable ENUM() column with just one value allowed makes little sense - basically you are forcing every row to have the same value ('0'). So either add more values to the list... or just remove the column.
try like below by using string inside enum i used 'N' instated 0
CREATE TABLE `project_inv`.`suppliers` (
`supp_id` INT(11) NOT NULL AUTO_INCREMENT ,
`supp_name` VARCHAR(255) NOT NULL ,
`supp_addr` VARCHAR(300) NOT NULL ,
`supp_phone` INT(20) NOT NULL ,
`supp_email` VARCHAR(255) NOT NULL ,
`supp_notes` VARCHAR(1000) NOT NULL ,
`status` ENUM('N') NOT NULL ,
PRIMARY KEY (`supp_id`),
UNIQUE `supp_name` (`supp_name`)
) ENGINE = InnoDB;

How to fix Error 1064 while creating tables with foreign keys?

MySql workbench tells me I have an error in my SQL syntax that corresponds to my MariaDB server version and I don't know why.
I've already looked at other questions but none of them seem to help so I'm asking my own question now... I really don't know what's wrong with my code and I don't know what the problem with my syntax is. I've tried to set some comas or rename the columns etc and I changed the SQL Version in the Model to the sql version I'm using with xampp. But still, after changing the version I'm getting that error...
The sql to create the tables is (I've took the comments out):
CREATE SCHEMA IF NOT EXISTS `testDB` DEFAULT CHARACTER SET utf8 ;
USE `testDB` ;
CREATE TABLE IF NOT EXISTS `testDB`.`person` (
`personID` INT NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(45) NULL,
`lastname` VARCHAR(45) NULL,
PRIMARY KEY (`personID`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `testDB`.`comment` (
`commentID` INT NOT NULL AUTO_INCREMENT,
`comment` VARCHAR(45) NOT NULL,
`person_personID` INT NOT NULL,
PRIMARY KEY (`commentID`),
INDEX `fk_comment_person_idx` (`person_personID` ASC) VISIBLE,
CONSTRAINT `fk_comment_person`
FOREIGN KEY (`person_personID`)
REFERENCES `testDB`.`person` (`personID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
And the error I'm getting is:
Executing SQL script in server
ERROR: 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 '
CONSTRAINT `fk_comment_person`
FOREIGN KEY (`person_personID`)
REFERE' at line 9
SQL Code:
-- -----------------------------------------------------
-- Table `testDB`.`comment`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testDB`.`comment` (
`commentID` INT NOT NULL AUTO_INCREMENT,
`comment` VARCHAR(45) NOT NULL,
`person_personID` INT NOT NULL,
PRIMARY KEY (`commentID`),
INDEX `fk_comment_person_idx` (`person_personID` ASC) VISIBLE,
CONSTRAINT `fk_comment_person`
FOREIGN KEY (`person_personID`)
REFERENCES `testDB`.`person` (`personID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 6 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Remove the word VISIBLE in your statement:
CREATE SCHEMA IF NOT EXISTS `testDB` DEFAULT CHARACTER SET utf8 ;
USE `testDB` ;
CREATE TABLE IF NOT EXISTS `testDB`.`person` (
`personID` INT NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(45) NULL,
`lastname` VARCHAR(45) NULL,
PRIMARY KEY (`personID`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `testDB`.`comment` (
`commentID` INT NOT NULL AUTO_INCREMENT,
`comment` VARCHAR(45) NOT NULL,
`person_personID` INT NOT NULL,
PRIMARY KEY (`commentID`),
INDEX `fk_comment_person_idx` (`person_personID` ASC),
CONSTRAINT `fk_comment_person`
FOREIGN KEY (`person_personID`)
REFERENCES `testDB`.`person` (`personID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Because in MariaDB is not yet implemented the INVISIBLE option. This option is implemented in ORACLE

error when creating table SQL

I'm trying to create a table in my gym_system database. Here is the code I'm using:
CREATE TABLE `Gym_System`.`login` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(30) NOT NULL,
`password` CHAR(128) NOT NULL,
) ENGINE = InnoDB;
However, I keep getting this error:
#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 ') ENGINE = InnoDB' at line 5
Can anyone see why?
Remove the last comma
CREATE TABLE `login` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(30) NOT NULL,
`password` CHAR(128) NOT NULL
) ENGINE = InnoDB;
See fiddle demo

MySql syntax error while trying to create a table

I am trying to create a table in my database (hosted with godaddy)
when ever I try to create a table in the database it gives me an error
#1046 - No database selected
So I am trying to create a table like this
USE orderformuser
CREATE TABLE `users`(
`user_id` int NOT NULL AUTO_INCREMENT,
`username` char(25),
`password` char(40),
UNIQUE (User_Id)
)
However I am getting this error now.. and I just don't know what I am doing wrong
#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 'CREATE TABLE `users`(
`user_id` int NOT NULL AUTO_INCREMENT,
`username` char(2' at line 3
any help would be appreciated
You forgot to add semicolon ; after use .... ; is used to terminate lines in mysql.
USE orderformuser;
CREATE TABLE `users`
(
`user_id` int NOT NULL AUTO_INCREMENT,
`username` char(25),
`password` char(40),
CONSTRAINT u_pk PRIMARY KEY (User_Id)
);
First of all you have to create a blank database orderformuser.
After you have to entry into this database and try to create the table with your code changed because you have forgot ";" like this:
USE orderformuser;
CREATE TABLE `users`
(
`user_id` int NOT NULL AUTO_INCREMENT,
`username` char(25),
`password` char(40),
UNIQUE (User_Id)
)

Error in SQL statement

I cannot find this error in the sql, could anyone help?
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 'ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT, datetimeTIMESTAMP, title TEXT NOT' at line 1
CREATE TABLE tableName1 (
ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
timestamp TIMESTAMP NOT NULL,
title TEXT NOT NULL,
titleEdited TEXT,
description TEXT NOT NULL,
descriptionEdited TEXT,
URL VARCHAR(255) NOT NULL,
URLEdited VARCHAR(255),
imgPath VARCHAR(255) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY (URL, imgPath)
)
CREATE TABLE tableName2 (
ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
linkID MEDIUMINT(9) NOT NULL,
timestamp TIMESTAMP NOT NULL,
comment TEXT NOT NULL,
userID BIGINT(20) NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(linkID) REFERENCES tableName1(ID),
FOREIGN KEY(userID) REFERENCES users(ID)
)
I think the only thing that is wrong here is that mySQL needs a semicolon after each execution. You should just have to end each create table with a ; and this should work
Here is a SQL Fiddle to prove that. (Minus the FK to users since that table is not in your example)
Are you sure you are creating this tables and getting error?
I created them succesfuly.
And with looking error, it seems you are trying to create another field named datetime which type is timestamp? datetime is reserverd word. You need to use backticks.
'ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT, datetimeTIMESTAMP, title TEXT NOT' at line 1