mysql常用函数和变量

mysql常用函数和变量

use mysql;
alter user “root”@“localhost” identified by “123”;

use mysql;
alter user “root”@“localhost” identified by “123”;
flush privileges;

SET PASSWORD Statement MySQL 5.7

GRANT Statement MySQL 8.0

MySQL 8.0 CREATE USER Statement

mysqladmin.exe -uroot -p原密码 password “新密码”

SET PASSWORD FOR ‘jeffrey’@‘localhost’ = ‘auth_string’;(高版本)

ubuntu下MySQL忘记密码重置方法

知道原密码的情况下:
低版本:
update mysql.user set password=password(‘新密码’) where user=’root’ and host=’127.0.0.1’; flush privileges; exit

高版本:
use mysql;
alter user "root"@"localhost" identified by "123";
flush privileges;


alter user "root"@"%" identified by "pentestroot";
注意:root用户对应的主机,

不知道密码的情况下:
1、找到mysql的配置文件 my.ini或者my.cnf注意系统之间的区别
2、打开该配置文件,在【mysqld】下面添加:skip-grant-tables
3、开启mysql服务,免密登录,修改密码
4、退出,删掉配置文件中skip-grant-tables
5、正常使用数据库~~~

添加账号并授权
create user “用户”@“主机IP” identified by “密码”;
主机IP:限源访问,mysql只能让指定的IP来访问
%:没有限制

grant 权限 on 库名.表名 to "用户"@"主机IP"
权限:insert  drop   update  select all 


grant insert,drop on *.* to "icq"@"192.168.179.100";
grant all on *.* to "icq"@"192.168.179.100";

数据库外连

知道原密码的情况下:
低版本:
update mysql.user set password=password(‘新密码’) where user=’root’ and host=’127.0.0.1’; flush privileges; exit

高版本:
use mysql;
alter user "root"@"localhost" identified by "123";
flush privileges;


alter user "root"@"%" identified by "pentestroot";
注意:root用户对应的主机,

不知道密码的情况下:
1、找到mysql的配置文件 my.ini或者my.cnf注意系统之间的区别
2、打开该配置文件,在【mysqld】下面添加:skip-grant-tables
3、开启mysql服务,免密登录,修改密码
4、退出,删掉配置文件中skip-grant-tables
5、正常使用数据库~~~
添加账号并授权
create user “用户”@“主机IP” identified by “密码”;
主机IP:限源访问,mysql只能让指定的IP来访问
%:没有限制

grant 权限 on 库名.表名 to "用户"@"主机IP"
权限:insert  drop   update  select all 


grant insert,drop on *.* to "icq"@"192.168.179.100";
grant all on *.* to "icq"@"192.168.179.100";

数据库外连
update user set host="%" where user=“root” and host=“localhost”
update user set host="%" where user=“icq” and host=“192.168.179.100”

flush privileges;
重启数据库服务器

修改配置文件,将bind-address 127.0.0.1 注释掉

select version();
select user();查看当前用户
select @@version;
select @@hostname;主机名
select @@tmpdir;查看临时目录
select @@basedir;数据库服务所在的位置
select @@datadir;数据库文件所在位置
H:\phpStudy\PHPTutorial\MySQL\data存放的文件的类型:
MYD:存放数据的地方
FRM:存储表结构
MYI:存储当前数据的配置信息和索引信息
select mid(字符串,开始位置,结束位置);截取字符串
select substr(“123”,1,2);同上
select substring(“123”,1,2);同上
select ord();显示ASCII
select concat(“123”,“456”);拼接字符串
select sleep(5);让数据库等待5s
select concat_ws(分隔符,字符串1,字符串2~~~~~);
select group_concat();分组并拼接

数据类型

int整型

bigint大整型

float浮点型

date日期

time时间

char字符

varchar不定长字符(可变字符/边长字符)

text文本

double双精度浮点数

bit比特

tinyint0/1

创建表:

create table 表名(字段1 数据类型, 字段2 数据类型, 字段3 数据类型);

主键:在表中能够代表某个事物的身份对应的字段。主键是唯一的。

create table stu(id primary key auto increasement not null, name varchar(50),phoneNumber bigint(11),gender tinyint) DEFAULT CHARSET=utf8;

在创建表的时候给出的数据类型要有长度。

CREATE TABLE stu(
stuid  bigint(20),
stuname VARCHAR(50),
stuClass VARCHAR(20),
gender TINYINT
);

CREATE TABLE teacher(
uid int(10),
tename VARCHAR(50),
teachClass VARCHAR(50),
gender TINYINT
);

约束

primary key主键

CREATE TABLE demo(
id int,
pname VARCHAR(50),
cname VARCHAR(100),
PRIMARY KEY (id) -- 给id设置主键
);

auto_increment自增

not null不为空

unique唯一标识数据库每个记录

CREATE TABLE demo1(
id int not null auto_increment, -- id不为空,且自增
pname VARCHAR(50),
cname VARCHAR(100),
PRIMARY KEY (id) -- 给id设置主键
UNIQUE (id)
);

注意要把约束放在数据类型后面

后续修改添加主键

ALTER TABLE stu add PRIMARY KEY (stuid);

使用表:

use table_name;
insert into stu(stuid, stuname, stuClass, gender) value(201602731,"demo","class-1",1);

查询数据

select 字段1,字段2 from 表名;
select 字段1,字段2,字段3 from 表名 where 字段名=指定值; (有条件)
select 字段1,字段2,字段3 from 表名 where 字段名=指定值 and 字段名2=指定值2; (有条件)

SELECT stuname from stu where gender=0 or stuClass="class-1";

限制查询

注意,limit a, b中a最大不超过表的记录数。

select * from stu LIMIT 1,1;
limit a, b 表示从第a行开始,选择b行的数据出来
limit b 表示从第0行开始,选择出b行的数据

更新

update 表名 set 字段1=新值,字段2=新值(更新所有的字段)

update表示 set 字段1=新值 where 条件;

UPDATE stu set stuname = "hacker" WHERE stuid="201503648";

drop table 表名;
delete from table; 删除表中所有内容

模糊查询

like

通配符:%任意字符,_单个字符

select * from stu where name like "hack%";

SELECT * FROM stu WHERE stuname LIKE "hack__";

联合查询

查询1 union 查询2

注意:两个查询出来的结果字段必须一样,否则报错

SELECT * FROM stu WHERE stuname ="hacker" union select 1,2,3,4;

SELECT * FROM stu WHERE stuname ="hacker" union select 1,2,3;

SELECT * FROM stu WHERE stuname ="hacker" union select 1,2,3 1222 - The used SELECT statements have a different number of columns 时间: 0.001s

select stuid, stuname FROM stu union select pname, cname from midori;

删除字段

alter table 表名 drop 字段名

ALTER TABLE stu drop gender;

添加字段

alter table 表名 add 字段名 数据类型

ALTER TABLE stu ADD xingbie int;

修改表名

alter table 原表名 rename to 新表名;

PHP连接mysql的方式

  1. mysql (PHP<=5.5) 2012年开始不建议使用该扩展,安全性差

  2. mysqli 对mysql的升级版,安全性高,只能用于操作mysql数据库

    • 面向对象的使用方式
    • 面向过程的使用方式
  3. PDO (PHP data object)

    目前支持连接的数据库类型有12中,安全性高,基于面向对象的使用方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值