HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Is it possible to make one column readonly?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
columnmakeonepossiblereadonly

Problem

I'm curious if it is possible to create a table with a column that can never be changed, but the other columns of the table can.

For instance I could imagine a CreatedByUser column that should never be changed.

Is there a built-in functionality in SQL Server for this, or is it only possible via triggers or something else?

Solution

There is no built in declarative support for non updatable columns (except for specific predefined cases such as IDENTITY)

This Connect item requested it but was rejected. Add DRI to enforce immutable column values. See also Allow us to mark columns as immutable or const on the current feedback sit

An UPDATE trigger would probably be the most robust way of achieving this. It could check IF UPDATE(CreatedByUser) and raise an error and rollback the transaction if true.

Context

StackExchange Database Administrators Q#36163, answer score: 31

Revisions (0)

No revisions yet.