dev
dev

Reputation: 496

How to create table with structure only as select table

There is any procedure to create a table with same structure from another table without data.

i.e table with structure only .at that time i am creating the table with query each time as new database require.

There is any procedure in mysql to create table with structure only with another database table or same database table please help me

Upvotes: 2

Views: 550

Answers (2)

Muhammad Arif
Muhammad Arif

Reputation: 1022

Try this query Create table like(https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html)

 CREATE TABLE table1 LIKE table2;

Upvotes: 2

Dylan Su
Dylan Su

Reputation: 6065

You can use CREATE TABLE LIKE.

CREATE TABLE t1 LIKE t2;

Manual

https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html

Demo

mysql> create table t1(c1 int primary key, c2 int auto_increment, key(c2));
Query OK, 0 rows affected (0.00 sec)

mysql> create table t2 like t1;
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t2;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                         |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `c1` int(11) NOT NULL,
  `c2` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`c1`),
  KEY `c2` (`c2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Upvotes: 3

Related Questions