MySQL Create Table 'closing parenthesis' - mysql

This is my sql query for MySQL:
DROP DATABASE IF EXISTS java_proj;
CREATE DATABASE java_proj
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE java_proj;
CREATE TABLE Konto(
idKonto SERIAL PRIMARY KEY,
login VARCHAR(14) UNIQUE CHECK(LENGTH(login)>4) NOT NULL,
haslo VARCHAR(14) CHECK(LENGTH(haslo)>4) NOT NULL,
data_rejestracji DATE DEFAULT NOW() NOT NULL,
data_urodzenia DATE NOT NULL,
imie VARCHAR(30) NOT NULL,
nazwisko VARCHAR(30) NOT NULL
);
I'm getting error out of nowhere, in the line of login.
Error: Syntax error: 'closing parenthesis'

As mentioned in comments MySQL doesn't support CHECK.
But you also have another problem with that query: the DATE field cannot have NOW() as DEFAULT value, you can change it in DATETIME and set CURRENT_TIMESTAMP as default value
Cleaned up your query should probably look like this
CREATE TABLE Konto(
idKonto SERIAL PRIMARY KEY,
login VARCHAR(14) UNIQUE NOT NULL,
haslo VARCHAR(14) NOT NULL,
data_rejestracji DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
data_urodzenia DATE NOT NULL,
imie VARCHAR(30) NOT NULL,
nazwisko VARCHAR(30) NOT NULL
);
You can read about some workaround to the use instead of CHECK at this link
CHECK constraint in MySQL is not working

Related

Laravel 7 - weird timestamp migration behavior

I'm creating a table via a migration file.
I have multiple timestamp columns which one of is not supposed to be nullable.
If i set that one column first in order the migration goes through just fine, if I place any nullable timestamp column before it, the migration fails with an exception:
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'credit_date' (SQL: create table `feedback_survey_records` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `feedback_survey_id` bigint unsigned not null, `reward` smallint not null, `credit_status` varchar(45) null, `answer_date` timestamp null, `open_date` timestamp null, `credit_date` timestamp not null, `decline_date` timestamp null, `decline_reason` varchar(255) null, `response_id` varchar(255) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = innodb)
Successful migration
$table->timestamp('credit_date');
$table->timestamp('answer_date')->nullable();
$table->timestamp('open_date')->nullable();
$table->timestamp('decline_date')->nullable();
Failing migration
$table->timestamp('answer_date')->nullable();
$table->timestamp('open_date')->nullable();
$table->timestamp('credit_date');
$table->timestamp('decline_date')->nullable();
I looked at the database and noticed that laravel automatically added a default value and extra instructions on the successful migration
Database structure Image
Can someone please shed some light on why this might be happening?

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.

Error Code: 1067. Invalid default value for 'last_visit'

A keep getting this error in my mysql statement even though I'm not specifying a default value. Previous responses said adding a NOT NULL would fix it, but I still get the error. What is odd is if I switch the lines of date_joined and last_visit, I get the same error, except on date_joined. Any ideas why its giving this error? Thanks
CREATE TABLE user_info(
user_id serial,
oauth_provider VARCHAR(255) NOT NULL,
oauth_id VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth date NOT NULL,
email VARCHAR(255) NOT NULL,
gender char NOT NULL,
date_joined timestamp NOT NULL,
last_visit timestamp NOT NULL,
account_enabled boolean NOT NULL,
CONSTRAINT user_info_pkey PRIMARY KEY (user_id)) ENGINE=InnoDB;
I use your code and me all work:
If you are using MySQL < 5.6 you cant have 2 timestamp column with same default values.
For MySQL 5.5 check this http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
So if you want to have 2 column with timestamp try this.
CREATE TABLE user_info(
user_id serial,
oauth_provider VARCHAR(255) NOT NULL,
oauth_id VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth date NOT NULL,
email VARCHAR(255) NOT NULL,
gender char NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMPL,
last_visit TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
account_enabled boolean NOT NULL,
CONSTRAINT user_info_pkey PRIMARY KEY (user_id)
) ENGINE=InnoDB;

