- Python 数据访问教程
- Python 数据访问 - 首页
- Python MySQL
- Python MySQL - 简介
- Python MySQL - 数据库连接
- Python MySQL - 创建数据库
- Python MySQL - 创建表
- Python MySQL - 插入数据
- Python MySQL - 查询数据
- Python MySQL - WHERE 子句
- Python MySQL - ORDER BY 子句
- Python MySQL - 更新表
- Python MySQL - 删除数据
- Python MySQL - 删除表
- Python MySQL - LIMIT 子句
- Python MySQL - JOIN 子句
- Python MySQL - 游标对象
- Python PostgreSQL
- Python PostgreSQL - 简介
- Python PostgreSQL - 数据库连接
- Python PostgreSQL - 创建数据库
- Python PostgreSQL - 创建表
- Python PostgreSQL - 插入数据
- Python PostgreSQL - 查询数据
- Python PostgreSQL - WHERE 子句
- Python PostgreSQL - ORDER BY 子句
- Python PostgreSQL - 更新表
- Python PostgreSQL - 删除数据
- Python PostgreSQL - 删除表
- Python PostgreSQL - LIMIT 子句
- Python PostgreSQL - JOIN 子句
- Python PostgreSQL - 游标对象
- Python SQLite
- Python SQLite - 简介
- Python SQLite - 建立连接
- Python SQLite - 创建表
- Python SQLite - 插入数据
- Python SQLite - 查询数据
- Python SQLite - WHERE 子句
- Python SQLite - ORDER BY 子句
- Python SQLite - 更新表
- Python SQLite - 删除数据
- Python SQLite - 删除表
- Python SQLite - LIMIT 子句
- Python SQLite - JOIN 子句
- Python SQLite - 游标对象
- Python MongoDB
- Python MongoDB - 简介
- Python MongoDB - 创建数据库
- Python MongoDB - 创建集合
- Python MongoDB - 插入文档
- Python MongoDB - 查找
- Python MongoDB - 查询
- Python MongoDB - 排序
- Python MongoDB - 删除文档
- Python MongoDB - 删除集合
- Python MongoDB - 更新
- Python MongoDB - LIMIT 子句
- Python 数据访问资源
- Python 数据访问 - 快速指南
- Python 数据访问 - 有用资源
- Python 数据访问 - 讨论
Python SQLite - 删除表
您可以使用 DROP TABLE 语句删除整个表。您只需要指定要删除的表的名称。
语法
以下是 PostgreSQL 中 DROP TABLE 语句的语法:
DROP TABLE table_name;
示例
假设我们使用以下查询创建了名为 CRICKETERS 和 EMPLOYEES 的两个表:
sqlite> CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); sqlite> CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT ); sqlite>
现在,如果您使用.tables命令验证表列表,您可以在其中看到上面创建的表(列表)如下:
sqlite> .tables CRICKETERS EMPLOYEE sqlite>
以下语句将从数据库中删除名为 Employee 的表:
sqlite> DROP table employee; sqlite>
由于您已删除 Employee 表,如果您再次检索表列表,您将只能看到其中一个表。
sqlite> .tables CRICKETERS sqlite>
如果您尝试再次删除 Employee 表,由于您已将其删除,您将收到一条错误消息,提示“no such table”,如下所示:
sqlite> DROP table employee; Error: no such table: employee sqlite>
要解决此问题,您可以将 IF EXISTS 子句与 DELTE 语句一起使用。如果表存在,则此语句将删除该表;否则,将跳过 DLETE 操作。
sqlite> DROP table IF EXISTS employee; sqlite>
使用 Python 删除表
您可以随时使用 MYSQL 的 DROP 语句删除表,但在删除任何现有表时需要非常小心,因为删除表后将无法恢复丢失的数据。
示例
要使用 python 从 SQLite3 数据库中删除表,请在游标对象上调用execute()方法,并将 drop 语句作为参数传递给它。
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Doping EMPLOYEE table if already exists
cursor.execute("DROP TABLE emp")
print("Table dropped... ")
#Commit your changes in the database
conn.commit()
#Closing the connection
conn.close()
输出
Table dropped...
广告