子查询mysql
将一个查询的结果做为下一次查询的条件或原数据(又称内查询)正则表达式
当你的需求一次查询没法知足需求时(一次select找不到你要的数据)就要用到子查询sql
子查询能干的是,多表联查也能干数据库
正则表达式匹配安全
关键字 regexp服务器
select *from emp where name like "刘%"tcp
select *from emp where name regxp "司.*光$";ide
mysql用户管理fetch
mysql是一个tcp服务器 用于操做服务器上的文件数据,编码
接受用户端发送的指令,接受指令时须要考虑安全问题
mysql把文件称为表
在mysql自带的mysql数据库中有4个表用于用户管理
分别是:优先级从高到低
user ->db ->tables_priv -> columns_priv
create user 用户名@"主机地址" identified by "密码"; create user scote@"127.0.0.1" identified by "123"; 此处的主机地址 不是服务器地址 而是表示 这个帐户能够在那台电脑上登陆
语法: grant [权限的名称 select insert.... | all ] on 数据库.表名 to 用户名@主机地址; # 授予 scote 这个用户全部权限 在全部数据库全部表中 grant all on *.* to scote@"localhost"; 能够访问 全部库和表 grant all on day41.* to scote@"localhost"; 能够访问day41库的全部表 grant all on day41.stu to scote@"localhost"; 能够访问day41库的stu表 grant select(id,name),insert(id,name) on day41.stu to scote@"localhost"; 仅能查看和添加 day41库的stu表中的 id和name字段 grant all on mydb1.* to testDBA@"%" identified by "123"; 3.grant [权限的名称 select insert.... | all ] on 数据库.表名 to 用户名@主机地址 with grant option; with grant option 这个用户能够将他有的权限授予别的帐户 特色: 若是受权时 用户不存在 直接自动建立用户
revoke 权限的名称 on 数据库.表名 from 用户名@"主机名" ; revoke all on *.* from scote@"localhost"; update mysql.user set Grant_priv = "N" where user ="scote" and host = "localhost"; *.刷新权限表 flush privileges;
drop user 用户名@"主机地址";
import pymysql """pymysql使用步骤 核心类 Connect连接用 和Cursor读写用 1.与数据库服务器创建连接 2.获取游标对象 (用于发送和接收数据) 3.用游标执行sql语句 4.使用fetch方法来获取执行的结果 5.关闭连接 先关游标 再关连接 游标的经常使用方法 1.建立游标 conn.cursor(指定查询结果的数据类型) 2.excute 执行sql 3.fetchone(当sql只有一条记录时) many(sql有多条而且须要指定条数) all(多条) 4.scroll 用于修改游标的当前位置 注意: pymysql 默认不提交修改 可是注意(指的是对表中记录的操做不提交) 像删库 删表 是没法撤销的 """ # 建立连接获得一个连接对象 conn = pymysql.Connect( host="127.0.0.1", # 数据库服务器主机地址 user="root", # 用户名 password="admin", # 密码 database="day42", #数据库名称 port=3306, # 端口号 可选 整型 charset="utf8" # 编码 可选 ) # 获取游标对象 pymysql.cursors.DictCursor指定 返回的结果类型 为字典 默认是元祖类型 cursor = conn.cursor(pymysql.cursors.DictCursor) # 查询数据 sql = "select *from emp" # 执行sql 若是是select 语句返回的是 查询的条数 res = cursor.execute(sql) print(res) # 获取查询的结果 # print(cursor.fetchall()) # print(cursor.fetchone()) # print(cursor.fetchone()) # print(cursor.fetchmany(1)) # print(cursor.fetchall()) # scroll print(cursor.fetchone()) cursor.scroll(-1) print(cursor.fetchall()) # 关闭连接 cursor.close() conn.close()
commit 提交修改
由于pymysql模块默认是启用事务的,sql语句若是不提交 至关于没有执行
roback 回滚,撤销数据修改