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;
Related
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
I want to look at a query used to create a table. The table was created already. I've seen this done in the past in the command console, but I cant find how to do this now. It was something like:
show create; --OR
select create; --none of these work
After running that it will display the query and some details about mysql.
SHOW CREATE TABLE tbl_name is the syntax in mysql. MySql Refernce.
maybe you're talking about show create table
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`;
Is it possible to use the create table query (create table t2 like t1) to get the query instead the actual table? Kind of like duplicating some management consoles Send To editor feature?
I think you're looking for SHOW CREATE TABLE tablename;
'tablename' can be the table within the current database, if you've selected one, or you can qualify it as in 'dbname.tablename'. The output can be used to create the table again. In fact, I sometimes use it in scripts that need to either use an existing table or create it if it doesn't exist. First I'll get the table creation info as shown above, and then I'll use something like"
CREATE TABLE IF NOT EXISTS tablename .........
Where '..........' is all the goodies that SHOW CREATE TABLE tablename gives me.
If you're interested in a good tutorial on MySQL (the database), as well as SQL (the language), you might have a look at the O'Reilly book, Learning MySQL. It's pretty good.
For example suppose I have a table M2011_03 and I wish to copy the structure but not the data into a table M2011_04. M2011_04 currently does not exist. How do I do this?
I think you are looking for:
CREATE TABLE new_tbl LIKE orig_tbl;
More information: http://dev.mysql.com/doc/refman/5.1/en/create-table.html
either
a) paste the results of SHOW CREATE TABLE M2011_03 to create the '04 one.
or
b)
CREATE TABLE M2011_04 SELECT * FROM M2011_03;
TRUNCATE TABLE M2011_04;