程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> 更多數據庫知識 >> 必須會的SQL語句(三) 數據插入,sql語句

必須會的SQL語句(三) 數據插入,sql語句

編輯:更多數據庫知識

必須會的SQL語句(三) 數據插入,sql語句


1.規范一些使用插入語句的小規范

  1)中文字符串前 最好 加一個N
  2)列名用中括號 擴起來   像這樣  [列名]
 
2.常規寫法

Insert into tableName
( [column1] , [column2] )
values
(N'中文','11ds')
 
3.多行一條語句插入多行

insert into 表名 ([列1],[列2])
     select  '值1','值2' union all     --這裡呢,union 和 union all的 區別
                                                      --主要是對於重復值得處理,union 會過濾掉重復行,而union all會全插進去
     select  '值3','值4' union         
     select  '值5','值6'
 
4.復制到新表 將原有表中的數據復制到一個不存在的新表中

  select * into newtable from oldtable
  --僅復制表結構如何做呢?
select * into newtable from oldtable where 1<>1
select top 0 * into newtable from oldtable 
 
5.插入其他表的數據  向一個已有表中,復制其他表的數據

insert into tablename(column,column2)
      select column,column2 from oldtable
 
6.強行寫入 強行寫入標識字段。

--對於已經設置自動增長的列,默認情況我們無法對其輸入值。
--可以用一下語句去強行寫入。
 
--1)開啟添加,(解除添加的限制)
Set indentity_insert tablename On
--2)可以手動插入id了
insert into 表明 (id,name) values ('1002','大二')
--3)關閉手動插入
Set indentity_insert tablename off

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved