SlideShare a Scribd company logo
1 of 33
Download to read offline
www.firebase.com.br 1 © 2014 – Carlos H. Cantu 
Understanding Numbers in Firebird 
Carlos H. Cantuwww.firebase.com.br 
www.firebirdnews.org
www.firebase.com.br 2 © 2014 – Carlos H. Cantu 
Who am I? 
• 
Maintainer of www.firebase.com.br and www.firebirdnews.org 
• 
Author of 2 Firebird books published in Brazil 
• 
Software developer for about 30 years 
• 
Organizer of the Firebird Developers Day conference 
• 
Firebird consultant
www.firebase.com.br 3 © 2014 – Carlos H. Cantu 
Do you wannago crazy?!
www.firebase.com.br 4 © 2014 – Carlos H. Cantu 
Warnings! 
1. 
Internal storage type depends on database dialect 
2. 
The dialect has influence in the precision of some types and in the results of calculations 
3. 
Depending on the datatypeused, the retrieved value can be different from the original value!! 
4. 
Decimal separator is always the dot “.”
www.firebase.com.br 5 © 2014 – Carlos H. Cantu 
INTEGER types 
• 
SMALLINT 
– 
16 bits 
– 
between -32.768 and32.767 
• 
INTEGER 
– 
32 bits 
– 
between -2.147.483.648 and 2.147.483.647 
• 
BIGINT 
– 
64 bits 
– 
between -9.223.372.036.854.775.808 and 9.223.372.036.854.775.807 
– 
Only available in dialect 3
www.firebase.com.br 6 © 2014 – Carlos H. Cantu 
FLOATING POINT types 
• 
FLOAT 
– 
32 bits: 1 for signal, 8 for exponent and 23 for the mantissa. 
– 
7 digits of precision 
– 
Between -3.4 x 1038and 3.4 x 1038 
• 
DOUBLE PRECISION 
– 
64 bits: 1 for signal, 11 for exponent and 52 for the mantissa. 
– 
15 digits of precision 
– 
Between -1.7 x 10308and 1.7 x 10308
www.firebase.com.br 7 © 2014 – Carlos H. Cantu 
Pros and cons of floating types 
• 
Stored following the standard defined by the IEEE (Institute of Electrical and Electronics Engineers), with an approximated representation of the real number. 
• 
Calculations uses the math co-processor (faster). 
• 
Not recommended due to lack of precision.
www.firebase.com.br 8 © 2014 – Carlos H. Cantu 
Accuracy in FLOAT/DOUBLE (1/2) 
SQL> select cast(1234567.1234 as float) from rdb$database; 
CAST 
============== 
1234567.1 
Result displayed by IBExpert: 
1234567.125
www.firebase.com.br 9 © 2014 – Carlos H. Cantu 
Imprecision in FLOAT/DOUBLE (2/2) 
SQL> select cast(1234567.4321 as float) from rdb$database; 
CAST 
============== 
1234567.4 
Result displayed by IBExpert: 
1234567.375
www.firebase.com.br 10 © 2014 – Carlos H. Cantu 
Fixed point 
• 
NUMERIC (p,s) / DECIMAL (p,s) 
• 
Is stored occupying either 16, 32 or 64 bits 
• 
p= precision (total digits) [1 <= p <= 18] s= scale (number of digits after the “comma”) 
• 
smust be always lower or equal to p 
• 
If pand sis not informed, the internal type will be INTEGER 
• 
In FB, palways determinates the minimum number of stored digits (not follow the standard) 
• 
The retrieved value is always exactly equal to the original value!
www.firebase.com.br 11 © 2014 – Carlos H. Cantu 
Internal storage of NUMERIC and DECIMAL 
PRECISION 
INTERNAL TYPE 
DIALECT 3 
DIALECT 1 
1..4 
NUMERIC 
SMALLINT (*) 
SMALLINT 
1..4 
DECIMAL 
INTEGER (*) 
INTEGER 
5..9 
NUMERIC e DECIMAL 
INTEGER 
INTEGER 
10..18 
NUMERIC e DECIMAL 
BIGINT 
DOUBLE PRECISION(!) 
In Firebird, DECIMAL andNUMERICsare thesamething, ifp < 10. 
(*) In this case, the range of supported values are different compared to NUMERIC and DECIMAL
www.firebase.com.br 12 © 2014 – Carlos H. Cantu 
Determining the capacity of chosen numeric/decimals 
1. 
Check the internal type used depending on the precision (p) of the field. 
2. 
Check the range of values supported by the internal type. 
3. 
Divide the min and max values by 10s to know the effective range of accepted values for the field.
www.firebase.com.br 13 © 2014 – Carlos H. Cantu 
Determining the capacity of chosen numeric/decimals 
Example: 
1. 
NUMERIC (9,2) or DECIMAL (9,2) 
2. 
Internally stored as INTEGER 
3. 
Integer = -2.147.483.648 to 2.147.483.647 
4. 
As s = 2, divide by 102 
5. 
Accepted range for a field declared as NUMERIC/DECIMAL (9,2) = -21.474.836,48 to 21.474.836,47
www.firebase.com.br 14 © 2014 – Carlos H. Cantu 
Testing the limits of numeric/decimal 
SQL> select cast(-21474836.48 as numeric (9,2)), cast(-21474836.48 as decimal (9,2)) from rdb$database; 
CAST CAST 
============ ============ 
-21474836.48 -21474836.48 
SQL> select cast(-21474836.49as numeric (9,2)), cast(-21474836.49as decimal (9,2)) from rdb$database; 
CAST CAST 
============ ============ 
Statement failed, SQLSTATE = 22003 
arithmetic exception, numeric overflow, or string truncation 
-numeric value is out of range
www.firebase.com.br 15 © 2014 – Carlos H. Cantu 
Testing the limits of numeric/decimal 
SQL> select cast(32768 as decimal(4,0)) from rdb$database; --integer 
CAST 
============ 
32768 
SQL> select cast(32768 as numeric(4,0)) from rdb$database; --smallint 
CAST 
======= 
Statement failed, SQLSTATE = 22003 
arithmetic exception, numeric overflow, or string truncation 
-numeric value is out of range
www.firebase.com.br 16 © 2014 – Carlos H. Cantu 
Moving from dialect 1 to 3 
• 
Is there any field declared as NUMERIC or DECIMALwith p > 9? 
– 
No: there will be no problem at all 
– 
Yes: you may have problems! 
• 
NUMERIC and DECIMAL with p > 9 are stored as double precision in dialect 1 and the existing fields will stay like this if the DB is “migrated” to dialect 3 using gfix-sql_dialect3. 
• 
New fields declared as NUM/DEC with p > 9, created after the DB was converted to dialect 3 will use BIGINTinternally. 
• 
Recommended solution: create a new DB using a script and pump the data from old to new database.
www.firebase.com.br 17 © 2014 – Carlos H. Cantu 
Integer divisions 
• 
Dialect 1, dividing intby intresults in double precisionI.e.: 1/3 = 0,3333333333333 
• 
Dialect 3, divide intby intresults in integerI.e.: 1/3 = 0
www.firebase.com.br 18 © 2014 – Carlos H. Cantu 
Division/Multiplication of fixed point numerics 
• 
In dialect 1, the division will always return a double precision. 
• 
In dialect 3, the result will be a type withp= 18 and s= sum of the scales of the involved types. 
SQL> select cast(0.33 as numeric (9,2))/ cast (1 as numeric(9,2)) from rdb$database; 
DIVIDE 
===================== 
0.3300
www.firebase.com.br 19 © 2014 – Carlos H. Cantu 
Division/Multiplication of fixed point numerics 
SQL> select (3.00/1.00*3.5)*2.00as totalfrom rdb$database; 
TOTAL 
===================== 
21.0000000 
SQL> select (3.00/1.00/3.5)/2.00as totalfrom rdb$database; 
TOTAL 
===================== 
0.4285700
www.firebase.com.br 20 © 2014 – Carlos H. Cantu 
Division/Multiplication of fixed point numerics 
• 
There can be overflows, specially with calculations involving multiple arguments! 
select cast(1 as numeric(15,6))* cast(1 as numeric(9,8)) * 
cast(1 as numeric(15,5)) from rdb$database 
~ 1.000000* 1.00000000* 1.00000 
Integer overflow. The result of an integer operation caused the most significant bit of the result to carry.
www.firebase.com.br 21 © 2014 – Carlos H. Cantu 
Addition/Subtraction of fixed point numbers 
• 
Result will have sequal the biggest scale of the bigger member of the operation. 
• 
In dialect 1, result will always have p= 9 
• 
In dialect 3, result will always have p= 18 
SQL> select cast(1 as numeric(9,2)) + 
cast(2 as integer) from rdb$database; 
ADD 
===================== 
3.00 
SQL> select cast(0.5 as numeric(9,2)) – 
cast(1 as numeric(9,3)) from rdb$database; 
SUBTRACT 
===================== 
-0.500
www.firebase.com.br 22 © 2014 – Carlos H. Cantu 
Tips summary 
• 
Always create the database in dialect 3, and connect to it using the same dialect. 
• 
For “monetary” fields, choose numericor decimalto guarantee the accuracy. 
• 
When need to store numbers with variable scale (s), choose double precision. 
• 
To migrate a DB from dialect 1 to 3, prefers to PUMP the data instead of using gfix. 
• 
Take care with overflows in calculations involving numeric/decimal.
www.firebase.com.br 23 © 2014 – Carlos H. Cantu 
Curiosities 
INDEXES 
• 
Numbers are stored in keys as double precision (exception to the rule is BIGINT) 
• 
Pros: 
– 
For numeric/decimal, allows changing p or s without needing to reindex 
– 
For smallint/integer, allows converting between the types or to a type having a scale (s) without need to reindex 
• 
Obs: Due to lack of precision of the double precision, the search if done in an interval between the bigger previous value and the lower next value related to the searched value. 
GENERATORS 
• 
Dialect 1 = integer 
• 
Dialect 3 = bigint
www.firebase.com.br 24 © 2014 – Carlos H. Cantu 
Curiosities (do you wannago more crazy??) 
CHECK CONSTRAINTS and CLIENT DIALECTS 
The rules applied by a check constraint are based on the dialect used by the client connection in the time the constraint was created. 
Ex: check (int1 / int2) > 0.5 (rule created with dialect 1 connection) 
When connecting to the DB using dialect 3: 
Insert ... (int1, int2) values (2, 3); --Success! ~ 0.66666666 
Ex: check (int1 / int2) > 0.5 (rule created with dialect 3 connection) 
Insert ... (int1, int2) values (2, 3); --FAILURE! ~ 0
www.firebase.com.br 25 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
• 
Raising the scale means shortening the range of accepted values 
I.e.: 
numeric (9,2): range -21.474.836,48 to21.474.836,47 
numeric (9,3): range -2.147.483,648 to2.147.483,647 
This operation is not defined for system tables. Unsuccessful metadata update. New scale specified for column AFIELD must be at most 2.
www.firebase.com.br 26 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
• 
Changing the scale of (9,2) to 3. 
• 
Solutions: 
– 
Create new field declared as (9,3) 
– 
Copy the values to the new field 
– 
Drop the old field 
– 
Rename the new field as the old one 
• 
Changing to (10,3) 
– 
Problem if there are indexes defined for that field, since the internal type changes to bigint!
www.firebase.com.br 27 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
create table test (afield numeric (9,2)); 
commit; 
insert into test values (10.12); commit; alter table test alter afield type numeric (9,3); 
This operation is not defined for system tables. Unsuccessful metadata update. New scale specified for column AFIELD must be at most 2. 
alter table test alter afield type numeric (10,3); commit; 
update test set afield = 10.123; commit; 
select afield from test; commit; 
Result:10.123
www.firebase.com.br 28 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
alter table test 
alter afieldtype numeric (10,2); commit; 
select afield from test; 
commit; 
Result:10.12 
alter table test 
alter afield type numeric (11,3); commit; 
select afield from test; 
commit; 
Result:10.123
www.firebase.com.br 29 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
alter table test alter afield type numeric (10,2); commit; 
select afield from test; commit; 
Result:10.12 
/* “Dumb” Update */ 
update test set afield = afield; commit; 
alter table test 
alter afield type numeric (11,3); commit; 
select afield from test; commit; 
Result:10.120
www.firebase.com.br 30 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
“Hardcore” solution: 
update RDB$FIELDS setRDB$FIELD_SCALE = -3where RDB$FIELD_NAME = 'RDB$nnn'; 
Warning! 
• 
Be sure that the existing values “fits” in the new range, otherwise some records will be inaccessible(corruption). 
• 
Will not work in Firebird 3!
www.firebase.com.br 31 © 2014 – Carlos H. Cantu 
Additional attention! 
• 
When changing the “size” of an existing field, it can be identified with a different type by the “language/access technology” used in the client application.
www.firebase.com.br 32 © 2014 – Carlos H. Cantu 
Roudings 
• 
Firebird uses “standard rounding”: -Chose what digit will be the limit-Add 1 if the next digit is >= 5-Don’t change the digit if the next is < 5 
I.e.: 
select cast(123.45 as numeric (9,1)) from rdb$database–result: 123.5 
select cast(123.42 as numeric (9,1)) from rdb$database–result: 123.4
www.firebase.com.br 33 © 2014 – Carlos H. Cantu 
FIM 
Questions? 
www.firebase.com.br 
www.firebirdnews.org 
ThankstoallConferencesponsors:

