473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

change to not allow nulls

Is there a *simple* way to change a collumn from allowing null to not
null?

I just unchecked "allow nulls" in EM and the SQL it generates to do
this one thing is astonishing, create table, drop FKs, copy data, drop
table, rename new table, rebuild FKs...

I'm saving a lot of these changes to run on another database at a later
date but would rather not require a terrabyte device to store the
script :-)

Jul 23 '05 #1
3 5530
I recommend you use TSQL scripts to make structure changes. You'll have
much more control and visibility over what happens and you'll be able
to test your scripts out before you go live with the change.

You can change nullability with an ALTER TABLE... ALTER COLUMN
statement but if the column is part of an index or constraint then
you'll have to drop that before you can make the change. That means
you'll also have to drop foreign keys that reference the column. EM
tries to make this easier by generating the script for you, so you
could save that script and take it as a starting point.

Another option that may be worth trying:
1. create a new column, populate it from the previous nullable one and
make it non-nullable
2. add constraints and indexes
3. drop the old column
4. rename the column you added

Possibly this method may incur less impact and downtime but that would
depend quite a lot on how the column is used and on other factors too
such as the size of the data and whether an existing index on the
column is clustered. Test it out and see.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2

David Portas wrote:
I recommend you use TSQL scripts to make structure changes. You'll have much more control and visibility over what happens and you'll be able
to test your scripts out before you go live with the change.
I am doing, I use EM to generate the scripts for some things if there's
a shed load to do in a table or if I don't know how to do something
(such as change a null column to a not null :-)
You can change nullability with an ALTER TABLE... ALTER COLUMN
statement but if the column is part of an index or constraint then
you'll have to drop that before you can make the change. That means
you'll also have to drop foreign keys that reference the column. EM
tries to make this easier by generating the script for you, so you
could save that script and take it as a starting point.


Thanks, it's just a description column so alter table should do it.

Jul 23 '05 #3
Trevor Best (go**********@besty.org.uk) writes:
Is there a *simple* way to change a collumn from allowing null to not
null?

I just unchecked "allow nulls" in EM and the SQL it generates to do
this one thing is astonishing, create table, drop FKs, copy data, drop
table, rename new table, rebuild FKs...

I'm saving a lot of these changes to run on another database at a later
date but would rather not require a terrabyte device to store the
script :-)


That is not the main problem with the scripts generated by Enterprise
Manager. The main problem is that if something goes wrong in the
middle of those scripts, you may end of with halfly-modified database,
and you may lose foreign keys forever. This is because the scripts
has a poor transaction scope.

Overall, I strongly discourage using the table designer in Enterprise
Manager to change tables. You can take the script as a starting point,
but there is a lot of problems to sort out. Some standard remedies
to apply:

o Remove all BEGIN and COMMIT TRANSACTION but the first BEGIN and last
COMMIT.
o Put all statements in EXEC('...'). (Except for calls to stored
procedures.
o Remove all GO.
o On all re-addition of foreign keys, insert WITH CHECK before
CHECK. (Yes, WITH CHECK CHCEK.)

For this particular case, adding a new column and then dropping the
old one may be a good idea, as David suggested.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Stearns | last post by:
Either I missed something, or ALTER TABLE does not have this capability. Is there any way of doing it except DROPping all constraints which mention this table, EXPORTing the data, DROPping the...
8
by: Mike | last post by:
The current databas structure that i'm working with allowed NULL's an now I'm converting the app to .NET and it will not allow NULLs in the fields when populated. So my question is, how can i...
0
by: Steve Amey | last post by:
Hi all I am having a problem when I am updating my database. I am using a strong DS and the PK column is an Identity column, but when I am udating my database I am getting the exception 'Column...
0
by: Jim | last post by:
I am trying to add records to a table using a windows form created in vb with a dataset object. I have dragged and dropped the fields I want onto my form. A BindingNavigator is created and am...
0
by: Jim | last post by:
I am trying to add records to a table using a windows form created in vb with a dataset object. I have dragged and dropped the fields I want onto my form. A BindingNavigator is created and am...
1
by: Martin Widmer | last post by:
Hi guys! I get this message when trying to insert a new record into a datatabel which is part of a dataset connected within VS 2005 (VB) from a datagridview control via a DataTableBindingSource...
3
by: Meelis Lilbok | last post by:
Hi How to get information from DataSet, allows the field NULL values or not? If i fill DataSet and read "MyDataSet.Tables("table").Columns("name").AllowDBNull" then this returns always True ...
0
by: Scott_from_Carematic | last post by:
I'm trying to use a strongly typed dataset in my asp.net (2.0) project with visual studio 2005. I have generated the dataset and tableadapters in the designer. I'm using SQL server EE 2005 as the...
5
by: John | last post by:
Hi I have a vb.net winform data app with sql server 2005 backend. The ID field in the tblClients table is an 'int' identity column that does not allow nulls. The problem is that when I try to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.