Add a column with a default value to an existing table in SQL Server

Опубликовано: 29 Сентябрь 2024
на канале: Learn 2 Excel
9,668
18

Published on Aug 13, 2017:
In this video tutorial, we will learn to add a new column to an existing table in SQL server.
We will also learn to specify default values for the new column

Please find below the create table command to create the new tables with data.
USE [AdventureWorksDW2012]
GO

Select * into [dbo].[Employee_1]
from
(
Select
1 EmployeeID,'Frank' EmployeeName
Union
Select 2 EmployeeId,'Raj' EmployeeName
Union
Select 3 EmployeeID,'Json' EmployeeName
Union
Select 4 EmployeeId,'Bing' EmployeeName

)A

Select * into [dbo].[Employee_2]
from
(
Select
1 EmployeeID,'Frank' EmployeeName
Union
Select 2 EmployeeId,'Raj' EmployeeName
Union
Select 3 EmployeeID,'Json' EmployeeName
Union
Select 4 EmployeeId,'Bing' EmployeeName

)A



Alter table command for the two tables is given below.

Alter Table dbo.Employee_1
Add Salary int Null
Constraint DefaultSalary Default -99999


Alter Table dbo.Employee_2
Add Salary int Not Null
Constraint DefaultSalary_2 Default -99999

Just follow the video to make additional changes.

Previous Video:   • Left Function SQL Server  

SUBSCRIBE to learn more about Power BI,Power Query, Power Pivot, Excel and SQL Server!
   / @learnexcelrelentless  



Download the sample database used for the demo here: https://goo.gl/SVyVmT

Our Playists:
SQL Playlist :https://goo.gl/PS5Ep6
DAX PlayList : https://goo.gl/S4W41D
Power BI PlayList: https://goo.gl/dXxvnB
Power Query Playlist: https://goo.gl/5QS7P4


ABOUT DAGDOO:
Website:
Home Page: http://www.dagdoo.org/
Power BI Tutorials: http://www.dagdoo.org/excel-learning/...


Questions? Comments and SUGESTIONS? You will find me here:

Twitter: @dagdooe

Category: Science & Technology
License: Standard YouTube License