简体   繁体   English

Big Query Tables 中列的默认值

[英]Default values for columns in Big Query Tables

Is there a way to set default values for columns in tables in big query?有没有办法在大查询中为表中的列设置默认值? I would like to set 'false' as a default value for a column of boolean data type.我想将“false”设置为 boolean 数据类型列的默认值。

A nullable column can (trivially) have a NULL default value, but there is no other notion of default in BigQuery (you either insert a particular value or omit the value and it will have the NULL value).可空列可以(简单地)具有 NULL 默认值,但 BigQuery 中没有其他默认值概念(您可以插入特定值或省略该值,它将具有 NULL 值)。

That said, if you want to wrap your raw table in a View, you can map a NULL column value to any default that you like.也就是说,如果您想将原始表包装在视图中,您可以将 NULL 列值映射到您喜欢的任何默认值。

There is no default values for columns in BigQuery BigQuery 中的列没有默认值
The simple way would be just to use NULL (in most cases this should be acceptable from requirements prospective)简单的方法就是使用 NULL (在大多数情况下,从需求的角度来看这应该是可以接受的)
Also conside cost aspect of this - storing NULL as a default cost you zero versus storing actual value will cost you column size multipled by count of rows with default values还要考虑成本方面 - 将 NULL 存储为默认成本为零与存储实际值将花费您的列大小乘以具有默认值的行数

If NULL will work for you but you still need to have your real default value for use in application - you can use field description to store some metadata about this field including default value, so than application will be able to retrieve it and use it based on whatever application logic is如果 NULL 对您有用,但您仍然需要在应用程序中使用真正的默认值 - 您可以使用字段描述来存储有关该字段的一些元数据,包括默认值,这样应用程序将能够检索它并基于它使用它在任何应用程序逻辑上

In BigQuery - View is the best way to expose your real default value在 BigQuery 中 - 查看是公开真实默认值的最佳方式

While you can't set a default at table level, you can do it in SQL at the time of insertion, if that works for your purposes.虽然您不能在表级别设置默认值,但您可以在插入时在 SQL 中进行设置,如果这对您有用的话。 You can use the IF() conditional expression , as such:您可以使用IF() 条件表达式,例如:

SELECT IF(field IS NOT NULL, field, "default") from `table`

In fact, you can do whatever which conditional expression floats your boat.事实上,你可以做任何让你的船漂浮的条件表达式。

Now BigQuery supports setting default values on columns in tables现在 BigQuery 支持在表中的列上设置默认值

https://cloud.google.com/bigquery/docs/default-values https://cloud.google.com/bigquery/docs/default-values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM