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.
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)
I am adding versioning to my database a bit later than I should, and as such I have some tables with inconsistent states. I have a table that a column was added to in Java, but not all tables are guaranteed to have that column at this point.
What I had been doing is on the first run of the program, checking if the column existed, and adding it if it did not exist.
The library (flyway.org) I am using to deal with versioning takes in a bunch of .sql files in order to set up the database. For many tables, this is simple, I just have an sql file that has "CREATE TABLE IF NOT EXISTS XXX," which means it is easily handled, those can still be run.
I am wondering if there is some way to handle these alter tables without SQLite generating an error that I haven't thought of, or if I haven't found out how to do it.
I've tried looking to see if there is a command to add a column if it doesn't exist, but there doesn't seem to be one. I've tried to find a way to handle errors in sqlite, for example running the alter table anyways, and just ignoring the error, but there doesn't seem to be a way of doing that (as far as I can tell). Does anyone have any suggestions? I want a solution 100% in a .sql script if possible.
There is no "IF NOT EXIST" clause for Alter Tables in SQLite, it doesn't exist.
There is a way to interrogate the database on what columns a table contains with PRAGMA table_info(table_name);. But there is no 100% SQL way to take that information and apply it to an Alter Table statement.
Maybe one day, but not today.
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.
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
Whenever I make changes to a table structure in SSMS, there is a alert raised:
saving the changes is not permitted.the changes u have been made to the following tables to be dropped and recreate..
Some changes cause a table to be dropped and recreated. One such example is adding a column to the middle of a table rather than to the end.
You can do one of two things:
Option 1
Use TSQL to make your changes and add the column to the end (or the equivalent non table-drop option for your specific edit)
Option 2
Alter the default behavior of SSMS (warning - this is a very dangerous thing in production environments)
Open SQL Server 2008 Management Studio (SSMS). In the menu, go to Tools / Options. In the Navigation pane, expan Designers, and select "Table and Database Designers".
Under Table options, uncheck “Prevent saving changes that require the table re-creation” option and click OK.
I've seen those kinds of messages most often come up when the changes you are making could potentially cause data to be truncated. So, if you were changing an nvarchar(20) to nvarchar(10), that would come up. It doesn't matter if the column only contains data that is 2 characters long, it only looks at the data type. There may be other reasons and other flavors of that message, but mostly they follow the same reasoning: If the change would cause the table to be more restricted in the data it could hold, it wants to do radical surgery.