Skip to content

Information functions

View as Markdown

Information functions return facts about values, the session, the database and the server, rather than computing on data.

CHARSET(expr)

Returns the character set of a string expression. Return type: VARCHAR.

COLLATION(expr)

Returns the collation of a string expression. Return type: VARCHAR.

SELECT CHARSET('abc'), CHARSET(_utf8'abc'), COLLATION('abc'), COLLATION(_utf8'abc') FROM db_root;
charset('abc') charset(_utf8'abc') collation('abc') collation(_utf8'abc')
utf8 utf8 utf8_bin utf8_bin
COERCIBILITY(expr)

Returns the collation coercibility level of an expression: when two expressions with different collations meet, the one with the lower level decides the collation of the result. For the levels, see Collation coercibility. Return type: INT.

SELECT COERCIBILITY(USER()), COERCIBILITY('abc'), COERCIBILITY(name)
FROM countries WHERE code = 'FRA';
coercibility( user() ) coercibility('abc') coercibility(name)
7 10 3

A column (3) wins over a function result (7), which wins over a constant (10).

CURRENT_USER
USER

Pseudocolumns that return the name of the user of the current session. Return type: VARCHAR.

USER()
SYSTEM_USER()

Return the current user name followed by a host name, as user@host. Return type: VARCHAR.

SELECT CURRENT_USER, USER(), SYSTEM_USER() FROM db_root;
DBA DBA@db-server-01 DBA@db-server-01

The users of the database are listed in the db_user catalog view.

DATABASE()
SCHEMA()

Return the name of the database the session is connected to. The two functions are the same. Return type: VARCHAR.

SELECT DATABASE(), SCHEMA() FROM db_root;
database() schema()
e-commerce e-commerce
DBTIMEZONE()

Returns the time zone of the database server, as an offset ('-05:00') or a region name ('Europe/Vienna'). Return type: VARCHAR.

SESSIONTIMEZONE()

Returns the time zone of the current session, which applies to TIMESTAMP WITH LOCAL TIME ZONE and DATETIME WITH LOCAL TIME ZONE values. It is the server's time zone unless the session changed it. Return type: VARCHAR.

SELECT DBTIMEZONE(), SESSIONTIMEZONE() FROM db_root;
Europe/Bucharest Europe/Bucharest

See also FROM_TZ, NEW_TIME, TZ_OFFSET and the timezone parameters.

DEFAULT
DEFAULT(column_name)

DEFAULT(column_name) returns the default value defined for a column. If the column has no default, the result is NULL; for a NOT NULL or PRIMARY KEY column without a default, it is an error. The keyword DEFAULT on its own stands for the column's default value in INSERT and UPDATE statements.

CREATE TABLE info_tbl (id INT DEFAULT 0, name VARCHAR(10));
INSERT INTO info_tbl VALUES (1, 'a'), (2, 'b'), (NULL, 'c');
SELECT id, DEFAULT(id) FROM info_tbl ORDER BY name;
id default(id)
1 0
2 0
NULL 0
UPDATE info_tbl SET id = DEFAULT WHERE id IS NULL;
DELETE FROM info_tbl WHERE id = DEFAULT(id);
INSERT INTO info_tbl VALUES (DEFAULT, 'd');

DISK_SIZE(expr)

Returns the number of bytes needed to store the value of expr in a table, taking string compression into account. Use it to estimate the storage a column needs. Return type: INT.

SELECT DISK_SIZE(name), DISK_SIZE(description), DISK_SIZE(unit_price), DISK_SIZE(id)
FROM product WHERE id = 1795;
disk_size(name) disk_size(description) disk_size(unit_price) disk_size(id)
24 16 16 4

For constants: DISK_SIZE('abc') is 7 and DISK_SIZE(1) is 4.

INDEX_CARDINALITY(table_name, index_name, key_pos)

Returns the number of distinct keys in an index, as recorded in the statistics. key_pos selects the key prefix: 0 counts distinct values of the first column, 1 of the first two columns, and so on. Run UPDATE STATISTICS first for current figures.

  • table_name, index_name: strings.
  • key_pos: an integer, or a value that can be converted to one (FLOAT and DOUBLE values are rounded).

Return type: INT. The result is NULL if an argument is NULL, if the table or index does not exist, or if key_pos is out of range.

The primary key of order_items is (order_id, product_id):

