Mysql automated date field, syntax errors - mysql

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)

Related

Stuck on a SQL Syntax error

I'm a little bit stuck with my SQL models. I use MySQL Workbench to create models graphically and i greated a schema which i think fits my use case. However i can't synchronnise with the database because it gives me the following error. Maybe someone else can see what i did wrong. Apparantly i'm blind currently...
Executing SQL script in server ERROR: Error 1064: 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 'NULL DEFAULT
CURRENT_TIMESTAMP,
last_activity DATETIME NULL DEFAULT NULL,
' at line 6
SQL Code:
CREATE TABLE IF NOT EXISTS `topas`.`user` (
`id` INT(255) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`email` VARCHAR(80) NULL DEFAULT NULL,
`password` VARCHAR(250) NOT NULL,
`created_at` NULL DEFAULT CURRENT_TIMESTAMP,
`last_activity` DATETIME NULL DEFAULT NULL,
UNIQUE INDEX `username_UNIQUE` (`username` ASC),
UNIQUE INDEX `email_UNIQUE` (`email` ASC),
UNIQUE INDEX `id_UNIQUE` (`id` ASC),
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
SQL script execution finished: statements: 3 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Thanks.
This line:
`created_at` NULL DEFAULT CURRENT_TIMESTAMP,
is missing type definition. It should be:
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
This line is missing a data type
`created_at` NULL DEFAULT CURRENT_TIMESTAMP,
You will need to set it to either TIMESTAMP or DATETIME:
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
you are missing the column type for created_at column.
should be:
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,

MySQL Create Table 'closing parenthesis'

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

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

How do i select current program from this table

I am trying to select current program (which is happening now) from this table. Please help Thanks.
CREATE TABLE `programs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`p_day` varchar(20) NOT NULL,
`program` varchar(255) NOT NULL,
`p_start` time NOT NULL DEFAULT '00:00:00',
`p_end` time NOT NULL DEFAULT '00:00:00',
PRIMARY KEY (`id`)
)
SELECT program FROM programs
WHERE CURDATE() = p_day
AND CURTIME() BETWEEN p_start AND p_end;
I'm making an assumption that your p_day is a valid date string, e.g. '2011-08-15' but it's not clear from your question. Why didn't you use a DATE datatype for the p_day?