2

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"

6
  • 1
    Why don't just leave the logout date as NULL if user never logs out ? Commented Aug 21, 2018 at 6:24
  • 3
    Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ... Commented Aug 21, 2018 at 6:27
  • are you using sql server or what? Commented Aug 21, 2018 at 6:27
  • Just add default constraint as current_timestamp to your column . Commented Aug 21, 2018 at 6:29
  • @rcs the "NULL" is in fact used for something else in this context. That's why I don't use it and I'm looking for another solution Commented Aug 21, 2018 at 6:46

1 Answer 1

2

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'
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is what I want to know how to do !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.