DEV Community

Sualeh Fatehi
Sualeh Fatehi

Posted on • Updated on

Use ChatGPT to Explore Your Database Schema

SchemaCrawler is a relational database exploration tool. It obtains database schema metadata such as tables, stored procedures, foreign keys, triggers and so on, and makes them available for search. The traditional way to use SchemaCrawler has been the command-line or an interactive shell.

SchemaCrawler is now integrated with ChatGPT to provide an interactive way to interrogate your database schema metadata. When you start SchemaCrawler with the “chatgpt” command, you will have an interactive chat shell with ChatGPT, enhanced with information about your database metadata. No database information will be sent to OpenAI servers.

You can try prompts such as the following ones:

  • "List all tables"
  • "Describe the Track table"
  • "What are the indexes on the Track table?"
  • "What are the Track columns?"
  • "What is the Track primary key?"
  • "Show me the triggers on Track"
  • "Find the parents of Track"
  • "What are the dependents of Album?"

To quit the console, you can type something like:

  • "I think I have everything I need" or simply, "done", "exit" or "quit".

To start using this integration, you will need to create your own OpenAI API key. Then download a SQLite database called "chinook-database-2.0.1.sqlite" into your current directory.

Run this command:

docker run \
--mount type=bind,source="${PWD}",target=/home/schcrwlr \
--rm -it \
schemacrawler/schemacrawler \
/opt/schemacrawler/bin/schemacrawler.sh \
--server=sqlite \
--database=chinook-database-2.0.1.sqlite \
--info-level=standard \
--command=chatgpt \
--api-key YOUR_OPENAI_API_KEY
Enter fullscreen mode Exit fullscreen mode

(If you are using PowerShell on Windows, replace the trailing backslash on each line with a back-tick, and map the current directory differently.)

At the prompt, enter some of the commands above.

After you have got this working, use the SchemaCrawler command-line to connect to your own database, and explore it using a natural language interface courtesy of ChatGPT.

Top comments (11)

Collapse
 
adriens profile image
adriens

Looks like we have a rainy week-end here... so asking for my favorit database assistant some help about a little database :

Image description

This is pretty exciting... and I have some fdedback/improvements : where would like me to drop them to you ?

Collapse
 
adriens profile image
adriens

Dedciated blog post on its way 🚀

Collapse
 
adriens profile image
adriens

AWESOME @sualeh , I'll give it a try very soon. I have to purchase a key.
BTW : would you share with us :

  • Which model you used undehood ? gpt-3.5 turbo like ?
  • How many tokens were used to standard database exploration ?
Collapse
 
sualeh profile image
Sualeh Fatehi

The default model is gpt-3.5-turbo. I spent $0.07 during development, so you can imagine that it does not use too many tokens.

Collapse
 
d3n_57 profile image
d-3-n

You probably didn't spend 0.07$. I was wondering why I still didn't get access to gpt4, b/c allegedly they have opened the access to everyone they have billed. It turns out they have never billed me anything. My guess is b/c trx costs would outweigh the value of the trx.

Collapse
 
adriens profile image
adriens

Guess I'll create a dedicated story telling, I already a target database I could inspect this way.
BtW, is the duckdb driver fixed ?

Thread Thread
 
sualeh profile image
Sualeh Fatehi

That will be great!

The DuckDB driver is not fixed yet.

Thread Thread
 
adriens profile image
adriens

ARFF ;-(

Collapse
 
vijai_bishnoi profile image
Info Comment hidden by post author - thread only accessible via permalink
Vijay Bishnoi
Collapse
 
mohanrajlearn profile image
Info Comment hidden by post author - thread only accessible via permalink
Mohanraj

I am unable to access uploaded .csv file for chatbot process by using openai api key in nextjs project.Kindly suggest me any libraries or logic to moveahead.

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more