Search code examples
sqltimestampdefault-value

Setting Default TIMESTAMP in SQL Column


I am creating a Table and I have a column that is TIMESTAMP.

I want to set a default value to it. How can I do that ?

For Example:

I have a USER_LAST_SEEN table, and I have two columns:

1) username 2) logout date

and I want to be able to tell if a user has never been logged out yet, by setting a default timestamp I can then look for. How do I do that ?

CREATE TABLE LAST_SEEN_TABLE (USERNAME STRING, LOGOUT_DATE TIMESTAMP [What am I missing here?])

Edit: Saving NULL is not a good solution, because of the scenario that a user already exists when I create the table. Showing "Never logged in", which is shown when a user DOES have NULL, is wrong in this case. I need a way to have ANOTHER indication that user DID log in before in order to show "N/A"


Solution

  • If you cannot use NULL, you can use a specific date, for example '1900-01-01'

    CREATE TABLE LAST_SEEN_TABLE 
    (
        USERNAME varchar(100), 
        LOGOUT_DATE TIMESTAMP default '1900-01-01 00:00:00'
    );