- 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 PostgreSQL - 创建表
您可以使用 CREATE TABLE 语句在 PostgreSQL 数据库中创建一个新表。执行此操作时,需要指定表名、列名及其数据类型。
语法
以下是 PostgreSQL 中 CREATE TABLE 语句的语法。
CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, );
示例
以下示例在 PostgreSQL 中创建一个名为 CRICKETERS 的表。
postgres=# CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age INT, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); CREATE TABLE postgres=#
您可以使用 \dt 命令获取 PostgreSQL 数据库中的表列表。创建表后,您可以验证表列表,如下所示,您可以在其中看到新创建的表:
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+------------+-------+----------
public | cricketers | table | postgres
(1 row)
postgres=#
同样,您可以使用 \d 命令获取已创建表的描述,如下所示:
postgres=# \d cricketers
Table "public.cricketers"
Column | Type | Collation | Nullable | Default
----------------+------------------------+-----------+----------+---------
first_name | character varying(255) | | |
last_name | character varying(255) | | |
age | integer | | |
place_of_birth | character varying(255) | | |
country | character varying(255) | | |
postgres=#
使用 Python 创建表
要使用 Python 创建表,需要使用 *pyscopg2* 的游标的 execute() 方法执行 CREATE TABLE 语句。
示例
以下 Python 示例创建一个名为 employee 的表。
import psycopg2
#Establishing the connection
conn = psycopg2.connect(
database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Doping EMPLOYEE table if already exists.
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
#Creating table as per requirement
sql ='''CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT
)'''
cursor.execute(sql)
print("Table created successfully........")
conn.commit()
#Closing the connection
conn.close()
输出
Table created successfully........
广告