More Related Content

Similar to Understanding Numbers in Firebird SQL

Data Analytics and Simulation in Parallel with MATLAB*
Data Analytics and Simulation in Parallel with MATLAB*Data Analytics and Simulation in Parallel with MATLAB*
Data Analytics and Simulation in Parallel with MATLAB*Intel® Software
 
Big data skew
Big data skewBig data skew
Big data skewayan ray
 
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014Dave Stokes
 
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dnU3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dnmit23cs
 
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2DataStax Academy
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types Hemantha Kulathilake
 
Float Data Type in C.pdf
Float Data Type in C.pdfFloat Data Type in C.pdf
Float Data Type in C.pdfSudhanshiBakre1
 
ScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
ScaleGraph - A High-Performance Library for Billion-Scale Graph AnalyticsScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
ScaleGraph - A High-Performance Library for Billion-Scale Graph AnalyticsToyotaro Suzumura
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen TatarynovFwdays
 
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docx
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docxSheet11-Which of the following is a nonrenewable resourceaSolar e.docx
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docxbagotjesusa
 
QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...Scality
 
Creating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQLCreating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQLMind The Firebird
 
Aerospike Go Language Client
Aerospike Go Language ClientAerospike Go Language Client
Aerospike Go Language ClientSayyaparaju Sunil
 
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...Lucidworks
 
