How to make a NULLable column (varchar2(20)) to NOT NULL of a table in oracle where I have more than 1 million records in the table
NOT NULL constraint specifies that a column cannot contain NULL values. To add a NOT NULL constraint to an existing table by using the ALTER TABLE statement.
ALTER TABLE table_name MODIFY ( column_name NOT NULL);
In this case, the column_name must not contain any NULL value before applying the NOT NULL constraint.
Related
I am trying to add a new column into an existing table, which has to be NOT NULL. It is a foreign key referring to ID of other table and I can't use default value.
I did it in couple of steps:
add the new column to the table with NULL constraint
run update scripts to update the new column with correct values for all rows
add foreign key constraint
change the new column to NOT NULL
The issue is that I want to follow a rule we have, that the DDL and DML scripts are separated (for couple of reasons). I can't figure out how to do this. Is there a way how to add a NOT NULL column into an existing table in a way that the DDL and DML would be separated?
Do step 1 and 3 together (if you do not then you risk adding values in step 2 that will not match the constraint).
ALTER TABLE table_name
ADD column_name NUMBER(8,0)
CONSTRAINT table_name__column_name__fk REFERENCES other_table(other_column);
You can also do step 4 at the same time if you make the NOT NULL constraint DEFERRABLE:
ALTER TABLE table_name
ADD column_name NUMBER(8,0)
CONSTRAINT table_name__column_name__nn NOT NULL DEFERRABLE
CONSTRAINT table_name__column_name__fk REFERENCES other_table(other_column);
Then update the data. For example, you can use a MERGE statement to update from a sub-query of pre-generated values:
MERGE INTO table_name dst
USING (
SELECT 1 AS id, 2 AS new_column_name FROM DUAL UNION ALL
SELECT 2, 2 FROM DUAL UNION ALL
SELECT 3, 1 FROM DUAL UNION ALL
SELECT 4, 4 FROM DUAL
) src
ON (dst.id = src.id)
WHEN MATCHED THEN
UPDATE
SET column_name = src.new_column_name;
Then:
COMMIT;
Which will enforce any DEFERRABLE NOT NULL constraints.
If you did not make the constraint DEFERRABLE
then change the column to NOT NULL:
ALTER TABLE table_name
MODIFY column_name NUMBER(8,0) NOT NULL;
db<>fiddle (not deferrable) (deferrable)
I have a column 'name' in student table. I need to add NOT NULL constraint on this column. But I get SQL error saying cannot add null constraint since the existing rows in the table has null values in the column. How would I add a null constraint along with default value in a single alter statement. Below is my query.
alter table Student alter column name nvarchar NOT NULL;
SQL Server does not make this easy. I think the only way is to set the existing values to the default that you want. Then change the column to have a default value and not null:
-- Get rid of the existing `NULL` values
update students set name = '' where name is null;
-- Add the NOT NULL constraint
alter table students
alter column name varchar(255) not null;
-- Add a default value
alter table students
add constraint df_t_name default '' for name ;
Here is what it looks like in practice.
But I get SQL error saying cannot add null constraint since the existing rows in the table has null values in the column.
Have you tried overwriting the existing NULL values?
You can't have a constraint on a column when existing values would violate that constraint. You need to make your table compliant first.
how to add not null column in existing table and then insert values in that column ??
in sql...
If you want to add a NOT NULL column you must specify a DEFAULT:
ALTER TABLE YourTable
ADD SomeColumn INT NOT NULL
CONSTRAINT DF_YourTable_SomeColumn DEFAULT(0);
Other possibility is to add it with NULL, add your data and alter it to NOT NULL later (see ALTER TABLE)
EDIT: Your comment about "how to insert values"...
This depends very much in your needs. If you want to set all rows to the same value it is:
UPDATE YourTable SET SomeColumn=0;
I have a table viz. expenses with three columns as under
ExpenseId int NOT NULL,
ExpenseName varchar(50) NOT NULL,
Invalid bit NOT NULL
To add a new column (OldCode char(4) not null), I used design feature for tables in Microsoft SQL Server Management Studio. But I get following error
'Expenses' table
- Unable to modify table. Cannot insert the value NULL into column 'OldCode', table 'TransportSystemMaster.dbo.Tmp_Expenses'; column does not allow nulls. INSERT fails. The statement has been terminated.
Incidentally I have been able to add same column with same specifications to other tables of the same database.
Any help?
Your Table Consist of Existing Records
and you are pushing a new column of type NOT NULL.
so for older records the data have to be something.
try something like this
ALTER TABLE MY_TABLE ADD Column_name INT NULL
GO
UPDATE MY_TABLE <set valid not null values for your column>
GO
ALTER TABLE MY_TABLE ALTER COLUMN Column_name INT NOT NULL
GO
Since OldCode is NOT NULL, you should specify a default value for it.
when you have some rows on your table you can't add a column that is not nullable you should provide a default value for it
Alter Table table_name add OldCode int not null DEFAULT(0);
You have to specify values for all the 4 fields of the table, its purely because, while designing the table you set the definition of the columns to be not null. Again you are adding a new column called OldCode and setting to be not null, all ready existing records hasn't got a value. So that is the reason its complains
I use MS Access (2003) database. Once I create a column I set NOT NULL using sql statement:
ALTER TABLE Table1
ALTER column myColumn INTEGER not null
Is there a way to change it back to allow null values? I already tried:
ALTER TABLE Table1
ALTER column myColumn INTEGER null
but nothing...
You cant specify null in ALTER TABLE (although not null is allowed)
See the below documentation and also this discussion on this toppic
Syntax
ALTER TABLE table {ADD {COLUMN field type[(size)] [NOT NULL] [CONSTRAINT index] | ALTER COLUMN field type[(size)] | CONSTRAINT multifieldindex} | DROP {COLUMN field I CONSTRAINT indexname} }
Old School Solution:-
create a new temporray field as null with the same datatype
update the new temporary field to the existing NOT NULL field
drop the old NOT NULL field
create the droped column with the same datatype again without NOT NULL
update the existing field to the temporary field
if there have been indices on the existing field, recreate these
drop the temporary field
Try something like this using MODIFY :-
ALTER TABLE Table1 MODIFY myColumn INT NULL;
The only way I've found is to use DAO directly on the table.
Set db.TableDefs(strTable1).Fields(strFieldName).Required = false