關於identity 的用法

 【學習點點滴滴】 在學習SQL 時,很多人都會與identity 打交道。當然我也纔剛剛學習到它。這裏我把我所理解的identity寫出來。記錄

 一下。因爲鄙人記心不太好。想以這種方式來加深理解與記憶。對identity的理解全拜CSDN的兄弟所賜。在此謝了哈!!

 

 

         在理解之前我們先建兩個表 create  table   [ta]([a] char,[b] char);create table  [tb]([c] char)

 要實現連個表的合併。但兩表都沒有主鍵。爲此我們要用到 就很容易做到。 identity(int ,1,1) 可自動實現編號。

    select * ,id=  identity (int ,1,1) into ta1 from ta // 

  select * , id= identity (int ,1,1) into tb1 from tb//

  這樣我們就ta,tb的各列值複製到表ta1,tb1中,並增加了id列。

  這樣我們就很容易實現 兩表的合併。 

 

 

            【  identity(int,i,j)實現ID自增,  從i 開始,每次以j 增長。 】

 

 

       【select * into table1 from table2 // 執行此SQL語句 :將table2 的值全複製到table1 中。】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章