Virtual training optimizing the tick stack
Virtual training  optimizing the tick stackVirtual training  optimizing the tick stack
Virtual training optimizing the tick stackInfluxData
 
MongoDB Days UK: Tales from the Field
MongoDB Days UK: Tales from the FieldMongoDB Days UK: Tales from the Field
MongoDB Days UK: Tales from the FieldMongoDB
 
OPTIMIZING THE TICK STACK
OPTIMIZING THE TICK STACKOPTIMIZING THE TICK STACK
OPTIMIZING THE TICK STACKInfluxData
 
Benchmarking Apache Druid
Benchmarking Apache Druid Benchmarking Apache Druid
Benchmarking Apache Druid Matt Sarrel
 
Benchmarking Apache Druid
Benchmarking Apache DruidBenchmarking Apache Druid
Benchmarking Apache DruidImply
 

Similar to Understanding Numbers in Firebird SQL (20)

Data Analytics and Simulation in Parallel with MATLAB*
Data Analytics and Simulation in Parallel with MATLAB*Data Analytics and Simulation in Parallel with MATLAB*
Data Analytics and Simulation in Parallel with MATLAB*
 
Big data skew
Big data skewBig data skew
Big data skew
 
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
 
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dnU3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
 
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
 
Introduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimizationIntroduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimization
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types
 
