1. 程式人生 > >mysql replace 命令的使用以及sql注入

mysql replace 命令的使用以及sql注入

本文從以下幾個方面介紹:replace into的應用場景;replace into的原理;replace into的應用注意事項,replace into的應用形式

 (1) MySQL replace into 是insert into 的增強版本,它主要用於以下場景:

在向表中插入資料的時候,經常遇到這樣的情況:1. 首先判斷資料是否存在; 2. 如果不存在,則插入;3.如果存在,則更新。

在 SQL Server 中可以這樣處理:

   if not exists (select 1 from t where id = 1)

      insert into t(id, update_time) values(1, getdate())

   else

      update t set update_time = getdate() where id = 1

那麼 mysql 中如何實現這樣的邏輯呢?彆著急 mysql中有更簡單的方法: replace into

replace into t(id, update_time) values(1, now());

replace into t(id, update_time) select 1, now();

(2)replace into原理

replace into 跟 insert 功能類似,不同點在於:replace into 首先嚐試插入資料到表中,

如果發現表中已經有此行資料(根據主鍵或者唯一索引判斷)則先刪除此行資料,然後插入新的資料。 2. 否則沒有此行資料的話,直接插入新資料。

(3)replace into的應用注意事項

1)插入資料的表必須有主鍵或者是唯一索引!否則的話,replace into 會直接插入資料,這將導致表中出現重複的資料。

2)如果資料庫裡邊有這條記錄,則直接修改這條記錄;如果沒有則,則直接插入,在有外來鍵的情況下,對主表進行這樣操作時,因為如果主表存在一條記錄,被從表所用時,直接使用replace into是會報錯的,這和replace into的內部原理是相關(ps.它會先刪除然後再插入)。

3)正確做法是- 即先刪除該條存在的資料,然後再次插入這條資料,這和外來鍵約束相悖呢,因此只能採用update和insert這樣的組合,來應對外來鍵約束

sql_select_1='''select * from one_and_two_stars where kn_id = %d ''' %( int(one_level_id))

            res_num_1= self.execute_kg(sql_select_1)

            if res_num_1 > 0:

                # 修改該條記錄

                sql_update_one_and_two_stars='''update one_and_two_stars set  kn_name = %s, parent_kn_id = %s where kn_id = %s'''   ("'"+str(kn_name_1)+"'", str(parent_kn_id_1), int(one_level_id))

                self.execute_kg(sql_update_one_and_two_stars)

                self.commit_kg()

            else:

                # 直接插入這條資料

                sql_insert_one_and_two_stars= '''insert into one_and_two_stars(kn_id,kn_name,parent_kn_id,ctime)

                                                 values('%s','%s','%s','%s')

                                                 ''' % (str(one_level_id),  str(kn_name_1),str(parent_kn_id_1),str(dt))

               self.execute_kg(sql_insert_one_and_two_stars)

               self.commit_kg()

(4)replace into的使用形式

MySQL replace into 有三種形式:

1) replace into tbl_name(col_name, ...) values(...)

2) replace into tbl_name(col_name, ...) select ...

3) replace into tbl_name set col_name=value, ...

前兩種形式用的多些。其中 “into” 關鍵字可以省略,不過最好加上 “into”,這樣意思更加直觀。另外,對於那些沒有給予值的列,MySQL 將自動為這些列賦上預設值。