SQL#

The {exec} sql directive allows executing SQL in the browser. Each block is executed in a new, empty database.

Database definition#

A database can be defined as a named {exec} sql block, to be referenced in the :after: option of other blocks.

 1create table countries (
 2  country text not null,
 3  country_code text not null,
 4  dial_code text not null,
 5  capital text not null,
 6  population int not null,
 7  food text
 8);
 9insert into countries values
10  ('Switzerland', 'CH', '+41', 'Bern', 8776000, 'fondue!'),
11  ('France', 'FR', '+33', 'Paris', 67970000, null),
12  ('Germany', 'DE', '+49', 'Berlin', 83800000, null),
13  ('Italy', 'IT', '+39', 'Rome', 58940000, null),
14  ('Austria', 'AT', '+43', 'Vienna', 9042000, 'Kaiserschmarrn'),
15  ('Lichtenstein', 'LI', '+423', 'Vaduz', 39327, null);

Queries#

The results of the first select statement in each {exec} sql block of the :after: and :then: sequence are displayed as tables.

1select * from countries;

The SQL code can be hidden by adding :class: hidden.

Empty results#

select * from countries where false;

Wide results#

select * from wide;

Tall results#

The height of large results tables can be limited with :output-style:.

select t1.value, t2.value from tall as t1, tall as t2;

Mutations#

1update countries set food = 'baguette' where country_code = 'FR';
2select * from countries where country_code = 'FR';

SQL errors#

select * from unknown_table;

Foreign key constraint enforcement is enabled by default.

create table users (id integer primary key);
create table orders (user integer, foreign key (user) references users (id));
insert into orders (user) values (1);