database concepts

Using a database: creating tables

Datatypes

The most common datatypes used in a database are probably text and numbers. To this end, we will look at these datatypes in Oracle.

Text
Fields such as names, addresses, phone numbers are stored as text. This includes postcodes and such, even though they are made up of numeric characters. A rule of thumb is that if the field is not used in a calculation, use a text datatype.

Oracle uses a variable-length character string datatype called VARCHAR2. When declaring a VARCHAR2, the maximum number of characters the data can hold is specified. For example, CustABN VARCHAR2(11), states that the customer ABN can have up to 11 characters in it.

Numbers
A field such as Quantity that is used to calculate something is stored as a number. Like most software, Oracle supports a range of numeric datatypes, but the one of interest is NUMBER. This datatype allows for precision before and after a decimal point, that has to be declared if it is required. A good point for this datatype is that if no precision is required NUMBER will hold the data exactly as it is entered.

Question:
What datatype should be used for a phone number?

Show me

You would use VARCHAR2. Here you can’t use NUMBER as area codes start with zero. It would be sized according to the type of number (local, mobile, international).