Python MySQL——创建和列出表
原文:https://www.studytonight.com/python/python-mysql-create-table
现在我们将学习如何在 Python 中的任何 MySQL 数据库中创建表,我们还将看到如何通过使用 Python 列出数据库中的所有表来检查表是否已经存在。
Python MySQL -创建表
基本上,为了在 MySQL 数据库中存储信息,需要创建表。也需要首先选择我们的数据库,然后在该数据库中创建表格。
在创建连接时,您还可以指定数据库的名称,如下所示:
import mysql.connector
db = mysql.connector.connect(
host = "localhost",
user = "yourusername",
password = "yourpassword",
database = "studytonight"
)
如果上面的代码是执行没有任何错误,那么这意味着你已经成功连接到名为的数据库 StudyTonight。
创建表的 SQL 查询
要在选定的数据库中创建表,将使用以下语句。让我们看看语法:
CREATE TABLE table_name;
让我们在指定的数据库中创建一个名为学生的表,即StudyTonight
在学生表中,我们将有以下字段:姓名、罗诺、分支、地址。
#for our convenience we will import mysql.connector as mysql
import mysql.connector as mysql
db = mysql.connect(
host = "localhost",
user = "yourusername",
passwd = "yourpassword",
database="studytonight"
)
cursor = db.cursor()
cursor.execute("CREATE TABLE students (name VARCHAR(255), rollno INTEGER(100), branch VARCHAR(255), address VARCHAR(255))")
如果该代码执行没有任何错误,则表示该表已经成功创建。
现在,如果您想检查数据库中现有的表,那么您可以使用SHOW TABLES
SQL 语句。
列出数据库中的现有表
现在我们已经在数据库中创建了一个表。让我们检查一下数据库中存在的表。使用下面给出的代码:
#for our convenience we will import mysql.connector as mysql
import mysql.connector as mysql
db = mysql.connect(
host = "localhost",
user = "yourusername",
passwd = "yourpassword",
database="studytonight"
)
cursor = db.cursor()
## getting all the tables which are present in 'datacamp' database
cursor.execute("SHOW TABLES")
tables = cursor.fetchall() ## it returns list of tables present in the database
## showing all the tables one by one
for table in tables:
print(table)
上述代码的输出如下
('学生',)
Python MySQL -带主键的表
因为我们已经在数据库中创建了一个名为学生的表,我们将在其中存储学生数据,并在需要时获取数据。但是在获取数据时,我们可能会找到同名的学生,这可能会导致获取错误的数据,或者造成一些混乱。
因此,为了唯一识别表中的每个记录,我们可以在表中使用 【主键】 。让我们先看看什么是主键?
什么是主键?
主键是使一列或一组列只接受唯一值的属性。借助主键,可以在表中唯一地找到每一行。
观看此视频,了解数据库管理系统键- 用示例解释的数据库管理系统键
因此,为了用从 1 开始的数字唯一地识别每个行。我们将使用如下语法:
INT AUTO_INCREMENT PRIMARY KEY
对任何列使用上述代码,我们可以使其值自动递增,这意味着即使在向表中插入新的数据行时没有为该列插入任何值,数据库也会自动添加一个递增的值。
在创建表期间添加主键
让我们看看如何在创建表时添加主键。其代码片段如下所示:
import mysql.connector as mysql
db = mysql.connect(
host = "localhost",
user = "yourusername",
passwd = "yourpassword",
database = "studytonight"
)
cursor = db.cursor()
## creating the 'students' table with the 'PRIMARY KEY'
cursor.execute("CREATE TABLE students (name VARCHAR(255), rollno INTEGER(100) NOT NULL AUTO_INCREMENT PRIMARY KEY, branch VARCHAR(255), address VARCHAR(255))")
如果上面的代码运行没有错误,那么这意味着您已经成功地创建了一个名为“学生”的表,列 rollno 作为主键。
Python MySQL -描述表格
我们可以使用下面的 python 代码来描述任何一个表,看看它有哪些列,以及关于该表及其所有列的所有元信息。
import mysql.connector as mysql
db = mysql.connect(
host = "localhost",
user = "yourusername",
passwd = "yourpassword",
database = "studytonight"
)
cursor = db.cursor()
cursor.execute("DESC students")
print(cursor.fetchall())
输出将是:
[('name ',' varchar(255)',' YES ','',None ''),(' rollno ',' int ',' no ',' PRI ',None,' auto_increment '),(' branch ',' varchar(255)',' YES ','',None ' ','),(' address ',' varchar(255)',' YES ',' ',None ' ')]
向现有表添加主键
现在在这个例子中,我们假设学生表中不存在rollino列。所以我们将学习如何在现有的表中添加一列作为作为主键。
让我们看看如何在现有表上创建主键。其代码片段如下所示:
import mysql.connector as mysql
db = mysql.connect(
host = "localhost",
user = "root",
passwd = "himaniroot@99",
database = "studytonight"
)
cursor = db.cursor()
## We are going to add rollno field with primary key in table students
cursor.execute("ALTER TABLE students ADD COLUMN rollno INT AUTO_INCREMENT PRIMARY KEY")
print(cursor.fetchall())
在上面的代码中,我们使用了 SQL Alter Query 向现有的表中添加一个新的列。您可以使用 description table 查询来查看新添加的列。
[('name ',' varchar(255)',' YES ','',None ''),(' branch ',' varchar(255)',' YES ','',None ' ','(' address ',' varchar(255)',' YES ',' ',None ' ','),(' rollno ',' int ',' no ',' PRI ',None,' auto_increment')]
因此,在本教程中,我们已经介绍了与用 python 创建新的 MySQL 表相关的所有内容。我们介绍了如何更改表,如何列出数据库中的所有表,以及如何将列定义为主键。