SQL syntax error - mysql

CREATE TABLE `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`type` ENUM( `member` , `admin` ) NOT NULL ,
`username` VARCHAR( 30 ) NOT NULL ,
`email` VARCHAR( 80 ) NOT NULL ,
`pass` VARBINARY( 32 ) NOT NULL ,
`first_name` VARCHAR( 20 ) NOT NULL ,
`last_name` VARCHAR( 40 ) NOT NULL ,
`date_expires` DATE NOT NULL ,
`date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`date_modified` TIMESTAMP NOT NULL DEFAULT `0000-00-00 00:00:00` ,
PRIMARY KEY ( `id` ) ,
UNIQUE KEY `username` ( `username` ) ,
UNIQUE KEY `email` ( `email` )
) ENGINE = MYISAM DEFAULT CHARSET = utf8;
MySQL said:
#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 'member,admin) NOT NULL, username VARCHAR(30) NOT NULL,
email VARCHAR(80)' at line 3
The shared server I am using, uses 4.4. Thanks for reading. I am an absolute novice, having only been learning php/mysql for one month, so please speak in layman's terms.

You need to quote ENUM values
type ENUM( 'member' , 'admin' ) NOT NULL ,
not backtick them as you do now
Same thing for this line
`date_modified` TIMESTAMP NOT NULL DEFAULT `0000-00-00 00:00:00`
should be
`date_modified` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'

This page indicates that your ENUM values should be strings, so need to be surrounded by single quotes.
type ENUM( 'member' , 'admin' ) NOT NULL