Float Data Type in C.pdf
Float Data Type in C.pdfFloat Data Type in C.pdf
Float Data Type in C.pdf
 
ScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
ScaleGraph - A High-Performance Library for Billion-Scale Graph AnalyticsScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
ScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
 
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docx
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docxSheet11-Which of the following is a nonrenewable resourceaSolar e.docx
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docx
 
QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...
 
Creating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQLCreating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQL
 
Aerospike Go Language Client
Aerospike Go Language ClientAerospike Go Language Client
Aerospike Go Language Client
 
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
 
Virtual training optimizing the tick stack
Virtual training  optimizing the tick stackVirtual training  optimizing the tick stack
Virtual training optimizing the tick stack
 
MongoDB Days UK: Tales from the Field
MongoDB Days UK: Tales from the FieldMongoDB Days UK: Tales from the Field
MongoDB Days UK: Tales from the Field
 
OPTIMIZING THE TICK STACK
OPTIMIZING THE TICK STACKOPTIMIZING THE TICK STACK
OPTIMIZING THE TICK STACK
 
Benchmarking Apache Druid
Benchmarking Apache Druid Benchmarking Apache Druid
Benchmarking Apache Druid
 
Benchmarking Apache Druid
Benchmarking Apache DruidBenchmarking Apache Druid
Benchmarking Apache Druid
 

