資料庫表插入數據
❶ 資料庫從一張表向另一張表怎麼插入數據
下面以mysql資料庫為例分情況一一說明:
兩張表:insertTest和insertTest2,前者中有測試數據:
1.如果2張表的欄位一致,並且希望插入全部數據,可以用這種方法:
INSERT INTO 目標表 SELECT * FROM 來源表;
insert into insertTest select * from insertTest2;
2.如果只希望導入指定欄位,可以用這種方法:
INSERT INTO 目標表 (欄位1, 欄位2, ...) SELECT 欄位1, 欄位2, ... FROM 來源表;
注意欄位的順序必須一致。
insert into insertTest2(id) select id from insertTest2;
3.如果您需要只導入目標表中不存在的記錄,可以使用這種方法:
INSERT INTO 目標表
(欄位1, 欄位2, ...)
SELECT 欄位1, 欄位2, ...
FROM 來源表
WHERE not exists (select * from 目標表
where 目標表.比較欄位 = 來源表.比較欄位);
1>.插入多條記錄:
insert into insertTest2
(id,name)
select id,name
from insertTest
where not exists (select * from insertTest2
where insertTest2.id=insertTest.id);
2>.插入一條記錄:
insert into insertTest
(id, name)
SELECT 100, 'liudehua'
FROM al
WHERE not exists (select * from insertTest
where insertTest.id = 100);
❷ SQL如何在資料庫中創建表並添加數據
- 01
新建表
雙擊打開MySQL軟體,在左側中找到【表】並且右擊選擇【新建表】,如下圖所示: - 02
添加數據
選擇新建表之後,在界面右側可以添加數據,點擊【添加欄目】就可以在下方再添加一行數據,如下圖所示: - 03
另存為
點擊【另存為】,會彈出一個【表名】窗口,輸入表名,比如Class,點擊【確定】按鈕,如下圖所示: - 04
顯示結果
在表的下方就會出現一個名為Class的表,如下圖所示:
❸ SQL怎樣把一個表的數據插入到另一個表裡
復製表結構及數據到新表select * into 目標表名 from 源表名
將資料庫A中某表的的某列欄位,更新到資料庫B中某表的某列欄位:(use master 資料庫)
update a
set a.name=b.name
from temp1.dbo.tableA a,temp2.dbo.tableA b
where a.id=b.id
❹ 如何直接往系統中資料庫表插入數據
添加數據需要知道往哪張表添加,以及自己要添加的內容,然後可用insert語句執行。 1、以sqlserver2008r2為例,登錄SQL Server Management Studio到指定的資料庫。 2、登錄後點擊「新建查詢」。 3、比如要往test表中插入數據,可先用如下語句查看一下表結構及表內數據: 1 select * from test; 4、根據自己的實際情況添加輸入,比如要添加一條「16,小小動」的數據。 1 insert into test (id,name) values (16,'小小動'); 執行成功後會有提示: 5、此時資料庫中數據如下,說明添加成功。
❺ 如何在資料庫中批量插入數據
在資料庫中批量插入數據的方法及其世襪步驟:
1、打開設備中的資料庫,並點擊資料庫頁面左邊的「編輯前200行」進入其中。
2、在「編輯前200行」的頁面中,會顯示出表中的列和爛薯圖中的列是相互對應的幾列數據。
3、點擊上一步顯示出的幾列數據,並進行手動數據添加操作。
4、數據添加完畢之後,在表中右擊選飢返者擇執行。
5、執行完畢之後,即成功在資料庫中批量插入數據。
❻ 六、MySQL資料庫之數據插入(insert into)
本節介紹數據的插入,復制數據到另一張表的Sql語法,主要語法有: insert into,insert into select,select into from 等用法,下面將一一為大家詳細說明:
以下面兩張表進行sql腳本說明
insert into有兩種語法,分別如下:
語法1:INSERT INTO table_name VALUES (value1,value2,value3,...); --這種形式無需指定要插入數據的列名,只需提供被插入的值即可:
語法2:INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...); --這種形式需指定要插入數據的列名,插入的值需要和列名一一對應:
eg:insert into customer values('1006','14006','王欣欣','27','深圳市'); --向表customer插入一條數據
eg:insert into customer values('1007','14007','孟一凡','27',''); --向表customer插入一條數據,最後一個值不填表示對應的值為空,非必填項可以不用插入值
eg:insert into customer (cus_id,cus_no,cus_name,cus_age,cus_adds) values('1008','14008','孔凡','26','廣州市'); --向表customer插入一條數據,插入的值與列名一一對應
詳解:insert into select --表示從一個表復制數據,然後把數據插入到一個已存在的表中。目標表中任何已存在的行都不會受影響。
語法1:INSERT INTO table_name2 SELECT * FROM table_name1; --表示將表table_name1中復制所有列的數據插入到已存在的表table_name2中。被插入數據的表為table_name2,切記不要記混了。
eg:insert into customer select * from asett --將表asett中所有列的數據插入到表customer中
語法2:INSERT INTO table_name2 (column_name(s)) SELECT column_name(s) FROM table_name1; --指定需要復制的列,只復制制定的列插入到另一個已存在的表table_name2中:
eg:insert into customer (cus_id,cus_no) select ast_id,ast_no from asett --將表asett中列ast_id和ast_no的數據插入到表customer對應的cus_id,cus_no列中
詳解:從一個表復制數據,然後把數據插入到另一個新表中。
語法1:SELECT * INTO newtable [IN externaldb] FROM table1; --復制所有的列插入到新表中:
eg:select * into customer from asett --將asett表中數據插入到customer中,被插入的 表customer不存在
eg:select * into customer from asett where ast_id = '1008' --只復製表asett中ast_id=1008的數據插入到customer中,被插入的 表customer不存在
語法2:SELECT column_name(s) INTO newtable [IN externaldb] FROM table1; --只復制指定的列插入到新表中:
eg:select ast_id,ast_no into customer from asett --將asett表中列ast_id,ast_no數據插入到customer中,被插入的 表customer不存在
區別1:insert into customer select * from asett where ast_id='1009' --插入一行,要求表customer 必須存在
區別2:select * into customer from asett where ast_id='1009' --也是插入一行,要求表customer 不存在
區別3:select into from :將查詢出來的數據復制到一張新表中保存,表結構與查詢結構一致。
區別4:insert into select :為已經存在的表批量添加新數據。