Sqlite: adding COMMENT ON descriptions to tables and columns?
Asked Answered
B

5

54

In MySQL Workbench you can add COMMENTs to tables and columns in a MySQL database.

Does Sqlite support adding comments to tables and columns?

Barsky answered 15/9, 2011 at 5:25 Comment(0)
S
66

I don't think it does. The "SQL As Understood By SQLite" page makes no mention of table or column comments nor does the CREATE TABLE or ALTER TABLE documentation.

Also, the Unsupported SQL wiki page has this:

2009-08-04: Table and column comments - I have scoured the doco and can't find anything about applying comments to tables or their columns.

Yes, that's a wiki page from 2009 but that note is supported by the rest of the documentation.

However, SQLite does preserve SQL comments that you put in your DDL. If you feed this to the sqlite3 CLI tool:

CREATE TABLE User
        -- A table comment
(
        uid INTEGER,    -- A field comment
        flags INTEGER   -- Another field comment
);

Then you get exactly that back from a .schema command:

sqlite> .schema
CREATE TABLE User
        -- A table comment
(
        uid INTEGER,    -- A field comment
        flags INTEGER   -- Another field comment
);

So you should be able to fake it if you can control the DDL used to create your tables.

Smoothtongued answered 15/9, 2011 at 5:39 Comment(2)
As well as using .schema, you can obtain the same result as an SQL statement using: SELECT name, sql FROM sqlite_master WHERE type='table' AND name='{$table}' ORDER BY name; (where $table is the name of the table). See: https://mcmap.net/q/279229/-sqlite-schema-information-metadataKeilakeily
This is great, though unfortunately the comments do not appear in SQLiteStudio's DDL view.Manlike
C
2

When creating a table using sqlite (I'm using sqlite3 in python), the COMMENT section is not supported.

This fails (works in full MySql syntax):

CREATE TABLE `Info` (
  `Test` VARCHAR(512) NOT NULL COMMENT 'Column info here'
);

This works (no COMMENT in the column declaration):

CREATE TABLE `Info` (
  `Test` VARCHAR(512) NOT NULL
);
Cattycornered answered 11/10, 2016 at 19:16 Comment(0)
C
1

(This isn't what the original poster was asking, but this is what I was looking for when I first found this question based on the keywords in the title.)

How to make comments in SQLite

There are two ways to make comments in SQLite code:

Hyphens

-- this is my comment
SELECT * FROM employees;

C-style

/* this is my comment */ 
SELECT * FROM employees;
Cottonweed answered 26/2, 2018 at 11:44 Comment(0)
A
0

I appreciate that this is an old post but for what it's worth, you can add comments when creating a table in SQLITE3, in Python and in Java. Probably works for other languages as well.

You need to add new lines to your sql string as you would if you were typing in the command at the SQLITE3 prompt -

sql_str = 'CREATE TABLE properties (\nproperty TEXT NOT NULL, -- A property\nvalue TEXT -- The value of the property\n);'

When executed the table is created like so:

sqlite> .schema

CREATE TABLE properties (
property TEXT NOT NULL, -- A property
value TEXT -- The value of the property
);

I suspect that this works because the connector is actually echoing in the commands via the command prompt, rather than some sort of API.

Abradant answered 7/2, 2021 at 7:31 Comment(0)
B
0

in Oracle table and column comments are visible in certain schema tables

would it be a good idea to have some standard sqlite3 schema tables that could be adopted by all tools to store such metadata. Future enhancements, perhaps via PRAGMA statements would be possible but external tools that already provide similar facilities could start using them now. Perhaps names such as

  • sqlite3_table_comments
  • sqlite3_column_comments
Brocklin answered 15/6, 2024 at 10:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.