Decimal Data Type in Cassandra Query Language - Cassandra / CQL Tutorial

Decimal Data Type in Cassandra Query Language ( CQL) 

Decimal Data Type in Cassandra is used to save integer and float values. Was checking the precision or digits limit that we can save like in other databases such as SQL Server has 38 but I was able to save more than 100 digits. If you have use decimal data type in table, you can save integer or decimal values. One more point to note, When I tried to save decimal such as .123 ( dot 123), it threw below error. If you need to save decimal like that start with zero e.g 0.123.
SyntaxException: line 1:47 no viable alternative at input '.' (...DecimalValue) Values ( 1, [.]...)

Also if we try to use decimal data type with scale such as decimal (12,2) , Cassandra throws below error. The below statment works fine in relational databases such as SQL Server.
CQLSH:techbrotherstutorial>CREATE TABLE tbs 
             ( 
                          id           INT PRIMARY KEY, 
                          decimalvalue DECIMAL(12,2) 
             );

SyntaxException: line 1:59 no viable alternative at input '(' (... int Primary Key, DecimalValue [Decimal](...)

Create table with decimal data type in Cassandra Query Language ( CQL) by using below command.
CQLSH:techbrotherstutorial>CREATE TABLE tbs 
             ( 
                          id           INT PRIMARY KEY, 
                          decimalvalue DECIMAL 
             );

Let's insert few records in table by using Cassandra Query Language ( CQL)

CQLSH:techbrotherstutorial>INSERT INTO tbs
            (
                        id,
                        decimalvalue
            )
            VALUES
            (
                        1,
                        22222222
            );INSERT INTO tbs
            (
                        id,
                        decimalvalue
            )
            VALUES
            (
                        2,
                        50.45
            );INSERT INTO tbs
            (
                        id,
                        decimalvalue
            )
            VALUES
            (
                        3,
                        0.129845
            );INSERT INTO tbs
            (
                        id,
                        decimalvalue
            )
            VALUES
            (
                        4,
                        22222222222222222222222222222222222222222222
            );

Check the data in table by using Select query in CQL
CQLSH:techbrotherstutorial>SELECT * 
FROM   tbs; 

No comments:

Post a Comment