Sql server添加列,並複製其他列的值粘貼在新添加的列中

1.基礎的增刪改
A.增:alter table [表名] add [字段名] 字段屬性 default 缺省值 default 是可選參數
B.刪:alter table [表名] drop 字段名
C.改:alter table [表名] alter [字段名] 字段屬性
注:自增列不能直接修改,必須將原有ID列刪除,然後重新添加一列具有identity屬性的ID字段。比如你要修改的字段名爲ID:
alter table 表名 drop column ID
alter table 表名 add ID int identity(1,1)

2.修改表:
A.重命名錶:EXEC sp_rename ‘oldname’,‘newname’
B.修改列屬性:alter table 學生信息 alter column 姓名 varchar(20) not null
C.添加列:alter table 學生信息 add 家庭住址 nvarchar(20) null
D.刪除列:alter table 學生信息 drop column 家庭住址
E.修改列名:exec sp_rename ‘表名.[字段原名]’,‘字段新名’,‘column’

3.複製表:
A. 複製整張表:select * into new_table from old_table
B. 複製表結構:select * into new_table from old_table where 1=2
C. 複製表內容:insert into new_tab select * from old_table

4.再展示如何將添加的列獲取另一列的值
A.update 表2 set (要插入的列名)= select 表1.某一列 from 表1 left jion 表2 on 表1和表2的關聯 where …
B.update 表1 set 表1.列=表2.列 from 表2 where 表2.id=表1.id
C.update _a set 列=_b.列 from 表1 _a join 表2 _b on _a.id=_b.id

如果將一個表中的一列複製到另一列中,可以這樣
update 表1 set 表1.列2=表1.列2 from 表1
小編參考的資料路徑
笑笑小白
NET未來之路

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