<p>【一】char和varChar的區別</p>
<pre class="best-text mb-10">區別: <br/>1.CHAR的長度是固定的,而VARCHAR2的長度是可以變化的, 比如,儲存字串“abc",對於CHAR (10),表示你儲存的字元將佔10個位元組(包括7個空字元),而同樣的VARCHAR2 (10)則只佔用3個位元組的長度,10只是最大值,當你儲存的字元小於10時,按實際長度儲存。 <br/>2.CHAR的效率比VARCHAR2的效率稍高。 <br/>3.目前VARCHAR是VARCHAR2的同義詞。工業標準的VARCHAR型別可以儲存<a class="baidu-highlight" href="https://www.baidu.com/s?wd=%E7%A9%BA%E5%AD%97%E7%AC%A6%E4%B8%B2&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PymkPAmsn164P1mkmWFB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHm4PHDLnH0" rel="nofollow" target="_blank">空字串</a>,但是oracle不這樣做,儘管它保留以後這樣做的權利。Oracle自己開發了一個資料型別VARCHAR2,這個型別不是一個標準的VARCHAR,它將在資料庫中varchar列可以儲存<a class="baidu-highlight" href="https://www.baidu.com/s?wd=%E7%A9%BA%E5%AD%97%E7%AC%A6%E4%B8%B2&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PymkPAmsn164P1mkmWFB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHm4PHDLnH0" rel="nofollow" target="_blank">空字串</a>的特性改為儲存NULL值。如果你想有<a class="baidu-highlight" href="https://www.baidu.com/s?wd=%E5%90%91%E5%90%8E%E5%85%BC%E5%AE%B9&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PymkPAmsn164P1mkmWFB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHm4PHDLnH0" rel="nofollow" target="_blank">向後相容</a>的能力,Oracle建議使用VARCHAR2而不是VARCHAR。 <br/>何時該用CHAR,何時該用varchar2? </pre>
<pre class="best-text mb-10">何時該用CHAR,何時該用varchar2? <br/>CHAR與VARCHAR2是一對矛盾的統一體,兩者是互補的關係. <br/>VARCHAR2比CHAR節省空間,在效率上比CHAR會稍微差一些,即要想獲得效率,就必須犧牲一定的空間,這也就是我們在<a class="baidu-highlight" href="https://www.baidu.com/s?wd=%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AE%BE%E8%AE%A1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PymkPAmsn164P1mkmWFB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHm4PHDLnH0" rel="nofollow" target="_blank">資料庫設計</a>上常說的‘以空間換效率’。 <br/>VARCHAR2雖然比CHAR節省空間,但是如果一個VARCHAR2列經常被修改,而且每次被修改的資料的長度不同,這會引起‘行遷移’(Row Migration)現象,而這造成多餘的I/O,是<a class="baidu-highlight" href="https://www.baidu.com/s?wd=%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AE%BE%E8%AE%A1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PymkPAmsn164P1mkmWFB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHm4PHDLnH0" rel="nofollow" target="_blank">資料庫設計</a>和調整中要盡力避免的,在這種情況下用CHAR代替VARCHAR2會更好一些。</pre>