How to Create Datetime Colum with Default Value in SQLite

How to create a table and add a field with date type field in SQLite with defining default date value for column by adding CURRENT_TIMESTAMP.
In today's article I will show a simple tutorial with example how you can create datetime column with default value n SQLite database. Here in today's article i am using SQLite Studio to demonstrate the example. 

So, for this article first we will open SQLite studio and create a new SQLite database named as EmployeeDB.

SQLite DB

Now let's create a sample table in the database and after creating table will add come values in it.

Employee Table

As we all know SQLite support only support BLOB, INTEGER, NUMERIC, REAL, TEXT. It does not have specific datatype in SQLite database. So, for date field we use datatype text. Here I have taken two field which expect value as date one is Date_Of_Birth and second one is Created_Date. Here I have user CURRENT_TIMESTAMP to set default datetime for Created_Date field. Now let's add some record in table and check for output.

Insert data in table
Here for Date_Of_Birth field I have passed date in yyyy-mm-dd format. After executing the query lets check the output in table.

SQLite Table Data
Here we are able to see the date value which we passed and value for Created_Date  is auto populating the value. 
Info! SQLite does not have any specific datatype for DateTime filed. We user TEXT, REAL, or INTEGER values: TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS).
For more detail you can refer https://sqlite.org/lang_datefunc.html

Post a Comment