📜  如何使用Python在 MySQL 中复制表定义?

📅  最后修改于: 2022-05-13 01:54:41.293000             🧑  作者: Mango

如何使用Python在 MySQL 中复制表定义?

Python需要一个接口来访问数据库服务器。 Python支持广泛的接口来与各种数据库进行交互。为了与 MySQL 数据库通信,使用了 MySQL Connector Python模块,这是一个完全用Python编写的 API。这个模块是自给自足的,这意味着它没有依赖项,只需要标准的Python库。

在 SQL 中复制表定义:

使用 CREATE 和 SELECT 语句,可以将定义和数据从现有表复制到新表。

句法:

CREATE TABLE new_table AS 
SELECT * FROM original_table;

此处,将现有表 (original_table) 的内容复制到新表 (new_table) 中。 CREATE 语句创建一个具有 SELECT 语句定义的结构的新表,并用选定的列填充新表(* 表示选择了所有列)。

注意:与原始表关联的数据库对象(如索引、键约束等)不会重复。

要复制表及其从属数据库对象,可以使用 CREATE、LIKE 和 INSERT 语句。

句法:

CREATE TABLE new_table 
LIKE original_table;

INSERT new_table 
SELECT * FROM original_table;

首先,CREATE 语句创建一个与现有表 ( original_table ) 具有相同结构和依赖对象的新表 ( new_table )。然后 INSERT 语句用从原始表中选择的值填充新表。简单地使用不带 INSERT 的 CREATE 语句将创建一个空表,该表具有现有表的结构和相关对象。

使用Python的步骤:

  1. 建立与数据库服务器的连接并创建游标对象。
  2. 使用游标对象执行 CREATE-SELECT 或 CREATE-LIKE-INSERT 语句来复制表。
  3. 检查表定义是否已被复制。

让我们看一些例子以便更好地理解。

使用中的数据库:

我们将使用带有描述产品和可用库存的产品表的商店数据库。

示例 1:使用 CREATE-SELECT 复制表定义 陈述

使用connect()函数建立与数据库服务器的连接,使用cursor()函数创建游标对象。使用此游标对象,执行 CREATE-SELECT 语句以使用execute()函数创建产品表新库存表的副本。要检查新创建的表是否与原始表具有相同的表定义,请使用 DESC 语句描述结构和 SELECT 语句检查表内容。

Python
# Import required packages
import mysql.connector
  
# Establish connection to MySQL database
db = mysql.connector.connect(
    host = "localhost",
    user = "username",
    password = "geeksforgeeks",
    database = "store"
)
  
# Create a cursor object
cursor = db.cursor()
  
# MySQL query for copying existing table,
# selecting new table data and
# describing new table structure
queries = "CREATE TABLE inventory1 AS SELECT * FROM products;\
           DESC inventory1;"
      
# Execute the query 
results = cursor.execute(queries, multi = True)
  
# Print data and description of newly created table
for result in results:
    if result.with_rows:
        for row in result:
            print(row)
              
# Close database connection
db.close()


Python
# Import required packages
import mysql.connector
  
# Establish connection to MySQL database
db = mysql.connector.connect(
    host="localhost",
    user="username",
    password="geeksforgeeks",
    database="store"
)
  
# Create a cursor object
cursor = db.cursor()
  
# MySQL queries for copying existing table
# without copying its data,
# selecting new table data and
# describing new table structure
queries = "CREATE TABLE inventory2 AS \
           SELECT * FROM products \
           WHERE 1=0; \
           DESC inventory2;"
  
# Execute the query
results = cursor.execute(queries, multi=True)
  
# Print data and description of newly created table
for result in results:
    if result.with_rows:
        for row in result:
            print(row)
  
# Close database connection
db.close()


Python
# Import required packages
import mysql.connector
  
# Establish connection to MySQL database
db = mysql.connector.connect(
    host="localhost",
    user="username",
    password="geeksforgeeks",
    database="store"
)
  
# Create a cursor object
cursor = db.cursor()
  
# MySQL queries for copying existing table,
# selecting new table data and
# describing new table structure
queries = "CREATE TABLE inventory3 LIKE products; \
           INSERT inventory3 SELECT * FROM products;\
           DESC inventory3;"
      
