SQLite implements the default time for the current Time column method

Source: Internet
Author: User

Original address: http://blog.csdn.net/derryzhang/article/details/5033209

In SQL Server, when creating a table, for time columns we can sometimes specify the default value as the current time (i.e., the default timestamp when the record is generated). For example:

[XHTML]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. CREATE TABLE log (
    2. Content varchar (256),
    3. LogTime datetime default GETDATE ()
    4. )

But how is it implemented in SQLite? Check the documentation that SQLite does not have the getdate () function, but its system built-in function has a datetime (), so you can follow the following syntax to implement the default timestamp:

[XHTML]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. CREATE TABLE log (
    2. Content varchar (256),
    3. LogTime datetime default datetime (' Now ')
    4. )

The answer is no, it will prompt a syntax error. So how do we declare it? As shown below:

[C-sharp]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. CREATE TABLE log (
    2. Content varchar (256),
    3. LogTime TIMESTAMP Default Current_timestamp
    4. )

This can be achieved, but the default time is based on Greenwich Mean time, so the words used in China will be just 8 hours earlier. To solve this problem, we can declare that:

[XHTML]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. CREATE TABLE log (
    2. Content varchar (256),
    3. LogTime TIMESTAMP Default (DateTime (' Now ', ' localtime ')
    4. )

Test it, everything is OK:)

SQLite implements the default time for the current Time column method (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.