Creating MYSQL Database Table? Need Help Revising - mysql

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

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 syntax Error when creating a table #1064

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;

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

Why always this error occured when I create table in MySQL database?

CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT "Default Name",
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id);
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 '' at line 13
You forgot to close your command with )! Change your query to the following:
CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT 'Default Name',
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id)
);
Additional comment about the " on DEFAULT:
Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL, but that can vary from database to database.
Source (see comments too): https://stackoverflow.com/a/1992331/3840840
You're missing a closing bracket at the end.
Missing ) closing bracket. Working query is:
CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT "Default Name",
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id));
Try this Query you miss ) in query:
CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT "Default Name",
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id));
syntax error
you missing the closing bracket at end
do it
CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT "Default Name",
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id));

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