How to create a new table from the first table using MySQL LIKE?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1
(
   EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   EmployeeName varchar(50)
);
Query OK, 0 rows affected (0.48 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1(EmployeeName) values('Tom');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable1(EmployeeName) values('Mike');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1(EmployeeName) values('Emma');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1(EmployeeName) values('Sam');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement::

mysql> select *from DemoTable1;

This will produce the following output −

+------------+--------------+
| EmployeeId | EmployeeName |
+------------+--------------+
|          1 | Tom          |
|          2 | Mike         |
|          3 | Emma         |
|          4 | Sam          |
+------------+--------------+
4 rows in set (0.00 sec)

Following is the query to create a new table from another table using LIKE in MySQL −

mysql> create table DemoTable2 LIKE DemoTable1;
Query OK, 0 rows affected (0.55 sec)

Let us check the description of the table −

mysql> desc DemoTable2;

This will produce the following output −

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| EmployeeId   | int(11)     | NO   | PRI | NULL    | auto_increment |
| EmployeeName | varchar(50) | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
raja
Published on 04-Oct-2019 07:24:17
Advertisements