How can I rename AdventureWorksLT2008 database to AdventureWorksLT2008_old with its .ldf and .mdf files renamed as well?
I would like to do it from sqlcmd. It is a local server. I would like to do it with -E option. I did try googling but results didn't work for me. Can anyone suggest a tried method.
Can anyone please help?
a quick google search got this as the top result. All you have to do is to everything from sqlcmd( I am assuming you know how to use sqlcmd..)
-- Replace all MyDBs with the name of the DB you want to change its name
USE [MyDB];
-- Changing Physical names and paths
-- Replace all NewMyDB with the new name you want to set for the DB
-- Replace 'C:\...\NewMyDB.mdf' with full path of new DB file to be used
ALTER DATABASE MyDB MODIFY FILE (NAME = ' MyDB ', FILENAME = 'C:\...\NewMyDB.mdf');
-- Replace 'C:\...\NewMyDB_log.ldf' with full path of new DB log file to be used
ALTER DATABASE MyDB MODIFY FILE (NAME = ' MyDB _log', FILENAME = 'C:\...\NewMyDB_log.ldf');
-- Changing logical names
ALTER DATABASE MyDB MODIFY FILE (NAME = MyDB, NEWNAME = NewMyDB);
ALTER DATABASE MyDB MODIFY FILE (NAME = MyDB _log, NEWNAME = NewMyDB_log);
Related
I have created a new schema for a new PostgreSQL DB that I want to create (IF NOT EXISTS).
What should the file extension be for this document so that I can run it from psql, .dbu or .sql?
.dbu is used as a "Database Utility" file
You need to use .sql
Then from psql you can use something like:
SET search_path = schemaName, something_else;
SELECT * FROM tableName; -- schemaName.tableName
or something to that effect depending on what you're trying to accomplish...
Environment:
SQL Server 2008 R2
Database not receiving active transactions.
In a production environment I need to move the MDF & LDF files to new drives. Since I have a Window of time to stop active transactions, I thought I could just take a backup of the database and then restore it while configuring the file groups to new location.
I figured this is much better than detaching and reattaching the database with new file name.
Since I am a novice, wanted to check with the experts here. Any suggestion/advise much appreciated.
Here's a simple example. It assumes your database has a single .mdf (data) file and a single .ldf (log) file. I will use the [model] database as an example.
--First, make note of the current location of the db files.
--Copy and paste the physical_names somewhere. Trust me, if you forget
--where the files were originally, this will save you some heartache.
SELECT d.name, f.name, f.physical_name
FROM master.sys.master_files f
JOIN master.sys.databases d
ON d.database_id = f.database_id
WHERE d.name = 'model' --Replace with the name of your db.
--Now set the new file paths.
--You can run the ALTER DATABASE statements while the db is online.
--Run once for the mdf/data file.
ALTER DATABASE [model] --Replace with the name of your db.
MODIFY FILE
(
NAME = 'modeldev', --this is the "logical" file name.
FILENAME = 'D:\SqlData\model.mdf' --Replace with the new path\filename.
)
--Run once for the ldf/data file.
ALTER DATABASE [model] --Replace with the name of your db.
MODIFY FILE
(
NAME = 'modellog',
FILENAME = 'D:\SqlData\modellog.ldf' --Replace with the new path\filename.
)
--When business rules allow, take the db OFFLINE.
ALTER DATABASE [model] --Replace with the name of your db.
SET OFFLINE
--Move the physical db files to the new location on disk.
--Bring the db back ONLINE to complete the task.
ALTER DATABASE [model] --Replace with the name of your db.
SET ONLINE
I am new to this. I have a database (created by someone else) that has 2 .ldf files. (blah_log.ldf and blah_log2.ldf). My manager asked me to remove one of the log files but I cannot. How do I do this? I tried to put it on another server, detach, delete log files, attach, but it gives an error. I thought that way it would create just one, but it wanted both. Then i tried to right click properties and delete the files, would not let me delete. It said the log file was not empty. How in the heck do I achieve this. I just want to make it where the dang database has one freaking log file not two. This shouldn't be this complicated. I am a beginner and know nothing so maybe it isn't really. Please HELP!
I just tried this:
empty SQL Server database transaction log file
backup log [dbname] with truncate_only
go
DBCC SHRINKDATABASE ([dbname], 10, TRUNCATEONLY)
go
Then I deleted the second log file and clicked ok. I guess this is all I need to do? I tried it on a test server from a restore.
This MSDN article describes how to accomplish this at a high-level:
You cannot move transaction log data from one log file to another to
empty a transaction log file. To remove inactive transactions from a
transaction log file, the transaction log must be truncated or backed
up. When the transaction log file no longer contains any active or
inactive transactions, the log file can be removed from the database.
And this blog post shows the actual T-SQL that will accomplish this task:
USE master
IF DB_ID('rDb') IS NOT NULL DROP DATABASE rDb
GO
CREATE DATABASE rDb
ON
PRIMARY
( NAME = N'rDb', FILENAME = N'C:\rDb.mdf' , SIZE = 50MB ,
FILEGROWTH = 1024KB )
LOG ON
(NAME = N'rDb_log2', FILENAME = N'C:\rDb_log2.ldf', SIZE = 3MB,
FILEGROWTH = 2MB)
,(NAME = N'rDb_log3', FILENAME = N'C:\rDb_log3.ldf', SIZE = 3MB,
FILEGROWTH = 2MB)
,(NAME = N'rDb_log4', FILENAME = N'C:\rDb_log4.ldf', SIZE = 3MB,
FILEGROWTH = 2MB)
GO
ALTER DATABASE rDb SET RECOVERY FULL
BACKUP DATABASE rDb TO DISK = 'C:\rDb.bak' WITH INIT
CREATE TABLE rDb..t(c1 INT IDENTITY, c2 CHAR(100))
INSERT INTO rDb..t
SELECT TOP(15000) 'hello'
FROM syscolumns AS a
CROSS JOIN syscolumns AS b
--Log is now about 46% full
DBCC SQLPERF(logspace)
--Check virtual log file layout
DBCC LOGINFO(rDb)
--See that file 4 isn't used at all (Status = 0 for all 4's rows)
--We can remove file 4, it isn't used
ALTER DATABASE rDb REMOVE FILE rDb_log4
--Check virtual log file layout
DBCC LOGINFO(rDb)
--Can't remove 3 since it is in use
ALTER DATABASE rDb REMOVE FILE rDb_log3
--What if we backup log?
BACKUP LOG rDb TO DISK = 'C:\rDb.bak'
--Check virtual log file layout
DBCC LOGINFO(rDb)
--3 is still in use (status = 2)
--Can't remove 3 since it is in use
ALTER DATABASE rDb REMOVE FILE rDb_log3
--Shrink 3
USE rDb
DBCC SHRINKFILE(rDb_log3)
USE master
--... and backup log?
BACKUP LOG rDb TO DISK = 'C:\rDb.bak'
--Check virtual log file layout
DBCC LOGINFO(rDb)
--3 is no longer in use
--Can now remove 3 since it is not in use
ALTER DATABASE rDb REMOVE FILE rDb_log3
--Check explorer, we're down to 1 log file
--See what sys.database_files say?
SELECT * FROM rDb.sys.database_files
--Seems physical file is gone, but SQL Server consider the file offline
--Backup log does it:
BACKUP LOG rDb TO DISK = 'C:\rDb.bak'
SELECT * FROM rDb.sys.database_files
--Can never remove the first ("primary") log file
ALTER DATABASE rDb REMOVE FILE rDb_log2
--Note error message from above
I am trying to make some normal (understand restorable) backup of mysql backup. My problem is, that I only need to back up a single table, which was last created, or edited. Is it possible to set mysqldump to do that? Mysql can find the last inserted table, but how can I include it in mysql dump command? I need to do that without locking the table, and the DB has partitioning enabled.... Thanks for help...
You can use this SQL to get the last inserted / updated table :-
select table_schema, table_name
from information_schema.tables
where table_schema not in ("mysql", "information_schema", "performance_schema")
order by greatest(create_time, update_time) desc limit 1;
Once you have the results from this query, you can cooperate it into any other language (for example bash) to produce the exact table dump).
./mysqldump -uroot -proot mysql user > mysql_user.sql
For dumping a single table use the below command.
Open cmd prompt and type the path of mysql like c:\program files\mysql\bin.
Now type the command:
mysqldump -u username -p password databasename table name > C:\backup\filename.sql
Here username - your mysql username
password - your mysql password
databasename - your database name
table name - your table name
C:\backup\filename.sql - path where the file should save and the filename.
If you want to add the backup table to any other database you can do it by following steps:
login to mysql
type the below command
mysql -u username -p password database name < C:\backup\filename.sql
I'm trying to do an Entity Framework walkthrough so I:
downloaded SQL script here: http://www.learnentityframework.com
in SQL Server Management Studio, I right-clicked Database, Create Database, named it
right-clicked on the new database, New Query
clicked on "Open File" and opened the script file: Create_ProgrammingEFDB1_SQLServer2008.sql
clicked "! Execute"
But the script (756K) has been running for 10 minutes now and still says "executing..."
My questions are:
Is this the standard way to read in an SQL script into SQL Server?
Is it supposed to take this long? This is how I would do it in MySQL/PHPMyAdmin it it might take a couple seconds, so I assume I'm not doing something right.
Here is the beginning of the script, I changed the file paths so they point to the right .mdf and .ldf files:
****/
--PART ONE CREATE THE DATABASE. Note the file paths in the first few commands.
--Change them for your own computer.--
USE [master]
GO
/****** Object: Database [ProgrammingEFDB1] Script Date: 01/28/2009 10:17:44 ******/
CREATE DATABASE [ProgrammingEFDB1] ON PRIMARY
( NAME = N'ProgrammingEFDB1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1.mdf' , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ProgrammingEFDB1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1_log.LDF' , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
ALTER DATABASE [ProgrammingEFDB1] SET COMPATIBILITY_LEVEL = 90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ProgrammingEFDB1].[dbo].[sp_fulltext_database] #action = 'disable'
end
...
ANSWER:
I had already created a database with the same name so it was trying to create a database that was already there which made it hang for some reason. I deleted that database, reran the script and it completed successfully in 3 seconds.
I don't know what does your script do exactly in the next 754K, but the lines you posted seem quite harmless.
Try adding the following to your script:
SET STATISTICS TIME ON
This will show queries execution times as they run, and it will help you to locate the problem more exactly.
But the script (756K)
Must be a lot more than just a CREATE DATABASE in the script, so very hard to say when the script is doing.
You can write progress reports from the script back to the client, or use SQL Profiler to see what commands are being executed.