SQL 使用当前日期作为列的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5718191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:12:16  来源:igfitidea点击:

use current date as default value for a column

sqlsql-server

提问by dcarneiro

Is there a way to set the default value of a column to DateTime.Nowin Sql Server?

有没有办法DateTime.Now在 Sql Server 中设置列的默认值?

Example:

例子:

table Event
Id int (auto-increment) not null
Description nvarchar(50) not null
Date datetime not null

The line:

线路:

Insert into Event(Description) values('teste');

should insert a row and the Date value should be the current date.

应该插入一行,日期值应该是当前日期。

回答by Jason

Add a default constraint with the GETDATE() function as value.

使用 GETDATE() 函数作为值添加默认约束。

ALTER TABLE myTable 
  ADD CONSTRAINT CONSTRAINT_NAME
    DEFAULT GETDATE() FOR myColumn

回答by Mayuresh

CREATE TABLE Orders(
    O_Id int NOT NULL,
    OrderNo int NOT NULL,
    P_Id int,
    OrderDate date DEFAULT GETDATE() // you can set default constraints while creating the table
)

回答by Blazes

You can use:

您可以使用:

Insert into Event(Description,Date) values('teste', GETDATE());

Also, you can change your table so that 'Date' has a default, "GETDATE()"

此外,您可以更改表格,以便“日期”具有默认值“GETDATE()”

回答by Khalid

Select Table Column Name where you want to get default value of Current date

选择要获取当前日期默认值的表列名称

 ALTER TABLE 
 [dbo].[Table_Name]
 ADD  CONSTRAINT [Constraint_Name] 
 DEFAULT (getdate()) FOR [Column_Name]

Alter Table Query

更改表查询

Alter TABLE [dbo].[Table_Name](
    [PDate] [datetime] Default GetDate())

回答by SherlockSpreadsheets

I have also come across this need for my database project. I decided to share my findings here.

我的数据库项目也遇到了这种需求。我决定在这里分享我的发现。

1) There is no way to a NOT NULL field without a default when data already exists (Can I add a not null column without DEFAULT value)

1)当数据已经存在时,没有默认值的 NOT NULL 字段是没有办法的(我可以添加一个没有 DEFAULT 值的非空列

2) This topic has been addressed for a long time. Here is a 2008 question (Add a column with a default value to an existing table in SQL Server)

2)这个话题已经讨论了很长时间。这是一个 2008 年的问题(向 SQL Server 中的现有表添加具有默认值的列

3) The DEFAULT constraint is used to provide a default value for a column. The default value will be added to all new records IF no other value is specified. (https://www.w3schools.com/sql/sql_default.asp)

3) DEFAULT 约束用于为列提供默认值。如果未指定其他值,则默认值将添加到所有新记录中。( https://www.w3schools.com/sql/sql_default.asp)

4) The Visual Studio Database Project that I use for development is really good about generating change scripts for you. This is the change script created for my DB promotion:

4) 我用于开发的 Visual Studio 数据库项目非常适合为您生成更改脚本。这是为我的数据库提升创建的更改脚本:

GO
PRINT N'Altering [dbo].[PROD_WHSE_ACTUAL]...';

GO
ALTER TABLE [dbo].[PROD_WHSE_ACTUAL]
    ADD [DATE] DATE DEFAULT getdate() NOT NULL;

-

——

Here are the steps I took to update my database using Visual Studio for development.

以下是我使用 Visual Studio 更新我的数据库进行开发所采取的步骤。

1) Add default value (Visual Studio SSDT: DB Project: table designer) enter image description here

1) 添加默认值(Visual Studio SSDT:DB Project:表设计器) 在此处输入图片说明

2) Use the Schema Comparison tool to generate the change script.

2) 使用模式比较工具生成更改脚本。

code already provided above

3) View the data BEFORE applying the change. enter image description here

3) 在应用更改之前查看数据。 在此处输入图片说明

4) View the data AFTER applying the change. enter image description here

4) 查看应用更改后的数据。 在此处输入图片说明

回答by Mohamad Reza Shahrestani

Right click on the table and click on Design,then click on column that you want to set default value.

右键单击表格并单击设计,然后单击要设置默认值的列。

Then in bottom of page in column properties set Default value or binding to : 'getdate()'

然后在列属性的页面底部将默认值或绑定设置为:'getdate()'

回答by Maher Ben Issa

To use the current dateas the defaultfor a date column, you will need to:

要将当前日期用作日期列的默认值,您需要:

1-open table designer

1-开放表设计器

2-select the column

2-选择列

3-go to column proprerties

3-转到列属性

4-set the value of Default value or bindingpropriete To (getdate())

4-设置默认值或绑定propriete To ( getdate())

enter image description here

在此处输入图片说明

回答by Sameer

Table creation Syntax can be like:

表创建语法可以是这样的:

Create table api_key(api_key_id INT NOT NULL IDENTITY(1,1) 
PRIMARY KEY, date_added date DEFAULT 
GetDate());

Insertion query syntax can be like:

插入查询语法可以是这样的:

Insert into api_key values(GETDATE());