数据库操作记录

Ikko Lv3

老是忘记数据库许多基本操作,开个贴记录一下

数据库一些基本操作

truncate table
删除一个表中所有的数据
注意truncate删除数据后是不可以rollback的。

从数据库一列提取无重复数据插入另一张表

1
2
INSERT INTO search_policygrade (policy_grade)
SELECT DISTINCT policy_grade FROM search_policy;

python:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pymysql

cnx = pymysql.connect(
user='root', password='.', host='', database='search')
cursor = cnx.cursor()

query = ("SELECT DISTINCT policy_grade FROM search_policy")

cursor.execute(query)
result = cursor.fetchall()


print(result)
result_list = []
for record in result:
result_list.append(record[0])
print(result_list)
  • Title: 数据库操作记录
  • Author: Ikko
  • Created at : 2023-03-21 22:31:38
  • Updated at : 2023-04-06 11:23:33
  • Link: https://redefine.ohevan.com/2023/03/21/数据库/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments