Sometimes you need to remove a content database from the Farm. Either to move it to another web application or database server or when dealing with an elaborate restore scenario. Prior to running the deletecontentdb command you need to run preparetomove on the database you are going to detach from SharePoint.
This ensures that the relationship between the database and the SSP will be severed and ensures that you can cleanly reattatch it. Failing to run the preparetomove command may result in the deletion of user membership metadata including the selections made in the Privacy and Grouping section on the Edit Profile page for My Site memberships.
STSADM –o preparetomove –contentdb <database server name:database name> -site <site url>
You can also undo the last preparetomove action by using the command undo, this will revert the changes and connect the database to the SSP again.
STSADM –o preparetomove –contentdb <database server name:database name> -site <site url> -undo
Note that the deletecontentdb does not actually delete the database nor does it detach the database in SQL Server, it only detaches the database from the SharePoint Farm Configuration.
STSADM -o deletecontentdb –url <site url> -databasename <database name> -databaseserver <database server name>
Then after you have moved the databse in/from SQL Server you can add it to the Web Application or a new Web Application by using the following command.
STSADM –o addcontentdb –url <site url> -databasename <database name> -databaseserver <database server name>
When moving databases in SQL always detach them from SharePoint with the deletecontentdb command and always use the preparetomove command prior to using the deletecontentb command (or shut down the entire farm).