Type of data in SQL

We already explained that when creating a table we must determine what fields (columns) it will have and what type of data each of them will store, that is, their structure.

The data type specifies the type of information that a field can store: characters, numbers, etc.

These are some basic Oracle data types (we will see others later and in more detail):

varchar2:

Used to store character strings. A string is a sequence of characters. It is enclosed in single quotes; example: 'Hello', 'Juan Perez', 'Colon 123'. This type of data defines a variable length string in which we determine the maximum number of characters between parentheses. You can save up to xxx characters. For example, to store strings of up to 30 characters, we define a field of type varchar2 (30), that is, in parentheses, next to the name of the field we put the length.

If we try to store a character string longer than the one defined, the string is not loaded, a message appears indicating this situation and the statement is not executed.

For example, if we define a field of type varchar (10) and try to store the string 'Good afternoon' in it, a message appears indicating that the value is too large for the column.


Number (p, s): 

Used to store numerical values ​​with decimals, from 1.0 x10-120 to 9.9 ... (38 places). We define fields of this type when we want to store numerical values ​​with which we will then perform mathematical operations, for example, quantities, prices, etc.

It can contain whole numbers or decimals, positive or negative. The parameter "p" indicates the precision, that is, the number of digits in total (counting the decimals) that the number will contain at most. The "s" parameter specifies the scale, that is, the maximum number of decimal digits. For example, a defined field "number (5,2)" can contain any number between 0.00 and 999.99 (positive or negative).

To specify integers, we can omit the "s" parameter or put the value 0 as the "s" parameter. The period (.) Is used as a separator.

If we try to store a larger value outside the allowed range when defining it, such value is not loaded, a message appears indicating such situation and the sentence is not executed.

For example, if we define a field of type number (4,2) and try to save the value 123.45, a message appears indicating that the value is too large for the column. If we enter a value with more decimal places than defined, the value is loaded but with the number of decimal places allowed, the remaining digits are ignored.

Before creating a table, we must think about its fields and choose the appropriate data type for each of them.

For example, if in a field we will store telephone numbers or a document number, we use "varchar2", not "number" because although they are digits, we do not perform mathematical operations with them. If we will save surnames in a field, and we assume that no surname will exceed 20 characters, we define the field "varchar2 (20)". If in a field we will store prices with two decimal places that will not exceed 999.99 pesos, we define a field of type "number (5,2)", that is, 5 digits in total, with 2 decimal places. If in a field we will store integer values ​​of no more than 3 digits, we define a field of type "number (3,0)".

Comments

Popular posts from this blog

Data Consistency and Inconsistency - What is Difference

Cardinality in DBMS – max/minimum cardinality & examples

Relational Model in DBMS