We have a star schema designed in Wherescape. The task is to add new columns to the Fact table.
The fact table have around 30gb in it. Is it possible to add columns without deleting the fact table? Or what technique should be used to retain the current data in the fact table, and at the same time have the new columns available. I keep getting a timeout error if I just try to add columns in management studio.
I think the guy before me actually just modified it in Wherescape (not too sure). In anycase if I have to do it manually in management studio, that works for me too.
thanks
Gemmo
Can't really do this without deleting the table. It's too big and no matter what you do, it will time out. Back up the table, delete it and create the table with the new structure. You'll just have to put the data in again. No shortcuts. For smaller tables, you can easily add a column no problem.
Best way to do this is to add the column to the metadata and then right click on your table/object and then click "Validate against the database".
this would allow you to alter the table instead of having to take the long route of moving data into a temp table, recreating the table
and moving the data back.
Related
I just started my new job and after looking at the DBM I was shocked. Its a huge mess.
Now first thing I wanted to do is get some consistency in the order of table columns. We publish new database versions over a .dacpac. My co-worker told me that changing the order of a column would force MSSQL to create a temporary table which stores all the data. MSSQL then creates a new table and inserts all the data into that table.
So lets say my server only runs 2GB of RAM and has 500MB storage left on the harddrive. The whole database weights 20GB. Is it possible that changing the order of columns will cause trouble (memory related)? Is the statement of my co-worker correct?
I couldnt find any good source for my question.
Thanks!
You should NOT "go one table by one".
You should leave your tables as they are, if you don't like the order of columns of some table just create a view reordering your columns as you want.
Not only changing order of columns will cause your tables to be recreated, all the indexes will be recreated, you'll get problems with FK constraints.
And after all, you'll gain absolutely nothig but do damage only. You'll waste server resources, make your tables temporarily inaccessible and the columns will not be stored as you defind anyway, internally they will be stored in "var-fix" format (divided into fixed-length and variable-length)
So I've noticed over the past few weeks that changing tables in SQL Server is very difficult, such as specifying a new primary key column or changing a column definition (ie. changing it's datatype). Half of the time I have to drop the table and start over. Or even re-ordering columns in a table results in about a half hour of working (even though it doesn't really matter to the databse engine what order they are in, but I like to have things in logical order).
Are there any simpler ways to make changes to tables like this without having to go through headaches of dropping tables or recreating them. Most of the time I get an error saying that the table has to be dropped and recreated but apparently SQL server can't do this. I don't want to turn off the "Prevent table changes from requiring a table to be dropped" option because of the possiblye problems it can cause later.
Sometimes, for example, I can cheat and generate a CREATE TABLE script and then change the definition of a table in the script, drop the actual table, and create it again with the script ,but sometimes this doesn't work. And re-ordering columns is a pain and a problem, or even changing data types in a column that's already in the table, or setting a primary key, or changing a field from "NULL" to "NOT NULL" using the checkbox.
Any ideas on how to better manage the tables and make changes? It frustrates me that Microsoft did not follow the SQL standard on some of its ALTER TABLE commands, among other things. In MYSQL this would be a lot easier, but we are using SQL Server unfortunately.
I have a db table that gets entirely re-populated with fresh data periodically. This data needs to be then pushed into a corresponding live db table, overwriting the previous live data.
As the table size increases, the time required to push the data into the live table also increases, and the app would look like its missing data.
One solution is to push the new data into a live_temp table and then run an SQL RENAME command on this table to rename it as the live table. The rename usually runs in sub-second time. Is this the "right" way to solve this problem?
Are there other strategies or tools to tackle this problem? Thanks.
I don't like messing with schema objects in this way - it can confuse query optimizers and I have no idea what will happen to any transactions that are going on while you execute the rename.
I much prefer to add a version column to the table, and have a separate table to hold the current version.
That way, the client code becomes
select *
from myTable t,
myTable_currentVersion tcv
where t.versionID = tcv.CurrentVersion
This also keeps history around - which may or not be useful; if it's not delete old records after setting the CurrentVersion column.
Create a duplicate table - exact copy.
Create a new table that does nothing more than keep track of the "up to date" table.
MostCurrent (table)
id (column) - holds name of table holding the "up to date" data.
When repopulating, populate the older table and update MostCurrent.id to reflect this table.
Now, in your app where you bind the data to the page, bind the newest table.
Would it be appropriate to only push changes to the live db table? For most applications I have worked with changes have been minimal. You should be able to apply all the changes in a single transaction. Committing the transaction will make them visible with no outage on the table.
If the data does change entirely, then you could configure the database so that you can replace all the data in a single transaction.
What are possible side effects when using option to "Prevent saving changes that require table-recreation." Specifically, just adding a new field to table.
You are referring to SSMS. By default, you cannot save schema changes that involve a table recreation. Whenever I install SSMS, I immediately turn this option off.
Some schema changes require a temp table to be created, the data from the original table copied to it, a new table created, with the new schema, and then the data from the temp table copied to the new table. The temp table is then dropped. When this option selected, any schema change that requires this process is not permitted in SSMS.
IMO, there is no downside to turning this off, as long as you are aware that some schema changes require this, and, with a table with a large number of rows, the operation could take a long time.
Just adding a new column to a table is fine, provided you can accept that the new column will "appear" at "the end" of the table.
It's when people want to position the new column in a particular place in the list of columns that problems occur, because there's no such actual command to allow this to happen in SQL; So SSMS has to fake this by creating a new table, copying data across, deleting the old table, and renaming the new. All of these steps take time, during which it's unsafe for anyone to be trying to access this table.
Damien is right, just adding a column causes no side effects.
This can easily be done using T-SQL, as this is one of the actions that is done without re-creating a table when using T-SQL. The other actions are:
Modifying the NULL setting of an existing column
Using RESEED for a column
Changing data type of an existing column
I have situation where I need to change the order of the columns/adding new columns for existing Table in SQL Server 2008. It is not allowing me to do without drop and recreate. But that is in production system and having data in that table. I can take backup of the data, and drop the existing table and change the order/add new columns and recreate it, insert the backup data into new table.
Is there any best way to do this without dropping and recreating. I think SQL Server 2005 will allow this process without dropping and recreating while changing to existing table structure.
Thanks
You can't really change the column order in a SQL Server 2008 table - it's also largely irrelevant (at least it should be, in the relational model).
With the visual designer in SQL Server Management Studio, as soon as you make too big a change, the only reliable way to do this for SSMS is to re-create the table in the new format, copy the data over, and then drop the old table. There's really nothing you can do about this to change it.
What you can do at all times is add new columns to a table or drop existing columns from a table using SQL DDL statements:
ALTER TABLE dbo.YourTable
ADD NewColumn INT NOT NULL ........
ALTER TABLE dbo.YourTable
DROP COLUMN OldColumn
That'll work, but you won't be able to influence the column order. But again: for your normal operations, column order in a table is totally irrelevant - it's at best a cosmetic issue on your printouts or diagrams..... so why are you so fixated on a specific column order??
There is a way to do it by updating SQL server system table:
1) Connect to SQL server in DAC mode
2) Run queries that will update columns order:
update syscolumns
set colorder = 3
where name='column2'
But this way is not reccomended, because you can destroy something in DB.
One possibility would be to not bother about reordering the columns in the table and simply modify it by add the columns. Then, create a view which has the columns in the order you want -- assuming that the order is truly important. The view can be easily changed to reflect any ordering that you want. Since I can't imagine that the order would be important for programmatic applications, the view should suffice for those manual queries where it might be important.
As the other posters have said, there is no way without re-writing the table (but SSMS will generate scripts which do that for you).
If you are still in design/development, I certainly advise making the column order logical - nothing worse than having a newly added column become part of a multi-column primary key and having it no where near the other columns! But you'll have to re-create the table.
One time I used a 3rd party system which always sorted their columns in alphabetical order. This was great for finding columns in their system, but whenever they revved their software, our procedures and views became invalid. This was in an older version of SQL Server, though. I think since 2000, I haven't seen much problem with incorrect column order. When Access used to link to SQL tables, I believe it locked in the column definitions at time of table linking, which obviously has problems with almost any table definition changes.
I think the simplest way would be re-create the table the way you want it with a different name and then copy the data over from the existing table, drop it, and re-name the new table.
Would it perhaps be possible to script the table with all its data.
Do an edit on the script file in something like notepad++
Thus recreating the table with the new columns but the same.
Just a suggestion, but it might take a while to accomplish this.
Unless you write yourself a small little c# application that can work with the file and apply rules to it.
If only notepadd++ supported a find and move operation