I am adding new columns to a table and it is forcing me to drop create the table. Do I have to manually re-add the indexes? Or can I get the generated script to recreate the indexes for me?
If you're using the table designer in Management Studio, then it should recreate any indices/foreign keys automatically.
The only thing I've seen drop from this kind of operation is a schemabound view. The schema binding (and any associated clustered/full text index) will drop from a schemabound view if you drop/recreate the source table.
Related
I want to alter column within the table to be NULL from NOT NULL.
The problem is that table has one constraint, one trigger and 4 indexes.
Can I somehow alter that column without dropping and recreating everything?
exec sys.sp_depends 'object_name'
List all dependencies and then drop them an recreate everything.
SQL Managements studio offer option with SCRIPT AS CREATE and you can save all views, triggers, tables as backup...
I want to alter all varchar columns in my database to nvarchar, using a T-SQL script.
There are a lot of dependencies (keys, constraints, and views) that cause problems when trying to alter.
The object 'X' is dependent on column 'Y'. ALTER TABLE ALTER COLUMN 'Y' failed because one or more objects access this column.
My unsuccessful attempts: forcing a change with PowerShell, and creating a generate script for the whole database and using that to create a copy with all the changes I need. These attempts didn't work, because I lost the table data and the generate scripts were too big for any program to handle.
How does one create all DROP and CREATE scripts of the objects that cause problems when trying to alter to make the changes and recreate the database schema like it was before altering?
I have to change my column data type and it is indexed. Altering the column giving me error that I need to drop the index, disabling won't work.
The question is how am I going to drop and recreate the exact index automatically? I can drop them but I have got no idea how I will be recreating them.
Cheers
What DBMS are you using? Some DB Management Systems have the ability to script their database objects, but the commands differ between them.
If you can create a script, you'd do so, drop the index, change the field's datatype, save, then run the script to re-create the index.
If you can't create a script for your index with your particular DBMS, you'll have to write down all the relevant fields and settings by hand, then drop the index, change the field's datatype, save, then manually recreate the index from your notes.
One way to do this would be to create your new index field then do an update query like:
UPDATE yourTable
SET newIdx = oldIdx
Then once the new index is filled just set it as the index field and remove the old field.
On our original design we screwed up a foreign key constraint in our table. Now that the table is full of data we cannot change it without dropping all of the records in the table. The only solution I could think of is to create a backup table and put all of the records in there, then delete all the records, alter the table and start adding them back. Any other (BETTER) ideas? Thanks!
Using MS SQL Server
I'm a bit late, just for reference.
If You are using SQL Server Management Studio, You could generate a DROP and RECREATE script with "Keep schema and data" option.
Right click on the desired DB in object explorer
Tasks > Generate scripts
Select the table you want to script
Then clicking on Advanced button
"Script DROP and CREATE" ->"Script DROP and CREATE"
"Types of data to script" -> "Schema and data"
Hope this helps
Here's some pseudo-code. No need to make a backup table, just make a new table with the right constraint, insert your records into it, and rename.
CREATE TABLE MyTable_2
(...field definitions)
<add the constraint to MyTable_2>
INSERT INTO MyTable_2 (fields)
SELECT fields
FROM MyTable
DROP TABLE MyTable
exec sp_rename 'MyTable2', 'Mytable'
Using SQL Server Management Studio (SSMS), you can use it's table designer to specify the final condition of the table. Before saving the changes, have it generate the change script and save that script. Cancel out of the design window, open the script and review it. SSMS may already have generated a script that does everything you need, fixing the primary-foreign key relationship while preserving all existing data. If not, you will have a script, already started, that performs most of what you need to do and should be able to modify it for your needs.
This is your only solution.
Create the backup table, empty the original one, modify the table and then insert step-by-step until you find a violation.
Update All Schema Database Old by new Schema Database .
Create script (Right click on the desired DB in object explorer Tasks > Generate scripts -> select option select specific database objects and tables ->next -> advanced-> option Type of data to script Data only -> ok ->next ->next.) to data only and backup Database to old database
Drop database old and create new database and make new DB is empty .
Excute script of Old Data only on new database .
I right click on my table in ssms 2008 and select Script Table as / Drop and Create Table to new window and I try to run the script but get an error:
Could not drop table because it is referenced by a foreign key constraint
What was the point of the Drop and Create generate script then?
Thanks,
rod.
The point of the Drop and Create generate script is exactly what you'd think - it gives you an easy way to script out dropping and re-creating a table. However you can't drop a table if other tables reference it via foreign key constraints, which is why you're getting the error message.
If you're just trying to add a column, you can right-click the table in Enterprise Manager and click Modify and just add the column in design view. There's no need to drop the table just to add a column. (And it's especially an awful approach if the table has data in it.)
The easiest way to add a column to an existing table? Write the ALTER TABLE statement yourself instead of relying on SQL Server Management Studio to do it for you:
ALTER TABLE YourTableName
ADD ColumnName int