Add default value of datetime field in SQL Server to a timestamp

I've got a table that collects forms submitted from our website, but for some reason, when they created the table, they didn't put a timestamp in the table. I want it to enter the exact date and time that the record was entered.

I know it's in there somewhere, but I can't seem to find how to set the default value (like in Access, you use getNow() or Now() ) but I don't know where to put it.


用于修改现有表中的现有列:

ALTER TABLE YourTable ADD CONSTRAINT DF_YourTable DEFAULT GETDATE() FOR YourColumn

This can also be done through the SSMS GUI.

  • Put your table in design view (Right click on table in object explorer->Design)
  • Add a column to the table (or click on the column you want to update if it already exists)
  • In Column Properties, enter (getdate()) in Default Value or Binding field as pictured below
  • 在这里输入图像描述


    In that table in SQL Server, specify the default value of that column to be CURRENT_TIMESTAMP . DataType of that column may be datetime or datetime2.

    eg

    Create Table Student
    (
      Name varchar(50),
      DateOfAddmission datetime default CURRENT_TIMESTAMP
    );
    

    I hope it will work.

    链接地址: http://www.djcxy.com/p/24708.html

    上一篇: SQL Server中Oracle的RowID等效

    下一篇: 在SQL Server中将日期时间字段的默认值添加到时间戳