1. 程式人生 > >mysql SQL相關知識

mysql SQL相關知識

目的 過程 del rep date操作 誤操作 oracl 幫助 current

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;

mysql SQL相關知識