Querying Schemaless Data¶
GlueSQL is an SQL database that provides a unique feature: it allows you to work with schemaless data, similar to NoSQL databases. Please note this point in the documentation.
Creating a Schemaless Table¶
To create a schemaless table, you don't need to specify columns when creating the table. For example:
This creates a schemaless table. You can now insert data freely into each row, like a NoSQL database. Nested data is also supported.
Example SQL Queries¶
Here are some example SQL queries that demonstrate how to use GlueSQL with schemaless data:
Creating Tables¶
Inserting Data¶
INSERT INTO Player VALUES ('{"id": 1001, "name": "Beam", "flag": 1}'), ('{"id": 1002, "name": "Seo"}');
INSERT INTO Item VALUES ('{"id": 100, "name": "Test 001", "dex": 324, "rare": false, "obj": {"cost": 3000}}'), ('{"id": 200}');
Selecting Data¶
SELECT name, dex, rare FROM Item WHERE id = 100;
SELECT name, dex, rare FROM Item;
SELECT * FROM Item;
Updating Data¶
DELETE FROM Item WHERE id > 100;
UPDATE Item SET id = id + 1, rare = NOT rare;
UPDATE Item SET new_field = 'Hello';
Selecting with Aliases and Joins¶
SELECT
Player.id AS player_id,
Player.name AS player_name,
Item.obj['cost'] AS item_cost
FROM Item
JOIN Player
WHERE flag IS NOT NULL;
Notable Exception Cases¶
Here are some example SQL queries that will raise errors, along with explanations of the issues:
Inserting Invalid Data¶
- Inserting multiple values for a schemaless row:
Schemaless rows accept only single values.
- Inserting data from a SELECT statement:
Schemaless rows cannot be inserted using a SELECT statement.
- Inserting a JSON array:
Only JSON objects are allowed for schemaless rows.
- Inserting a boolean value:
Text literals are required for schemaless rows.
- Inserting an expression result:
Map or string values are required for schemaless rows.
- Inserting data from a SELECT statement with LIMIT:
Map type values are required for schemaless rows.
Selecting Invalid Data¶
- Using IN with a schemaless subquery:
Schemaless projections are not allowed for IN subqueries.
- Using a comparison with a schemaless subquery:
Schemaless projections are not allowed for subqueries.