Search code examples
databasecassandradistributed-databaseyugabytedb

Can I write a program to see if a table exists in YugaByte's YCQL (Cassandra) api?


Is there a programmatic way to check if a table exists in YugaByte's YCQL (Cassandra) api?

For example, in Postgres one can do something like:

How to check if a table exists in a given schema

SELECT EXISTS (
   SELECT 1
   FROM   information_schema.tables 
   WHERE  table_schema = 'schema_name'
   AND    table_name = 'table_name'
   );

Is there an equivalent in YCQL?"


Solution

  • Yes, you can do the same for YugaByte DB's YCQL. Here's an example that shows how to check for the existence of a keyspace and that of a table via cqlsh.

    Setup:

    cqlsh> CREATE KEYSPACE IF NOT EXISTS ksp;
    
    cqlsh> CREATE TABLE IF NOT EXISTS ksp.t(k int PRIMARY KEY, v int);
    

    To check if a keyspace exists

    cqlsh> select count(*) from system_schema.keyspaces 
           where keyspace_name = 'ksp';
    
    
     count
    -------
         1
    
    (1 rows)
    cqlsh> select count(*) from system_schema.keyspaces 
           where keyspace_name = 'non-existent-ksp';
    
     count
    -------
         0
    
    (1 rows)
    

    To check if a table exists

    cqlsh> select count(*) from system_schema.tables 
           where keyspace_name = 'ksp' and table_name = 't';
    
     count
    -------
         1
    
    (1 rows)
    cqlsh> select count(*) from system_schema.tables 
            where keyspace_name = 'ksp' and table_name = 'non-existent-t';
    
     count
    -------
         0
    
    (1 rows)