MySQL Syntax for setting Default Date - sql

How can we set element to some default date in mysql ?
In oracle we would do something like
start_date DATE DEFAULT to__date('01011900','DDMMYYYY')
mutation_date_time DATE DEFAULT SYSDATE
CONSTRAINT entity_specification UNIQUE(external_name, start_date_time, end_date_time))
Also do we have any site or resource where we can get MySQL equivalent syntax to Oracle Syntax.
Update
I tried to do as mentioned in answer but again am getting error message:
create table product_offer_type(object_id INT(19), snapshot_id INT(19), PRIMARY KEY(object_id,snapshot_id), enum_value VARCHAR(64) NOT NULL, external_name VARCHAR(64) NOT NULL, description VARCHAR(255), business_validation INT(1), valid_for_start_date_time DATE DEFAULT '1900-01-10', valid_for_end_date_timeDATE DEFAULT '4712-01-01', mutation_date_time DATE DEFAULT 'SYSDATE()', mutation_user VARCHAR(32) DEFAULT 'USER');
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 'DEFAULT '4712-01-01', mutation_date_time DATE DEFAULT 'SYSDATE()', mutation_user' at line 1
mysql>

Simply pass it a formatted string.
start_date DATE DEFAULT '1900-01-01'

For a fixed default date on a DATE field, the format is 'YYYY-MM-DD', like start_date DATE DEFAULT '2009-10-30'. DATETIME would be 'YYYY-MM-DD HH:MM:SS'.
For a dynamic date based on the current date, you can use DEFAULT CURRENT_TIMESTAMP if you are using a TIMESTAMP field; otherwise, it is not possible to use a function as a DEFAULT in MySQL. TIMESTAMP fields are formatted as 'YYYY-MM-DD HH:MM:SS'. Otherwise, you'd have to get the current date in your code before inserting the data.

Related

Microsoft SQL Constraint Check for Year higher than 2000

I'm creating a database and I want to create a Constraint Check that only allows to insert Dates that are after year 2000.
This is where i store dates:
CREATE TABLE User (
username varchar(20) NOT NULL,
birthdate DATE NOT NULL,
CONSTRAINT user_birthdate_ck CHECK (birthdate > 2000)
)
2000 is not a date, it's a number.
You need to specify a date in quotes using an unambiguous date format. Best to use 'YYYYMMDD'.
CREATE TABLE [User] (
username varchar(20) NOT NULL,
birthdate DATE NOT NULL,
CONSTRAINT user_birthdate_ck CHECK (birthdate > '20000101')
);
db<>fiddle
Assuming you actually wanted after the year 2000 then you probably want
CONSTRAINT user_birthdate_ck CHECK (birthdate >= '20010101')
Do not use > '20001231' if you have a time component as it won't be correct.
Change the year to a properly formed date as follows:
CONSTRAINT user_birthdate_ck CHECK (birthdate > '2000/12/31')
It would also be prudent to explicitly set the date format being used before the CREATE TABLE as follows:
SET DATEFORMAT ymd;
GO
BEWARE all the strings expression of dates showns in answers are not in the ISO SQL format and won't do the job under certain session parameters like language or some others, espcially mixed with non Transct SQL in a batch...
The only string format that never cause you any trouble is the SQL ISO long date format as :
'AAAA-MM-JJ'

not null default date in postgresql

I want to create a new table that following code below;
create table dwh.dim_produk (
sk_produk serial primary key,
kode_produk varchar(25),
nama_produk varchar(50),
kode_kategori varchar(50),
nama_kategori varchar(50),
date_from date not null default current_timestamp,
date_to date not null default '12/31/9999');
but i get result such as;
SQL Error [22008]: ERROR: date/time field value out of range: "12/31/9999"
Hint: Perhaps you need a different "datestyle" setting.
Position: 253
thank in advance
If you want a date that is "in the future", use infinity
date_from date not null default current_date,
date_to date not null default 'infinity'
Or if you really want a date in the year 9999, use a proper DATE literal formatted using ANSI style:
date_from date not null default current_date,
date_to date not null default DATE '9999-12-31'
Alternatively you can represent a range using the daterange data type:
valid_during daterange not null default daterange(current_date,null)
A null for the upper range means "infinity" as well.

Formatting the default date and time for HSQLDB

