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;
来源: http://www.bubuko.com/infodetail-2769600.html