正则表达式用于模糊查询,模糊查询已经讲过了
like 仅支持 % 和 _ 远没有正则表达式灵活
当然绝大多数情况下 like足够使用
#语法select *from table where name regexp "正则表达式";#实例#准备数据create table emp (id int,name char(10),sex char,age int,dept_id int,job char(10),salary double);insert into emp values(1,"刘备","男",26,1,"总监",5800),(2,"张飞","男",24,1,"员工",3000),(3,"关羽","男",30,1,"员工",4000),(4,"孙权","男",25,2,"总监",6000),(10,"刘备2","男",26,2,"总监",5800),(5,"周瑜","男",22,2,"员工",5000),(6,"小乔","女",31,2,"员工",4000),(7,"曹操","男",19,3,"总监",10000),(8,"司马懿","男",24,3,"员工",6000);mysql> select *from emp where name regexp(‘司*‘);+------+-----------+------+------+---------+--------+--------+| id | name | sex | age | dept_id | job | salary |+------+-----------+------+------+---------+--------+--------+| 8 | 司马懿 | 男 | 24 | 3 | 员工 | 6000 |+------+-----------+------+------+---------+--------+--------+mysql> select *from emp where name regexp(‘懿$‘);+------+-----------+------+------+---------+--------+--------+| id | name | sex | age | dept_id | job | salary |+------+-----------+------+------+---------+--------+--------+| 8 | 司马懿 | 男 | 24 | 3 | 员工 | 6000 |+------+-----------+------+------+---------+--------+--------+
MYSQL 是一个tcp 服务器,用于操作服务器上的文件数据,接收用户端发送的指令, 接收指令时需要考虑安全问题
atm 购物车中的用户认证和mysql的用户认证原理是一样的,mysql中把文件称为表
在mysql自带的mysql数据库中有4个表用于用户管理的
优先级从高到低,分别是: user -> db -> tables_priv -> columns_priv
注:对用户管理进行操作后,要使用flush privileges;刷新
#语法create user 用户名@‘客户端地址‘ identified by ‘密码‘;#实际操作(在本地上操作)mysql> create user xcq@‘localhost‘ identified by ‘123‘;Query OK, 0 rows affected (0.13 sec)#查看是否创建完成mysql> use mysqlmysql> select *from user \G;************************** 4. row *************************** Host: localhost User: xcq Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257#但此时的用户没有任何的权限
#语法grant 全部[all]/权限名称[select(字段名),updata(字段名),alter(字段名),delete(字段名)] on 数据库名.表名 to 用户名@‘主机地址‘;#可以访问 所有库和表grant all on *.* to xcq@"localhost" identified by "123";#可以访问day42库的所有表grant all on day42.* to xcq@"localhost" identified by "123"; #可以访问day42库的emp表grant all on day42.emp to xcq@"localhost" identified by "123"; #仅能查看和添加 day42库的emp表中的 id和name字段grant select(id,name),insert(id,name) on day42.emp to xcq@"localhost" identified by "123";#all可以给用户添加除了grant(赋予权限)以外的所有权限#如果想把拥有的权限赋予别人#使用 with grant option #如果授权时,被授权的用户不存在,直接创建用户grant 全部[all]/权限名称[select(字段名),updata(字段名),alter(字段名),delete(字段名)] on 数据库名.表名 to 用户名@‘主机地址‘ with grant option;
#语法revoke 权限的名称 on 数据库名.表名 from 用户名@‘主机地址‘;#实例revoke all on *.* from xcq@‘localhost‘;
#语法 drop user 用户名@"主机地址";#实例drop user xcq@‘localhost‘;
python编写的mysql客户端
pip install pymysql
import pymysql#1.链接数据库conn=pymysql.Connect( host=‘127.0.0.1‘,#服务器地址 user=‘root‘,#用户名 password=‘root‘,#密码 database=‘day41‘,#数据库名称 port=3306,#端口号,可选 charset=‘utf8‘#编码,可选)#2.获取游标对象cursor=conn.cursor(pymysql.cursors.DictCursor)#3.sql语句sql=‘select *from emp‘#4.执行语句res=cursor.execute(sql)#返回执行的行数print(res)# fetchall\fetchone\fetchmany 获取查询结果# print(cursor.fetchall())#全部取出来# print(cursor.fetchone())#每次取一个,然后游标往下一位# print(cursor.fetchone())#每次取一个,然后游标往下一位## print(cursor.fetchmany(1))#取的时候指定行数# print(cursor.fetchall())#将剩下全部取出来#scroll 游标移动# cursor.scroll(1,‘relative‘)#个数与模式。relative相对位置;absolute绝对位置#5.关闭游标cursor.close()#6.关闭链接conn.close()
import pymysql#1.链接数据库conn=pymysql.Connect( host=‘127.0.0.1‘,#服务器地址 user=‘root‘,#用户名 password=‘root‘,#密码 database=‘day41‘,#数据库名称 port=3306,#端口号,可选 charset=‘utf8‘#编码,可选)#2.获取游标对象cursor=conn.cursor(pymysql.cursors.DictCursor)#3.sql语句sql=‘insert into emp value(15,"黄月英","女","市场","员工",4500)‘#4.执行语句res=cursor.execute(sql)#返回执行的行数print(res)cur=cursor.fetchall()for i in cur: print(i)#撤销数据,只要数据还没提交,都可以撤销#conn.rollback()#提交数据,因为pymysql 默认是启用事务,对记录操作的sql语句如果不提交,不会执行。#但是对于库和表得操作会默认提交conn.commit();#5.关闭游标cursor.close()#6.关闭链接conn.close()