How to Set a existing column of MS SQL table as NOT NULL?
ALTER TABLE tablename
ALTER COLUMN columnname datatype NOT NULL
You will obviously have to make sure that the column does not contain any NULL values before doing this.
E.g.
ALTER TABLE orders
ALTER COLUMN customer_id INT NOT NULL
Firstly ensure that the fields have non null values. In this case I'm working with a field that has a GUID nvarchar so I'll do
UPDATE tablename
SET fieldname = Newid()
WHERE fieldname IS NULL;
Then as Adam Ralph says
ALTER TABLE tablename ALTER COLUMN fieldname datatype NOT NULL
Related
Currently I have a column named CreatedDate in a Shipper table I created. It is nullable. My task is to change the created date to be a required field, not allow nulls, and have a default of GetDate(). This has to be done in a single query... Keep in mind there is no data in my table yet. I've tried the following code and I can't seem to get it to work. This is a homework assignment and I'm only looking for guidance. Any help would be greatly appreciated. Thanks in advance.
USE Business
ALTER TABLE Shipper
ALTER COLUMN CreatedDate date NOT NULL
CONSTRAINT CNDefaultCreatedDate
DEFAULT GETDATE() For CreatedDate;
Two different statements, I assume this is SQL Server:
--Add the default
ALTER TABLE [Shipper] --What table
ADD CONSTRAINT [def_createddate] --Give the constraint a name
DEFAULT GETDATE() FOR [CreatedDate]; --default of what for which column
--Set to not allow NULL
ALTER TABLE [Shipper] --What table
ALTER COLUMN [CreatedDate] DATE NOT NULL; --altering what column and either NULL or NOT NULL
Understand adding a default will not update existing data. I know you mentioned your table does not have data, but in the future the null values must be updated to some value before the ALTER COLUMN NOT NULL is allowed.
Here's reference to the MS documentation ALTER TABLE
If the column did not already exist you can add it, set it not null and then even update existing rows in one statement:
--If Shipper did not already have the column CreatedDate
ALTER TABLE [Shipper]
ADD [CreatedDate] DATE NOT NULL
CONSTRAINT [def_createddate] DEFAULT GETDATE()
WITH VALUE --if column is nullable use this and it will update existing records with the default. It column is NOT NULL this is applied by default.
Have you tried this:
USE Business
ALTER TABLE Shipper
ALTER COLUMN CreatedDate date NOT NULL DEFAULT GETDATE();
Can the datatype of a field be changed to int from nvarchar??
alter table employee alter column designation int
is this valid?? If not can it be done in some other way??
P.S: I am using MS SQL Server
You can try doing an alter table. If it fails do this:
Create a new column that's an integer:
ALTER TABLE tableName ADD newCol int;
Select the data from the old column into the new one:
UPDATE tableName SET newCol = CAST(oldCol AS int);
Drop the old column
It is possible only when you column has no value or blank. If your column has some value which have nvarchar value and you should try to convert it into int, it will give error.
ALTER TABLE [table_name] ALTER COLUMN [column_name] [data_type]
Add new numeric column.
Copy from old char column to new column with trim and conversion.
Drop old char column.
Rename numeric column to old column name.
This worked for me (with decimals but I suppose it will work with ints):
alter table MyTable add MyColNum decimal(15,2) null
go
update MyTable set MyColNum=CONVERT(decimal(15,2), REPLACE(LTRIM(RTRIM(MyOldCol)), ',', '.')) where ISNUMERIC(MyOldCol)=1
go
alter table MyTable drop column MyOldCol
go
EXEC sp_rename 'MyTable.MyColNum', 'MyOldCol', 'COLUMN'
go
Can be done even simpler in just 2 steps
Update the column and set all non numberic values to null so alter won't fail.
Alter the table and set the type to int.
UPDATE employee
SET designation = (CASE WHEN ISNUMERIC(designation)=1 THEN CAST(CAST(designation AS FLOAT) AS INT)END )
ALTER TABLE employee
ALTER COLUMN designation INT
This takes the assumption that that the columns allow nulls. If not then that needs to be handled as well. For example: By altering the column to allow null, then after it has been converted to int then set all null values to 0 and alter the table to not allow null
Create a temp column
ALTER TABLE MYTABLE ADD MYNEWCOLUMN NUMBER(20,0) NULL;
Copy and casts the data from the old column to the new one
UPDATE MYTABLE SET MYNEWCOLUMN=CAST(MYOLDCOLUMN AS NUMBER(20,0));
Delete the old column
ALTER TABLE MYTABLE DROP COLUMN MYOLDCOLUMN;
Rename the new one to match the same name as the old one.
ALTER TABLE MYTABLE RENAME COLUMN MYNEWCOLUMN TO MYOLDCOLUMN;
Can you try this ?
alter table MyTable add MyColNum Varchar(500) null;
alter table MyTable add MyColNum int null;
How to make a NULLABLE column to NOT NULL in SQL SERVER. I made a table with four columns:
My table:
id int null
fname varchar not null
last name not null
contact int null.
Now what i am required is to make id column to be not null.So please help me how we can make a null column to be a not null column.
Use the following statements to do it. Change the name of column id as required
UPDATE [Table] SET id=0 WHERE id IS NULL;
ALTER TABLE [Table] ALTER COLUMN id INTEGER NOT NULL;
Replace [Table] with the name of the table
first Update id Column '0'(Zero) where NULL then right Click on table then select Design and uncheck Allow Nulls checkbox of your Column from sql table design.
ALTER TABLE tableName
ALTER COLUMN columnName nvarchar(200) [NULL|NOT NULL]
Need to alter a table to allow nulls on a column -- but cant drop the column...can I do this? Was trying something like:
ALTER TABLE myTable MODIFY myColumn NULL;
But to no avail....
ALTER TABLE myTable ALTER COLUMN myColumn {DataType} NULL
where {DataType} is the current data type of that column (For example int or varchar(10))
Sure you can.
ALTER TABLE myTable ALTER COLUMN myColumn int NULL
Just substitute int for whatever datatype your column is.
The syntax is very based on database service
SQL Server / MS Access:
ALTER TABLE table_name
ALTER COLUMN column_name datatype NULL;
My SQL / Oracle (prior version 10G):
ALTER TABLE table_name
MODIFY COLUMN column_name datatype NULL;
Oracle 10G and later:
ALTER TABLE table_name
MODIFY column_name datatype NULL;
if you want to set a default value then:
ALTER TABLE table_name
ALTER COLUMN column_name datatype DEFAULT default_value;
For MYSQL
ALTER TABLE myTable MODIFY myColumn {DataType} NULL
I am trying to remove not null constraint in sql server 2008 without losing data.
ALTER TABLE YourTable ALTER COLUMN YourColumn columnType NULL
Remove constraint not null to null
ALTER TABLE 'test' CHANGE COLUMN 'testColumn' 'testColumn' datatype NULL;
Remove column constraint: not null to null
ALTER TABLE test ALTER COLUMN column_01 DROP NOT NULL;
Reference: https://www.tutorialspoint.com/How-can-we-remove-NOT-NULL-constraint-from-a-column-of-an-existing-MySQL-table
ALTER TABLE tableName MODIFY columnName columnType NULL;