简体   繁体   English

update_date NOT NULL约束的数据库表设计最佳实践

[英]Database table design best practice for update_date NOT NULL constraints

I have database table contains updated_date column as below. 我的数据库表包含updated_date列,如下所示。

CREATE TABLE table_name
(
  id character varying(20) NOT NULL,
  created_date timestamp NOT NULL,
  updated_date timestamp,
  isactive smallint DEFAULT 1,
  version smallint NOT NULL DEFAULT 0,
  CONSTRAINT table_name_pkey PRIMARY KEY (id)
);

Please suggest updated_date timestamp NOT NULL is best practice? 请建议updated_date timestamp NOT NULL是最佳实践吗?

It depends a lot on how you are going to use those columns. 这很大程度上取决于您将如何使用这些列。

I see nothing wrong with declaring the column NOT NULL and using the same created_date and updated_date when a record is created (I would have chosen names that do not contain “date” though). 我发现声明记录为NOT NULL并在创建记录时使用相同的created_dateupdated_date没什么错(尽管我会选择不包含“ date”的名称)。

I think that it is a good habit to have columns NOT NULL if that is possible for the application without bending its arm (a bad example would be using 1970-01-01 00:00:00 to represent missing data). 我认为,如果应用程序可以在1970-01-01 00:00:00手臂的情况下使用NOT NULL列,则是一个好习惯(一个不好的例子是使用1970-01-01 00:00:00来表示丢失的数据)。 It usually makes queries simpler and hence more efficient if they don't have to deal with NULL values. 如果不必处理NULL值,通常会使查询更简单,因此效率更高。

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

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