SlideShare uma empresa Scribd logo
1 de 33
Baixar para ler 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:

Mais conteúdo relacionado

Semelhante a Understanding Numbers in Firebird SQL

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 DruidBenchmarking Apache Druid
Benchmarking Apache DruidImply
 
Benchmarking Apache Druid
Benchmarking Apache Druid Benchmarking Apache Druid
Benchmarking Apache Druid Matt Sarrel
 
Unit3 overview of_c_programming
Unit3 overview of_c_programmingUnit3 overview of_c_programming
Unit3 overview of_c_programmingCapuchino HuiNing
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12Rabia Khalid
 

Semelhante a Understanding Numbers in Firebird SQL (20)

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 DruidBenchmarking Apache Druid
Benchmarking Apache Druid
 
Benchmarking Apache Druid
Benchmarking Apache Druid Benchmarking Apache Druid
Benchmarking Apache Druid
 
Unit3 overview of_c_programming
Unit3 overview of_c_programmingUnit3 overview of_c_programming
Unit3 overview of_c_programming
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 

Mais de Mind 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
 
Firebird 3 Windows Functions
Firebird 3 Windows  FunctionsFirebird 3 Windows  Functions
Firebird 3 Windows FunctionsMind The Firebird
 

Mais de Mind The Firebird (20)

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
 
Firebird 3 Windows Functions
Firebird 3 Windows  FunctionsFirebird 3 Windows  Functions
Firebird 3 Windows Functions
 

Último

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 

Último (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 

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: