Python MySQL Xóa Bảng

Xóa bảng

Bạn có thể sử dụng lệnh "DROP TABLE" để xóa bảng đã có:

Mô Hình

Xóa bảng "customers":

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DROP TABLE customers"
mycursor.execute(sql)

Chạy Mô Hình

Chỉ xóa khi bảng tồn tại

Nếu bảng cần xóa đã bị xóa hoặc không tồn tại do bất kỳ nguyên nhân nào khác, bạn có thể sử dụng từ khóa IF EXISTS để tránh lỗi.

Mô Hình

Xóa bảng "customers" (nếu có):

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DROP TABLE IF EXISTS customers"
mycursor.execute(sql)

Chạy Mô Hình