Dropping database table - mysql

While I was importing production data into my development database, something went off and I now have two database tables that are similar, differing only that one contains the db name as a table prefix.
In this case, I have a database rmstg, and two tables
main_stratkeys
rmstg.main_stratkeys
How can I drop the latter table?
DROP TABLE rmstg.main_stratkeys drops the first table, but not the latter.
DROP TABLE rmstg.rmstg.main_stratkeys returns a SQL syntax error with the inner .rmstg. declaration.

Try using backquotes:
drop table `rmstg.main_stratkeys`;
or
drop table rmstg.`rmstg.main_stratkeys`;

Assuming you're using the CLI:
USE rmstg;
DROP TABLE main_stratkeys;
Is also an option.
Backquotes should also be helpful, dunno that syntax, exactly, though.

Related

MySQL: Does `DROP TABLE` completely remove the table or just the structure?

I am under the impression that the MySQL command DROP TABLE User will just remove the data, columns, and relevant constraints. However, the table shell still exists. Is this correct?
Using DROP TABLE will remove the entire table and all records contained inside it. If you want to retain the table structure but remove all data, then consider using TRUNCATE TABLE. Truncating a table is implemented by dropping the entire table and then recreating it. This is faster than doing DELETE FROM yourTable, which removes records one-by-one.
After Drop Table, the table will not exist anymore, so no data and no table definition(which you called 'table shell'); TRUNCATE TABLE keep your table definition and delete all the data ,and reset table auto-increment as well, but be careful about TRUNCATE if the table size is huge, it will expand your tablespace and not easy to shrink.
As mysql manual on drop table says:
Be careful with this statement! It removes the table definition and all table data.
So, no shell (whatever that should mean) remains after dropping a table.
What do you mean by table shell?
From MqSQL dev site:
DROP TABLE removes one or more tables. You must have the DROP privilege for each table.
Be careful with this statement! It removes the table definition and all table data. For a partitioned table, it permanently removes the table definition, all its partitions, and all data stored in those partitions. It also removes partition definitions associated with the dropped table.

MySQL MERGE Storage Engine - DROP & ALTER

I need to Add & Delete merged tables in the UNION=() line. According to the MySQL docs it says:
DROP the MERGE table and re-create it.
Use ALTER TABLE tbl_name UNION=(...) to change the list of
underlying tables.
The only "DROP" I'm aware of is DROP TABLE tablename; Are these instructions suggesting that I drop the MRG_MyISAM table, then recreate it with an empty UNION=() field? To then be followed by an ALTER TABLE tbl_name UNION=(...) with all the tables I need to have connected?
If possible, could you post an example of the commands?
Thanks
Oh boy, am I late here. But this page is in the top google search results for "alter table tbl_name union=(...)". So I guess it needs an answer
So here's the answer.
To change the union list of underlying tables for merge table you only need to execute this statement
alter table tbl_name union=(`t1`,`t2`,`t3`);
where t1,t2,t3 is a list of tables you want to have in a union.
You can drop merge table and recreate it with a new list of underlying tables.
Drop statement execute on merge table will only delete the merge table itself and won't affect underlying tables.
But altering it should be sufficient. And you don't need to recreate it with empty union, if you ever do that, just use list of tables that you want to have in it.
For more, please refer to documentation:
https://dev.mysql.com/doc/refman/5.7/en/merge-storage-engine.html

MySQL workbench creates forward script dropping of tables in wrong sequence, violating foreign key constraints

The subject line actually says it all.
I have a schema, now when I create a forward script it generates the tables and I added the option to do a DROP TABLE IF EXISTS in front of every create table SQL. The creation part is fine. But if you run the script twice you notice that the drop sequence is the wrong order.
I think the concept of aligning the drop sequence with the create sequence is just conceptually arguable. I think you might be able to create schemas where you won't be able to create the tables in the same sequence as you would drop them.
Anyhow, I can't find any option to change the order or do anything. Has anyone an idea how I can change the drop sequence manually ?
I am sorry to be not able to share any SQL, however I think the problem is really generic. And you want solve it by writing different SQL. So it should be possible to answer based on discussion, not on code.
That's Workbench version 6.3.6. So almost latest. (Currently 6.3.7)
You must drop table in reverse order of creating them.
When creating:
Create table a...;
Create table b...;
Create table c...;
When dropping:
Drop table c;
Drop table b;
Drop table a;

Can't drop table after creating table with wrong engine

I'm trying to drop a table containing several hundred thousand column-based records. Normally when creating the database I use a column-based engine (infinidb) but in this case I forgot to include the ENGINE statement. So the database is pretty much unusable for my needs. Now I have a database full of tables that are taking forever to drop (it's been two hours and nothing has happened). I tried the ALTER TABLE table ENGINE=INFINIDB command but again, it's taking forever (see above re: two hours). EDIT: The first command I tried was DROP TABLE. It hung with every single table. Then I tried the ALTER command in case that was faster for some reason, but it wasn't.
Is there another way to get rid of this database? E.g. manually going into the /mysql/ directory and deleting the database? I guess I could just rename it and leave it, but I'd rather get rid of it entirely so it's not taking up space.
First of all you said Can't drop table. But in post you mentioned ALTER TABLE table ENGINE=INFINIDB.
But DROP != ALTER it is two different things.
So you can do following:
CREATE new table with same structure but engine you need.
copy(UPDATE) data from old table to the one you just created.
DROP old table.
RENAMErename new one to old name
It turned out that another process (a website) was using this database and had a couple of queries that got 'stuck' in the SQL server and caused the table to hang due to the database using the wrong engine, which I'm assuming was InnoDB since I didn't specify an engine when I initially used the "CREATE TABLE table1 AS SELECT * FROM table2" command. We finally managed to wipe the database and start over. Thanks for your help.

Merge two database into a third one

I have two MySQL database k_db1 and k_db2 on a single server.
In k_db1, I have k_db1.table1 and k_db1.table2.
In k_db2, I have k_db2.table3 and k_db2.table4.
I want to create a third database k_db3 where I copy/paste tables of others databases.
It will result in k_db3.db1-table1, k_db3.db1-table2, k_db3.db2-table3, k_db3.db2-table4. I want to transfer data, indexes etc... and I don't want to delete k_db1 and k_db2 tables in the process. It must duplicate datas.
Do you know a way to do this just with SQL command?
Thanks in advance for your help.
You can try something like this:
DROP TABLE IF EXISTS k_db3.db1_table_1;
CREATE TABLE k_db3.db1_table_1 AS
SELECT * FROM db1.table_1;
Then you can recreate the indexes on the new table via ALTER TABLE statements.
Also I would avoid using - in table names.