Home  >  Article  >  Database  >  让你提前认识软件开发(36):如何扩展数据表字段?

让你提前认识软件开发(36):如何扩展数据表字段?

WBOY
WBOYOriginal
2016-06-07 15:58:361014browse

第2部分 数据库SQL语言 如何扩展数据表字段? 【文章摘要】 在通信类软件中,经常会与数据库打交道。由于需求变化,或者是程序优化升级等原因,对数据表字段进行扩展是常有的事情。这就要求开发人员必须熟练掌握对数据表字段进行扩展的操作流程。 本文基于作

第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,欢迎关注!)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn