SQLServr添加資料列

來源:互聯網
上載者:User

標籤: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添加資料列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.