How to use databases in KDB.AI
This page provides details on how to create, retrieve and delete databases in KDB.AI.
In KDB.AI, the database is the fundamental structure for storing and organizing your data. Each database is a collection of tables, providing data storage, indexing, and API support.
Tip: For the best experience, we recommend reading about databases in KDB.AI first.
Key principles of database management in KDB.AI
To simplify database design/management and prevent naming conflicts, follow the rules below:
- Default database: You don't need to create a database to create tables. If you create a table without specifying a database, it will be placed in a default, undeletable database.
- Unique table names within a database: Tables within a database must have unique names, but different databases can contain tables with the same name. This is similar to the concept of namespaces.
- Cascade deletion: When deleting a database, all child entities (tables) will also be deleted.
Setup
Before you start, make sure you have:
- An active KDB.AI Cloud or Server license
- Installed the latest version of KDB.AI Cloud or Server
- A valid API key if you're using KDB.AI Cloud
- Python Client
Import dependencies
Start by importing the following dependencies:
import sys
import kdbai_client as kdbai
from pprint import pprint # for pretty printing
import pandas as pd
import numpy as np
Create database
To create a database, you can either use the create_database
function (in Python), the createDatabase
function (in q), or send a POST
request to /api/v2/databases
(in REST). Each database must have a unique name.
session.create_database("myDatabase")
curl -s localhost:8082/api/v2/databases -d '{"database" : "myDatabase"}'
`gw set hopen 8082;
gw(`createDatabase;enlist[`database]!enlist `myDatabaseName)
Database name rules
- Max length is 128 characters
- Must contain only alphanumeric characters and underscore
- Must start with an alpha character
Get database
To retrieve the details of a database with a given name, including list of its tables metadata, run the following:
session.database("myDatabase")
curl -s localhost:8082/api/v2/databases/default
`gw set hopen 8082;
gw(`getDatabase;enlist[`database]!enlist `myDatabaseName)
Refresh database
If multiple users can manage your database, to ensure that the list of tables associated with your database is current, use the database.refresh
function (only available in Python):
database.refresh()
N/A
N/A
List databases
Retrieve a list of database names in ascending order, including the default database:
session.databases()
curl -s localhost:8082/api/v2/databases
`gw set hopen 8082;
gw(`listDatabases;`)
Delete database
To delete a database with a given name and all associated tables, run the following command:
db=session.database("myDatabase")
db.drop()
curl -s -X DELETE localhost:8082/api/v2/databases/myDatabase
`gw set hopen 8082;
gw(`deleteDatabase;enlist[`database]!enlist `myDatabaseName)
Next steps
Now that you know how to work with databases, learn how to:
- Create and manage tables in KDB.AI.
- Ingest data.