473,735 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to setup default value "0000-00-00" for "date" type under PostgreSQL?

Hello all,

I have a question about "date" & "timestamp" types in PostgreSQL. I want
to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for
them. However, it seems that PostgreSQL does not support it. Could
someone helps me please?

The example table:

T1 (col1 varchar(7) not null,
col2 varchar(4) not null,
col3 date not null,
col 4 varchar(3),
primary key(col1, col2, col3)
)
In my design model, "col3" has to be one of the primary key part. Since
at the beginning of the data population, we do not know the value of
"col3"; values for "col3" are input throught GUI. Therefore, when I use
MySQL, the default values I gave is "0000-00-00". However, after I
migrate to postgreSQL, I could not setup the default values as
"0000-00-00" any more. Could somebody help me about it please? I really
want to know how I can save '0000-00-00' as the default value for "date"
and "timestamp" types.

Thanks a lot!
Emi Lu


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
12 13911
> I really
want to know how I can save '0000-00-00' as the default value for "date"
and "timestamp" types.

You can not. They are invalid dates.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
On Wed, 18 Aug 2004, Emi Lu wrote:
Hello all,

I have a question about "date" & "timestamp" types in PostgreSQL. I want to
setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them.
However, it seems that PostgreSQL does not support it. Could someone helps me
please?


how about using NULL ? You say the correct value is still undefined so
NULL should be right on the spot.

Greetings
Christian

--
Christian Kratzer ck@cksoft.de
CK Software GmbH http://www.cksoft.de/
Phone: +49 7452 889 135 Fax: +49 7452 889 136

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 23 '05 #3
Emi Lu wrote:
Hello all,

I have a question about "date" & "timestamp" types in PostgreSQL. I want
to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for
them. However, it seems that PostgreSQL does not support it. Could
someone helps me please?
PostgreSQL doesn't and almost certainly never will support "0000-00-00"
as a date. That's because it isn't a valid date. You also can't store
13.723, "Hello world" or (12,13) in a date column either.

Where you don't have a valid date to store you should use NULL. This
business of storing zeroes is a horrible MySQL design mistake.
The example table:

T1 (col1 varchar(7) not null,
col2 varchar(4) not null,
col3 date not null,
col 4 varchar(3),
primary key(col1, col2, col3)
)
In my design model, "col3" has to be one of the primary key part. Since
at the beginning of the data population, we do not know the value of
"col3"; values for "col3" are input throught GUI.


If you don't know the value of col3, it can't be part of your primary
key. That's part of the definition of "primary key" and trying to work
around it is what's causing you problems here.

If you have an initial population of data that then needs to have "col3"
set then it sounds like you need two tables T0 with primary-key
(col1,col2) and T1 with (col1,col2,col3 ) and copy from T0=>T1 as users
supply values. Difficult to say without knowing more about your situation.

HTH
--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 23 '05 #4
Hi,

On Fri, 20 Aug 2004, Richard Huxton wrote:
Emi Lu wrote:
Hello all,

I have a question about "date" & "timestamp" types in PostgreSQL. I want
to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for
them. However, it seems that PostgreSQL does not support it. Could someone
helps me please?


PostgreSQL doesn't and almost certainly never will support "0000-00-00" as a
date. That's because it isn't a valid date. You also can't store 13.723,
"Hello world" or (12,13) in a date column either.

Where you don't have a valid date to store you should use NULL. This business
of storing zeroes is a horrible MySQL design mistake.


which is because the last time when I last used mysql it did not support
NULLs in indexed columns (at least not in myisam tables).

The workaround was to use something else like 0 to represent undefined
values.... Horrible ...

Greetings
Christian

--
Christian Kratzer ck@cksoft.de
CK Software GmbH http://www.cksoft.de/
Phone: +49 7452 889 135 Fax: +49 7452 889 136

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #5
Richard Huxton wrote:
Where you don't have a valid date to store you should use NULL. This
business of storing zeroes is a horrible MySQL design mistake.


Well, yes and no. It certainly is a design mistake and introduces
incosistency into the database, but after I was bitten several times by
NULL values I'd go for solution like this any day. Let me explain.

We had a table of messages, which was inserted to randomly and every few
minutes we'd walk through the unprocessed messages and perform some work
on them. I, trying to have the database as clean as possible, used this
table definition (simplified):

messages (
id serial,
data text,
arrived timestamp default now,
processed timestamp)

