Mysql syntax Error when creating a table #1064 - mysql

When I create a table in my localhost and it says
#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 'NOT NULL,
register_date DATETIME NOT NULL,
last_login DATETIME NOT NUL' at line 6"
(WampServer v 3.0.6 64bit, Apache 2.4.23 - MySql 5.7.14)
I've already try to change DB engine into InnoDB but the error was still appear.
Here is my Query
CREATE TABLE `inventry`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(225) NOT NULL,
`email` VARCHAR(225) NOT NULL,
`password` VARCHAR(300) NOT NULL,
`usertype` ENUM(0) NOT NULL,
`register_date` DATETIME NOT NULL,
`last_login` DATETIME NOT NULL,
`notes` VARCHAR(225) NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB;
U P D A T E D******
I want store data in this field. but when i save this it getting error. How can i fix this.?

Try below -
CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(225) NOT NULL,
email VARCHAR(225) NOT NULL,
password VARCHAR(300) NOT NULL,
usertype ENUM('0') NOT NULL,
register_date DATETIME NOT NULL,
last_login DATETIME NOT NULL,
notes VARCHAR(225) NOT NULL,
primary key (id)
)

just put, ENUM values, in quotes
CREATE TABLEinventry.users(
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(225) NOT NULL,
email VARCHAR(225) NOT NULL,
password VARCHAR(300) NOT NULL,
usertype ENUM(**'0'**) NOT NULL,
register_dateDATETIME NOT NULL,
last_loginDATETIME NOT NULL,
notesVARCHAR(225) NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;

Related

Sql error syntaxes with phpmyadmin

Good day,
I have an issue using the phpmyadmin for my database, I'm in new in this and this is the mysql structure from previewmysql:
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM(0) NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15) NULL,
`marital_status` ENUM(0) NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM(0) NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM(0) NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
The error is:
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 , `email` VARCHAR(20) NOT NULL , `phone` INT(15) NULL , `marital_sta' at line 1
And also attached is my phpmyadmin table structure.
Will appreciate any help.
Try it.Hope errors goes boom.I just fixed your errors. But your table structure is not good enough. give time, then day by day you will also be expert on it.
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM('0','1') NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15),
`marital_status` ENUM('0','1') NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM('0','1') NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM('0','1') NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
Modify ENUM declaration as ENUM ('male', 'female') for gender column and others also as shown in your table. It will not accept ENUM(0).
ENUM(0) is wrong format , if you want that for gender roles then you can use :-
ENUM('Male', 'Female') i.e you can run this query :-
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM('Male', 'Female') NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15) NULL,
`marital_status` ENUM('Single','Married','divorced') NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM('no','yes') NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM('Cape Town','Woodstock') NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
You have used ENUM data type in your table. And provided 0 as argument but
An enumeration value must be a quoted string literal
You can refer the mySql documentation for more information
http://dev.mysql.com/doc/refman/5.7/en/enum.html

What is wrong with my code for phpmyadmin?

I tried to make a database is phpmyadmin, but I keep getting an 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) NULL,
Datum Verkocht DATE NULL,
Personeel VARCHAR(30) NULL,
`Min' at line 9
That's for this code
CREATE TABLE `veilingen`.`Veilingen`(
`Nr` INT(3) NOT NULL,
`Artikel` VARCHAR(100) NULL,
`Datum Geveild` DATE NULL,
`Tel. Verkoper` INT(10) NULL,
`Datum Ingeleverd` DATE NULL,
`Kast`
SET
(0) NULL,
`Datum Verkocht` DATE NULL,
`Personeel` VARCHAR(30) NULL,
`Minimum` DECIMAL NULL,
`Koop Nu` DECIMAL(100) NULL,
`Geveild Voor` DECIMAL(100) NULL,
`Betaald` ENUM(0) NULL,
`Cadeaubon` DECIMAL(100) NULL,
`Versturen` VARCHAR(100) NULL,
`Opmerkingen` VARCHAR(100) NULL,
PRIMARY KEY(`Nr`(1))
) ENGINE = MyISAM;
Sorry for the mess, I can't get the code span to work.
Can somebody help me?
Following is the right syntax for create table in php, you can edit this code according to your table name,column name and it's datatype.
CREATE TABLE employee (
empId int(11) NOT NULL AUTO_INCREMENT,
empName varchar(255) NOT NULL,
empDept varchar(255) NOT NULL,
PRIMARY KEY (empId)
)

Error trying to create table in MySQL via terminal

