数据库基础3
DCL 语句
数据库控制语句
- 授权
- 删除权限
grant
grant 权限 on 库.表 to '用户'@'主机' identified by '密码' with grant option;
权限: select insert update delete * #*代表所有的权限
库: * 所有的库 指定的库名
表: *所有的表 指定的表名
主机: ip地址 % 任何地址
用户不存在 新建用户 存在的化就是授权
注意: 操作之前一定要先 use mysql;
flush privileges;
mysql> grant select,insert on myfirst_database.users to 'shiyang'@'%' identified by '123456' with grant option;
mysql -u shiyang -p
123456
revoke
use mysql;
revoke 权限 on 库.表 from '用户'@'主机';
flush privileges;
mysql> revoke insert on myfirst_database.users from 'shiyang'@'%';
MySQL 忘记密码
C:\ProgramData\MySQL\MySQL Server 5.7\
修改 my.ini #复制到别的地方 改完替换
[mysqld]
skip-grant-tables
保存 替换
net stop mysql57
net start mysql57
mysql -u root -p #两次回车不需要输入密码即可进入数据库
mysql> update mysql.user set authentication_string=password('你的密码') where user='root';
mysql> flush privileges;
再次修改 my.ini
[mysqld]
#skip-grant-tables #注释掉
重启mysql
net stop mysql57
net start mysql57
下次就是以新密码 进去了
更新语句
update 表名 set 字段1=值1,字段2=值2,字段n=值n where 条件;
mysql> update stars set username='小岳岳' where id=3; #修改一个字段
mysql> update stars set username='小岳岳',balance=123.456,province='中原' where id=3; #修改多个字段
两个表同时更新
update 表1,表2 set 字段1=值1,…,字段n=值n where 条件
mysql> update user u,order_goods o set u.tel=u.tel+o.buytime where u.uid=o.uid;
Query OK, 4 rows affected (0.02 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> update user u,order_goods o set u.tel=u.tel-o.buytime,o.buytime=123 where u.uid=o.uid;
Query OK, 8 rows affected (0.00 sec)
Rows matched: 8 Changed: 8 Warnings: 0
三张表 更新
mysql> update user u,order_goods o,stars s set u.tel=u.tel+s.balance,o.buytime=1111,s.age=s.age+10 where u.uid=o.uid and o.uid=s.id;
删除 语句
mysql> delete from users where id>=15; #删除指定的数据
mysql> delete from users; #不加where 条件 清空整张表
mysql> truncate table users; #清空整张表
delete from 清空表 再次插入数据 id 从原来的数据 往后自增
truncate table 清空表 再次插入数据 就从1 开始
alter table 表名 auto_increment=1; #delete from清空表以后 再次插入数据 id 从1开始
切记
- 删除的时候一定要加条件
- 删除之前务必备份
索引
为了提升数据查询的效率,缩小查询的范围 就像书的目录一样
- 普通索引 任何字段都可以添加的索引 没有任何限制
- 唯一索引 要求这个字段对应的数据行 每个数据都是唯一的 不能有重复值 年龄 性别这样的字段 不能创建唯一索引
- 主键索引 特殊的唯一索引 不能有空值 不能有重复值
- 全文索引 需要全局搜索的数据 NLP
- 复合索引 多个字段同时添加索引
? index
create index 不能创建主键索引
alter table
1.普通索引
alter table 表名 add index 名称(字段);
show index from 表名\G #列出该表所有的索引
mysql> alter table 表名 drop index 索引名称; #删除索引
create index 索引名 on 表名(字段); #创建索引
mysql> drop index in_name on user; #删除索引
2.唯一索引 unique
mysql> alter table test66 add unique un_name(tel);
show index from 表名\G #列出该表所有的索引
alter table 表名 drop index 索引名称;
3.主键索引
创建表的时候 我们一般设置主键
create table 表名(
id int(11) unsigned not null primary key auto_increment,
);
删除主键索引 先要把主键自增给他消除
alter table 表名 modify id int(11) unsigned not null;#消除主键自增
alter table 表名 drop primary key; #删除主键索引
alter table 表名 add primary key(id);#添加主键索引
alter table 表名 modify id int(11) unsigned not null auto_increment; #让主键自增
4.全文索引
mysql> ALTER TABLE stars add fulltext f_name(username);
show index from 表名\G
alter table 表名 drop index 索引名称;
https://www.iteye.com/blog/sg552-1560834
5.复合索引
mysql> alter table stars add index in_name(username,balance); #多个字段同时添加索引
Query OK, 9 rows affected (0.04 sec)
Records: 9 Duplicates: 0 Warnings: 0
索引是占空间的 并不是越多越好
什么时候 添加索引 ?
- where group by 、order by 条件常用的字段
内置函数
-
字符串
- concat() 拼接字符串
- lcase() 转成小写
- ucase() 转成大写
- length() 字符串长度
- ltrim() 去除左侧的空格
- rtrim() 去除右侧的空格
- repeat() 重复
- replace()替换
- substring(字符串,偏移量,截取的个数) 切片
- space() 空格
mysql> select concat('hello','world') as 口号; +------------+ | 口号 | +------------+ | helloworld | +------------+ 1 row in set (0.01 sec) mysql> select lcase('HELLO'); +----------------+ | lcase('HELLO') | +----------------+ | hello | +----------------+ 1 row in set (0.01 sec) mysql> select ucase('haha'); +---------------+ | ucase('haha') | +---------------+ | HAHA | +---------------+ 1 row in set (0.01 sec) mysql> select length('haha'); +----------------+ | length('haha') | +----------------+ | 4 | +----------------+ 1 row in set (0.01 sec) mysql> select ltrim(' haha'); +-------------------------+ | ltrim(' haha') | +-------------------------+ | haha | +-------------------------+ 1 row in set (0.00 sec) mysql> select concat(rtrim(' haha '),'world'); #去除右侧空格 +------------------------------------------------+ | concat(rtrim(' haha '),'world') | +------------------------------------------------+ | hahaworld | +------------------------------------------------+ 1 row in set (0.00 sec) mysql> select repeat('5201314',3); #重复3次 +-----------------------+ | repeat('5201314',3) | +-----------------------+ | 520131452013145201314 | +-----------------------+ 1 row in set (0.01 sec) mysql> select substr("python is so good",1,5); #从下标1开始截取5个 +---------------------------------+ | substr("python is so good",1,5) | +---------------------------------+ | pytho | +---------------------------------+ 1 row in set (0.00 sec) mysql> select concat(space(20),'haha'); +--------------------------+ | concat(space(20),'haha') | +--------------------------+ | haha | +--------------------------+ 1 row in set (0.01 sec)
-
数学
- bin() # 10进制转2进制
- ceiling() # 向上取整
- floor() #向下取整
- max()
- min()
- sqrt()
- rand()
mysql> select bin(99); +---------+ | bin(99) | +---------+ | 1100011 | +---------+ 1 row in set (0.01 sec) mysql> select floor(13.1); +-------------+ | floor(13.1) | +-------------+ | 13 | +-------------+ 1 row in set (0.01 sec) mysql> select ceiling(13.1); +---------------+ | ceiling(13.1) | +---------------+ | 14 | +---------------+ 1 row in set (0.00 sec) mysql> select sqrt(10); +--------------------+ | sqrt(10) | +--------------------+ | 3.1622776601683795 | +--------------------+ 1 row in set (0.00 sec) mysql> select rand(); +--------------------+ | rand() | +--------------------+ | 0.3009522643880474 | +--------------------+ 1 row in set (0.01 sec)
-
日期
now() #当前的日期加时间 year(date) # date日期坐在的年份 month(date) # date日期坐在的月份 week(date) # date日期坐在的第几周 unix_timestamp(date) #日期所在的时间戳 from_unixtime(时间戳) #时间戳转日期 mysql> select datediff('1998-10-20','2015-7-23'); #日期差额