数据库

数据库

SQL注入问题

​ 输入用户名: xxxx ‘ or 1=1 #

​ 输入密码: xxxx

select * from user where name = 'xxxxx ' or 1=1 # ' and password = 'sssdsd'

产生的原因:

​ 因为过于相信用户输入的内容,根本没有做任何的检验

解决方法:

​ sql = 'select * from user where name=%s and password=%s'

​ cursor.execute(sql,(user,pwd))

连接:

conn = pymysql.connect(host='127.0.0.1',user='root',password = '123',database ='数据库名',charset = 'utf8')
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)

查:

fetchall()    #取出所有的数据 返回的是列表套字典
fetchone()	  #取出一条数据 返回的是字典
fetchmany(size)  #取出size条数据 返回的是列表套字典

增:

sql = 'insert into user(name,password) values(%s,%s)'
#cursor.execute(sql,('xxx','qqq'))
data = [
    ('zzzz','sss'),
    ('wwww','pppp')
]
cursor.executemany(sql,data)
conn.commit()

改:

sql = 'update user set name=%s where id = %s'
cursor.execute(sql,('sss',2))
conn.commit
cursor.close()
conn.close()

删:

sql = 'delete from user where id = %s'
cursor.execute(sql,(2,))
conn.commit()
cursor.close()
conn.close()

索引

为什么使用索引以及索引的作用

​ 使用索引就是为了提高查询效率

类比: 查询字典目录

索引的本质:

​ 一个特殊的文件

索引的底层原理:

​ B+数

索引的种类

主键索引: 加速查找 + 不能重复 + 不能为空

唯一索引: 加速查找 + 不能重复

​ 联合唯一索引 unique(name, email)

普通索引: 加速查找

​ 联合索引: index(name,email)

索引的创建

#主键索引
create table xxx(
	id int auto_increment , 
    primary key (id)
)
alter table xxx change id id int auto_increment prinmary key ;
alter table xxx add primary key (id);
#删除主键
alter table t1 drop primary key ;

#唯一索引:
增加:
    1.
    create table t2(
    id int auto_increment primary key,
    name varchar(32) not null default '',
    unique u_name (name)
    )charset utf8 
    2. create unique index 索引名 on 表名(字段名)
    3.alter table t2 add unique index 名称 (字段)
    删除:
    alter table t2 drop u_name
    
    
    普通索引
    1.
   create table t3(
      id int auto_increment primary key ,
      name varchar(32) not null default '' ,
      index u_name(name)
   )
	2. create index 索引名 on 表名 (字段);
    3. alter table t3 add index 名称 (字段)
    

组合索引最左原则

explain 查询语句的匹配原则 索引长度越短越好

慢查询

show variables like '%slow%'

show variables like '%long%'

set global 变量名 = 值

posted @ 2019-10-31 21:39  小凯子+  阅读(165)  评论(0编辑  收藏  举报