<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sybase数据库技术，数据库恢复专家 &#187; 列顺序</title>
	<atom:link href="http://www.dbainfo.net/tag/%e5%88%97%e9%a1%ba%e5%ba%8f/feed" rel="self" type="application/rss+xml" />
	<link>https://www.dbainfo.net</link>
	<description>提供Sybase ASE及Sybase SQL Anywhere数据库修复服务，电话：13811580958(微信)，QQ：289965371！We have many years of experience in recovering data from damanged Sybase devices. Contact us by Phone: +86 13811580958 Wechat: 13811580958 Email: 289965371@qq.com</description>
	<lastBuildDate>Sat, 14 Jun 2025 16:28:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>调整新添加的列在表中的位置,调整数据库表中列的位置</title>
		<link>https://www.dbainfo.net/sybase-ase-table-newcolumn-place.htm</link>
		<comments>https://www.dbainfo.net/sybase-ase-table-newcolumn-place.htm#comments</comments>
		<pubDate>Tue, 24 Aug 2010 06:35:52 +0000</pubDate>
		<dc:creator>dbainfo</dc:creator>
				<category><![CDATA[Sybase ASE]]></category>
		<category><![CDATA[DDL]]></category>
		<category><![CDATA[列顺序]]></category>

		<guid isPermaLink="false">http://www.dbainfo.net/?p=222</guid>
		<description><![CDATA[在Sybase中，表内新添加的列都位于表的最后。其它数据库也基本相同，这和数据库的内部物理存储是有必然关系的。我研究过sybase 中数据的物理存储结构，理解在sybase以及其它数据库中新增加的列必须放置在最后的原因。 在项目实践中，可能有这样的需求：需要将最后添加的一列放置到前面的位置。比如：表test中原来的列为：a,b,c，不想让最后添加的列d放在第四个位置，而让列d放在第2个位置，形成：a,d,b,c这样的顺序。如何实现呢？ 一般的办法是： 1.新建一个临时表：test_bak, create table test_bak ( a 列属性, d 列属性, b 列属性, c 列属性 ) &#160; 2.将原表test的数据拷贝到临时表test_bak中 insert into test_bak(a,d,b,c) select a,d,b,c from test &#160; 3.将原表删除，临时表改名为原表的名字 drop table test sp_rename test_bak,test &#160; 以上是常规的办法。如果表内的数据量很大的时候，则会比较耗费时间。 下面我介绍另外一个比较简便的方法。 主要思路是调整表在syscolumns中对应的colid的值。 先建立一个新的测试表test 1&#62; drop table test 2&#62; go 1&#62; create table test(id int not null,name varchar(30) null,age tinyint [...]]]></description>
			<content:encoded><![CDATA[<p>在Sybase中，表内新添加的列都位于表的最后。其它数据库也基本相同，这和数据库的内部物理存储是有必然关系的。我研究过sybase 中数据的物理存储结构，理解在sybase以及其它数据库中新增加的列必须放置在最后的原因。 在项目实践中，可能有这样的需求：需要将最后添加的一列放置到前面的位置。比如：表test中原来的列为：a,b,c，不想让最后添加的列d放在第四个位置，而让列d放在第2个位置，形成：a,d,b,c这样的顺序。如何实现呢？ 一般的办法是： 1.新建一个临时表：test_bak,</p>
<blockquote>
<p>create table test_bak ( a 列属性,</p>
<p>		d 列属性,</p>
<p>		b 列属性,</p>
<p>		c 列属性</p>
<p>		)<br />
		&nbsp;</p>
</blockquote>
<p>2.将原表test的数据拷贝到临时表test_bak中</p>
<blockquote>
<p>insert into test_bak(a,d,b,c)</p>
<p>		select a,d,b,c from test<br />
		&nbsp;</p>
</blockquote>
<p>3.将原表删除，临时表改名为原表的名字</p>
<blockquote>
<p>drop table test</p>
<p>		sp_rename test_bak,test<br />
		&nbsp;</p>
</blockquote>
<p>以上是常规的办法。如果表内的数据量很大的时候，则会比较耗费时间。 下面我介绍另外一个比较简便的方法。</p>
<p><strong>主要思路是调整表在syscolumns中对应的colid的值。</strong></p>
<p>先建立一个新的测试表test</p>
<blockquote>
<p>1&gt; drop table test</p>
<p>2&gt; go</p>
<p>1&gt; create table test(id int not null,name varchar(30) null,age tinyint not null, se_x char(1) not null,birthday datetime null)</p>
<p>2&gt; go</p>
<p>1&gt; select *&nbsp;&nbsp;from test<br />
		2&gt; go<br />
		id&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; name&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;age se_x birthday<br />
		----------- ------------------------------ --- --- --------------------------</p>
<p>		(0 rows affected)</p>
</blockquote>
<blockquote>
<p><strong>插入一条测试数据</strong></p>
<p>		1&gt; insert into test<br />
		2&gt; values(1,&#39;andkylee&#39;,28,&#39;F&#39;,&#39;1982-03-20 12:00:00&#39;)<br />
		3&gt; go<br />
		(1 row affected)<br />
		1&gt; select *&nbsp;&nbsp;from test<br />
		2&gt; go<br />
		id&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; name&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;age se_x birthday<br />
		----------- ------------------------------ --- --- --------------------------<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;1 andkylee&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;28 F&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; Mar 20 1982 12:00PM<br />
		(1 row affected)</p>
</blockquote>
<p>
	<strong>调整各个的顺序，使之倒序。将原来的id,name,age,se_x,birthday改成：birthday,se_x,age,name,id<br />
	<font color="red">调整方法：修改syscolumns表中测试表test的各个列的colid的顺序。</font></strong></p>
<blockquote>
<p>1&gt; select *&nbsp;&nbsp;from test<br />
		2&gt; go<br />
		birthday&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; se_x age name&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;id<br />
		-------------------------- --- --- ------------------------------ -----------<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Mar 20 1982 12:00PM F&nbsp; &nbsp; 28 andkylee&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;1</p>
<p>		(1 row affected)</p>
</blockquote>
<p>	<strong>列的顺序逆序后，显示插入数据。</strong><br />
	&nbsp;</p>
<blockquote>
<p>1&gt; insert into test(id,name,age,se_x,birthday)<br />
		2&gt; values(2,&#39;liu&#39;,30,&#39;M&#39;,&#39;2000-01-01 11:59:59&#39;)<br />
		3&gt; go<br />
		(1 row affected)<br />
		1&gt; select * from test<br />
		2&gt; go<br />
		birthday&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; se_x age name&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;id<br />
		-------------------------- --- --- ------------------------------ -----------<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Mar 20 1982 12:00PM F&nbsp; &nbsp; 28 andkylee&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;1<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Jan&nbsp;&nbsp;1 2000 11:59AM M&nbsp; &nbsp; 30 liu&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;2</p>
<p>		(2 rows affected)</p>
</blockquote>
<p>
	<strong>如果按照调整数据之前的列的顺序插入数据，则会报错。</strong></p>
<blockquote>
<p>1&gt; insert into test<br />
		2&gt; values(3,&#39;zhang&#39;,29,&#39;F&#39;,&#39;1980-01-01 11:59:59&#39;)<br />
		3&gt; go</p>
</blockquote>
<p>
	Msg 206, Level 16, State 2:<br />
	Server &#39;SYB_NFJD_TEST&#39;, Line 1:<br />
	Operand type clash: INT is incompatible with DATETIME<br />
	Msg 257, Level 16, State 1:<br />
	Server &#39;SYB_NFJD_TEST&#39;, Line 1:<br />
	Implicit conversion from datatype &#39;VARCHAR&#39; to &#39;INT&#39; is not allowed.&nbsp;&nbsp;Use the<br />
	CONVERT function to run this query.</p>
<p>
	<strong>新的数据要按照修改后的列的顺序插入。</strong></p>
<blockquote>
<p>1&gt; insert into test<br />
		2&gt; values(&#39;1980-01-01 11:59:59&#39;,&#39;F&#39;,29,&#39;zhang&#39;,3)<br />
		3&gt; go<br />
		(1 row affected)</p>
</blockquote>
<p>	<strong>最后，查看测试表的数据。</strong></p>
<blockquote>
<p>1&gt; select * from test<br />
		2&gt; go<br />
		birthday&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; se_x age name&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;id<br />
		-------------------------- --- --- ------------------------------ -----------<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Mar 20 1982 12:00PM F&nbsp; &nbsp; 28 andkylee&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;1<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Jan&nbsp;&nbsp;1 2000 11:59AM M&nbsp; &nbsp; 30 liu&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;2<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Jan&nbsp;&nbsp;1 1980 11:59AM F&nbsp; &nbsp; 29 zhang&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;3</p>
<p>		(3 rows affected)</p>
</blockquote>
<p>	<strong>备注:绝大多数sybase客户端工具显示表的列时都是按照colid的顺序显示的。所以，此种方法能够实现调整数据库表中列的位置的功能。</strong></p>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;<br />
	&mdash;&mdash;&mdash; 本文为andkylee个人原创，请在尊重作者劳动成果的前提下进行转载；<br />
	&mdash;&mdash;&mdash; 转载务必注明原始出处 : <a href="https://www.dbainfo.net/"><font color="#f26522">http://www.dbainfo.net</font></a><br />
	&mdash;&mdash;&mdash; 关键字： 列显示顺序 syscolumns colid<br />
	&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;</p>
<div style="clear: both; margin: 10px 0pt; border: 1px dashed rgb(153, 153, 153); font-size: 12px; padding: 5px 10px;">
<li>本文链接地址：<a href="https://www.dbainfo.net/sybase-ase-table-newcolumn-place.htm">https://www.dbainfo.net/sybase-ase-table-newcolumn-place.htm</a>；</li>
<li>本文为dbainfo个人原创，请在尊重作者劳动成果的前提下进行转载；</li>
<li>转载务必注明原始出处 : <a href="https://www.dbainfo.net/">Sybase数据库技术，数据库恢复专家</a>；</li>
<li>对《<a href="https://www.dbainfo.net/sybase-ase-table-newcolumn-place.htm">调整新添加的列在表中的位置,调整数据库表中列的位置</a>》有何疑问或见解，请在本文下方发表；</li>
<li>对网站还有其他问题或建议，请提交在<a href="https://www.dbainfo.net/messages" target="_blank">留言板</a>，谢谢！</li>
</div>
<h2  class="related_post_title">相关文章</h2><ul class="related_post"><li>2010-06-01 -- <a href="https://www.dbainfo.net/sybase-ase-get-object-ddl-with-ddlgen.htm" title="用DDLGEN从ASE数据库中提取对象的定义语法">用DDLGEN从ASE数据库中提取对象的定义语法</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>https://www.dbainfo.net/sybase-ase-table-newcolumn-place.htm/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
