Information Schema
Every catalog has an INFORMATION_SCHEMA schema with tables that describe the
tables, columns and constraints in the catalog. They can be queried like any other table:
CREATE TABLE products (id INT PRIMARY KEY, title VARCHAR(100) NOT NULL);
SELECT table_name, table_type FROM information_schema.tables
WHERE table_schema = 'PUBLIC';
-- TABLE_NAME: PRODUCTS TABLE_TYPE: BASE TABLE
SELECT column_name, data_type, character_maximum_length, is_nullable
FROM information_schema.columns
WHERE table_name = 'PRODUCTS';
-- COLUMN_NAME: ID DATA_TYPE: INTEGER CHARACTER_MAXIMUM_LENGTH: NULL IS_NULLABLE: NO
-- COLUMN_NAME: TITLE DATA_TYPE: CHARACTER VARYING CHARACTER_MAXIMUM_LENGTH: 100 IS_NULLABLE: NO
The tables are defined by the SQL standard as views (ISO/IEC 9075-11). vsql doesn’t have views, so they’re built from the table definitions each time they’re read. A table that’s created in a transaction is only described in that transaction until it’s committed.
The INFORMATION_SCHEMA can’t be changed. Creating, changing or dropping
anything in it (or the schema itself) is a 42501 error. It isn’t stored in
the database file, and isn’t part of a dump. Virtual tables (see
Virtual Tables) aren’t described because they’re part of a connection,
not the catalog.
TABLES
One row for each table, including the tables of the INFORMATION_SCHEMA
(which are a VIEW). Rows are ordered by schema and table name.
Column |
Description |
|---|---|
|
The fully qualified name of the table. |
|
|
|
|
|
Always |
|
Always |
COLUMNS
One row for each column of each table in TABLES.
Column |
Description |
|---|---|
|
The fully qualified name of the table. |
|
The name of the column. |
|
The position of the column in the table, starting at 1. |
|
The SQL of the |
|
|
|
The name of the data type, without its size, such as
|
|
For |
|
For numbers, the precision in binary digits ( |
|
For |
|
For |
|
The ordinal position, as text. |
|
Always |
|
|
|
|
|
The |
|
|
|
|
|
The expression of a generated column, otherwise |
|
|
|
Always |
CHECK_CONSTRAINTS
One row for each check constraint of each table (but not the tables of the
INFORMATION_SCHEMA). A NOT NULL column is the same as a
CHECK (column IS NOT NULL), so it’s a check constraint too. That includes
the columns of a PRIMARY KEY, which are always NOT NULL. Rows are
ordered by schema and table name, then the NOT NULL columns in the order of
the columns, and then the CHECK constraints in the order they’re defined.
CREATE TABLE products (id INT PRIMARY KEY, price INT CHECK (price > 0));
SELECT constraint_name, check_clause FROM information_schema.check_constraints;
-- CONSTRAINT_NAME: PRODUCTS_NOT_NULL_1 CHECK_CLAUSE: ID IS NOT NULL
-- CONSTRAINT_NAME: PRODUCTS_CHECK_1 CHECK_CLAUSE: PRICE > 0
Column |
Description |
|---|---|
|
The catalog and schema of the table. |
|
The name of the constraint. A constraint that wasn’t given a name is
named after its table (see “Constraint Names” in CREATE TABLE),
such as |
|
The SQL of the condition, such as |
TABLE_CONSTRAINTS
One row for each constraint of each table (but not the tables of the
INFORMATION_SCHEMA): the PRIMARY KEY, then each UNIQUE, then each
FOREIGN KEY, then the same check constraints as CHECK_CONSTRAINTS, in
the same order and with the same names.
CREATE TABLE products (id INT PRIMARY KEY, code INT UNIQUE, price INT CHECK (price > 0));
SELECT constraint_name, constraint_type FROM information_schema.table_constraints;
-- CONSTRAINT_NAME: PRODUCTS_PRIMARY_KEY CONSTRAINT_TYPE: PRIMARY KEY
-- CONSTRAINT_NAME: PRODUCTS_UNIQUE_1 CONSTRAINT_TYPE: UNIQUE
-- CONSTRAINT_NAME: PRODUCTS_NOT_NULL_1 CONSTRAINT_TYPE: CHECK
-- CONSTRAINT_NAME: PRODUCTS_CHECK_1 CONSTRAINT_TYPE: CHECK
Column |
Description |
|---|---|
|
The catalog and schema of the table. |
|
The name of the constraint, such as |
|
The table the constraint is on. |
|
|
|
|
|
Always |
REFERENTIAL_CONSTRAINTS
One row for each FOREIGN KEY (see CREATE TABLE), in the same order
and with the same names as TABLE_CONSTRAINTS.
CREATE TABLE categories (id INT PRIMARY KEY);
CREATE TABLE products (category_id INT REFERENCES categories);
SELECT constraint_name, unique_constraint_name FROM information_schema.referential_constraints;
-- CONSTRAINT_NAME: PRODUCTS_FOREIGN_KEY_1 UNIQUE_CONSTRAINT_NAME: CATEGORIES_PRIMARY_KEY
Column |
Description |
|---|---|
|
The |
|
The |
|
Always |
|
|
|
|
The types of the columns of these tables are the domains of the
INFORMATION_SCHEMA in the standard: a name is a CHARACTER VARYING(128),
other text is a CHARACTER VARYING(10485760), a number is an INTEGER and
YES or NO is a CHARACTER VARYING(3).