# Execute the query 
results = cursor.execute(queries, multi = True)
  
# Print data and description of newly created table
for result in results:
    if result.with_rows:
        for row in result:
            print(row)
              
# Close database connection
db.close()


Python
# Import required packages
import mysql.connector
  
# Establish connection to MySQL database
db = mysql.connector.connect(
    host="localhost",
    user="username",
    password="geeksforgeeks",
    database="store"
)
  
# Create a cursor object
cursor = db.cursor()
  
# MySQL queries for copying existing table
# without copying its data,
# selecting new table data and
# describing new table structure
queries = "CREATE TABLE inventory4 LIKE products; \
           DESC inventory4;"
  
# Execute the query
results = cursor.execute(queries, multi=True)
  
# Print data and description of newly created table
for result in results:
    if result.with_rows:
        for row in result:
            print(row)
  
# Close database connection
db.close()


输出:

产品表中的所有值都被复制到库存表中。结构,即数据类型和列被保留。但是,像prod_id的主键约束这样的数据库对象没有保留。

示例 2:使用 CREATE-SELECT 语句复制没有表内容的表定义

使用与上述示例相同的代码来建立连接和验证。要简单地复制表定义而不是内容,请将 WHERE 子句添加到 SELECT 语句,使其返回一个空集并且不复制任何值,如下所示。

Python

# Import required packages
import mysql.connector
  
# Establish connection to MySQL database
db = mysql.connector.connect(
    host="localhost",
    user="username",
    password="geeksforgeeks",
    database="store"
)
  
# Create a cursor object
cursor = db.cursor()
  
# MySQL queries for copying existing table
# without copying its data,
# selecting new table data and
# describing new table structure
queries = "CREATE TABLE inventory2 AS \
           SELECT * FROM products \
           WHERE 1=0; \
           DESC inventory2;"
  
# Execute the query
results = cursor.execute(queries, multi=True)
  
# Print data and description of newly created table
for result in results:
    if result.with_rows:
        for row in result:
            print(row)
  
# Close database connection
db.close()

CREATE-SELECT 查询中的1 = 0条件的计算结果始终为 false。因此,查询返回一个空表,创建一个与现有产品表具有相同结构的新空库存表。

输出:

示例 3:使用 CREATE-LIKE 语句复制表定义和依赖数据库对象

使用与示例 1 相同的代码来建立连接和验证。要复制表及其所有依赖对象和内容,请使用 CREATE-LIKE-INSERT 语句,如下所示。

Python

# Import required packages
import mysql.connector
  
# Establish connection to MySQL database
db = mysql.connector.connect(
    host="localhost",
    user="username",
    password="geeksforgeeks",
    database="store"
)
  
# Create a cursor object
cursor = db.cursor()
  
# MySQL queries for copying existing table,
# selecting new table data and
# describing new table structure
queries = "CREATE TABLE inventory3 LIKE products; \
           INSERT inventory3 SELECT * FROM products;\
           DESC inventory3;"
      
# Execute the query 
results = cursor.execute(queries, multi = True)
  
# Print data and description of newly created table
for result in results:
    if result.with_rows:
        for row in result:
            print(row)
              
# Close database connection
db.close()

输出:

注意prod_id的主键约束被保留。

示例 4:使用 CREATE-LIKE 语句复制没有表数据的表定义和依赖对象

使用与示例 1 相同的代码来建立连接和验证。要简单地复制表定义而不是内容,请删除 INSERT 语句,如下所示。

Python

# Import required packages
import mysql.connector
  
# Establish connection to MySQL database
db = mysql.connector.connect(
    host="localhost",
    user="username",
    password="geeksforgeeks",
    database="store"
)
  
# Create a cursor object
cursor = db.cursor()
  
# MySQL queries for copying existing table
# without copying its data,
# selecting new table data and
# describing new table structure
queries = "CREATE TABLE inventory4 LIKE products; \
           DESC inventory4;"
  
# Execute the query
results = cursor.execute(queries, multi=True)
  
# Print data and description of newly created table
for result in results:
    if result.with_rows:
        for row in result:
            print(row)
  
# Close database connection
db.close()

输出: