How to view table structure sql query? - mysql

I want to make a new table like as an existing table in MySQL database with some "column name" changes. So, is there any way to see the SQL dump of the existing table without importing?

You can use SHOW CREATE TABLE:
SHOW CREATE TABLE oldtable

Related

How to SHOW CREATE TABLE in a particular database in MySQL?

It is possible to get CREATE TABLE statement in specific database without using USE before?
Something like SHOW CREATE TABLE my_table IN my_database; (this doesn't works).
Thanks
show create table database_name.table_name;

Is there any way to import database table schema only

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.

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.

SQL command to automatically create table based on data being inserted

I have to load some data into a temporary table, but the data is never uniform, the datatype and number of columns will always be different.
Is there an SQL command that will automatically create table specifications based on data that will be loaded into it?
Assuming that you're populating it from a query, you can use the syntax CREATE TABLE tablename SELECT ...; see ยง12.1.14.1. CREATE TABLE ... SELECT Syntax in the MySQL 5.6 Reference Manual.
SELECT ... INTO should do what you want.

Error while trying to copy mysql table from one database to another

I want to copy mysql table from one database to another using mysql command line
I am trying to executing commmand
DROP TABLE IF EXISTS `db1.tablename`; CREATE TABLE `db1.tablename` like `db2.tablename`;
but it is giving me error no database is selected.
but if I fire
use db2
DROP TABLE IF EXISTS `db1.tablename`; CREATE TABLE `db1.tablename` like `db2.tablename`;
then it is create table db1.tablename inside db 2.
How to fix it ?
I think your create statement should look like this:
CREATE TABLE `db1.tablename` SELECT * FROM `db2.tablename`;
The table name in your query includes the database name without separation. It should be something like:
DROP TABLE IF EXISTS `db1`.`tablename`;
CREATE TABLE `db1`.tablename` like `db2`.`tablename`;