主题:mysql表格已经创建好后,索引的操作
1.查看索引:
show index from 表名;
2.创建索引:
create index 索引名 on 表名(字段名(长度));
3.删除索引:
drop index 索引名 on 表名;
代码示意如下:
import pymysql
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='mysql',
db='testdb',
charset='utf8'
)
cursor = conn.cursor()
# 查看索引
# show index from 表名;
sql = 'show index from school_student;'
cursor.execute(sql)
print('索引名:', cursor.fetchall())
# 创建索引
# create index 索引名称 on 表名(字段名称(长度));
creat_index_sql = 'create index index_name on school_student(name);'
cursor.execute(creat_index_sql)
conn.commit()
# 删除索引
# drop index 索引名 on 表名;
drop_index_sql = 'drop index index_name on school_student;'
cursor.execute(drop_index_sql)
conn.commit()
-------------------------------------------END---------------------------------------------