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

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

Jun 07, 2016 pm 03:58 PM
how Field Expand data sheet software development

第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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

Extensions and third-party modules for PHP functions Extensions and third-party modules for PHP functions Apr 13, 2024 pm 02:12 PM

To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

How to install mbstring extension under CENTOS7? How to install mbstring extension under CENTOS7? Jan 06, 2024 pm 09:59 PM

1.UncaughtError:Calltoundefinedfunctionmb_strlen(); When the above error occurs, it means that we have not installed the mbstring extension; 2. Enter the PHP installation directory cd/temp001/php-7.1.0/ext/mbstring 3. Start phpize(/usr/local/bin /phpize or /usr/local/php7-abel001/bin/phpize) command to install php extension 4../configure--with-php-config=/usr/local/php7-abel

Transformative Trend: Generative Artificial Intelligence and Its Impact on Software Development Transformative Trend: Generative Artificial Intelligence and Its Impact on Software Development Feb 26, 2024 pm 10:28 PM

The rise of artificial intelligence is driving the rapid development of software development. This powerful technology has the potential to revolutionize the way we build software, with far-reaching impacts on every aspect of design, development, testing and deployment. For companies trying to enter the field of dynamic software development, the emergence of generative artificial intelligence technology provides them with unprecedented development opportunities. By incorporating this cutting-edge technology into their development processes, companies can significantly increase production efficiency, shorten product time to market, and launch high-quality software products that stand out in the fiercely competitive digital market. According to a McKinsey report, it is predicted that the generative artificial intelligence market size is expected to reach US$4.4 trillion by 2031. This forecast not only reflects a trend, but also shows the technology and business landscape.

How do the types of PHP function return values ​​relate to the interoperability of PHP extensions? How do the types of PHP function return values ​​relate to the interoperability of PHP extensions? Apr 15, 2024 pm 09:06 PM

PHP function return value types can be expressed as type description syntax, which clearly specifies the return value type of each function. Understanding return value types is critical to creating extensions that are compatible with the PHP core engine, avoiding unexpected conversions, improving efficiency, and enhancing code readability. Specifically, extension functions can define a return value type so that the PHP engine can optimize code execution based on that type and allow developers to explicitly handle the return value. In practice, extension functions can return PHP objects, and PHP code can handle the returned results according to the return value type.

Artificial Intelligence Applications in Software Development: Automation and Optimization Artificial Intelligence Applications in Software Development: Automation and Optimization Sep 02, 2023 pm 01:53 PM

As a cutting-edge technology, artificial intelligence (AI) is showing great potential in various fields. In the field of software development, the application of artificial intelligence has also attracted widespread attention. From automating tasks to code optimization, AI brings many innovative ways for developers to increase efficiency, quality, and creativity. This article will explore the application of artificial intelligence in software development, focusing on the development of automation and optimization. Automation tasks 1. Code generation By learning existing code bases, artificial intelligence can automatically generate code snippets or even complete modules. This is very helpful for developers to quickly create basic frameworks, saving time and effort. For example, some AI tools can generate boilerplate code based on requirements, allowing developers to get to work faster2

Reasons and solutions why D drive cannot be expanded in win11 system Reasons and solutions why D drive cannot be expanded in win11 system Jan 08, 2024 pm 12:30 PM

Some users feel that their d drive space is not enough and want to expand the d drive space. However, during the operation, they find that their win11d drive cannot be expanded and the extended volume is gray. In fact, this may be due to insufficient disk space. Let’s take a look at the solutions below. Why the win11d disk cannot be expanded: 1. Insufficient space 1. First of all, to expand the d disk, you need to ensure that your disk has "available space", as shown in the figure. 2. If there is no available space like this, then there is naturally no way to expand. 3. If you want to expand the D drive at this time, you can find other disks, right-click and select "Compress Volume" 4. Enter the space you want to expand to compress, and then click "OK" to obtain the available space. 2. The disks are not adjacent 1. To expand a disk, you can

What should I do if the extension displayed in the upper right corner of Sogou browser is missing? What should I do if the extension displayed in the upper right corner of Sogou browser is missing? Jan 31, 2024 pm 02:54 PM

What should I do if the extension displayed in the upper right corner of Sogou Browser is missing? The extension bar of Sogou Browser is missing. How can I display it? There is an extension bar in the upper right corner of Sogou Browser, which displays various extensions that users have downloaded and installed. However, due to some of our operations, the extension bar is missing. What should we do? How do we operate it so that it will be displayed! The editor below has compiled solutions for what to do if the extension displayed in the upper right corner of the Sogou browser is missing. If not, follow me and read on! What should I do if the extension displayed in the upper right corner of Sogou Browser is missing? 1. First open Sogou Browser. You can see a "Show Menu" icon composed of three horizontal lines in the upper right corner of the browser. Use the mouse to click on the icon. 2. After clicking, a menu window will pop up below.

See all articles