So after the message arrived, it had the processed field set to null,
which was perfectly consistent and represented what it realy was--an
unknown value.

We'd then simply SELECT * FROM messages WHERE processed IS NULL and were
happy ever after, only to realise after the table had grown to few
thousands rows, that the SELECT took ages, because the system had to
perform seqscan. Aha! So we added an index on processed, because common
sense told me, that as long as there are 100k rows and only 10 of them
are NULL, the index would be very selective and therefore useful.

I guess you know where it ends--the index is not used for IS [NOT] NULL
expressions. The obvious workaround was to add DEFAULT value to
"processed" in form of kind of anchor (we used '-infinity') and then do
SELECT * FROM messages WHERE processed='-infinity'.

Bingo! The thing went 100x faster. So we could choose to have
standards-compliant, very clean database design OR the thing that does
what it's supposed to do in reasonable time. And believe me, it's kind
of difficult to explain to our logistics department that we could have
done the thing to return results in milliseconds instead of 10 secs, but
chose not to for sake of clean design.

It'd be really nice if we didn't have to use such hacks, but hey, life's
inperfect.

And so that this would not be just a literary exercise and to answer
Emi's question--you can't do that, but use some valid date which you are
never going to use for your ordinary data (like the '-infinity', or
1.1.1970 00:00). Just make sure you make a note of it somewhere and my
suggestion is you write a COMMENT ON that column for future generations.

--
Michal Taborsky
http://www.taborsky.cz
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #6
> I guess you know where it ends--the index is not used for IS [NOT] NULL
expressions. The obvious workaround was to add DEFAULT value to
"processed" in form of kind of anchor (we used '-infinity')


Wouldn't it have worked to add an index

... WHERE processed IS NULL

and go from there ?

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 23 '05 #7
Michal Taborsky said:
Richard Huxton wrote:
Where you don't have a valid date to store you should use NULL. This
business of storing zeroes is a horrible MySQL design mistake.


Well, yes and no. It certainly is a design mistake and introduces
incosistency into the database, but after I was bitten several times by
NULL values I'd go for solution like this any day. Let me explain.

We had a table of messages, which was inserted to randomly and every few
minutes we'd walk through the unprocessed messages and perform some work
on them. I, trying to have the database as clean as possible, used this
table definition (simplified):

messages (
id serial,
data text,
arrived timestamp default now,
processed timestamp)

So after the message arrived, it had the processed field set to null,
which was perfectly consistent and represented what it realy was--an
unknown value.

We'd then simply SELECT * FROM messages WHERE processed IS NULL and were
happy ever after, only to realise after the table had grown to few
thousands rows, that the SELECT took ages, because the system had to
perform seqscan. Aha! So we added an index on processed, because common
sense told me, that as long as there are 100k rows and only 10 of them
are NULL, the index would be very selective and therefore useful.

I guess you know where it ends--the index is not used for IS [NOT] NULL
expressions. The obvious workaround was to add DEFAULT value to
"processed" in form of kind of anchor (we used '-infinity') and then do
SELECT * FROM messages WHERE processed='-infinity'.

Bingo! The thing went 100x faster. So we could choose to have
standards-compliant, very clean database design OR the thing that does
what it's supposed to do in reasonable time. And believe me, it's kind
of difficult to explain to our logistics department that we could have
done the thing to return results in milliseconds instead of 10 secs, but
chose not to for sake of clean design.

It'd be really nice if we didn't have to use such hacks, but hey, life's
inperfect.


It'd probably be better design to not use the date as a flag. This issue
actually came up for me yesterday with an application that is now being ported
to Postgres. Previously a null "ship date" indicated that an item to be
shipped had not gone yet. I'm adding a flag, not just because of this issue
you describe, but it is also more intuitive for anyone looking at the data
who is unfamiliar with the business logic.

Best,

Jim Wilson
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #8
Karsten Hilbert wrote:
I guess you know where it ends--the index is not used for IS [NOT] NULL
expressions. The obvious workaround was to add DEFAULT value to
"processed" in form of kind of anchor (we used '-infinity')


Wouldn't it have worked to add an index

... WHERE processed IS NULL

and go from there ?


Yes, you use a partial index. The 8.0beta1 docs mention this:

Indexes are not used for <literal>IS NULL</> clauses by default.
The best way to use indexes in such cases is to create a partial index
using an <literal>IS NULL</> comparison.

