Changing names of tables in a MySql Database (Oracle sql developer) [duplicate] - mysql

So basically I have a joomla database in MySQL which has a bunch of tables that have the prefix 'jmla_'. I would like to rename all of these tables by replacing the 'jmla_' prefix with a 'jos_' prefix. Any ideas about how to do this with a simple SQL script or SQL query?

SELECT concat ('rename table ',table_name,' to ',table_name,'_old;')
FROM information_schema.tables
WHERE table_name like 'webform%'
and table_schema='weiss_db_new'
will work.

Run this statement:
SELECT 'rename table '||table_name||' to '||'jos'||substr(table_name,5)||';'
FROM information_schema.tables
WHERE table_name like 'jmla%'
This creates a script that will rename all the tables. Just copy & paste the output into your SQL client.
(You will need to change the || to MySQL's non-standard concatenation operator in case you are not running it in ANSI mode)

RENAME TABLE jmla_whatever to jos_whatever;
You'll have to write a script to cover all your tables - you can populate your script with the output of show tables. See http://dev.mysql.com/doc/refman/5.0/en/rename-table.html for details.

Export using phpmyadmin to .sql file
Use any text editor (I prefer vim, work excellent on large files) with function "find and replace" to open file
Do "find and replace", putting your actual prefix in find box, and updated in replace box
Import file using phpmyadmin.
Remember to drop old database before importing by phpmyadmin. This may be done, checking suitable options during export.

Have you considered using Akeeba Admin Tools Component for Joomla! and then using the Database Prefix Editor to change table prefixes, works really well. Akeeba Admin tools can be used to secure your Joomla! installation
Akeeba can be obtained here

Related

Easy edit and delete row from mysql

I'm not that good at mysql but some of my tables has this line where I can edit and delete the row easy without writing any SQL? How can I make that row inside another table?
You can use any GUI editor like phpmyadmin or the tools which P.Salmon mentioned.
Using GUI editor, you can connect to the database using connection string and you can view tables and see the result which can edited and deleted.

query to replace term in all tables of database

I want to search for http://example.com and replace with https://example.com.
I know I can target a specific table and column with this approach:
UPDATE table_name SET post_content = REPLACE(column_name, 'http://example.com', 'https://example.com');
But how do I run a query which targets all tables/columns: the entire database?
Do a DB dump and open it as a text file. Find and replace. Save and then re-import.
As far as I know, I don't think you can use REPLACE on all tables in one query.
There a two ways to do it. The first is to create SQL UPDATE via the information_schema and execute it as prepared statement. this is much work.
you must look at each column if you can do a replace, so you must ignore INTS and ENUMs etc.
The second way is not a real SQL change, but it works: Generate a full SQL-Dump from your database and make the changes in this file via editor or via commandline with AWK or SED. After this you can import the changed file

How do I change the database name using MySQL? [duplicate]

This question already has answers here:
How do I rename a MySQL database (change schema name)?
(46 answers)
Closed 9 days ago.
How can I change the database name of my database?
I tried to use the rename database command, but on the documents about it it is said that it is dangerous to use. Then what should I need to do to rename my database name?
For example, if I want to rename my database to this.
database1 -> database2?
Follow bellow steps:
shell> mysqldump -hlocalhost -uroot -p database1 > dump.sql
mysql> CREATE DATABASE database2;
shell> mysql -hlocalhost -uroot -p database2 < dump.sql
If you want to drop database1 otherwise leave it.
mysql> DROP DATABASE database1;
Note : shell> denote command prompt and mysql> denote mysql prompt.
I don't think it's possible.
You can use mysqldump to dump the data and then create a schema with your new name and then dump the data into that new database.
Unfortunately, MySQL does not explicitly support that (except for dumping and reloading database again).
From http://dev.mysql.com/doc/refman/5.1/en/rename-database.html:
13.1.32. RENAME DATABASE Syntax
RENAME {DATABASE | SCHEMA} db_name TO new_db_name;
This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23. ... Use of this statement could result in loss of database contents, which is why it was removed. Do not use RENAME DATABASE in earlier versions in which it is present.
"As long as two databases are on the same file system, you can use RENAME TABLE to move a table from one database to another"
-- ensure the char set and collate match the existing database.
SHOW VARIABLES LIKE 'character_set_database';
SHOW VARIABLES LIKE 'collation_database';
CREATE DATABASE `database2` DEFAULT CHARACTER SET = `utf8` DEFAULT COLLATE = `utf8_general_ci`;
RENAME TABLE `database1`.`table1` TO `database2`.`table1`;
RENAME TABLE `database1`.`table2` TO `database2`.`table2`;
RENAME TABLE `database1`.`table3` TO `database2`.`table3`;
http://dev.mysql.com/doc/refman/5.7/en/rename-table.html
You can change the database name using MySQL interface.
Go to http://www.hostname.com/phpmyadmin
Go to database which you want to rename. Next, go to the operation tab. There you will find the input field to rename the database.
InnoDB supports RENAME TABLE statement to move table from one database to another. To use it programmatically and rename database with large number of tables, I wrote a couple of procedures to get the job done.
You can check it out here - SQL script #Gist
To use it simply call the renameDatabase procedure.
CALL renameDatabase('old_name', 'new_name');
Tested on MariaDB and should work ideally on all RDBMS using InnoDB transactional engine.
I agree with above answers and tips but there is a way to change database name with phpmyadmin
Renaming the Database
From cPanel, click on phpMyAdmin. (It should open in a new tab.)
Click on the database you wish to rename in the left hand column.
Click on the Operations tab.
Where it says "Rename database to:" enter the new database name.
Click the Go button.
When it asks you to want to create the new database and drop the old database, click OK to proceed. (This is a good time to make sure you spelled the new name correctly.)
Once the operation is complete, click OK when asked if you want to reload the database.
here's the video tutorial:
http://support.hostgator.com/articles/specialized-help/technical/phpmyadmin/how-to-rename-a-database-in-phpmyadmin
Another way to rename the database or taking an image of the database is by using the reverse engineering option in the database tab. It will create an ER diagram for the database. Rename the schema there.
After that, go to the File menu and go to export and forward engineer the database.
Then you can import the database.
Sequel Ace database client have a rename database functionality. Select the database you would like to edit and click Database in the menu and then click Rename Database from the dropdown. Rename the database and ckick rename. Done!
After much aggravation this is what I have found to work"simply".
First thing, I am using MYSQL Workbench and the import would not work as it should, as the import dump file would always revert to the original schema name.
I spent several hours trying every thing to no avail,all for a spelling error.
I solved the issue by opening one of the .sql dump files in notebook and hand editing the typo's of the schema name, take care to rename all instances schema name has three in the beginning, save the file and then import. this worked perfectly for me and hope that it will help others looking for the simple answer to changing database names/schema names.
One more tip that I have found true, when programs do not do as they should go to the "source" literally find the source code.
Hope this helps someone
Low rep so they wont let me comment on the prior/post answer(it keeps changing rank or position), so I added it here. reverse engineering will work fine as long as there is no data in the sever table. if data exists and you try to update the server after the name change it will either pull an error or just create a new database/schema with no data, I know I tried ten times to no avail.
The above works simply and avoids headaches, as one can review the SQL code for other errors if any or change table names or creation data.
the .sql file is just a compiled SQL code so in theory one could copy and add it through PHP or the script console of the database management tool.
You can use below command
alter database Testing modify name=LearningSQL;
Old Database Name = Testing,
New Database Name = LearningSQL
Go to data directory and try this:
mv database1 database2
It works for me on a 900 MB database size.
Try:
RENAME database1 TO database2;

In MySQL, how do I batch rename tables within a database?

So basically I have a joomla database in MySQL which has a bunch of tables that have the prefix 'jmla_'. I would like to rename all of these tables by replacing the 'jmla_' prefix with a 'jos_' prefix. Any ideas about how to do this with a simple SQL script or SQL query?
SELECT concat ('rename table ',table_name,' to ',table_name,'_old;')
FROM information_schema.tables
WHERE table_name like 'webform%'
and table_schema='weiss_db_new'
will work.
Run this statement:
SELECT 'rename table '||table_name||' to '||'jos'||substr(table_name,5)||';'
FROM information_schema.tables
WHERE table_name like 'jmla%'
This creates a script that will rename all the tables. Just copy & paste the output into your SQL client.
(You will need to change the || to MySQL's non-standard concatenation operator in case you are not running it in ANSI mode)
RENAME TABLE jmla_whatever to jos_whatever;
You'll have to write a script to cover all your tables - you can populate your script with the output of show tables. See http://dev.mysql.com/doc/refman/5.0/en/rename-table.html for details.
Export using phpmyadmin to .sql file
Use any text editor (I prefer vim, work excellent on large files) with function "find and replace" to open file
Do "find and replace", putting your actual prefix in find box, and updated in replace box
Import file using phpmyadmin.
Remember to drop old database before importing by phpmyadmin. This may be done, checking suitable options during export.
Have you considered using Akeeba Admin Tools Component for Joomla! and then using the Database Prefix Editor to change table prefixes, works really well. Akeeba Admin tools can be used to secure your Joomla! installation
Akeeba can be obtained here

Utility to create "data dictionary" for MySQL database

I'm wondering if there is a utility that exists to create a data dictionary for a MySQL database.
I'm considering just writing a php script that fetches the meta data about the database and displays it in a logical format for users to understand but I'd rather avoid that if there is some pre-built utility out there that can simply do this for me.
Have you looked into HeidiSQL or phpMyAdmin?
Also, MySQL Admin.
Edit#1 fixed typo, added more info
Take a look at https://stackoverflow.com/a/26703098/4208132
There is a db_doc.lua plugin for MySQL Workbench CE
[EDITED]
It seems that the LUA plugin support was discontinued.
So I wrote a plugin in Python to generate data dictionaries.
It is available at: https://github.com/rsn86/MWB-DBDocPy
Looks like MySQL Admin is now MySQL Workbench and you need the Enterprise version to get their reporting tool called DBDoc. It explains a little about customizing DBDoc reporting templates at http://dev.mysql.com/doc/workbench/en/dbdoc-templates.html
The easiest thing to do is Download Toad for MySQL, which is free, and create your own query against the mysql information_schema internal database. You can add columns you want to the query below. Then select all results and export as csv using TOAD.
use information_schema;
desc columns;
select c.table_name, c.column_name, c.data_type from columns c
where c.table_schema = "mydatabaseinstance";