I have been trying to format the date and time in the CREATE command (to apply for cases where user does not enter any value for date/time). According to the manual, there are the TO_DATE and TO_TIMESTAMP format elements. How do I fix my SQL statement?
http://hsqldb.org/doc/guide/builtinfunctions-chapt.html#N142F5
CREATE TABLE test1(
Id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"Date In" DATE DEFAULT TO_TIMESTAMP(CURRENT_DATE, 'DD/MM/YYYY'),
"Time In" TIME DEFAULT TO_TIMESTAMP(CURRENT_TIME,'HH:MM')
);
I tried with TO_CHAR but still return an error.
https://wiki.documentfoundation.org/Faq/Base/HSQLFunctions
DATE, TIMESTAMP or TIME values do not have "a format". They are stored in a binary way and formatted when they are displayed. So you can't apply "a format" to a DATE column. Additionally, calling to_timestamp() on a value that is a date makes no sense. to_timestamp() is used to convert a string (character) value to a date (or timestamp).
Just define both columns without (wrongly) applying a conversion from a string to a timestamp:
CREATE TABLE test1(
Id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"Date In" DATE DEFAULT CURRENT_DATE,
"Time In" TIME DEFAULT CURRENT_TIME
);
Given the name of the two columns, I think it would make more sense to store that in a single timestamp column:
CREATE TABLE test1(
Id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"Date Time In" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

"Not a valid month" or number

Getting this error while trying to put a few inserts into a table.
Getting an error regarding not a valid month and when I try change it around i'm getting invalid number error.
ORA-01843: not a valid month ORA-06512: at "SYS.DBMS_SQL"
Code:
CREATE TABLE ExpenseReport (
ERNo NUMERIC(10) NOT NULL,
ERDesc VARCHAR(255) NOT NULL,
ERSubmitDate DATE DEFAULT CURRENT_TIMESTAMP,
ERStatusDate DATE NOT NULL,
ERStatus VARCHAR(8) DEFAULT 'PENDING',
SubmitUserNo NUMERIC(10) NOT NULL,
ApprUserNo NUMERIC(10) NOT NULL,
CONSTRAINT ExpenseReport_CK1 CHECK (ERStatusDate >= ERSubmitDate),
CONSTRAINT ExpenseReport_CK2 CHECK (ERStatus = 'PENDING'/'APPROVED'/'DENIED'),
CONSTRAINT ExpenseReport_PK1 PRIMARY KEY(ERNo),
CONSTRAINT ExpenseReport_FK1 FOREIGN KEY(SubmitUserNo) REFERENCES Users(UserNo),
CONSTRAINT ExpenseReport_FK2 FOREIGN KEY(ApprUserNo) REFERENCES (USerNo)
);
INSERT INTO ExpenseReport
(ERNo, ERDesc, ERSubmitDate, ERStatusDate, ERStatus, SubmitUserNo, ApprUSerNo)
VALUES (1,'Sales Presentation','8/10/2002','8/26/2002','APPROVED',3,4);
I've also tried using the TO_DATE but having no luck there,
by any chance can anyone see where i'm going wrong.
Use the DATE keyword and standard date formats:
INSERT INTO ExpenseReport (ERNo, ERDesc, ERSubmitDate, ERStatusDate, ERStatus, SubmitUserNo, ApprUSerNo)
VALUES (1, 'Sales Presentation', DATE '2001-08-10', DATE '2001-08-2006', 'APPROVED', 3, 4);
In addition to the satisfaction of using standard date formats, this protects you against changes in local settings.
In your DDL statement:
CONSTRAINT ExpenseReport_CK2 CHECK (ERStatus = 'PENDING'/'APPROVED'/'DENIED')
Should be:
CONSTRAINT ExpenseReport_CK2 CHECK (ERStatus IN ( 'PENDING', 'APPROVED', 'DENIED' ) )
When you are trying to insert values the check constraint is being evaluated and it is trying to perform a division operation on the three string values'PENDING'/'APPROVED'/'DENIED' which results in ORA-01722: invalid number.
Once you change this then using TO_DATE('01/01/02','DD/MM/YY') (as you wrote in comments) or an ANSI date literal DATE '2002-01-01' should work in your DML statements.
(Note: Be careful using 2-digit years or you can find that dates are inserted with the wrong century.)
Check your date format: select sysdate from dual;
and enter as it show. OR
change your date format: alter session set nls_date_format= 'DD-Mon-YYYY HH24:MI:SS';
It Was Easy :
if Your code Like This just remove hem and write that
Example :
Your code : values ('30178','K111', '22/12/2008')
Do This : values ('30178','K111', '22/Dec/2008')

MySQL Default Keyword usage errors

When am trying to set default date and default sysdate am getting following errors:
MySQL Query:
create table product_offer_type(object_id INT(19), snapshot_id INT(19), PRIMARY KEY(object_id,snapshot_id), enum_value VARCHAR(64) NOT NULL, external_name VARCHAR(64) NOT NULL, description VARCHAR(255), business_validation INT(1), valid_for_start_date_time DATE DEFAULT '1900-01-10', valid_for_end_date_time DATE DEFAULT '4712-01-01', mutation_date_time DATE SYSDATE, mutation_user VARCHAR(32) DEFAULT 'USER');
Error Message:
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 'SYSDATE, mutation_user VARCHAR(32) DEFAULT 'USER')' at line 1
Any pointer's would be highly appreciated.
If you're trying to track the last modification time of the row, I usually use something like
mutation_date_time timestamp default current_timestamp, on update current_timestamp
You might need slight modification if you really want Date and not timestamp.