Open In App

How to Alter a Column from Null to Not Null in SQL Server?

Last Updated : 23 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

With this article, we will learn how to alter a column from accepting Null values to Not Null in SQL Server. The prerequisites of this article are you should be having a MSSQL server on your computer.

What is a query?

A query is a statement or a group of statements written to perform a specific task, like retrieve data, save data into a database.

So, we will create a database first:

Step 1: Creating Database

CREATE DATABASE GFG

Step 2: Using database

USE GFG

Step 3: Create a table with a null column

CREATE TABLE gfgTutorial(
id integer,
Name varchar(20)
)

Describe the table

sp_help 'dbo.gfgTutorial'

Step 4: Change the id column to not null

ALTER TABLE gfgTutorial ALTER COLUMN id VARCHAR (50) NOT NULL;

So now our table id column is changed to not null

Changed null id to not null

So we have successfully changed the id column to not null in the gfgTutorial table.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads