<p>I have a table defined (see code snippet below). How can I add a constraint or whatever so that the LastUpdate column is automatically updated anytime the row is changed?</p>

<pre><code>CREATE TABLE dbo.Profiles
(
        UserName                                varchar(100)            NOT NULL,
        LastUpdate                              datetime                NOT NULL  CONSTRAINT DF_Profiles_LastUpdate DEFAULT (getdate()),
        FullName                                varchar(50)             NOT NULL,
        Birthdate                               smalldatetime           NULL,
        PageSize                                int                     NOT NULL CONSTRAINT DF_Profiles_PageSize DEFAULT ((10)),
        CONSTRAINT PK_Profiles PRIMARY KEY CLUSTERED (UserName ASC),
        CONSTRAINT FK_Profils_Users FOREIGN KEY (UserName) REFERENCES dbo.Users (UserName) ON UPDATE CASCADE ON DELETE CASCADE  
)
</code></pre>

[{'answer_id': 36002, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 2, 'selected': False, 'text': "<p>You're going to have to use triggers for that.</p>\n"}, {'answer_id': 36011, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 3, 'selected': True, 'text': '<p>A default constraint only works on inserts; for an update use a trigger.</p>\n'}, {'answer_id': 36018, 'author': 'Rob Allen', 'author_id': 149, 'author_profile': 'https://Stackoverflow.com/users/149', 'pm_score': -1, 'selected': False, 'text': "<p>My suggestion would be to create a stored procedure which defaults the lastUpdate to getdate(). </p>\n\n<p>I've tried to avoid triggers in the past because pre-SQL2005 locating and editing them was a pain in the rump. Especially for developers who are new to your project. </p>\n\n<p>Also add that as the default value for your column definition.</p>\n"}, {'answer_id': 36068, 'author': 'Kevin Fairchild', 'author_id': 3743, 'author_profile': 'https://Stackoverflow.com/users/3743', 'pm_score': 5, 'selected': False, 'text': "<p>I agree with the others -- set a default value of GetDate() on the LastUpdate column and then use a trigger to handle any updates.</p>\n\n<p>Just something simple like this:</p>\n\n<pre><code>CREATE TRIGGER KeepUpdated on Profiles\nFOR UPDATE, INSERT AS \nUPDATE dbo.Profiles \nSET LastUpdate = GetDate()\nWHERE Username IN (SELECT Username FROM inserted)\n</code></pre>\n\n<p>If you want to get really fancy, have it evaluate what's being changed versus what's in the database and only modify LastUpdate if there was a difference.</p>\n\n<p>Consider this...</p>\n\n<ul>\n<li><p><strong>7am</strong> - User 'jsmith' is created with a last name of 'Smithe' (oops), LastUpdate defaults to 7am</p></li>\n<li><p><strong>8am</strong> - 'jsmith' emails IT to say his name is incorrect. You immediately perform the update, so the last name is now 'Smith' and (thanks to the trigger) LastUpdate shows 8am</p></li>\n<li><p><strong>2pm</strong> - Your slacker coworker finally gets bored with StumbleUpon and checks his email. He sees the earlier message from 'jsmith' regarding the name change.  He runs: <em>UPDATE Profiles SET LastName='Smith' WHERE Username='jsmith'</em> and then goes\nback to surfing MySpace. The trigger doesn't care that the last name was already 'Smith', however, so LastUpdate now shows 2pm.</p></li>\n</ul>\n\n<p>If you just blindly change LastUpdate whenever an update statement runs, it's TECHNICALLY correct because an update did happen, but it probably makes more sense to actually compare the changes and act accordingly.  That way, the 2pm Update statement by the coworker would still run, but LastUpdate would still show 8am.</p>\n\n<p>--Kevin</p>\n"}, {'answer_id': 751692, 'author': 'HLGEM', 'author_id': 9034, 'author_profile': 'https://Stackoverflow.com/users/9034', 'pm_score': 2, 'selected': False, 'text': "<p>I agree with the trigger idea, although I would use a join to inserted instead of a subquery. However, I want to point out that username is a particularly poor choice for a primary key. Usernames often change and when they do you need to change all related tables. It is far better to have a user id as the key and then put a unique index on username. Then when the user name changes, you don't need to change anything else.</p>\n"}]
['https://Stackoverflow.com/questions/36001', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3747/']