--
Bruce Momjian | http://candle.pha.pa.us
pg***@candle.ph a.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #9
On Fri, 20 Aug 2004 11:12:40 -0400 (EDT), Bruce Momjian wrote:
Yes, you use a partial index. The 8.0beta1 docs mention this:

Indexes are not used for <literal>IS NULL</> clauses by default.
The best way to use indexes in such cases is to create a partial
index using an <literal>IS NULL</> comparison.


Is this because nulls aren't indexed (I'm sure I remember someone
saying that they are, though), or because (null=null) is null rather
than true? If it's the latter, why couldn't an explicit IS NULL test
be allowed to use the index?

--
Peter Haworth pm*@edison.iopp ublishing.com
Znqr lbh ybbx

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6281
by: Eino Mäkitalo | last post by:
It seems that urrlib2 default redirection does not allow me to handle Cookies. Service I'm trying seems to use IP switcher and session id's with cookies. After successful login it changes session id (PD-H_SESSION-ID) in 302 Moved temporarily. Urllib2 is so clever that it handles redirection but with wrong cookies. Some hookings? Just own version from source. What is the most lazy way to handle this. Older urllib?
16
2098
by: WindAndWaves | last post by:
Hi there I have $initstartdate = date("d-m-Y"); in my code How can I get it to be date() + 1 or 7 for that matter. Because my server is in the US and I am in New Zealand, they are always a day behind....
7
1968
by: Ian | last post by:
Using Access 97 I have a form with several text boxes, I am trying to get each text box to display the word "Required" for the appropriate boxes on a new record. A friend gave me the following line of code that I put into the text box Format property: @;"Required" This works perfectly, problem is that this text box's Format also has to be set as Short Date, I don't seem to be able to combine the two so its formatted as a short date...
1
5431
by: Steve Grahovac | last post by:
I have been having trouble the last few days opening any ASP.NET web forms in the design view in the designer. Every time I clicked on an aspx file I got a message box with the error "Unable to open in designer. Illegal characters in path" and the file is instead opened in HTML view. I don't know what application or situation may have caused this error. I looked on the newsgroups and noticed that numerous other people were having this...
5
3516
by: Amelyan | last post by:
How can I get state of dynamically created controls (RadioButton, CheckBox, TextBox.Text) on post back when I click submit button? The only way I know is by traversing Response.Form enumberator; Response.Form.GetEnumerator(), etc. while, identifying specific controls by programmatically assigned unique id (e.g. MyButton_AnswerID_123). However, I am not sure if that is the proper way. What is the common practice?
2
1614
by: | last post by:
I like the "view in browser" feature of vs.net: right click from an aspx page, and preview a working version of the page. I don't like how this can't be launched from the pages' codebehind. The "view in browser" option does not appear on the context menu of VS.NET 2005. Is there a workaround or hotkey that I can use to launch a preview from the codebehind page? -KF
1
1765
by: Henry Law | last post by:
(I posted this to comp.infosystems.www.browsers.misc but there seems to be very little traffic there. I can't see another suitable group; is it on topic here? If not, any suggestions as to where?) I'm coding an application which serves out files for download via HTTP. I can provide the file name thus Content-Disposition:attachment;filename="somename.dat" I read in RFC2183 that I should be able to supply the modification date
0
874
by: alanwo | last post by:
Hi Experts, I am trying to send out email and bounce back to different from "FROM" email address: Dim webmail As New System.Net.Mail.MailMessage("B@FROM.com", "ghyddfhg@gmail.com", "hello", "return errors to") 'webmail.Headers.Add("Errors-To", "a1@a1.com") 'webmail.Headers.Add("X-Sender", "a1@a1.com") webmail.Headers.Add("Sender", "a1@a1.com")
12
11543
by: Dave H. | last post by:
Please redirect me if this message is posted to the wrong group. Given the intention of delivering content to an HTTP user agent (such as Internet Explorer) which is to be immediately opened by an associated application, there are the Content-Disposition type options of "inline" or "extension-token". Specifically as regards Internet Explorer, I've tried both inline and the specific filename extension (xls,csv,pdf,doc,...) and the...
1
1309
by: Iyhamid | last post by:
Hello Every 1: I Have this query: Tran Date Total Tran Per Day 200 0000 4 0710 38 0711 78 0712 15
0
8965
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9466
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9202
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3278
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2191
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.