-
索引
""" 索引结构 树 线 平衡树(Balance Tree,B Tree) 不一定是二叉树 b+树 在叶子接待你中有一个双向链表指针,查找快 两种索引的差别 聚集索引(聚簇索引) Innodb 必有且仅有一个(主键) 非聚(簇)集索引 辅助索引 Innodb myisam innodb中主键默认创建聚集索引 索引的创建与删除 创建主键 primary key 聚集索引 + 非空 + 唯一 创建唯一约束 unique 辅助索引 + 唯一 创建一个普通索引 create index 索引名 on 表名(字段名); drop index 索引名 on 表名; 正确使用索引 1.查询的条件字段不是索引字段,对哪一个字段创建了索引,就用那个字段作为查询条件 2.创建索引的时候应该对区分度比较大的列进行创建 1/10一下的重复率比较适合创建索引 名字/id等 3.范围 范围越大越慢 范围越小越快 kike 'a%' 快 kike '%a' 慢 4.条件列参与计算 慢, 也不能使用函数 5.and/or id name id = 1000000 and name = 'eva' id = 1000000 or name = 'eva' 不会命中索引 多个条件的组合,如果使用and链接 其中一列含有索引,都可以加快查找速度 如果使用or连接 必须所有的列都含有索引,才能加快查找速度 6.联合索引:最左前缀原则(必须带着最左边的列做条件,从出现范围开始整条索引失效) create index ind_mix on s1(id,name,email); select * from s1 where id = 1900000 and name='eva' and email='[email protected]'; select * from s1 where id>10000 and name='eva' and email='[email protected]'; 不行 """
-
pymysql
""" pymysql 连接数据库 获取游标 执行sql(增删改查) 如果设计到修改:提交 关闭游标 关闭库 sql注入 传参数,注意sql注入问题,传参数通过execute方法来传 cur.execute(sql, (user, pwd)) 事务 锁:有多个人去修改数据库中表的内容,防止有时间差而没有实时性 加行级锁 begin select * from emp where id = 1 for update; # 加行级锁 update emp set salary = 1000 where id = 1; # 完成更新 commit begin后其他人不能对正在操作的表做任何操作,直到commit后才可以。 """ import pymysql conn = pymysql.connect(host='localhost', user='root', password="", database='laowang') cur = conn.cursor() # cursor游标,默认为元组 # cur = conn.cursor(cursor=pymysql.cursors.DictCursor) # 后边拿到的结果都变字典类型的数据 # 查 # cur.execute('select * from employee;') # 加不加分号都可以 # ret = cur.fetchone() # print(cur.rowcount) # print(ret) # 一次查一个 没有了返回None # print('*' * 20) # ret2 = cur.fetchmany(10) # 拿10个 # print(ret) # ret3 = cur.fetchall() # 拿全部 # print(ret3) # 增 删 改 最后要commit才行 # try: # cur.execute("insert into t1 values(5,'小王','female','喝酒,烫头')") # conn.commit() # 提交后才行 # except Exception as e: # print(e) # conn.rollback() # 出问题后try中代码相当于没执行 # 实际操作mysql的时候遇到的问题 # 结合数据库和python写一个登录 # username password user = input('username:') pwd = input('password:') # sql = f'select * from userinfo where user = "{user}" and password = "{pwd}"' # 这样品hi出现注入问题 sql = f'select * from userinfo where user = %s and password = %s' print(sql) # cur.execute(sql) cur.execute(sql, (user, pwd)) # 这样写,交给sql去执行,不会出现注入问题,注意是元组传入 print(cur.fetchone()) # sql注入 # select * from userinfo where user = "dasdad" or 1=1;-- " and password = "sadas"; # 同样能得到(1, 'xiaoli', '23456') cur.close() conn.close()
-
备份和恢复和小练习
""" 数据库的逻辑备份 表和数据备份 在cmd下直接执行 mysqldump -h 服务器 -u用户 -p密码 数据库名 > 备份文件.sql; 在mysql中切到备份的数据库中执行 source 备份文件.sql; # 恢复所有的东西 备份库 mysqldump -uroot -p --databases homework > 备份文件.sql source 备份文件.sql 作业: 写一个注册登录 基于数据库 加上hashlib密文验证 create table userinfo( id int primary key auto_increment, user_pwd char(64) ); """ # 作业 import pymysql import hashlib def conn_mysql(host, user, password, database): con = pymysql.connect(host=host, user=user, password=password, database=database) return con def get_md5(username, password): m = hashlib.md5() m.update(username.encode('utf-8')) m.update(password.encode('utf-8')) return m.hexdigest() def login(username, password): # 注册不需要加行级锁了,修改密码才加 user_pwd = get_md5(username, password) conn = conn_mysql(host='localhost', user='root', password="", database='laowang') cur = conn.cursor() sql = "select * from userinfo where user_pwd=%s" cur.execute(sql, (user_pwd,)) if cur.rowcount == 1: cur.close() conn.close() return '欢迎登录!' else: cur.close() conn.close() return '登录失败!' def register(username, password): user_pwd = get_md5(username, password) conn = conn_mysql(host='localhost', user='root', password="", database='laowang') cur = conn.cursor() sql = "insert into userinfo(user_pwd) values(%s)" # id auto_increment try: cur.execute(sql, (user_pwd,)) conn.commit() except Exception as e: print(e) conn.rollback() if cur.rowcount == 1: cur.close() conn.close() return '注册成功!' else: cur.close() conn.close() return '注册失败!' if __name__ == '__main__': while True: way = int(input('1:登录;2.注册;3.推出。\n>>>')) if way == 3: print('谢谢!') break user = input('请输入用户名:') pwd = input('请输入密码:') if way == 1: print(login(user, pwd)) elif way == 2: print(register(user, pwd)) """ 1:登录;2.注册;3.推出。 >>>2 请输入用户名:老王 请输入密码:abc 注册成功! 1:登录;2.注册;3.推出。 >>>1 请输入用户名:老王 请输入密码:abc 欢迎登录! 1:登录;2.注册;3.推出。 >>>3 谢谢! mysql> select * from userinfo; +----+----------------------------------+ | id | user_pwd | +----+----------------------------------+ | 1 | d8e61f86ea4726c3c85613002c4191e7 | +----+----------------------------------+ 1 row in set (0.00 sec) """
mysql---索引、pymysql、备份和恢复
猜你喜欢
转载自blog.csdn.net/qq_31910669/article/details/109311090
今日推荐
周排行