457

I'm just getting started learning SQLite. It would be nice to be able to see the details for a table, like MySQL's DESCRIBE [table]. PRAGMA table_info [table] isn't good enough, as it only has basic information (for example, it doesn't show if a column is a field of some sort or not). Does SQLite have a way to do this?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Matthew
  • 25,652
  • 26
  • 93
  • 158

6 Answers6

575

The SQLite command line utility has a .schema TABLENAME command that shows you the create statements.

Ned Batchelder
  • 323,515
  • 67
  • 518
  • 625
298
PRAGMA table_info([tablename]);
Alex Bitek
  • 6,273
  • 4
  • 44
  • 77
Strater
  • 3,013
  • 1
  • 11
  • 2
  • 21
    This seems more equivalent to MySQL's describe than `.schema tablename` to me. – tybro0103 Jun 25 '12 at 20:34
  • 2
    Yep. This worked for me. .schema TABLENAME didn't. .schema alone, however, does show you all the create statements, but the result from PRAGMA is a lot more useful if I just want to look at one table. – Dev Kanchen Aug 07 '12 at 09:08
  • 17
    This seems like it should be the accepted answer since it works through querying instead of being dependent on a command line interface. +1 from me. – Akoi Meexx Sep 02 '12 at 20:37
  • Addendum: The only thing I'm noticing is that it does not output PRIMARY KEY when I create a table with INTEGER PRIMARY KEY, just INTEGER. – Akoi Meexx Sep 02 '12 at 20:47
  • 4
    @AkoiMeexx: From my original question: "`PRAGMA table_info [table]` isn't good enough, as it only has basic information (for example, it doesn't show if a column is a field of some sort or not)." – Matthew Feb 02 '13 at 23:40
  • It also displays the data in a more table like format rather than looking at the CREATE statement from the .schema command – OzzyTheGiant Sep 27 '16 at 20:30
122

Are you looking for the SQL used to generate a table? For that, you can query the sqlite_master table:

sqlite> CREATE TABLE foo (bar INT, quux TEXT);
sqlite> SELECT * FROM sqlite_master;
table|foo|foo|2|CREATE TABLE foo (bar INT, quux TEXT)
sqlite> SELECT sql FROM sqlite_master WHERE name = 'foo';
CREATE TABLE foo (bar INT, quux TEXT)
Mark Rushakoff
  • 224,642
  • 43
  • 388
  • 389
54

To see all tables:

.tables

To see a particular table:

.schema [tablename]
Ross Snyder
  • 1,835
  • 11
  • 11
6

To prevent that people are mislead by some of the comments to the other answers:

  1. If .schema or query from sqlite_master not gives any output, it indicates a non-existent tablename, e.g. this may also be caused by a ; semicolon at the end for .schema, .tables, ... Or just because the table really not exists. That .schema just doesn't work is very unlikely and then a bug report should be filed at the sqlite project.

... .schema can only be used from a command line; the above commands > can be run as a query through a library (Python, C#, etc.). – Mark Rushakoff Jul 25 '10 at 21:09

  1. 'can only be used from a command line' may mislead people. Almost any (likely every?) programming language can call other programs/commands. Therefore the quoted comment is unlucky as calling another program, in this case sqlite, is more likely to be supported than that the language provides a wrapper/library for every program (which not only is prone to incompleteness by the very nature of the masses of programs out there, but also is counter acting single-source principle, complicating maintenance, furthering the chaos of data in the world).
Jean-François Fabre
  • 126,787
  • 22
  • 103
  • 165
Radagast
  • 111
  • 1
  • 9
  • 1
    Anybody writing a program to retrieve data from any SQL database should use the proper SQL drivers available to their programming language for accessing the database and performing queries on it. That is the appropriate way to access a database. I would never recommend hacking a command-line program designed to provide ad-hoc queries. Your suggestion is deeply mistaken. A command-line program for ad-hoc queries is CERTAINLY NOT the most appropriate access point for program code to run queries on a database. Using SQL drivers is CERTAINLY NOT 'complicating maintenance' - it is best practice. – Medlock Perlman Nov 16 '17 at 13:11
  • I agree it is no bad, it is similar to libraries. Which is why Linux|BSD distros ship package managers. And why there is 0install cross platform PM. My point was just to clarify that not all programs need wrappers. It does not make sense everytime. In this case (DB handling) of course it not a bad idea to use a wrapper. – Radagast Nov 22 '17 at 19:10
1

If you're using a graphical tool. It shows you the schema right next to the table name. In case of DB Browser For Sqlite, click to open the database(top right corner), navigate and open your database, you'll see the information populated in the table as below.

enter image description here

right click on the record/table_name, click on copy create statement and there you have it.

Hope it helped some beginner who failed to work with the commandline.

Mujeeb Ishaque
  • 1,034
  • 11
  • 12