okey fixed it here is it :)
CREATE TABLE `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
type ENUM( 'member' , 'admin' ) NOT NULL ,
`username` varchar(30) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`first_name` varchar (15) NOT NULL,
`last_name` varchar (30) NOT NULL,
`gender` ENUM('male', 'female') NOT NULL default 'male',
`email` varchar(50) NOT NULL default '',
`skype` varchar(50) NOT NULL default 'Not Specified',
`facebook` varchar(150) NOT NULL default 'Not Specified',
`location` varchar(100) NOT NULL default 'Not Specified',
PRIMARY KEY (`id`),
UNIQUE KEY (`email`),
KEY (`email`, `password`)
) ENGINE = MYISAM DEFAULT CHARSET = utf8;

Related

How to resolve an error near NULL when creating a table

Whenever I try to create a table
CREATE TABLE registration` (`id` INT NOT NULL , `name` VARCHAR(30) NOT NULL , `email` VARCHAR(20) NOT NULL , `password` VARCHAR(15) NOT NULL , `DOB` DATE NOT NULL , `age` INT NOT NULL , `number` BIGINT NOT NULL , `religion` VARCHAR(10) NOT NULL , `education` VARCHAR(20) NOT NULL , `profession` VARCHAR(20) NOT NULL , `gender` ENUM NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
The following error occurs
#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 , PRIMARY KEY (id)) ENGINE = InnoDB' at line 1
ENUM needs values so you need to define the values they can get
CREATE TABLE registration (
`id` INT NOT NULL
, `name` VARCHAR(30) NOT NULL
, `email` VARCHAR(20) NOT NULL
, `password` VARCHAR(15) NOT NULL
, `DOB` DATE NOT NULL
, `age` INT NOT NULL
, `number` BIGINT NOT NULL
, `religion` VARCHAR(10) NOT NULL
, `education` VARCHAR(20) NOT NULL
, `profession` VARCHAR(20) NOT NULL
, `gender` ENUM ('male','female')
, PRIMARY KEY (`id`)
) ENGINE = InnoDB;
When you declare a ENUM column (your gender column) you have to assign all possible values for the enumeration (See here for syntax)

error upload .sql file of wordpress site

this is the 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 'AS ical subscribe access key,
created_on datetime DEFAULT NULL,
`updated' at line 20
Erreur
requĂȘte SQL:
-- --------------------------------------------------------
--
-- Structure de la table `pec_calendars`
--
CREATE TABLE `pec_calendars` (
`id` INT( 11 ) UNSIGNED NOT NULL ,
`type` ENUM( 'user', 'group', 'url' ) DEFAULT 'user',
`user_id` INT( 11 ) UNSIGNED DEFAULT NULL ,
`name` VARCHAR( 100 ) DEFAULT NULL ,
`description` TEXT,
`color` VARCHAR( 7 ) DEFAULT NULL ,
`admin_id` INT( 11 ) DEFAULT NULL ,
`status` ENUM( 'on', 'off' ) DEFAULT 'on',
`show_in_list` ENUM( '0', '1' ) DEFAULT NULL ,
`public` TINYINT( 3 ) UNSIGNED DEFAULT '0',
`reminder_message_email` TEXT,
`reminder_message_popup` TEXT,
`access_key` VARCHAR( 32 ) DEFAULT NULL COMMENT AS `ical subscribe access key` ,
`created_on` DATETIME DEFAULT NULL ,
`updated_on` DATETIME DEFAULT NULL
) ENGINE = INNODB DEFAULT CHARSET = utf8;
Guess your query should be:
CREATE TABLE pec_calendars
(
id INT(11) UNSIGNED NOT NULL,
type ENUM('user', 'group', 'url') DEFAULT 'user',
user_id INT(11) UNSIGNED DEFAULT NULL,
name VARCHAR(100) DEFAULT NULL,
description TEXT,
color VARCHAR(7) DEFAULT NULL,
admin_id INT(11) DEFAULT NULL,
status ENUM('on', 'off') DEFAULT 'on',
show_in_list ENUM('0', '1') DEFAULT NULL,
public TINYINT(3) UNSIGNED DEFAULT '0',
reminder_message_email TEXT,
reminder_message_popup TEXT,
access_key VARCHAR(32) DEFAULT NULL,
created_on DATETIME DEFAULT NULL,
updated_on DATETIME DEFAULT NULL
)
engine = innodb
DEFAULT charset = utf8;

Table creation failed: (1064) You have an error in your SQL syntax

What's wrong with my code?
$query = "CREATE TABLE IF NOT EXISTS 'data_table' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'name' VARCHAR(255) NOT NULL,
'active' TINYINT NOT NULL DEFAULT '1',
'create_date' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'income' INT DEFAULT NULL,
PRIMARY KEY ('id')
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
$createTable = mysqli_query($connect, $query);
you do not need single quotes for the table and column names try without that:you can use " or backticks instead.
CREATE TABLE IF NOT EXISTS data_table(
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
name VARCHAR( 255 ) NOT NULL ,
active TINYINT NOT NULL DEFAULT '1',
create_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
income INT DEFAULT NULL ,
PRIMARY KEY ( id )
) ENGINE = MYISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT =1
Try:
$query = "CREATE TABLE IF NOT EXISTS `data_table` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`active` TINYINT NOT NULL DEFAULT 1,
`create_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`income` INT DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
Note that the single quote is replaced with the backtick

Why doesn't the follow SQL table creation work?

CREATE TABLE `auth_user` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`password` VARCHAR( 128 ) COLLATE utf8_bin NOT NULL ,
`last_login` DATETIME( 6 ) DEFAULT NULL ,
`is_superuser` TINYINT( 1 ) NOT NULL ,
`username` VARCHAR( 30 ) COLLATE utf8_bin NOT NULL ,
`first_name` VARCHAR( 30 ) COLLATE utf8_bin NOT NULL ,
`last_name` VARCHAR( 30 ) COLLATE utf8_bin NOT NULL ,
`email` VARCHAR( 254 ) COLLATE utf8_bin NOT NULL ,
`is_staff` TINYINT( 1 ) NOT NULL ,
`is_active` TINYINT( 1 ) NOT NULL ,
`date_joined` DATETIME( 6 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE KEY `username` ( `username` )
) ENGINE = INNODB AUTO_INCREMENT =2 DEFAULT CHARSET = utf8 COLLATE = utf8_bin;
*\#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 '(6) DEFAULT NULL,
`is_superuser` tinyint(1) NOT NULL,
`username` varchar(30)' at line 4***
This is the auth_user table from a django project. The SQL file was exported from my local MySQL database, but I can't import to the online mysql database. Could somebody can help me?
You do not need the length for datetime
`date_joined` DATETIME( 6 ) NOT NULL ,
should be
`date_joined` DATETIME NOT NULL ,
https://dev.mysql.com/doc/refman/5.0/en/datetime.html

Cannot add or update a child row: a foreign key constraint fails mysql php

I am getting a
"Cannot add or update a child row: a foreign key constraint fails
(smarturbia.pois, CONSTRAINT fk_pois_cities1 FOREIGN KEY
(city) REFERENCES cities (id) ON DELETE NO ACTION ON UPDATE NO
ACTION) (1509)"
and i have no idea why.
My tables are generated through Mysql workbench 5.2, if you need the rest of the tables let me know :
-- -----------------------------------------------------
-- Table `smarturbia`.`cities`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `smarturbia`.`cities` ;
CREATE TABLE IF NOT EXISTS `smarturbia`.`cities` (
`id` BIGINT(11) NOT NULL AUTO_INCREMENT ,
`published` VARCHAR(1) NOT NULL DEFAULT '0' ,
`open` VARCHAR(1) NOT NULL DEFAULT '0' ,
`path` VARCHAR(25) NOT NULL DEFAULT 'taxi' ,
`key` VARCHAR(50) NOT NULL ,
`world` VARCHAR(10) NOT NULL DEFAULT 'earth' ,
`name` VARCHAR(50) NOT NULL ,
`description` VARCHAR(250) NULL DEFAULT NULL ,
`logo` VARCHAR(250) NULL DEFAULT NULL ,
`footer` VARCHAR(250) NOT NULL DEFAULT '/taxi/assets/img/taxi_smarturbia_image_default.png' ,
`footer_large` VARCHAR(250) NOT NULL ,
`leftpub` VARCHAR(50) NOT NULL ,
`rightpub` VARCHAR(50) NOT NULL ,
`model` VARCHAR(250) NOT NULL ,
`modelxscale` FLOAT NULL DEFAULT '1' ,
`modelyscale` FLOAT NULL DEFAULT '1' ,
`modelzscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelxscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelyscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelzscale` FLOAT NULL DEFAULT '1' ,
`allwheels` VARCHAR(250) NULL DEFAULT NULL ,
`frontleftwheel` VARCHAR(250) NULL DEFAULT NULL ,
`frontrightwheel` VARCHAR(250) NULL DEFAULT NULL ,
`rearleftwheel` VARCHAR(250) NULL DEFAULT NULL ,
`rearrightwheel` VARCHAR(250) NULL DEFAULT NULL ,
`axisdistance` FLOAT NOT NULL DEFAULT '2.5' ,
`wheelsdistance` FLOAT NULL DEFAULT '1' ,
`wheelsheight` FLOAT NULL DEFAULT '1' ,
`kms` FLOAT NULL DEFAULT '0' ,
`maxspeed` FLOAT NULL DEFAULT '160' ,
`accel` FLOAT NULL DEFAULT '25' ,
`accelstep` FLOAT NULL DEFAULT '25' ,
`minaccelstep` FLOAT NULL DEFAULT '5' ,
`maxrevspeed` FLOAT NULL DEFAULT '30' ,
`decel` FLOAT NULL DEFAULT '90' ,
`gravity` FLOAT NULL DEFAULT '70' ,
`camheight` FLOAT NULL DEFAULT '5' ,
`camtilt` FLOAT NULL DEFAULT '90' ,
`traildistance` FLOAT NULL DEFAULT '15' ,
`mass` FLOAT NULL DEFAULT '3000' ,
`vehicleagility` FLOAT NULL DEFAULT '0.0005' ,
`suspensionstiffness` FLOAT NULL DEFAULT '0.5' ,
`suspensionrestlength` FLOAT NULL DEFAULT '0.5' ,
`suspensiondamping` FLOAT NULL DEFAULT '-0.15' ,
`suspensiondeltatime` FLOAT NULL DEFAULT '0.25' ,
`turnspeedmin` FLOAT NULL DEFAULT '20' ,
`turnspeedmax` FLOAT NULL DEFAULT '60' ,
`speedmaxturn` FLOAT NULL DEFAULT '5' ,
`speedminturn` FLOAT NULL DEFAULT '50' ,
`steerroll` FLOAT NULL DEFAULT '-1' ,
`rollspring` FLOAT NULL DEFAULT '0.5' ,
`rollclamp` FLOAT NULL DEFAULT '50' ,
`mapiconurl` VARCHAR(250) NULL DEFAULT NULL ,
`vehicleshadow` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclesound` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclesoundtime` FLOAT NULL DEFAULT '150' ,
`vehiclefastsound` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclefastsoundtime` FLOAT NULL DEFAULT '150' ,
`backgroundsound` VARCHAR(250) NULL DEFAULT NULL ,
`backgroundsoundtime` FLOAT NULL DEFAULT '150' ,
`crashsound` VARCHAR(250) NULL DEFAULT NULL ,
`crashsoundtime` FLOAT NULL DEFAULT '150' ,
`vehicletype` VARCHAR(50) NULL DEFAULT 'car' ,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) ,
INDEX `key` (`key` ASC) )
ENGINE = InnoDB
AUTO_INCREMENT = 153
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `smarturbia`.`pois`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `smarturbia`.`pois` ;
CREATE TABLE IF NOT EXISTS `smarturbia`.`pois` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`city` BIGINT(11) NOT NULL ,
`name` VARCHAR(50) NOT NULL ,
`description` VARCHAR(250) NOT NULL ,
`lat` DOUBLE NOT NULL ,
`lon` DOUBLE NOT NULL ,
`heading` FLOAT NULL DEFAULT '0' ,
PRIMARY KEY (`id`) ,
INDEX `fk_pois_cities1_idx` (`city` ASC) ,
CONSTRAINT `fk_pois_cities1`
FOREIGN KEY (`city` )
REFERENCES `smarturbia`.`cities` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 408
DEFAULT CHARACTER SET = latin1;
Insert the values in the table in which your primary key is defined..Then try to insert in the table in which you having the foreign key...
I think you are inserting the values in the table of foreign key without inserting in the table having primary key..
It is not recommended but you can try this
set foreign_key_checks=0;
To enable foreign key checking
set foreign_key_checks=1;