MySQL 更新基礎資料表

2021-08-28 21:36:36 字數 1576 閱讀 2764

在pacs的mysql資料庫裡,收費專案與his收費專案不能同步,需要手工更新。

一、使用insert

into

table   

onduplicate

keyupdate 語句,將資料直接更新。資料庫會判斷,如果主鍵存在,則更新相應字段,如果主鍵不存在,則插入該行資料。

相當於是 update 和 insert 的集合。

優點:語句執行失敗不會影響業務。要執行的語句數少,減少失誤。

缺點:需要表具有主鍵,以主鍵為判斷依據。如果是多列聯合主鍵,不能更新聯合主鍵中的列。

--更新資料。如果主鍵存在,則更新name、price、py三個字段

insert into chargeitem ( id,wb,py,itemkind,unit,spec,price,name) values

('256504','cectgr','sfctps','08','','','462.00','雙肺ct(平掃)') ,

('21006','ut','bz','16','','','0.00','病重'),

('216156','rrjqssgy','smdjjcyz','12','元/項','','307.00','掃瞄電鏡檢查與診斷')

on duplicate key update name=values(name),price=values(price),py=values(py)

二、使用 delete 後 insert 。

將原資料全部刪除,再將新資料匯入。需要提前做好資料備份,防止匯入失敗,影響業務。

--備份資料表

create table chargeitem20170718 (select * from chargeitem)

--檢視備份情況

select * from chargeitem20170718

--刪除原資料

delete from chargeitem

--插入新資料

insert chargeitem ( id,wb,py,itemkind,unit,spec,price,name) values

('256504','cectgr','sfctps','08','','','462.00','雙肺ct(平掃)') ,

('21006','ut','bz','16','','','0.00','病重'),

('216156','rrjqssgy','smdjjcyz','12','元/項','','307.00','掃瞄電鏡檢查與診斷')

--如果插入失敗,還原資料表。先drop原表,再匯入備份的資料

drop table chargeitem

--還原備份的資料

create table chargeitem ( select * from chargeitem20170718)

insert into chargeitem (id,wb,py,itemkind,unit,spec,price,name) select id,wb,py,itemkind,unit,spec,price,name from chargeitem20180307

mysql更新表結構 mysql更新資料表結構命令

mysql中alter命令的用法,用於編輯表結構 修改表名alter table test rename test1 修改字段型別alter table employee change depno depno int 5 not null 加索引alter table 表名 add index 索引...

MySQL資料表基礎

2.1.建立資料表 格式 create table 表名 欄位名 字段型別 字段引數 在命令中加 index key number 則設number為主鍵。index test number number 則為number欄位建立了乙個普通索引值。注意事項 在建立 前要先使用對應的資料庫然後在裡面建...

mysql 更新資料表的記錄

對於表裡的記錄值,可以通過update 命令進行更改,語法如下 update tablename set field1 value1,field2.value2,fieldn valuen where condition 例如,將表emp 中ename 為 lisa 的薪水 sal 從3000 更改...