讓你提前認識軟體開發(36):如何擴充資料表欄位?,軟體開發欄位

來源:互聯網
上載者:User

讓你提前認識軟體開發(36):如何擴充資料表欄位?,軟體開發欄位

第2部分 資料庫SQL語言

如何擴充資料表欄位?

 

【文章摘要】

        在通訊類軟體中,經常會與資料庫打交道。由於需求變化,或者是程式最佳化升級等原因,對資料表欄位進行擴充是常有的事情。這就要求開發人員必須熟練掌握對資料表欄位進行擴充的操作流程。

        本文基於作者的資料庫方面的工作經驗,以實際的SQL程式為例,詳細介紹了如何對對資料表欄位進行擴充,為相關的開發工作提供了參考。

【關鍵詞】

        資料庫  資料表  擴充  SQL  開發

 

一、前言

        在實際的軟體開發項目中,對資料表欄位的擴充包括如下兩個方面:

        第一,對原有欄位值的擴充。例如,原表有一個欄位“result”,表示結果,之前的取值為0和1,現在要擴充其取值範圍,添加一個結果值2。對於此類擴充,資料表的結構不用動,只需要讓相關模組知道有這個值的擴充即可。

        第二,新增加欄位。即原來表已有的欄位不能滿足當前的要求,需要新增一個或幾個欄位,這就涉及到資料表結構的改變。

        本文主要討論第二種情況下資料表欄位擴充的流程和操作方法。本文中的所有指令碼都是基於Sybase資料庫。

 

二、資料表欄位擴充的流程

        對於新增欄位的情況,不能通過簡單的刪除表和重建表來完成,因為在實際的軟體運行環境中,幾乎每個資料表裡面都會有很多的資料。如果不管三七二十一,將表刪除了,會導致某些重要資料的丟失,造成極為不良的影響,甚至會引起客戶的投訴。

        在實際的軟體開發項目中,對資料表欄位的擴充流程1所示。

圖1 對資料表欄位的擴充流程

 

三、資料表欄位擴充操作樣本

        有一個員工資訊表,包含了工號、姓名和年齡三個欄位,如下所示:

create table tb_employeeinfo

(

    workid    int                default(0)  not null,  -- workid

    name     varchar(50)  default('')   not null,  -- name

    age        int                 default(0)  not null    -- age

)

go

 

create unique index idx1_tb_employeeinfo on tb_employeeinfo(workid)

go

 

print 'create table tb_employeeinfo ok'

go

 

        現在需要在原表的基礎之上擴充一個地址(address)欄位,用於記錄員工的居住地址,該欄位可以為空白。

        整個欄位擴充的執行SQL指令碼如下:

-- 第一步: 建立備份表

if exists(select * from sysobjects where name='tb_employeeinfobak')

    drop table tb_employeeinfobak

go

create table tb_employeeinfobak

(

    workid    int                default(0)   not null,  -- workid

    name      varchar(50)  default('')   not null,  -- name

    age       int                  default(0)  not null   -- age

)

go

 

create unique index idx1_tb_employeeinfobak on tb_employeeinfobak(workid)

go

 

print 'create table tb_employeeinfobak ok'

go

 

-- 第二步: 將原表內容插入到備份表中

insert into tb_employeeinfobak(workid, name, age) select workid, name, age from tb_employeeinfo

go

 

-- 第三步: 將原表刪除掉

if exists(select * from sysobjects where name='tb_employeeinfo')

    drop table tb_employeeinfo

go

 

-- 第四步: 建立新表

create table tb_employeeinfo

(

    workid       int                 default(0)  not null,    -- workid

    name        varchar(50)   default('')   not null,    -- name

    age          int                 default(0)  not null,     -- age

    address   varchar(100)                        null     -- address

)

go

 

create unique index idx1_tb_employeeinfo on tb_employeeinfo(workid)

go

 

print 'create table tb_employeeinfo ok'

go

 

-- 第五步: 將備份表內容插入到新表中

insert into tb_employeeinfo(workid, name, age) select workid, name, age from tb_employeeinfobak

go

 

-- 第六步: 刪除備份表

if exists(select * from sysobjects where name='tb_employeeinfobak')

    drop table tb_employeeinfobak

go

 

        經過以上六個步驟,即實現了對資料表的欄位擴充,並且原來的資料也沒有丟失。

 

四、總結

        本文以實際的SQL指令碼為例,詳細介紹了對資料表欄位進行擴充的整個流程,為相關軟體開發活動的開展提供了有益的參考。

 

 

(本人微博:http://weibo.com/zhouzxi?topnav=1&wvr=5,號:245924426,歡迎關注!)

 


怎在資料表中增加預留欄位

alter table 表名 add 欄位名 屬性
增加一個無所謂的,反正你裡邊又不放資料
就算修改類型和長度也沒關係,到時候再alter一下就行了,只要那個欄位裡邊不存放資料就行,想用就用,不想用了再drop掉也沒關係
 
資料庫怎增加欄位

簡單的方法:

1)create 一個新資料庫,(增加欄位後的)
2)從舊資料庫裡匯入,匯入相同的欄位,新欄位空白,或固定的值。
 

相關文章

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.