在开始/结束时间将数据写入表
如果我正确理解“NOT NULL”,则将其用于可以在创建表时写入的任何数据,并且如果存在需要但要在表创建后写入的列数据,则需要为 DEFAULT NULL 或 NOT空默认“xxx”?
Usecase是会话表。 该行是在用户登录时写入的。因此,此时我们写入日期、IP、会话 ID、会员 ID 等。但是当用户注销时,我需要写入结束日期时间,即他所在的最后一页 因此,
对于这些结束列,如果我使用 NOT NULL,则在创建行时将没有值可写入。我是正确的还是我遗漏了一些关于数据如何写入的内容?
If i understand correctly "NOT NULL" when used it for any data that can be written at the point of table creation, and if there are colunm data which are required but to be written after table creation thy need to be either DEFAULT NULL or NOT NULL DEFAULT 'xxx'?
Usecase is the session table.
The row is written when the user logs in. So at that point we write the date, IP, session id, member id, etc. But when the user logs off then i need to write the end datetime, the last page he was on, mode of log off, etc.
So for these end colunms if i use NOT NULL there will be no value to write at row creation time. Am i correct or am i missing something on how data is written?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用默认值,也可以将结束日期移动到新表中,这样就不必与开始日期同时填充。通常,这两个选项中的任何一个都比空日期更可取。
You could use a default value or you could move the end date to a new table so that it doesn't have to be populated at the same time as start date. As a rule, either of those two options would be preferable to having a null date.
如果将这些结束列设置为 NOT NULL,则当您尝试在用户登录时写入数据时,数据库将抛出异常。将列设置为 NOT NULL 意味着插入新行时它们不能为 NULL。在您的情况下,您希望将结束列(结束日期时间、最后一页等)设置为可为空,而将日期、IP、会话 ID、成员 ID 等列设置为 NOT NULL。
If you set those end columns as NOT NULLs, the database will throw you an exception when you are trying to write data upon user login. Setting a column as NOT NULL means they must not be NULL when insert a new row. In your case, you want to make the end columns (end datetime, last page, etc) to be nullable whereas you make columns like date, IP, session ID, member Id to be NOT NULLs.