I am trying to create a table in my database via terminal.
Here is my syntax:
CREATE TABLE `users` (
PRIMARY KEY(id) NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHAR NOT NULL,
`gender` VARCHAR NOT NULL,
`fav_color` VARCHAR NOT NULL,
`birthdate` DATE NOT NULL
);
I am getting 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 'NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHA' at line 2
What am i dong wrong here?
Besides the issues that Jens and Marc pointed out, You have to declare the length of your Varchar fields in order for this statement to work, like so:
CREATE TABLE `test`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(45) NOT NULL,
`first_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`fav_color` VARCHAR(45) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY (`id`));
the syntax of your create statement is wrong:
the correct one is this:
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(255) NOT NULL,
`first_name` VARCHAR(255) NOT NULL,
`gender` VARCHAR(255) NOT NULL,
`fav_color` VARCHAR(255) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY(`id`)
);
For more Information see the offical documentation.
it should be
id int primary key auto_increment not null
You're trying to define a primary key on a field that doesn't exist. Keys cannot be "not null" and definitely cannot be "auto_increment".

#1067 - Invalid default value for 'LastUpdated'

I made my ER diagram via MySQL WorkBench and exported it as a sql. Below is code for one table.
CREATE TABLE IF NOT EXISTS `xxx`.`Agent` (
`idAgent` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(100) NOT NULL,
`RegistrationNumber` VARCHAR(45) NOT NULL,
`RegistrationDate` DATE NULL,
`DateOfDealStart` DATE NULL,
`AddressLine1` VARCHAR(100) NULL,
`AddressLine2` VARCHAR(100) NULL,
`Country` VARCHAR(45) NULL,
`CurrentStatus` TINYINT(1) NOT NULL,
`DateCreated` TIMESTAMP NOT NULL,
`LastUpdated` TIMESTAMP NOT NULL,
PRIMARY KEY (`idAgent`))
ENGINE = InnoDB;
However when I am trying to import the script into MySQL via PhpMyAdmin, it says below.
#1067 - Invalid default value for 'LastUpdated'
I have not provided any default value, so how can it could be "Invalid"?

MySQL Create Table Error 1064

I am trying to create a table in MySQL but it doesn't want to play:
create table traders(
traderID INT(9) ZEROFILL NOT NULL AUTO_INCREMENT UNSIGNED,
traderProfileName VARCHAR(64) NOT NULL,
traderPassword CHAR(128) NOT NULL,
traderFirstName VARCHAR(40) NOT NULL,
traderSurname VARCHAR(40) NOT NULL,
traderContactPhone VARCHAR(14) NOT NULL,
locationPostCode CHAR(4) NOT NULL,
traderEmail VARCHAR(120) NOT NULL,
traderBio VARCHAR(255) DEFAULT NULL,
traderReviewRating DECIMAL(5,2) DEFAULT NULL,
traderLastLogin DATETIME DEFAULT NULL,
PRIMARY_KEY(traderID)
);
And I am getting 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 'UNSIGNED,
traderProfileName VARCHAR(64) NOT NULL,
traderPassword CHAR(128) NOT ' at line 2"
Is this something simple as I am using incorrect parameters for the table settings?
When you use UNSIGNED you must put it right beside the data type, that is: INT UNSIGNED.
Your corrected CREATE statement should look like this:
create table traders(
traderID INT(9) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,
-- ^^^^^^^^^^^^^^^ Here is the problem.
-- Also, you can define this column as a primary key +---^^^^^^^^^^^
-- directly in the column definition |
traderProfileName VARCHAR(64) NOT NULL,
traderPassword CHAR(128) NOT NULL,
traderFirstName VARCHAR(40) NOT NULL,
traderSurname VARCHAR(40) NOT NULL,
traderContactPhone VARCHAR(14) NOT NULL,
locationPostCode CHAR(4) NOT NULL,
traderEmail VARCHAR(120) NOT NULL,
traderBio VARCHAR(255) DEFAULT NULL,
traderReviewRating DECIMAL(5,2) DEFAULT NULL,
traderLastLogin DATETIME DEFAULT NULL,
);
You may ask, "Why?" that's because there are two "types" of integer:
INT signed (which can store values from -2147483648 to 2147483647)
INT UNSIGNED (which can store values from 0 to 4294967295)
Reference:
MySQL reference: Data types > Numeric types > Integer types
Auto increment is an integer by default, no need to define unsigned.
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
try this
create table traders
(
traderID INT(9) ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,
traderProfileName VARCHAR(64) NOT NULL,
traderPassword CHAR(128) NOT NULL,
traderFirstName VARCHAR(40) NOT NULL,
traderSurname VARCHAR(40) NOT NULL,
traderContactPhone VARCHAR(14) NOT NULL,
locationPostCode CHAR(4) NOT NULL,
traderEmail VARCHAR(120) NOT NULL,
traderBio VARCHAR(255) DEFAULT NULL,
traderReviewRating DECIMAL(5,2) DEFAULT NULL,
traderLastLogin DATETIME DEFAULT NULL
);
no need for unsigned
use primary key in same line as trader(id)
here working demo
You have some MySQL syntax errors. Here's the fix:
CREATE TABLE IF NOT EXISTS `traders` (
`traderID` INT(9) NOT NULL AUTO_INCREMENT,
`traderProfileName` VARCHAR(64) NOT NULL,
`traderPassword` CHAR(128) NOT NULL,
`traderFirstName` VARCHAR(40) NOT NULL,
`traderSurname` VARCHAR(40) NOT NULL,
`traderContactPhone` VARCHAR(14) NOT NULL,
`locationPostCode` CHAR(4) NOT NULL,
`traderEmail` VARCHAR(120) NOT NULL,
`traderBio` VARCHAR(255) DEFAULT NULL,
`traderReviewRating` DECIMAL(5,2) DEFAULT NULL,
`traderLastLogin` DATETIME DEFAULT NULL,
PRIMARY KEY (`traderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;
I would add the preferred engine, charset, collation and auto increment start number.
If you'd like to do so just substitute the last line with:
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;
And modify as desired.
Otherwise leave the closing parenthesis.
);