I have two databases. Now I'm trying to import all table schema only from first database to second database. I already exported all table structure from first database using phpmyadmin. But I can't import the table structures in the second database. phpmyadmin says
#1050 - Table 'XXXXXX' already exists
How can I import only the table structure correctly?
Note: Both databases had same table and all table had same structure. I have changed some table structures in the first database that I can't remember right now. Now I need to merge both table structure only. Also both database contains different data set and I can't afford any data loss from both databases.
Before executing any command I would recommend taking full database backup, otherwise you may lost a few days of sleep.
Using ALTER command
Use ALTER command to modify table structure. Here's sql that adds age not nullable age field to users table.
ALTER TABLE users ADD age int(11) not null
Re-creating table
I wouldn't recommend this method because you'll have data loss. Drop old table then create with new schema.
DROP TABLE mytable;
CREATE TABLE mytable (
...
);
Or if you want to keep data you can:
Duplicate or rename table to different name.
Create a new table with new schema.
Copy data from old table: INSERT INTO newtable SELECT * FROM oldtable
Renaming tables might cause relationship issues. I would recommend using ALTER command as much as possible. You can also take a look at scheme migration(aka: code first migration, database migration).
The main Issue is merging the tables. To identify the differences between the two tables I use a small software called SQL Power Architect.
Related
I have a problem with a mysql table which I'd like to clone and give another name. When I try the
CREATE TABLE data1 AS SELECT * FROM data;
It gets stuck in execution, and never actually do anything, and I have to abort.
The thing is that my table "data" has some extra commands in the sql source like
CREATE INDEX keywords ON data (keywords);
It has a few of these, and I suspect that this is what's causing the issue, since I have been able to clone other tables without these extra commmands. I'm new in sql, so I have no idea how to overcome this problem. Anyone care to help?
Use is create table like syntax. to create a table with the same structure and after this you can copy the data.
https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html
a second solution is to use mysqldump to extract the table structur including the data, rename the table in dump file and Reimport the it.
-- To copy table with constraints
CREATE TABLE new_table_name LIKE old_table_name;
-- To copy without constraints
CREATE TABLE new_table_name
(SELECT * FROM old_table_name WHERE 0);
source
I am using MySQL to capture snapshots of my data everyday, so I need to create multiple tables with the same columns.
my_foobar_table_20170125
my_foobar_table_20170126
my_foobar_table_20170127
What's the easiest way to create the tables? Would it be with a table schema file? Or, with a create table query?
I am leaning toward using a table schema file if that is possible. As it seems cleaner than a sql query to create the table.
I have googled around, and surprisingly there is no clear answer on this. In fact, it's not even obvious what exactly is a "table schema file", or how to generate this from mysql workbench, or use the schema file to create the table.
Definitely create a table .sql file and load it into MySQL then log in. Manually making it each time is a ton of work and this allows you to also customize each table if needed, and to use drop database and to refresh easily as well.
CREATE DATABASE databasename;
USE database;
CREATE TABLE tableofthings (
ID INT NOT NULL AUTO_INCREMENT,
otherdatastring VARCHAR(50)...
PRIMARY KEY (ID)
);
...then put other tables below...
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.
I'm aware that copying data will only copy the data and no constraints, I'll lose my pk and fk etc.
But what if i were to copy the data back into a table with primary keys and foreign. because i want to remodel my db on workbench but don't want to lose the data i have imput into my tables so i was thinking of making a copy deleting the original remodelling the db and forward engineering and copying the data back into the table will this work?
Not sure if I gut your question right but from what it looks you only need a new table with the data to mass with. have you tried using CREATE TABLE AS SELECT?
http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html
CREATE TABLE t AS SELECT * from old_t
Is it possible to alter the schema of a mysql table by simply providing the new schema and having the database figure out how to migrate? For example, let's say I have a table with two columns: id, name. I want to modify this table by adding a new column: title. I know that I can issue the command ALTER TABLE tbl ADD COLUMN title. Is there a way I can just provide the complete schema and have mysql figure out that it needs to add a title column?
I hope that makes sense what I am asking.
MySQL can't do this by itself, but I found a blog that says MySQL Workbench can do it.
No - It cannot do that just giving your database a new schema. It would have to go about and second guess what to do with the data. (Also what happens to triggers, stored procedures ...)
You have a couple of choices
Modify each table and decide what needs to be done
Export the data, create a new database and then figure out how to import it into the new regime.