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)

) ;

1)按列名插入

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

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

 4)删除数据

2.备份数据库

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

 3.select(研发优化,最好不用*)

select from where

限制返回行数(默认从第0行开始):

关联表select:

 

 4.explain查询SQL执行状态

 5.修改表中数据

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

如果update时没有指定where条件,则整个表都会被更新,此时则要做数据恢复,可能会丢失新的数据,则需执行增量恢复:

mysql -uroot -p456 oldboy </opt/oldboy_bak.sql

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

此参数改完需重启数据库:

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

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

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

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

cp mysqlbin_oldboy.000001 /opt/

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

3)恢复

mysql -uroot -p456 oldboy </opt/oldboy_bak.sql

此时在数据库外面可查看恢复情况:

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

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

mysqlbinlog mysql

mysqlbinlog -d oldboy mysqlbin_oldboy.00001 >bin.sql

mysql -uroot -p456 oldboy <bin.sql

防止数据库误操作:

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程序就会拒绝执行

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
 

提示:不加条件无法删除,目的达到。

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程序拒绝执行

6.删除表中数据

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

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

7.增删改表的字段

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

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

修改:change、modify

rename table test to test1;

猜你喜欢

转载自www.cnblogs.com/wangke2017/p/9640906.html