mysql - Invalid default value - mysql

MySQL Distrib 5.5.31
I have dropped old DB because of incorrect charset. So I try to create new DB and load correct dump. Also I had an old version mysql before.
While creating a BD and loading a dump I am getting the following errors:
ERROR 1067 (42000) at line 225234: Invalid default value for 'ts_created'
ERROR 1146 (42S02) at line 225243: Table 'ardostore.toc_piwik_site' doesn't exist
ERROR 1067 (42000) at line 225252: Invalid default value for 'date_registered'
ERROR 1146 (42S02) at line 225263: Table 'ardostore.toc_piwik_user' doesn't exist
225234:
create table toc_piwik_site (
idsite int(10) unsigned not null auto_increment,
name varchar(90) not null,
main_url varchar(255) not null,
ts_created timestamp default 'CURRENT_TIMESTAMP' not null,
feedburnerName varchar(100),
PRIMARY KEY (idsite)
);
225243:
insert into toc_piwik_site (idsite, name, main_url, ts_created, feedburnerName) values ('1', 'Store Name', 'http://ardostore.com', '2012-10-18 19:58:49', NULL);
drop table if exists toc_piwik_site_url;
create table toc_piwik_site_url (
idsite int(10) unsigned not null,
url varchar(255) not null,
PRIMARY KEY (idsite, url)
);
225252:
create table toc_piwik_user (
login varchar(100) not null,
password char(32) not null,
alias varchar(45) not null,
email varchar(100) not null,
token_auth char(32) not null,
date_registered timestamp default 'CURRENT_TIMESTAMP' not null,
PRIMARY KEY (login),
UNIQUE uniq_keytoken (token_auth)
);
225263
insert into toc_piwik_user (login, password, alias, email, token_auth, date_registered) values ('toc_piwik_view', '5f4dcc3b5aa765d61d8327deb882cf99', 'toc_piwik_view', 'sonarius#gmail.com', 'a674bf3fe5d9c0651ac32b28fcbe74f8', '2012-10-18 19:58:49');
drop table if exists toc_piwik_user_dashboard;
create table toc_piwik_user_dashboard (
login varchar(100) not null,
iddashboard int(11) not null,
layout text not null,
PRIMARY KEY (login, iddashboard)
);
UPD
ERROR 1170 (42000) at line 225099: BLOB/TEXT column 'query' used in key specification without a ke y length
create table toc_piwik_log_profiling (
query text not null,
count int(10) unsigned,
sum_time_ms float,
UNIQUE query (query)
);
What's causing the issue? And how to fix?

default value 'CURRENT_TIMESTAMP' is a string. remove quotes and it will work:
create table toc_piwik_site (
idsite int(10) unsigned not null auto_increment,
name varchar(90) not null,
main_url varchar(255) not null,
ts_created timestamp default CURRENT_TIMESTAMP not null,
feedburnerName varchar(100),
PRIMARY KEY (idsite)
);

Remove the ' around `CURRENT_TIMESTAMP. Those make it a string.
Write it for example like this:
create table toc_piwik_site (
idsite int(10) unsigned not null auto_increment,
name varchar(90) not null,
main_url varchar(255) not null,
ts_created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
feedburnerName varchar(100),
PRIMARY KEY (idsite)
);

Related

MySQL error near syntax 'CREATE TABLE user_bans...'

I am working on my database that used to work fine until recently, here I keep getting the following error: 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 user_bans ( id INT PRIMARY KEY AUTO_INCREMENT, user INT NOT' at line 34, the code is below. I did my best to fix it and find answers, but none of them helped.
-- Create 'users' table
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
nickname VARCHAR(255) NOT NULL, -- Unlike the 'username' field, this one is NOT unique
email VARCHAR(255) DEFAULT NULL,
email_verified TINYINT(1) DEFAULT 0 NOT NULL,
email_verified_time INT DEFAULT NULL,
password VARCHAR(255) NOT NULL,
register_ip VARCHAR(255) NOT NULL,
last_ip VARCHAR(255) NOT NULL,
description TEXT DEFAULT NULL,
status VARCHAR(255) DEFAULT NULL,
profile_image VARCHAR(255) NOT NULL, -- Set a random one when creating
-- Ranks
admin TINYINT(1) DEFAULT 0 NOT NULL,
beta_tester TINYINT(1) DEFAULT 0 NOT NULL,
-- Options
profile_nsfw TINYINT(1) DEFAULT 0 NOT NULL,
profile_private TINYINT(1) DEFAULT 0 NOT NULL,
-- Moderation
deleted TINYINT(1) DEFAULT 0 NOT NULL,
banned TINYINT(1) DEFAULT 0 NOT NULL,
-- Time fields
time_registered INT NOT NULL, -- Set to current UNIX timestamp when creating
time_last_seen INT NOT NULL -- Set to current UNIX timestamp when going offline
);
-- Create 'user_bans' table
CREATE TABLE user_bans (
id INT PRIMARY KEY AUTO_INCREMENT,
user INT NOT NULL,
banned_by INT NOT NULL,
time_ban INT NOT NULL, -- Set to current UNIX timestamp when creating
time_unban INT NOT NULL,
ban_category VARCHAR(255) NOT NULL,
content TEXT DEFAULT NULL,
mod_note TEXT DEFAULT NULL,
status VARCHAR(255) DEFAULT 'banned' NOT NULL,
unbanned_by INT DEFAULT NULL,
FOREIGN KEY (user)
REFERENCES users (id)
ON DELETE CASCADE,
FOREIGN KEY (banned_by)
REFERENCES users (id)
ON DELETE CASCADE
);
This continues to happen even if the tables consist of just a single field.
According to the font color, "user" looks like a reserved word in your system,
try using back ticks `` to escape it.

