| View previous topic :: View next topic |
| Author |
Message |
d4rksh4rk General User

Joined: 08 Jul 2009 Posts: 16
|
Posted: Fri Jun 18, 2010 1:20 am Post subject: Add a new field to an existing table with a default value |
|
|
Hi,
i need to add a new field (tinyint) to an existing table.
I need that for all the existing rows in the table the value of the new field is 1, but for the new rows the number could be different (so 1 should not be the default value).
i've read that if i create the new field and then run this code in Tools->SQL ecc
alter table mytable alter column mycolumn set default 1;
i should set the value 1 to all the selected column fields, but when i try the code Base, i get a table not found error.
I've tried this code
alter table "mytable" alter column "mycolumn" set default 1;
and this time the command has been executed, but the value of the new field in the existing row is still null and even in the new rows the filed is empty...
any suggestion? |
|
| Back to top |
|
 |
FreewayFred Power User

Joined: 22 Feb 2007 Posts: 85 Location: Wisconsin USA
|
Posted: Fri Jun 18, 2010 4:44 am Post subject: |
|
|
The following
| Quote: | | alter table "mytable" alter column "mycolumn" set default 1; |
would, based on my experience, supply the default value to future entries to the table. I use code like this to avoid entering data, like dates, that will be the same for all the entries in a batch of entries. |
|
| Back to top |
|
 |
d4rksh4rk General User

Joined: 08 Jul 2009 Posts: 16
|
Posted: Fri Jun 18, 2010 5:25 am Post subject: |
|
|
| So i still need to find a way to write the new field for all the old rows of my table... |
|
| Back to top |
|
 |
Sliderule Super User


Joined: 29 May 2004 Posts: 2474 Location: 3rd Rock From The Sun
|
Posted: Fri Jun 18, 2010 6:47 am Post subject: |
|
|
d4rksh4rk:
You said:
| d4rksh4rk wrote: | | So i still need to find a way to write the new field for all the old rows of my table... |
Therefore, you do NOT want to use a DEFAULT command. But, yes, it can 'easily' be accomplished . . . despite all the words below. It should take about 60 seconds, or less . . . alot more time to read it compared to doing it.
OK, to explain the process in English first . . . to be followed by 'code' . . . this is can accomplished 'easily'.
First, need to ALTER ( change ) your current table ( in the example below I will assume a table name of "MyTable" ) . . . change it to your real table name ) to include this new field ( in the example below I will assume a field name of "MyField" ) . . . next will UPDATE the contents of "MyField" to a value of 1.
How to accomplish the task.
- Make a backup copy of your OpenOffice database file ( *.odb ), just in case. Doubtful you will need the 'backup', but, just in case a good SAFETY procedure to follow
- Open your OpenOffice database file ( *.odb )
- Two ways to ALTER your table to add the field, from the GUI ( Graphics User Interface ) or with direct SQL commands EITHER
- From the GUI:
- Click on Tables on the left
- Right click on your the table name you want to edit and from the drop-down menu, select Edit
- Under Field Name, enter the new field name of "MyField"
- Under Field Type, select Tiny Integer [ TINYINT ]
- Click on Save icon on the Toolbar
- Direct entry via SQL
- From the Menu: Tools -> SQL...
- Enter the following code in the Command to execute box ( change "MyTable" and "MyField" to the correct names in your database ):
| Code: | | ALTER TABLE "MyTable" Add Column "MyField" TINYINT |
Press the Execute button
Command successfully executed should be displayed
Press the Close button
Now, to UPDATE all of your CURRENT records to a value of 1
- From the Menu: Tools -> SQL...
- Enter the following code in the Command to execute box ( change "MyTable" and "MyField" to the correct names in your database ):
| Code: | | UPDATE "MyTable" SET "MyField" = 1 |
Press the Execute button
Command successfully executed should be displayed
Press the Close button
Remember to change "MyTable" to your real table name, and "MyField" to your real field name. The above may seem like alot of steps, but, I was just attempting to explain it to you, and, OTHERS, that may read this, with different levels of computer experience.
For detailed documentation on ALTER and UPDATE commands with HSQL, see:
I hope this helps, please be sure to let me / us know.
Sliderule
Thanks to add [Solved] in your first post title ( edit button ) if your issue has been fixed / resolved. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|