Добавить
Уведомления

Copy structure with data to a new table with unique constraints auto increment using IF EXISTS

We will learn how to copy one table with data to a new table. So first we will create a table with some sample data. Here is our student table . Using this we will create another table by copying the same table. CREAET TABLE student2 SELECT * FROM student Let us copy all class four students and create the table . CREATE TABLE student2 SELECT * FROM student WHERE class=’Four’ CREATE TABLE student2 SELECT * FROM student WHEREmark = 60 We can specify columns to be copied CREATE TABLE student2 SELECT id,name, mark FROM student To Copy table structure only without data , we can use this command CREATE TABLE student2 LIKE student This command will create student2 and copy all the constraints including auto-increment field. Here no data is copied. To prevent error if table is already exists then we will use IF NOT EXISTS CREATE TABLE IF NOT EXISTS student5 SELECT * FROM student WHERE class='Four' Copy table with data and auto-increment column we can use like this. CREATE TABLE student2 (id INT(3) auto_increment primary key) SELECT student.name,student.class, student.mark from student Source code here https://www.plus2net.com/sql_tutorial/sql_copy_table.php SQL dump of student table https://www.plus2net.com/sql_tutorial/sql_student_dump.php #CopyTable #CopyDataNewTable #CopyStructure #CopyConstraints #CopyColumns #SQL #MySQLQuery #plus2net

12+
14 просмотров
2 года назад
3 декабря 2023 г.
12+
14 просмотров
2 года назад
3 декабря 2023 г.

We will learn how to copy one table with data to a new table. So first we will create a table with some sample data. Here is our student table . Using this we will create another table by copying the same table. CREAET TABLE student2 SELECT * FROM student Let us copy all class four students and create the table . CREATE TABLE student2 SELECT * FROM student WHERE class=’Four’ CREATE TABLE student2 SELECT * FROM student WHEREmark = 60 We can specify columns to be copied CREATE TABLE student2 SELECT id,name, mark FROM student To Copy table structure only without data , we can use this command CREATE TABLE student2 LIKE student This command will create student2 and copy all the constraints including auto-increment field. Here no data is copied. To prevent error if table is already exists then we will use IF NOT EXISTS CREATE TABLE IF NOT EXISTS student5 SELECT * FROM student WHERE class='Four' Copy table with data and auto-increment column we can use like this. CREATE TABLE student2 (id INT(3) auto_increment primary key) SELECT student.name,student.class, student.mark from student Source code here https://www.plus2net.com/sql_tutorial/sql_copy_table.php SQL dump of student table https://www.plus2net.com/sql_tutorial/sql_student_dump.php #CopyTable #CopyDataNewTable #CopyStructure #CopyConstraints #CopyColumns #SQL #MySQLQuery #plus2net

, чтобы оставлять комментарии