MYSQL error #1072 - Key column 'id ' doesn't exist in table in XAMPP, phpmyadmin

I am trying to create a new table using the mysql GUI in phpmyadmin. This is the error I am getting #1072 - Key column 'id ' doesn't exist in table
CREATE TABLE `loginsys`.`groups`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`permission` TEXT NOT NULL,
PRIMARY KEY(`id `)
) ENGINE = InnoDB
Please correct the extra space in when you're defining PRIMARY KEY(id )
This should be your query :-
CREATE TABLE `loginsys`.`groups`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`permission` TEXT NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB
simply run the query without "`"
CREATE TABLE loginsys.groups(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
permission TEXT NOT NULL,
PRIMARY KEY(id))

Multiple Errors in Uploading Database Columns MySQL

I am new to MySQL and am uploading a database for the first time. I am not uploading the data, just the columns for now. The issue is that I get an error for every line of code provided:
#1064 - You have an error in your SQL syntax;
I can understand if I was getting the message for just one or two lines of the code, but I am beginning to think I misformatted all the database file since I'm getting that error message for every line.
I previously changed the primary/foreign keys
CONSTRAINT `PurchasePK` PRIMARY KEY (`P_ORDERNO`)
CONSTRAINT `PurchaseFK` FOREIGN KEY (`SUPPLY_CODE`)
to: P_ORDERNO int(5) NOT NULL auto_increment PRIMARY KEY,
And still get errors.
I would appreciate if someone took a look at the contents of the .sql file below and let me know if there is something I am missing that is giving me consistent errors.
-- Table structure for table `Purchase`
CREATE TABLE IF NOT EXISTS `Purchase` (
`P_ORDERNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_CODE` int(5) NOT NULL auto_increment FOREIGN KEY,
`P_ORDER_DATE` timestamp NOT NULL,
`P_ORDER_AMT` int(5) NOT NULL,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Vendor`
CREATE TABLE IF NOT EXISTS `Vendor` (
`VENDORNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`VENDOR_NAME` varchar(50) NULL,
`VENDOR_STREET` varchar(50) NULL,
`VENDOR_CITY` varchar(50) NULL,
`VENDOR_STATE` varchar(2) NULL,
`VENDOR_ZIP` varchar(3) NULL,
`VENDOR_AREA_CODE` varchar(5) NULL,
`VENDOR_PHONE` varchar(10) NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Supply`
CREATE TABLE IF NOT EXISTS `Supply` (
`SUPPLY_CODE` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
You have trailing commas
`SUPPLY QTY` int(5) NOT NULL,
^---here
);
in each of the table definitions. That makes the DB server expect another field definition, but instead it runs into );

#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 ')'

This is the code. However I kept 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 ')' at line 7
Weirdly line 7 is the CREATE TABLE academicnews( line. Which does not contain ')' .
CREATE TABLE academicnews(
anewsID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
anewsContent TEXT NOT NULL,
imagePath VARCHAR(200) NOT NULL,
timeNews DATE NOT NULL,
); #Line 7
Get rid of the last comma. It is unnecessary and invalid.
CREATE TABLE academicnews(
anewsID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
anewsContent TEXT NOT NULL,
imagePath VARCHAR(200) NOT NULL,
timeNews DATE NOT NULL, <-- HERE
);
It should be
CREATE TABLE academicnews(
anewsID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
anewsContent TEXT NOT NULL,
imagePath VARCHAR(200) NOT NULL,
timeNews DATE NOT NULL
);
You are getting this error bcoz of an addition comma.
CREATE TABLE academicnews(
anewsID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
anewsContent TEXT NOT NULL,
imagePath VARCHAR(200) NOT NULL,
timeNews DATE NOT NULL, <--- This is the error
);
CREATE TABLE IF NOT EXISTS `testinfo` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`sl_no` int(10) NOT NULL,
`p1` int(3) DEFAULT NULL,
`p2` int(3) DEFAULT NULL,
`p3` int(3)DEFAULT select [p1]+[p2],
`mid` int(8) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `mid` (`mid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

ERROR 1064 (42000): You have an error in your SQL syntax;

I have a MySQL commands:
CREATE DATABASE IF NOT EXISTS courses;
USE courses
CREATE TABLE IF NOT EXISTS teachers(
id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VAR_CHAR(50) NOT NULL,
addr VAR_CHAR(255) NOT NULL,
phone INT NOT NULL,
);
When I run it, I get an 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 'VAR_CHAR(50) NOT NULL, addr VAR_CHAR(255) NOT
NULL, phone INT NOT NULL, )' at line 3
It is varchar and not var_char
CREATE DATABASE IF NOT EXISTS courses;
USE courses;
CREATE TABLE IF NOT EXISTS teachers(
id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
addr VARCHAR(255) NOT NULL,
phone INT NOT NULL
);
You should use a SQL tool to visualize possbile errors like MySQL Workbench.
Try this:
Use back-ticks for NAME
CREATE TABLE `teachers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`addr` varchar(255) NOT NULL,
`phone` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Use varchar instead of VAR_CHAR and omit the comma in the last line i.e.phone INT NOT NULL
);. The last line during creating table is kept "comma free".
Ex:- CREATE TABLE COMPUTER
(
Model varchar(50)
);
Here, since we have only one column ,that's why there is no comma used during entire code.