How to rename database using phpMyAdmin tool? - sql

I created a fresh database in phpmyadmin which does not contain any tables yet since its fresh, however I accidentally made a typo. How can I rename the database?
If this happens to me I usually just execute the SQL command:
DROP DATABASE dbname;
and create another database. But is it possible to rename it? I was already searching SO but found nothing helpful.

I found two possible solutions.
Rename it via the phpmyadmin backend UI (preferable):
Or just execute this SQL (only use it if the database is fresh and does not contain any data yet, otherwise it will be lost!)
CREATE DATABASE newname;
DROP DATABASE oldname;

ALTER DATABASE oldName MODIFY NAME = newName

I don't think you can do this. I think you'll need to dump that database, create the newly named one and then import the dump.
If this is a live system you'll need to take it down. If you cannot, then you will need to setup replication from this database to the new one.
If you want to see the commands try this link, Rename MySQL database

Try using an aux temporary db (as copy of the original)
$ mysqldump dbname > dbname_dump.sql //create a backup
$ mysqladmin create dbname_new //create your new db with desired name
$ mysql dbname_new < dbname_dump.sql //restore the backup to the new one
$ mysql drop database dbname; //drop old one

Related

How to make a copy of a database in SQL Server

I can't seem to find any SQL that will clone one database in SQL Server within the same server.
Let's say I have a database called MyDB. I simply want to make a copy of MyDB to MyDb2. I thought that this would work:
BACKUP DATABASE MyDB TO MyDB2;
But I get this error when I try to execute it:
Backup device 'DbTestBack' does not exist. To view existing backup devices, use the sys.backup_devices catalog view. To create a new backup device use either sp_addumpdevice or SQL Server Management Studio.
Does anyone know what the best way to do this is? I want an exact duplicate of the original including security permissions.
A simple way is taking a back up copy of current DB and restoring it.
You Can do this in single step with a simple script
backup database MyDB
to disk='D:\MyDB.bak';
restore database MyDB2
from disk='D:\MyDB.bak'
WITH move 'MyDB_Data' to 'D:\MyDB2_Data.mdf',
move 'MyDB_log' to 'D:\MyDB2_Data.ldf';
GO
Note: I made an assumption on your current data file and log file name (MyDB_Data, MyDB_log), you need to check them and make correct
DBAtools is your friend here.
Use Copy-DbaDatabase
ie.
Copy-DbaDatabase -Source SRV1 -Destination SRV1 -Database myDB -BackupRestore -SharedPath \\<<your temporary server location such as c:\temp>>

choosing where to save sqlite database

This is probably a simple question, but I could use some help. I am trying to build a small database for an application that will only be run on my computer so I want to create a local database.
To do this I am trying to use sqlite. I can use the command prompt to make what seems to be a database by using the sqlite3 databaseName; functionality, but I do not know where it is being stored.
I need to be able to find the database to access it through the application I am experimenting with. I already know all of the basic sql and such for creating the database tables and data, but I cannot figure out how to simply make the database connection.
is there a way to specify where the database .db file will be stored, and why can I not find the file it seems to be making?
Using sqlite3 shell? Some help using sqlite3 -help:
Usage: sqlite3 [OPTIONS] FILENAME [SQL]
If FILENAME is not supplied, shell uses an temporary database.
If you start shell without supplying a filename, you may save temporary database at any time using:
sqlite> .backup MAIN "folder\your_file.extension"
Or you can ATTACH an existing database an use SQL methods:
sqlite> ATTACH DATABASE "path\stored.db" AS other;
sqlite> INSERT OR REPLACE INTO other.table1 SELECT * FROM this_table1;
sqlite> DETACH other;
For doing such things , you can use Sqlite Manager , you'll get it as a Firefox addon. It's excellent in creating / Managing Sqlite database.
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Thanks everyone for answering, but it turns out my issue was much simpler than I thought.
I was trying to name the database after already starting the shell.
I was supposed to create the database from command line by doing sqlite3 name.db
But I was trying to use that command within the sqlite shell so nothing was being created.

Create database explicitly before restoring to it?

When I setup my PostgreSQL server one of the first things I will do is import a database for an external source. Which of the following is the right way to do it?
Create a database called "NEWDB" on the PostgreSQL server and then
import my external "BACKUPDB" database from my pg_dump into the
"NEWDB".
Don't create a database on the PostgreSQL server, and import the
"NEWDB" database, thereby automatically creating "NEWDB" on the
postgresql server.
I guess my question is, if I want to import an existing database to the PostgreSQL server, do I first need to create a database for it to go into?
You don't have to. It depends on what you want to achieve. If you dump a single database with pg_dump, CREATE DATABASE and ALTER DATABASE commands are not included. You are expected to connect to an existing database. So you have to create it first.
I quote advice from the manual:
If your database cluster has any local additions to the template1
database, be careful to restore the output of pg_dump into a truly
empty database; otherwise you are likely to get errors due to
duplicate definitions of the added objects. To make an empty database
without any local additions, copy from template0 not template1, for
example:
CREATE DATABASE foo WITH TEMPLATE template0;
And also:
The dump file also does not contain any ALTER DATABASE ... SET
commands; these settings are dumped by pg_dumpall, along with database
users and other installation-wide settings.
pg_dumpall, on the other hand, dumps the whole DB cluster including meta-objects like users. It includes CREATE DATABASE statements and connects to each DB when restoring. You can even include DROP DATABASE statements with the -c (--clean) option. Careful with that.
Every instance of PostgreSQL has a default maintenance db named "postgres" that you can connect to - to create databases for instance or start a full restore (from pg_dumpall). But a single-DB dump (from pg_dump) has to be run against its target database.
Finally:
Once restored, it is wise to run ANALYZE on each database so the
optimizer has useful statistics. You can also run vacuumdb -a -z to
analyze all databases.

Restore a database using SQL Server 2005

I have backed up a database into a file using SQL Server from my old server.
Now i would like to restore that file into a new database on my new server.
I created a DB with the same name , I am getting an error saying :
"The Backup set holds a backup of the database other than the existing '*****' database"
Any thoughts?
Thanks
Add a WITH REPLACE option to your restore:
Specifies that SQL Server should
create the specified database and its
related files even if another database
already exists with the same name
Drop the new database - it's sitting in the way of the one you want to restore.
THen when you try to restore your old database, select the file to restore from, and the name will magically appear in the "to database" destination field in SSMS.
When you restore a database from backup, you are creating a new database on the SQL instance. If a database by that name is already present on that SQL instance, you will get an error--unless you select the option to overwrite any existing database, in which case the old database will be wiped out and replaced.
I was having the same issue, but even when putting WITH REPLACE, the error occurred. I had an empty database with the same name as the back up, but the problem was my .trn file I was using to backup from had two backup sets and I was choosing to restore from the full database AND the transaction log. I chose only the Full Database and it worked.

reload a .sql schema without restarting mysqld

Is it possible to reload a schema file without having to restart mysqld? I am working in just one db in a sea of many and would like to have my changes refreshed without doing a cold-restart.
When you say "reload a schema file", I assume you're referring to a file that has all the SQL statements defining your database schema? i.e. creating tables, views, stored procecures, etc.?
The solution is fairly simple - keep a file with all the SQL that creates the tables, etc. in a file, and before all the CREATE statements, add a DELETE/DROP statement to remove what's already there. Then when you want to do a reload, just do:
cat myschemafile.sql | mysql -u userid -p databasename