What could be wrong here - mysql

DROP TABLE IF EXISTS 'ci_sessions';
CREATE TABLE 'ci_sessions' (
'session_id' varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
'ip_address' varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
'user_agent' varchar(120) COLLATE utf8_bin DEFAULT NULL,
'last_activity' int(10) unsigned NOT NULL DEFAULT '0',
'user_data' text COLLATE utf8_bin NOT NULL, PRIMARY KEY ('session_id'),
KEY 'last_activity_idx' ('last_activity') )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I've tried to run this code on phpMyAdmin 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 ''ci_sessions'' at line 1

It's the same error in all your table and column names. You use ' while you should use backticks:
`
So this should work:
DROP TABLE IF EXISTS `ci_sessions`;
CREATE TABLE `ci_sessions` (
`session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
`ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
`user_agent` varchar(120) COLLATE utf8_bin DEFAULT NULL,
`last_activity` int(10) unsigned NOT NULL DEFAULT '0',
`user_data` text COLLATE utf8_bin NOT NULL, PRIMARY KEY (`session_id`),
KEY `last_activity_idx` (`last_activity`) )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
As a sidenote - you use NOT NULL and then set the default value to the string 0. quite often. That indicates bad practice, or at least more work for later queries. A column named last_activity for example indicates that null values should be allowed...
Second sidenote - last_activity would typically be a DATETIME column, not an int(10)

Related

Why is sql not letting me create this table?

I'm trying to migrate a db
from: MySQL Distrib 5.5.60-MariaDB, for Linux (x86_64)
to: MySQL 5.5.4, UNIX
I tried importing the db as a zip package and it started throwing errors so now I'm trying to re-create each table one at a time on phpMyAdmin.
The query below is throwing a #1064 Syntax error, and I'm having trouble figuring out the issue.
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 '(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),
`st' at line 6
I'm looking at line 6, trying to find any reserved words, missing data, typos, and or obsolete commands but no luck.
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP,
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I could use some help.
Thanks in advance.
CURRENT_TIMESTAMP is not good
Try this:
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This works in SQL Fiddle:
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
i.e., remove the precision (the (2)) from the definition of the date_added column.
TIMESTAMP(2) is valid syntax, but not in combination with the DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP auto-initializers.

Error when importing to server

SQL query:
CREATE TABLE `wp_layerslider` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`author` int(10) NOT NULL DEFAULT '0',
`name` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
`slug` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
`data` mediumtext COLLATE utf8mb4_unicode_520_ci NOT NULL,
`date_c` int(10) NOT NULL,
`date_m` int(10) NOT NULL,
`schedule_start` int(10) NOT NULL DEFAULT '0',
`schedule_end` int(10) NOT NULL DEFAULT '0',
`flag_hidden` tinyint(1) NOT NULL DEFAULT '0',
`flag_deleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
MySQL said: Documentation
1273 - Unknown collation: 'utf8mb4_unicode_520_ci'
When attempting to import a database. I'm very new to all of this, hoping someone can assist in simple terms for me
could be you are using an oldeversion of mysql db .. and the collate you need is not avialable .. you could use try alter the table or change the collate
ALTER TABLE `wp_layerslider` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

XAMPP's PHPMyAdmin Ignores AUTO_INCREMENT Attribute When Exporting the Table

The problem that I am going to tell is for all the tables in the DB, When I dump my DB it ignores AUTO_INCREMENT attribute. My actual table is as the following:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL AUTO_INCREMENT,
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
But if I dump the table using PHPMyAdmin it does not add auto_increment which I mentioned before.
The output of the exported .sql file's content is here:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL, -- AUTO_INCREMENT is missing
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
I especially checked that if auto_increment is disabled in CREATE TABLE options but no, it is not.
This was fixed in phpMyAdmin version 4.5.0.1 (Sep 2015):
https://github.com/phpmyadmin/phpmyadmin/issues/11492
I supposed it ignores auto increment but I explore file and at the bottom of sql I found that it altered to auto increment somewhere else

Whats wrong in my sql query?

Here I'm trying to create a new table, but I don't know what I did wrong when creating the table.
SQL query:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT COMMENT,
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_active tinyint(1) NOT NULL DEFAULT '0' COMMENT,
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_password_reset_timestamp bigint(20) DEFAULT NULL COMMENT,
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_failed_logins tinyint(1) NOT NULL DEFAULT '0' COMMENT,
user_last_failed_login int(10) DEFAULT NULL COMMENT,
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';
When I run this I get an error. I've added ; after use wan_ecommerce;, but I'm still getting:
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 '
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_passwo' at line 2
What am I doing wrong here?
How can I solve this?
Use semicolon to terminate each statement.
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce; -- <----- semi colon was missing
CREATE TABLE IF NOT EXISTS users (
-- ....
The 'COMMENT' is a keyword. If you use it,you must to add a description to flowing 'COMMENT',like this code:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'name',
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'password_hash',
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'email',
user_active tinyint(1) NOT NULL DEFAULT '0' COMMENT 'active',
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'activation_hash',
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'password_reset_hash',
user_password_reset_timestamp bigint(20) DEFAULT NULL COMMENT 'password_reset_timestamp',
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'rememberme_token',
user_failed_logins tinyint(1) NOT NULL DEFAULT '0' COMMENT 'failed_logins',
user_last_failed_login int(10) DEFAULT NULL COMMENT 'last_failed_login',
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';
If you don't need this 'COMMENT',you can remove it.The code like this:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL,
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL,
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL,
user_active tinyint(1) NOT NULL DEFAULT '0',
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
user_password_reset_timestamp bigint(20) DEFAULT NULL,
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
user_failed_logins tinyint(1) NOT NULL DEFAULT '0',
user_last_failed_login int(10) DEFAULT NULL,
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';

incomprehensible error on this simple create table query

I've just exported the CREATE sql of a singular table using phpmyadmin. this is the result:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`register_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`account_type` int(11) NOT NULL,
`active` int(11) NOT NULL,
`email` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`login` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
But when i run this code on the very phpmyadmin i receive 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 '(`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREME' at line 14
I have tried many ways, but my skills aren't enough.
You forgot to add comma before PRIMARY KEY (id)
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL, -- <== this
PRIMARY KEY (`id`)
add comma and it will work, see this fiddle (click link)