Showing posts with label nulls. Show all posts
Showing posts with label nulls. Show all posts

Thursday, March 8, 2012

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 :-)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
--|||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.|||Trevor Best (googlegroups@.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, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

change timestamp from null to not null

Hi,
I have a table with a timestamp column which allows nulls.
I wanted to
ALTER TABLE [dbo].[MyTbl] ALTER COLUMN [MyClmn] [timestamp]
NOT NULL
but it gives me msg 4927 - Cannot alter column 'MyClmn' to
be data type timestamp.
Is there any elegant/easy way around this?
--
Many thanks,
OskHi,
Would dropping the column and recreating it with the 'not
null' clause be the advisable way to do it?
--
Many thanks,
Osk
>--Original Message--
>Hi,
>I have a table with a timestamp column which allows nulls.
>I wanted to
>ALTER TABLE [dbo].[MyTbl] ALTER COLUMN [MyClmn] [timestamp]
>NOT NULL
>but it gives me msg 4927 - Cannot alter column 'MyClmn' to
>be data type timestamp.
>Is there any elegant/easy way around this?
>--
>Many thanks,
>Osk
>.
>|||Osk wrote:
> Hi,
> Would dropping the column and recreating it with the 'not
> null' clause be the advisable way to do it?
>
>> --Original Message--
>> Hi,
>> I have a table with a timestamp column which allows nulls.
>> I wanted to
>> ALTER TABLE [dbo].[MyTbl] ALTER COLUMN [MyClmn] [timestamp]
>> NOT NULL
>> but it gives me msg 4927 - Cannot alter column 'MyClmn' to
>> be data type timestamp.
>> Is there any elegant/easy way around this?
>> --
>> Many thanks,
>> Osk
>> .
If you need to preserve the data in the table, create a second table,
copy the data, change the first table DDL and then migrate the data
back.
--
David Gugick
Imceda Software
www.imceda.com|||Hi David,
Thanks for your answer. Can you please tell me why would I
lose any data except the old timestamp values (which seems
to be inevitable anyway) by dropping and
redefining/recreating the timestamp column?
--
Many thanks,
Osk
>--Original Message--
>Osk wrote:
>> Hi,
>> Would dropping the column and recreating it with the 'not
>> null' clause be the advisable way to do it?
>>
>> --Original Message--
>> Hi,
>> I have a table with a timestamp column which allows nulls.
>> I wanted to
>> ALTER TABLE [dbo].[MyTbl] ALTER COLUMN [MyClmn] [timestamp]
>> NOT NULL
>> but it gives me msg 4927 - Cannot alter column 'MyClmn' to
>> be data type timestamp.
>> Is there any elegant/easy way around this?
>> --
>> Many thanks,
>> Osk
>> .
>If you need to preserve the data in the table, create a
second table,
>copy the data, change the first table DDL and then migrate
the data
>back.
>--
>David Gugick
>Imceda Software
>www.imceda.com
>.
>|||anonymous@.discussions.microsoft.com wrote:
> Hi David,
> Thanks for your answer. Can you please tell me why would I
> lose any data except the old timestamp values (which seems
> to be inevitable anyway) by dropping and
> redefining/recreating the timestamp column?
>
I guess it really doesn't matter if you lose the data in the column.
--
David Gugick
Imceda Software
www.imceda.com|||Yes, I lose the timestamp data in the column, but the rest
of the table data should be remaining intact. And from this
perspective the result of using either the drop-
redefine/recreate method or the export/import method would
be the same, except that the latter takes more effort to
implement.
--
Thanks,
Osk
>--Original Message--
>anonymous@.discussions.microsoft.com wrote:
>> Hi David,
>> Thanks for your answer. Can you please tell me why would I
>> lose any data except the old timestamp values (which seems
>> to be inevitable anyway) by dropping and
>> redefining/recreating the timestamp column?
>I guess it really doesn't matter if you lose the data in
the column.
>--
>David Gugick
>Imceda Software
>www.imceda.com
>.
>