gpt4 book ai didi

SQL 服务器 : understanding default values

转载 作者:行者123 更新时间:2023-12-02 01:07:18 25 4
gpt4 key购买 nike

我正在学习数据库基础类(class),后评估中的问题之一是:

Default values must be set for NOT NULL rows. True|False

我被引导相信答案是正确的,因为我回答错误而且错误,问题是我不明白为什么它是正确的。

如果问题是:

Default values must be set for NOT NULL columns when using ALTER TABLE to add a new column.

然后我知道那是真的并且明白为什么。我是在误读问题还是在其他地方误解了什么?

任何帮助将不胜感激。李.

最佳答案

你是对的。当列声明为 not null 时,不需要默认定义。如果您查看 syntax diagram for create table,这一点就很明显了。 :

<column_definition> ::=  
column_name <data_type>
. . .
[ CONSTRAINT constraint_name [ DEFAULT constant_expression ] ]
. . .
[ NULL | NOT NULL ]

如果需要语法,它看起来更像这样:

<column_definition> ::=  
column_name <data_type>
. . .
[ CONSTRAINT constraint_name [ DEFAULT constant_expression ] ]
. . .
[ NULL | NOT NULL [CONSTRAINT constraint_name] DEFAULT constant_expression ]

我什至不确定默认定义是否适合 not null 列。在许多情况下,您希望 insert 失败,而不是插入一些人工值。

唯一的关系是当altering tables有数据:

If the new column does not allow null values and the table is not empty, a DEFAULT definition must be added with the new column, and the new column automatically loads with the default value in the new columns in each existing row.

这很明显。现有行将被赋予新列的 NULL 值——但 NOT NULL 约束不允许这样做,因此需要 DEFAULT

关于SQL 服务器 : understanding default values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46987962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com