本文介绍了Oracle数据类型:我应该使用VARCHAR2还是CHAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在Oracle中使用VARCHAR2还是CHAR作为数据类型?

Should I use VARCHAR2 or CHAR as a datatype in Oracle?

有人建议我对我需要的这些新表使用CHAR,但我担心,因为这些新表将用于填充使用VARCHAR2数据类型的现有表.我担心在VARCHAR2字段中放置多余的空格以及比较问题.我知道可以通过修整或转换来比较它们,但恐怕会使我的代码变得混乱和混乱.

It's been suggested to me to use CHAR for these new tables I need but I'm concerned since these new tables which will be used to populat existing tables that use a VARCHAR2 datatype. I'm concerned about extra spaces being placed in the VARCHAR2 fields and with comparison issues. I know there are ways to compare them using by trimming or converting them but I'm afraid it will make my code messy and buggy.

您对此有何看法?

推荐答案

实际上是相反的.如果字符串太短,则使用CHAR可以通过在字符串之间填充空格来强制将字符串固定为固定长度.因此,在使用数据的任何应用程序中将CHAR与常规字符串进行比较时,该应用程序每次都需要添加修饰.换句话说,VARCHAR2是自然导致更清晰代码的选择.

It's actually quite the opposite. Using CHAR will force your strings to be a fixed length by padding them with spaces if they're too short. So when comparing CHARs to regular strings in whatever app is using the data, that app would need to add a trim every time. In other words, VARCHAR2 is the choice that naturally leads to cleaner code.

通常,除非您有非常特定的原因想要使用CHAR列,否则通常应始终使用VARCHAR2.

In general you should always use VARCHAR2, unless you have a very specific reason why you want a CHAR column.

如果您担心字符串的开头或结尾有多余的空格,那么您会想到一些选择:

If you're worried about strings that have extra spaces in the front or end, then there's a few options that come to mind:

  • 在插入之前,请确保执行插入操作的任何过程都会对其进行修整.
  • 在该列上添加检查约束,以确保string = trim(string).
  • 添加一个插入前行级触发器,该触发器在插入字符串时对其进行修剪.
  • 请确保在查询表时对字符串进行修剪

这篇关于Oracle数据类型:我应该使用VARCHAR2还是CHAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:53