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;
Related
I'm trying to create a mysql table which contains two fields that are automatically given the current date when data is first put into them
CREATE TABLE IF NOT EXISTS `database`.`characters` (
`character_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`ckey` VARCHAR(30) NOT NULL,
`character_name` VARCHAR(26) NOT NULL,
`birth` DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_seen` DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`character_id`),
UNIQUE INDEX `character_id_UNIQUE` (`character_id` ASC) VISIBLE)
this is not working, i get invalid default value error, which is progress
Everything else i've tried throws error 1064, sql syntax
In place of current timestamp I have tried...
GET_DATE()
GET_DATE
GETDATE
CURDATE
CURDATE()
CURRENT_DATE
CURRENT_DATE()
All of these are throwing the same syntax error, there is no error if i remove the default value.
I am almost entirely clueless about database code, this is a real newbie project, any help would be appreciated
Use datetime instead of date.
CREATE TABLE IF NOT EXISTS `characters` (
`character_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`ckey` VARCHAR(30) NOT NULL,
`character_name` VARCHAR(26) NOT NULL,
`birth` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_seen` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`character_id`),
UNIQUE INDEX `character_id_UNIQUE` (`character_id` ASC) VISIBLE)
CREATE TABLE users(
user_id int(11) auto_increment primary key,
f_name VARCHAR(255) NOT NULL,
l_name VARCHAR(255) not NULL,
u_name VARCHAR NOT NULL UNIQUE,
phone int(15) not null UNIQUE,
email VARCHAR not null UNIQUE,
pwd VARCHAR not null,
confpwd VARCHAR not null,
state VARCHAR(255),
lga VARCHAR(255) DEFAULT null,
preference enum('m','f','mf') DEFAULT 'mf',
gender enum('m','f') DEFAULT null,
profile_image VARCHAR(255),
bio VARCHAR(1000) DEFAULT null,
forgot_pwd_code VARCHAR (255),
forgot_pwd_time TIMESTAMP DEFAULT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
hobbies_id int(11),
FOREIGN KEY (hobbies_id) REFERENCES hobbies (userId) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
i keep getting this error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL UNIQUE, phone int(15) not null UNIQUE, email VARCHAR not null' at line 5
The VARCHAR datatypes requires a length (that represents the maximum number of characters allowed) - not providing it is a syntax error.
This is true not only for column uname, but for also for pwd and confpwd. Each of these columns should be declared like varchar(<N>), where <N> is the length of the column, instead of just varchar.
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
I tried this code
CREATE TABLE users (
userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL,
password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate DATETIME DEFAULT CURRENT_TIMESTAMP() NOT NUll,
updatedDate DATETIME
);
but the following error will come
1067 - Invalid default value for 'createdDate'
thanks
Use simply CURRENT_TIMESTAMP instead CURRENT_TIMESTAMP()
CREATE TABLE users (
userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL, password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate DATETIME DEFAULT CURRENT_TIMESTAMP NOT NUll,
updatedDate DATETIME
);
In addition, you can initialize or update any TIMESTAMP column to the
current date and time by assigning it a NULL value, unless it has been
defined with the NULL attribute to permit NULL values.
For more knowledge click the link http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
Change the datatype datetime to timestamp it will work.
CREATE TABLE users ( userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL,
password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate **timestamp** DEFAULT CURRENT_TIMESTAMP() NOT NUll,
updatedDate DATETIME )
Try this code, it should work.
CREATE TABLE users (
userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL,
password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate DATETIME DEFAULT CURRENT_TIMESTAMP NOT NUll,
updatedDate DATETIME
);
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).