More from Mind The Firebird

Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tablesMind The Firebird
 
Using Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easilyUsing Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easilyMind The Firebird
 
A year in the life of Firebird .Net provider
A year in the life of Firebird .Net providerA year in the life of Firebird .Net provider
A year in the life of Firebird .Net providerMind The Firebird
 
How Firebird transactions work
How Firebird transactions workHow Firebird transactions work
How Firebird transactions workMind The Firebird
 
Using ТРСС to study Firebird performance
Using ТРСС to study Firebird performanceUsing ТРСС to study Firebird performance
Using ТРСС to study Firebird performanceMind The Firebird
 
Firebird Performance counters in details
Firebird Performance counters in detailsFirebird Performance counters in details
Firebird Performance counters in detailsMind The Firebird
 
Threading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyondThreading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyondMind The Firebird
 
New SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad KhorsunNew SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad KhorsunMind The Firebird
 
Orphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and LoggingOrphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and LoggingMind The Firebird
 
Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016Mind The Firebird
 
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...Mind The Firebird
 
Working with Large Firebird databases
Working with Large Firebird databasesWorking with Large Firebird databases
Working with Large Firebird databasesMind The Firebird
 
Stored procedures in Firebird
Stored procedures in FirebirdStored procedures in Firebird
Stored procedures in FirebirdMind The Firebird
 
Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...Mind The Firebird
 
Continuous Database Monitoring with the Trace API
Continuous Database Monitoring with the Trace APIContinuous Database Monitoring with the Trace API
Continuous Database Monitoring with the Trace APIMind The Firebird
 

More from Mind The Firebird (20)

Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tables
 
Using Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easilyUsing Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easily
 
A year in the life of Firebird .Net provider
A year in the life of Firebird .Net providerA year in the life of Firebird .Net provider
A year in the life of Firebird .Net provider
 
How Firebird transactions work
How Firebird transactions workHow Firebird transactions work
How Firebird transactions work
 
SuperServer in Firebird 3
SuperServer in Firebird 3SuperServer in Firebird 3
SuperServer in Firebird 3
 
Copycat presentation
Copycat presentationCopycat presentation
Copycat presentation
 
Using ТРСС to study Firebird performance
Using ТРСС to study Firebird performanceUsing ТРСС to study Firebird performance
Using ТРСС to study Firebird performance
 
Overview of RedDatabase 2.5
Overview of RedDatabase 2.5Overview of RedDatabase 2.5
Overview of RedDatabase 2.5
 
Firebird Performance counters in details
Firebird Performance counters in detailsFirebird Performance counters in details
Firebird Performance counters in details
 
Threading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyondThreading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyond
 
New SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad KhorsunNew SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad Khorsun
 
Orphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and LoggingOrphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and Logging
 
Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016
 
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
 
Working with Large Firebird databases
Working with Large Firebird databasesWorking with Large Firebird databases
Working with Large Firebird databases
 
Stored procedures in Firebird
Stored procedures in FirebirdStored procedures in Firebird
Stored procedures in Firebird
 
Firebird on Linux
Firebird on LinuxFirebird on Linux
Firebird on Linux
 
Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...
 
Firebird meets NoSQL
Firebird meets NoSQLFirebird meets NoSQL
Firebird meets NoSQL
 
Continuous Database Monitoring with the Trace API
Continuous Database Monitoring with the Trace APIContinuous Database Monitoring with the Trace API
Continuous Database Monitoring with the Trace API
 

Recently uploaded

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 

