একটি টেবিলের ডেটা অন্য টেবিলে কপি করতে INSERT INTO SELECT স্টেটমেন্ট ব্যবহার করতে হয়। সিনট্যাক্স:
INSERT INTO yourDatabaseName.yourTableName(SELECT *FROM yourDatabaseName.yourTableName);
উদাহরণের জন্য দুটি ডেটাবেসে টেবিল তৈরি করি। প্রথম ডেটাবেস ‘bothinnodbandmyisam’:
mysql> create table Student_Information (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> Name varchar(10),
-> Age int
-> ); Query OK, 0 rows affected (0.67 sec)
রেকর্ড ইনসার্ট করি:
mysql> insert into Student_Information(Name,Age) values('Larry',30); Query OK, 1 row affected (0.20 sec)
mysql> insert into Student_Information(Name,Age) values('Mike',26); Query OK, 1 row affected (0.19 sec)
mysql> insert into Student_Information(Name,Age) values('Bob',26); Query OK, 1 row affected (0.12 sec)
mysql> insert into Student_Information(Name,Age) values('Carol',24); Query OK, 1 row affected (0.15 sec)
ডেটা দেখি:
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | Larry | 30 | | 2 | Mike | 26 | | 3 | Bob | 26 | | 4 | Carol | 24 | +----+-------+------+ 4 rows in set (0.00 sec)
দ্বিতীয় ডেটাবেস ‘sample’-এ টেবিল তৈরি করি:
mysql> use sample; Database changed
mysql> create table Student_Table_sample (
-> StudentId int NOT NULL AUTO_INCREMENT,
-> StudentName varchar(20),
-> StudentAge int,
-> PRIMARY KEY(StudentId)
-> ); Query OK, 0 rows affected (0.57 sec)
এখন প্রথম টেবিল থেকে দ্বিতীয় টেবিলে ডেটা কপি করি:
mysql> insert into sample.Student_Table_sample(select *from bothinnodbandmyisam.Student_Information); Query OK, 4 rows affected (0.23 sec) Records: 4 Duplicates: 0 Warnings: 0
কপি হওয়া ডেটা যাচাই করি:
mysql> select *from Student_Table_sample;
ফলাফল:
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Larry | 30 | | 2 | Mike | 26 | | 3 | Bob | 26 | | 4 | Carol | 24 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)