0

I am new to MySQL and databases in general so I am sorry if this is a novice question. I am creating a MySQL database with a users table. One of the columns is UserAccept and the other is UserAcceptWhen. When they log in to the program, which is built using C#, they have to accept a user agreement first. If they click Accept, then then get in, but the question is about updating the table. Once they click Accept, UserAccept will change from 0 to 1, but how can I then have UserAcceptWhen update with the current date and time? Also, can I lock that column from changing, unless the UserAccept column changes?

4

2 回答 2

1

正如@Matt Clark 所说,您可以使用 MySQL NOW() 函数来存储当前时间值。一个小的附录是考虑在 UserAccept 更改时使用触发器来更新 UserAcceptWhen。像这样的东西:

DELIMITER //
CREATE TRIGGER UserAcceptedNOW BEFORE UPDATE ON user
FOR EACH ROW
  BEGIN
    IF (NEW.UserAccepted = 1) THEN
      SET NEW.UserAcceptedWhen = NOW();
    END IF;  
  END
//

不要忘记将 UserAcceptWhen 设置为 Timestamp 类型。

对于第二个问题:afaik 你不能锁定一列进行更改。

通常最好使用数据库之外的逻辑(转储数据库会更容易),但这也是一种解决方案。

于 2013-10-28T16:40:32.020 回答
1

MySQL 中有一个很棒的函数,NOW()它可以返回时间和日期。

UPDATE table SET UserAcceptWhen = NOW() WHERE id = 1;
于 2013-10-28T16:31:30.053 回答