標籤:lte 樣本 多列 info 資料庫管理 script rop ima int
資料列定義
表中資料行的資料插入和資料類型都是基於資料列的,學會添加資料列在開發過程中是必不可少的。
使用SSMS資料庫管理工具添加資料列
在資料表中添加一列或者多列步驟相同
1、串連資料庫,選擇資料表-》右鍵點擊-》選擇設計。
2、在新開啟的視窗中輸入中-》輸入列名,資料類型,是否可空-》在下面輸入列注釋等屬性-》點擊儲存按鈕(或者ctrl+s)。
3、如果想在指定列前面添加資料列-》選擇要指定列,右鍵點擊-》插入資料列-》輸入列名,列類型,是否可空,屬性等,點擊儲存。
使用T-SQL指令碼資料列添加資料列
文法:alter table 資料庫名.dbo.表名 add 列名 列類型 [not] null;
樣本:
--添加可空資料列
alter table testss.dbo.test1 add height1 nvarchar(50) null;
--添加不可空資料列
alter table testss.dbo.test1 add height2 nvarchar(50) not null;
添加帶注釋的資料列
文法:
alter table 資料庫名.dbo.表名 add 列名 列資料類型 [not] null;
execute sp_addextendedproperty N‘MS_Description‘, N‘列說明‘, N‘user‘, N‘dbo‘, N‘table‘, N‘表明, N‘column‘, N‘列名‘;
樣本:
alter table testss.dbo.test1 add height3 nvarchar(50) null;
execute sp_addextendedproperty N‘MS_Description‘, N‘身高3‘, N‘user‘, N‘dbo‘, N‘table‘, N‘test1‘, N‘column‘, N‘height3‘;
添加資料列時指定預設值
文法:alter table 資料庫名.dbo.表名 add 列名 int not null default 值;
樣本:alter table testss.dbo.test1 add testid int not null default 1;
添加多個資料列
文法:
alter table 資料庫名.dbo.表名 add 列名 列類型 not null default 值,列名 列類型 null default 值;
樣本:
alter table testss.dbo.test1 add height5 int not null default 1,
height6 nvarchar(20) null default ‘178cm‘;
總結
在生產或者開發階段,資料列的添加建議使用T-SQL指令碼,方便開發和生產,且易於維護。
SQLServr添加資料列