Using the clause WITH VALUES when you are adding a new column to an existing table using Microsoft SQL Server is a handy little trick. The use of WITH VALUES is when you want to add a default constraint to an existing table that allows null . Because when you add a default constraints that allows null it will NOT fill the existing rows with a value, this can effect your database logics ( if you have written some code depending on these values ). Let me demonstrate this with some simple tables. Create a simple table CREATE TABLE Students( id int NOT NULL, name varchar(50) NULL ) Add some sample data insert into Student (id,name) values (1,'Cappadonna'), (2,'UGod') Add a new column to the table , with NOT NULL clause ALTER TABLE Student ADD age int not null CONSTRAINT age_default DEFAULT 99 Now if you query your table you will see that the existing rows are being populated with your default value. This is because we added the constraint as NOT NULL. No...
Comments
Post a Comment