SELECT INDEX_CARDINALITY('order_items', 'pk_order_items_order_id_product_id', 0),
INDEX_CARDINALITY('order_items', 'pk_order_items_order_id_product_id', 1),
INDEX_CARDINALITY('order_items', 'pk_order_items_order_id_product_id', 2)
FROM db_root;
index_cardinality(..., 0) index_cardinality(..., 1) index_cardinality(..., 2)
11230 11230 NULL
INET_ATON(ip_string)

Converts an IPv4 address 'a.b.c.d' to the number a × 256³ + b × 256² + c × 256 + d. Return type: BIGINT.

INET_NTOA(number)

Converts a number back to an IPv4 address string. Return type: VARCHAR.

SELECT INET_ATON('192.168.0.10'), INET_NTOA(3232235530) FROM db_root;
inet_aton('192.168.0.10') inet_ntoa(3232235530)
3232235530 192.168.0.10
LAST_INSERT_ID()

Returns the AUTO_INCREMENT value generated by the most recent successful INSERT of the session. Return type: NUMERIC(38,0).

  • After a multi-row INSERT, it returns the value generated for the first row.
  • A value you insert yourself is not reported; only generated values are.
  • A failed INSERT does not change it, but the AUTO_INCREMENT counter may still have advanced, so the next value can skip numbers.
  • A rollback does not reset it.
  • Each session has its own value. A value generated inside a trigger is not visible outside the trigger.
CREATE TABLE ai (a INT PRIMARY KEY AUTO_INCREMENT, b INT UNIQUE);
INSERT INTO ai VALUES (NULL, 1);
SELECT LAST_INSERT_ID(); -- 1
INSERT INTO ai VALUES (NULL, 11), (NULL, 12), (NULL, 13);
SELECT LAST_INSERT_ID(); -- 2, the first of 2, 3, 4
INSERT INTO ai VALUES (500, 20), (NULL, 21);
SELECT LAST_INSERT_ID(); -- 5: 500 was given, not generated

See also AUTO_INCREMENT.

ROW_COUNT()

Returns the number of rows changed by the last INSERT, UPDATE or DELETE statement of the session. Return type: INT.

CREATE TABLE rc (i INT);
INSERT INTO rc VALUES (1), (2), (3), (4), (5), (6), (7);
SELECT ROW_COUNT(); -- 7
UPDATE rc SET i = 0 WHERE i > 3;
SELECT ROW_COUNT(); -- 4
DELETE FROM rc WHERE i = 0;
SELECT ROW_COUNT(); -- 4
LIST_DBS()

Returns the names of all databases in the database location file (databases.txt), separated by spaces. Return type: VARCHAR.

SELECT LIST_DBS() FROM db_root;
chinook e-commerce healthcare movie-lens nba2014-2015 northwind northwind-rel olympics1988-2004 sakila star-wars
VERSION()

Returns the version of the CoraDB server, for example 1.0.0.1586. Return type: VARCHAR.

LABEL(graph_element)

Returns the label of a vertex or edge, that is, the name of its vertex or edge table. Use it in MATCH queries to tell which table a matched element belongs to. Return type: VARCHAR.

SELECT LABEL(b), LABEL(m), LABEL(p)
FROM MATCH (b:brand)-[m:manufactures]->(p:product)
WHERE p.id = 1795;
label(b) label(m) label(p)
brand manufactures product

The argument must be a graph element; LABEL('text') is an error (Incompatible argument type char, expected type is: object).

PROPERTY_OF(graph_element, property_name, table_name)

Returns one property (column) of a vertex or edge. It is mainly used with the path functions NODES, RELATIONSHIPS, FIRST and LAST, which return graph elements rather than rows of a known table.

  • graph_element: a vertex or edge.
  • property_name: the column to return, written as an identifier.
  • table_name: the vertex or edge table that defines the column. When several tables have a column with that name, it says which one is meant.

Return type: the type of the column.

The first and last vertex of each path from customer 5 to the products it likes:

SELECT PROPERTY_OF(FIRST(NODES(pth)), company_name, customer) AS customer,
PROPERTY_OF(LAST(NODES(pth)), name, product) AS product
FROM MATCH pth = (c:customer)-[:likes]->(p:product)
WHERE c.id = 5
ORDER BY 2;
customer product
Brown, Roman and Allen Circe Hooded Ice Fleece
Brown, Roman and Allen Sirop derable
Brown, Roman and Allen Sybil Running Short