Recently uploaded (20)

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 

Understanding Numbers in Firebird SQL

  • 1. www.firebase.com.br 1 © 2014 – Carlos H. Cantu Understanding Numbers in Firebird Carlos H. Cantuwww.firebase.com.br www.firebirdnews.org
  • 2. www.firebase.com.br 2 © 2014 – Carlos H. Cantu Who am I? • Maintainer of www.firebase.com.br and www.firebirdnews.org • Author of 2 Firebird books published in Brazil • Software developer for about 30 years • Organizer of the Firebird Developers Day conference • Firebird consultant
  • 3. www.firebase.com.br 3 © 2014 – Carlos H. Cantu Do you wannago crazy?!
  • 4. www.firebase.com.br 4 © 2014 – Carlos H. Cantu Warnings! 1. Internal storage type depends on database dialect 2. The dialect has influence in the precision of some types and in the results of calculations 3. Depending on the datatypeused, the retrieved value can be different from the original value!! 4. Decimal separator is always the dot “.”
  • 5. www.firebase.com.br 5 © 2014 – Carlos H. Cantu INTEGER types • SMALLINT – 16 bits – between -32.768 and32.767 • INTEGER – 32 bits – between -2.147.483.648 and 2.147.483.647 • BIGINT – 64 bits – between -9.223.372.036.854.775.808 and 9.223.372.036.854.775.807 – Only available in dialect 3
  • 6. www.firebase.com.br 6 © 2014 – Carlos H. Cantu FLOATING POINT types • FLOAT – 32 bits: 1 for signal, 8 for exponent and 23 for the mantissa. – 7 digits of precision – Between -3.4 x 1038and 3.4 x 1038 • DOUBLE PRECISION – 64 bits: 1 for signal, 11 for exponent and 52 for the mantissa. – 15 digits of precision – Between -1.7 x 10308and 1.7 x 10308
  • 7. www.firebase.com.br 7 © 2014 – Carlos H. Cantu Pros and cons of floating types • Stored following the standard defined by the IEEE (Institute of Electrical and Electronics Engineers), with an approximated representation of the real number. • Calculations uses the math co-processor (faster). • Not recommended due to lack of precision.
  • 8. www.firebase.com.br 8 © 2014 – Carlos H. Cantu Accuracy in FLOAT/DOUBLE (1/2) SQL> select cast(1234567.1234 as float) from rdb$database; CAST ============== 1234567.1 Result displayed by IBExpert: 1234567.125
  • 9. www.firebase.com.br 9 © 2014 – Carlos H. Cantu Imprecision in FLOAT/DOUBLE (2/2) SQL> select cast(1234567.4321 as float) from rdb$database; CAST ============== 1234567.4 Result displayed by IBExpert: 1234567.375
  • 10. www.firebase.com.br 10 © 2014 – Carlos H. Cantu Fixed point • NUMERIC (p,s) / DECIMAL (p,s) • Is stored occupying either 16, 32 or 64 bits • p= precision (total digits) [1 <= p <= 18] s= scale (number of digits after the “comma”) • smust be always lower or equal to p • If pand sis not informed, the internal type will be INTEGER • In FB, palways determinates the minimum number of stored digits (not follow the standard) • The retrieved value is always exactly equal to the original value!
  • 11. www.firebase.com.br 11 © 2014 – Carlos H. Cantu Internal storage of NUMERIC and DECIMAL PRECISION INTERNAL TYPE DIALECT 3 DIALECT 1 1..4 NUMERIC SMALLINT (*) SMALLINT 1..4 DECIMAL INTEGER (*) INTEGER 5..9 NUMERIC e DECIMAL INTEGER INTEGER 10..18 NUMERIC e DECIMAL BIGINT DOUBLE PRECISION(!) In Firebird, DECIMAL andNUMERICsare thesamething, ifp < 10. (*) In this case, the range of supported values are different compared to NUMERIC and DECIMAL
  • 12. www.firebase.com.br 12 © 2014 – Carlos H. Cantu Determining the capacity of chosen numeric/decimals 1. Check the internal type used depending on the precision (p) of the field. 2. Check the range of values supported by the internal type. 3. Divide the min and max values by 10s to know the effective range of accepted values for the field.
  • 13. www.firebase.com.br 13 © 2014 – Carlos H. Cantu Determining the capacity of chosen numeric/decimals Example: 1. NUMERIC (9,2) or DECIMAL (9,2) 2. Internally stored as INTEGER 3. Integer = -2.147.483.648 to 2.147.483.647 4. As s = 2, divide by 102 5. Accepted range for a field declared as NUMERIC/DECIMAL (9,2) = -21.474.836,48 to 21.474.836,47
  • 14. www.firebase.com.br 14 © 2014 – Carlos H. Cantu Testing the limits of numeric/decimal SQL> select cast(-21474836.48 as numeric (9,2)), cast(-21474836.48 as decimal (9,2)) from rdb$database; CAST CAST ============ ============ -21474836.48 -21474836.48 SQL> select cast(-21474836.49as numeric (9,2)), cast(-21474836.49as decimal (9,2)) from rdb$database; CAST CAST ============ ============ Statement failed, SQLSTATE = 22003 arithmetic exception, numeric overflow, or string truncation -numeric value is out of range
  • 15. www.firebase.com.br 15 © 2014 – Carlos H. Cantu Testing the limits of numeric/decimal SQL> select cast(32768 as decimal(4,0)) from rdb$database; --integer CAST ============ 32768 SQL> select cast(32768 as numeric(4,0)) from rdb$database; --smallint CAST ======= Statement failed, SQLSTATE = 22003 arithmetic exception, numeric overflow, or string truncation -numeric value is out of range
  • 16. www.firebase.com.br 16 © 2014 – Carlos H. Cantu Moving from dialect 1 to 3 • Is there any field declared as NUMERIC or DECIMALwith p > 9? – No: there will be no problem at all – Yes: you may have problems! • NUMERIC and DECIMAL with p > 9 are stored as double precision in dialect 1 and the existing fields will stay like this if the DB is “migrated” to dialect 3 using gfix-sql_dialect3. • New fields declared as NUM/DEC with p > 9, created after the DB was converted to dialect 3 will use BIGINTinternally. • Recommended solution: create a new DB using a script and pump the data from old to new database.
  • 17. www.firebase.com.br 17 © 2014 – Carlos H. Cantu Integer divisions • Dialect 1, dividing intby intresults in double precisionI.e.: 1/3 = 0,3333333333333 • Dialect 3, divide intby intresults in integerI.e.: 1/3 = 0
  • 18. www.firebase.com.br 18 © 2014 – Carlos H. Cantu Division/Multiplication of fixed point numerics • In dialect 1, the division will always return a double precision. • In dialect 3, the result will be a type withp= 18 and s= sum of the scales of the involved types. SQL> select cast(0.33 as numeric (9,2))/ cast (1 as numeric(9,2)) from rdb$database; DIVIDE ===================== 0.3300
  • 19. www.firebase.com.br 19 © 2014 – Carlos H. Cantu Division/Multiplication of fixed point numerics SQL> select (3.00/1.00*3.5)*2.00as totalfrom rdb$database; TOTAL ===================== 21.0000000 SQL> select (3.00/1.00/3.5)/2.00as totalfrom rdb$database; TOTAL ===================== 0.4285700
  • 20. www.firebase.com.br 20 © 2014 – Carlos H. Cantu Division/Multiplication of fixed point numerics • There can be overflows, specially with calculations involving multiple arguments! select cast(1 as numeric(15,6))* cast(1 as numeric(9,8)) * cast(1 as numeric(15,5)) from rdb$database ~ 1.000000* 1.00000000* 1.00000 Integer overflow. The result of an integer operation caused the most significant bit of the result to carry.
  • 21. www.firebase.com.br 21 © 2014 – Carlos H. Cantu Addition/Subtraction of fixed point numbers • Result will have sequal the biggest scale of the bigger member of the operation. • In dialect 1, result will always have p= 9 • In dialect 3, result will always have p= 18 SQL> select cast(1 as numeric(9,2)) + cast(2 as integer) from rdb$database; ADD ===================== 3.00 SQL> select cast(0.5 as numeric(9,2)) – cast(1 as numeric(9,3)) from rdb$database; SUBTRACT ===================== -0.500
  • 22. www.firebase.com.br 22 © 2014 – Carlos H. Cantu Tips summary • Always create the database in dialect 3, and connect to it using the same dialect. • For “monetary” fields, choose numericor decimalto guarantee the accuracy. • When need to store numbers with variable scale (s), choose double precision. • To migrate a DB from dialect 1 to 3, prefers to PUMP the data instead of using gfix. • Take care with overflows in calculations involving numeric/decimal.
  • 23. www.firebase.com.br 23 © 2014 – Carlos H. Cantu Curiosities INDEXES • Numbers are stored in keys as double precision (exception to the rule is BIGINT) • Pros: – For numeric/decimal, allows changing p or s without needing to reindex – For smallint/integer, allows converting between the types or to a type having a scale (s) without need to reindex • Obs: Due to lack of precision of the double precision, the search if done in an interval between the bigger previous value and the lower next value related to the searched value. GENERATORS • Dialect 1 = integer • Dialect 3 = bigint
  • 24. www.firebase.com.br 24 © 2014 – Carlos H. Cantu Curiosities (do you wannago more crazy??) CHECK CONSTRAINTS and CLIENT DIALECTS The rules applied by a check constraint are based on the dialect used by the client connection in the time the constraint was created. Ex: check (int1 / int2) > 0.5 (rule created with dialect 1 connection) When connecting to the DB using dialect 3: Insert ... (int1, int2) values (2, 3); --Success! ~ 0.66666666 Ex: check (int1 / int2) > 0.5 (rule created with dialect 3 connection) Insert ... (int1, int2) values (2, 3); --FAILURE! ~ 0
  • 25. www.firebase.com.br 25 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields • Raising the scale means shortening the range of accepted values I.e.: numeric (9,2): range -21.474.836,48 to21.474.836,47 numeric (9,3): range -2.147.483,648 to2.147.483,647 This operation is not defined for system tables. Unsuccessful metadata update. New scale specified for column AFIELD must be at most 2.
  • 26. www.firebase.com.br 26 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields • Changing the scale of (9,2) to 3. • Solutions: – Create new field declared as (9,3) – Copy the values to the new field – Drop the old field – Rename the new field as the old one • Changing to (10,3) – Problem if there are indexes defined for that field, since the internal type changes to bigint!
  • 27. www.firebase.com.br 27 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields create table test (afield numeric (9,2)); commit; insert into test values (10.12); commit; alter table test alter afield type numeric (9,3); This operation is not defined for system tables. Unsuccessful metadata update. New scale specified for column AFIELD must be at most 2. alter table test alter afield type numeric (10,3); commit; update test set afield = 10.123; commit; select afield from test; commit; Result:10.123
  • 28. www.firebase.com.br 28 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields alter table test alter afieldtype numeric (10,2); commit; select afield from test; commit; Result:10.12 alter table test alter afield type numeric (11,3); commit; select afield from test; commit; Result:10.123
  • 29. www.firebase.com.br 29 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields alter table test alter afield type numeric (10,2); commit; select afield from test; commit; Result:10.12 /* “Dumb” Update */ update test set afield = afield; commit; alter table test alter afield type numeric (11,3); commit; select afield from test; commit; Result:10.120
  • 30. www.firebase.com.br 30 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields “Hardcore” solution: update RDB$FIELDS setRDB$FIELD_SCALE = -3where RDB$FIELD_NAME = 'RDB$nnn'; Warning! • Be sure that the existing values “fits” in the new range, otherwise some records will be inaccessible(corruption). • Will not work in Firebird 3!
  • 31. www.firebase.com.br 31 © 2014 – Carlos H. Cantu Additional attention! • When changing the “size” of an existing field, it can be identified with a different type by the “language/access technology” used in the client application.
  • 32. www.firebase.com.br 32 © 2014 – Carlos H. Cantu Roudings • Firebird uses “standard rounding”: -Chose what digit will be the limit-Add 1 if the next digit is >= 5-Don’t change the digit if the next is < 5 I.e.: select cast(123.45 as numeric (9,1)) from rdb$database–result: 123.5 select cast(123.42 as numeric (9,1)) from rdb$database–result: 123.4
  • 33. www.firebase.com.br 33 © 2014 – Carlos H. Cantu FIM Questions? www.firebase.com.br www.firebirdnews.org ThankstoallConferencesponsors: