資料庫表中資料的插入複製更新刪除

2021-10-07 05:52:43 字數 1014 閱讀 2050

--插入單條資料

insert into demotype(typename)--需要操作多個列用逗號隔開,下面的值同樣用逗號隔開

values('值')

insert demotype(typename)

select '值'--select不需要()

--批量插入多條資料操作,但是只能操作一列

insert into demotype(typename)

values('值1'),('值2'),('值3'),('值4'),('值4'),('值5')

--對乙個表中多列插入多個資料

(1)insert demotype(主鍵,列1,列2)

select 1 'a' 'b' union

select 2 'c' 'd' union

select 3 'e' 'f'

(2)insert demotype(主鍵,列1,列2)--如果設定主鍵為標識列,則不允許設定主鍵數值

select 1 'a' 'b' union

select 2 'c' 'd' union all--union all允許插入相同資料 union不允許

select 3 'c' 'd'

--複製表

(1)insert into 目標表(目標列)

select 源表需要複製的列 from 源表

(2)select 源表需要複製的列 into 新錶 from 源表--將資料複製到新錶

--更新資料

update 表名 set 列名='值' ,列名='值' where id=1--注意加where,否則修改全表資料

--刪除資料

(1)刪除資料不刪除表

delete from 表名 where id=1或者id>10--刪除部分或全部資料,但是標識列不恢復

truncate table 表名--清空表,標識列也恢復

--truncate,drop都是即時操作,不能rollback,而delete,update,insert都會記錄在日誌中,可以恢復

資料庫表資料複製

同乙個資料庫中不同表之間的資料複製,比如我想將乙個資料庫demo中的table1表的資料複製到demo資料庫中表table2中 我們可以這樣寫 insert into table1 name,password money select name,password money from table2 ...

資料庫表複製

select into from 和 insert into select都是用來複製表,兩者的主要區別為 select into from 要求目標表不存在,因為在插入時會自動建立。insert into select from 要求目標表存在 備份表資料 create table emp as ...

c 更新mysql資料 MySQL插入更新刪除資料

資料插入 插入完整的行 insert into customers values null,pep e.lapew 100 main street los angeles ca 90046 usa null null 此例子插入乙個新客戶到customers表。儲存到每個表列中的資料在values子...