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

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.

Related

Is this mysql correctly translated to sql server?

I have this, for mysql
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(249) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`username` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`verified` tinyint(1) unsigned NOT NULL DEFAULT '0',
`registered` int(10) unsigned NOT NULL,
`last_login` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I'm attempting to convert to sql server:
CREATE TABLE users(
id int PRIMARY KEY IDENTITY NOT NULL,
email varchar(255) UNIQUE,
password varchar(255) NOT NULL ,
username varchar(100) DEFAULT NULL,
verified tinyint NOT NULL DEFAULT '0',
registered int NOT NULL,
last_login int DEFAULT NULL,
);
That worked, but I had to omit some things, mostly the Collate and character set. Can you tell me if I missed anything too important and how to translate those things from mysql to sql server?
Some notes:
PRIMARY KEY means NOT NULL.
Constants should be in the same type as their variable.
For generalized character sets, use NVARCHAR().
DEFAULT NULL is redundant (you can keep it if you like).
Hence:
CREATE TABLE users (
id int PRIMARY KEY IDENTITY,
email nvarchar(255) UNIQUE,
password nvarchar(255) NOT NULL ,
username nvarchar(100),
verified tinyint NOT NULL DEFAULT 0,
registered int NOT NULL,
last_login int
);

mysql error 1215 cannot add foreign key contraint

DROP TABLE IF EXISTS desk;
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS authority;
CREATE TABLE authority
(
authorityId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskId INT DEFAULT 0,
authorityName VARCHAR(50) NOT NULL,
descriptions VARCHAR(100) NOT NULL,
userLevel TINYINT NOT NULL,
actions VARCHAR(300) DEFAULT NULL,
setDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE desk
(
deskId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskName CHAR(50) NOT NULL,
deskLink VARCHAR(100) NOT NULL,
details VARCHAR(350) DEFAULT NULL,
setDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
closeDate TIMESTAMP DEFAULT NULL,
userId INT DEFAULT NULL,
status TINYINT(1) DEFAULT 0
);
CREATE TABLE user
(
userId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userName CHAR(150) NOT NULL,
password VARCHAR(500) NOT NULL,
department VARCHAR(150) DEFAULT NULL,
deskId INT NOT NULL,
status TINYINT(1) DEFAULT NULL,
userLevel TINYINT NOT NULL,
FOREIGN KEY (deskId) REFERENCES desk (deskId) ON DELETE CASCADE,
FOREIGN KEY (userLevel) REFERENCES authority (userLevel)
);
Error while executing query.
Error Code: 1215 .Cannot add foreign key contraint
I have tried many way but still cannot solve the problem, i tried change userLevel type to INT both table user and authority but nothing went to better
I really not know how to fix this problem, please help
Follow below steps
1) You must change your second query to
DROP TABLE IF EXISTS desk;
CREATE TABLE desk
(
deskId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskName CHAR(50) NOT NULL,
deskLink VARCHAR(100) NOT NULL,
details VARCHAR(350) DEFAULT NULL,
setDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
closeDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
userId INT DEFAULT NULL,
status TINYINT(1) DEFAULT 0
);
2) As your column authority.userLevel is not a primary key and you want to use it as foreign key in other table you need to create index on authority.userLevel column
use below query
ALTER TABLE `authority` ADD INDEX `UserLevelIndex` (`userLevel`);
3) Run your third query as it is to create user table
Run Following queries
DROP TABLE IF EXISTS desk;
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS authority;
CREATE TABLE authority
(
authorityId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskId INT DEFAULT 0,
authorityName VARCHAR(50) NOT NULL,
descriptions VARCHAR(100) NOT NULL,
userLevel TINYINT NOT NULL,
actions VARCHAR(300) DEFAULT NULL,
setDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
index `cr_index` (userLevel)
);
CREATE TABLE desk
(
deskId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskName CHAR(50) NOT NULL,
deskLink VARCHAR(100) NOT NULL,
details VARCHAR(350) DEFAULT NULL,
setDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
closeDate TIMESTAMP DEFAULT '0000-00-00 00:00:00',
userId INT DEFAULT NULL,
status TINYINT(1) DEFAULT 0
);
CREATE TABLE `user`
(
userId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userName CHAR(150) NOT NULL,
password VARCHAR(500) NOT NULL,
department VARCHAR(150) DEFAULT NULL,
deskId INT NOT NULL,
status TINYINT(1) DEFAULT NULL,
userLevel TINYINT NOT NULL,
FOREIGN KEY (deskId) REFERENCES desk (deskId) ON DELETE CASCADE,
FOREIGN KEY (userLevel) REFERENCES authority (userLevel)
);
In your query you write timestamp default value NULL and you can create a non index key userLevel as a foreign key so from your query remove default value or change the default value 0000-00-00 00:00:00(in desk table) and add index in userLevel column (in authority table)
I hope this will work for you

mysql - Invalid default value

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)
);

Creating MYSQL Database Table? Need Help Revising

I've created a login system. I have SOME knowledge of MYSQL and I would like to have this big junk of code revised.
This is the code:
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS Country;
CREATE TABLE Country(
country_code char(2) not null,
country_name varchar(60) not null,
primary key(country_code)
)
CREATE TABLE users(
pk_user int unsigned not null auto_increment,
email varchar(120) not null,
flname varchar(100) not null,
password varchar(64) not null,
country_code char(2) not null,
usr_ip varchar(15),
usr_nmb_logins int(10) unsigned not null default 0,
usr_signup_date timestamp not null default CURRENT_TIMESTAMP,
usr_userid varchar(32),
usr_confirm_hash varchar(255) not null, # for the account confirmation
usr_is_confirmed tinyint(1) not null default 0, # after confirming its set to 1
usr_resetpassword_hash varchar(255) not null, # when the user resets password (forgot password)
usr_is_blocked tinyint(1) not null default 0, # blocked or not
usr_is_admin tinyint(1) not null default 0, # admin or not
foreign key(country_code) references Country(country_code),
unique index(email),
primary key(pk_user)
)
Also there is a LARGE amount of inserts like the following:
insert into Country(country_code,country_name) values("n","?");
This is the first one out of AT LEAST a hundred in the same format.
This is the error PHPMyAdmin is giving me:
#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(
pk_user int unsigned not null auto_increment,
ema' at line 6
But when I take out that error or fix it way more appear and to tell you the truth I don't know what to do.
CREATE TABLE Country(
country_code char(2) not null,
country_name varchar(60) not null,
primary key(country_code)
);
CREATE TABLE users(
pk_user int unsigned not null auto_increment,
email varchar(120) not null,
flname varchar(100) not null,
password varchar(64) not null,
country_code char(2) not null,
usr_ip varchar(15),
usr_nmb_logins int(10) unsigned not null default 0,
usr_signup_date timestamp not null default CURRENT_TIMESTAMP,
usr_userid varchar(32),
usr_confirm_hash varchar(255) not null, # for the account confirmation
usr_is_confirmed tinyint(1) not null default 0, # after confirming its set to 1
usr_resetpassword_hash varchar(255) not null, # when the user resets password (forgot password)
usr_is_blocked tinyint(1) not null default 0, # blocked or not
usr_is_admin tinyint(1) not null default 0, # admin or not
foreign key(country_code) references Country(country_code),
unique index(email),
primary key(pk_user)
);

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.
);