mysql相关知识_mysql SQL相关知识

1.insert介绍

insert into (字段名) values (值)

例:

create table test (

id int(4) not null auto_increment,

name char(20) not null,

primary key(id)

) ;

f5990a1c0c3cd49bff509ae6cdf93328.png

1.1)按列名插入

a17c561e49a6766709e72c7673930b18.png

82d51aa054f4a1bda75734ed7a2a5fe5.png

1.2)不指定列名,则需按顺序插入

1.3)支持批量插入(可作为研发优化点)

2f3fcf58eedd5eb40e5d413bafc26eee.png

1.4)删除数据

ec9eddda0a0c9bb098e846250386feac.png

46ff884bd7dec95dc414a1623f3c7eab.png

2.备份数据库dump介绍

57328a0e2cc37320458a7497e94f9c2d.png

dump把数据库操作以SQL语句保存下来,理论上是逻辑备份

780a525bfecf9476c1786c69aee92c0e.png

d2cb77b93c1ff576be31c0f1e6a4a8e5.png

3.select介绍

select XXX from XXX where XXX                        #研发优化,最好不用*

b5dd46e6ff573d631c33c60c5468cf46.png

查询时限制返回行数                                       #默认从第0行开始

5f2791d019a7fc3028a27b901963dc34.png

a336c5ab947aae5bcad1c14879aa3bbe.png

关联表select:

4ceba0116e3fa9ee7d42b2208d06a35d.png

83c223bdc029a984b68f59dcf3997420.png

c45f651d5d8fa069b88ba6f0c1fe820c.png

193a4a92d72db2b45a64cdf185148b7c.png

0992c6c337d35fd05212356e04d05a3b.png

3a9e2bca71ed11e0d9c4c66901321fe2.png

1ead441da5a34b42a0635d205798b277.png

15489ba3380559d194376e9830fbc88f.png

4.explain查询SQL执行状态

a9493bf292c84d6230dff32777ccafba.png

dc9ec12ca0ce3f2be268f963f3421bf6.png

5.修改表中数据update介绍

update 表名 set 字段=新值,...where 条件()

b0f67bbdedaf868eb6ad2b722573d2de.png

补充:如果update时没有指定where条件,则整个表都会被更新,此时则要通过备份做数据恢复,可能会丢失新的数据,对于此部分则需执行增量恢复

#对于全量及增量备份恢复在后续章节会有详细介绍,此篇博客只做部分引入

mysql -uroot -p456 oldboy

执行增量恢复的条件,log-bin需打开,生产中需对此文件备份

ddebead521cd2fea19edb927767b4d5b.png

此参数改完需重启数据库

cbb6f6b1c6b8e4c03b3f7a1161a951df.png

做一次Update操作后,会生成文件

a3eb19e68329b6f20d402258e694c650.png

该文件是二进制文件,需用mysqlbinlog打开,即可以看到相关的update语句

8dfc1e74d817b5d3b68504d13b5d569a.png

若想恢复到此binlog文件时的数据库状态,修复过程简易版

1)将该文件从data目录下备份到其他目录:

cp mysqlbin_oldboy.000001 /opt/

2)mysqladmin -uroot -p456 flush-log 切割,可以看到data下此时变成两个binlog文件

92cc7ced2f10ff007c987ba2df4746d3.png

3)恢复

mysql -uroot -p456 oldboy

此时在数据库外面可查看恢复情况                              #通过mysql -e来实现

mysql -uroot -p456 -e "select * from oldboy.test";

176bcfcd6cb42a94affff183ea7edc59.png

此时恢复的数据库应缺少dump到发生错误操作之间的数据,要想恢复到00001文件的时间点:

mysqlbinlog mysql

mysqlbinlog -d oldboy mysqlbin_oldboy.00001 >bin.sql

mysql -uroot -p456 oldboy

6.删除表中数据

delete from 表名 where 条件;                              #相当于逻辑清除,按行删

truncate table 表名;                                             #清空表中所有内容,比delete更快,相当于清空物理文件

7.增删改表的字段

alter table 表名 add字段 类型 其他;

如:alter table test add age int(4) after name;

1b5a1176c8f1c4b164ab21b6c447790c.png

修改:change、modify

rename table test to test1;

4448f9f79fa65df4ab2e02efee2720d7.png

8.防止数据库误操作:

8.1)mysql帮助说明

[oldboy_c64 ~]# mysql --help|grep dummy

-U, --i-am-a-dummy Synonym for option --safe-updates, -U.

i-am-a-dummy FALSE

#在mysql命令加上选项-U后,当发出没有WHERE或LIMIT关键字的UPDATE或DELETE时,mysql程序就会拒绝执行8.2)指定-U登录测试                         #提示:不加条件无法删除,目的达到。

[oldboy_c64 ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -U

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 14

Server version: 5.5.32-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> delete from oldboy.student;

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

mysql> quit

Bye

8.3)做成别名防止老大和DBA误操作

[oldboy_c64 ~]# alias mysql='mysql -U'

[oldboy_c64 ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 15

Server version: 5.5.32-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> delete from oldboy.student;

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

mysql> delete from oldboy.student where Sno=5;

Query OK, 1 row affected (0.02 sec)

mysql> quit

Bye

[oldboy_c64 ~]# echo "alias mysql='mysql -U'" >>/etc/profile

[oldboy_c64 ~]# . /etc/profile

[oldboy_c64 ~]# tail -1 /etc/profile

alias mysql='mysql -U'

结论:在mysql命令加上选项-U后,当发出没有WHERE或LIMIT关键字的UPDATE或DELETE时,mysql程序拒绝执行

2018年10月30日

祝好!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值