create table error in mysql. 1071 error

hi all can some one plz help me,
while creating table in mysql i am getting the following error
MySQL error 1071: Specified key was too long; max key length is 767 bytes
my table definition is as follows.
CREATE TABLE oauth_consumer (id char(36) NOT NULL ,
name varchar(255) NULL ,
date_entered datetime NULL ,
date_modified datetime NULL ,
modified_user_id char(36) NULL ,
created_by char(36) NULL ,
description text NULL ,
deleted bool DEFAULT '0' NULL ,
assigned_user_id char(36) NULL ,
c_key varchar(255) NULL ,
c_secret varchar(255) NULL,
PRIMARY KEY (id),
UNIQUE ckey (c_key)
);
No problem in your query. May be you are running old version of MySql. Use new version. If you are using new version try to use following query to create table
CREATE TABLE `oauth_consumer` (
`id` CHAR(36) NOT NULL,
`name` VARCHAR(255) NULL DEFAULT NULL,
`date_entered` DATETIME NULL DEFAULT NULL,
`date_modified` DATETIME NULL DEFAULT NULL,
`modified_user_id` CHAR(36) NULL DEFAULT NULL,
`created_by` CHAR(36) NULL DEFAULT NULL,
`description` TEXT NULL,
`deleted` TINYINT(1) NULL DEFAULT '0',
`assigned_user_id` CHAR(36) NULL DEFAULT NULL,
`c_key` VARCHAR(255) NULL DEFAULT NULL,
`c_secret` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `ckey` (`c_key`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
First of all, your database character set is most likely set to utf8, so your 255 characters get blown up to 765 characters; though close, that's not greater than the imposed limit of 767, but it gives an idea of why you're hitting that limit nonetheless.
Regardless, it's not recommended to use such a wide key; you could calculate an md5 or sha1 of the value and use that for the unique key instead (btw, you should use the binary representation of the hash).

#1366 - Incorrect integer value:MYsql

hi every one i have a problem in mysql
my table is
CREATE TABLE IF NOT EXISTS `contactform` (
`contact_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL,
`addition` varchar(50) NOT NULL,
`surname` varchar(50) NOT NULL,
`Address` varchar(200) NOT NULL,
`postalcode` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`phone` varchar(20) NOT NULL,
`emailaddress` varchar(30) NOT NULL,
`dob` varchar(50) NOT NULL,
`howtoknow` varchar(50) NOT NULL,
`othersource` varchar(50) NOT NULL,
`orientationsession` varchar(20) NOT NULL,
`othersession` varchar(20) NOT NULL,
`organisation` int(11) NOT NULL,
`newsletter` int(2) NOT NULL,
`iscomplete` int(11) NOT NULL,
`registrationdate` date NOT NULL,
PRIMARY KEY (`contact_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=39 ;
mysql>insert into contactform values('','abhi','sir','shukla','vbxcvb','342342','asdfasd','234234234','abhi#gmail.com','1999/5/16','via vrienden of familie','','19','20','6','1','1','2010-03-29')
i get following error.
#1366 - Incorrect integer value: '' for column 'contact_id' at row 1
this query work fine on my local machine but give error on server
Had the same problem with some legacy project. I didn't want to turn off strict mode globally, but also didn't want to go through all the code to fix it, so I disabled it only within that application by executing the following query once after the connection to the db is made:
SET sql_mode = ""
Try using NULL instead of '' for contact_id in
insert into contactform values(NULL,......
Try to find following line in my.cnf/my.ini:
sql-mode=”STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”
Now comment it out with # and restart your mysql server.
'' is an empty string, you want a integer, but this integer is created by the database. Drop the columnname in the INSERT and don't insert any value.
'' is not an integer, is it?
Also, that is some seriously weird indentation.
In mysql console, try this:
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
If you are importing - make sure your CSV/TXT file is UTF-8 WITHOUT the BOM - the BOM will pass characters that are not intgers and MySQL will think you are importing that into the first field...