Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
テーブルを変更して列のデフォルト値を追加するときに、古い行の値を更新するにはどうすればよいですか
type smallint NOT NULL DEFAULT 0
しかし、古い行はまだ null です。なぜ自動でデフォルト値に更新できないのですか?
トランザクションで、alter table ステートメントの前に update ステートメントを発行します。
update table set col = 0 where col is null
テーブルの変更は、その仕事や責任ではないため、データを更新しません。更新は更新の仕事です。(私が知っている唯一の例外は、列の型を変更する場合です。この場合、using 句を使用して、必要に応じてデータを変換できます。)