Insert Query¶
The insert function adds new rows to a Table. It can insert a single row or multiple rows at once.
(set employees (table [name age] (list ['Alice 'Bob] [25 30])))
(set employees (insert employees (list 'Charlie 35))) ;; Insert a single new row
(select {name: name age: age from: employees})
┌─────────┬────────────────────────────┐
│ name │ age │
├─────────┼────────────────────────────┤
│ Alice │ 25 │
│ Bob │ 30 │
│ Charlie │ 35 │
├─────────┴────────────────────────────┤
│ 3 rows (3 shown) 2 columns (2 shown) │
└──────────────────────────────────────┘
The insert function takes two arguments: the table to insert into and the data to insert. The data can be provided as a List, Dictionary, or another Table.
Inserting Multiple Rows¶
Insert multiple rows using a list of Vectors:
(set employees (insert employees (list ['David 'Eve] [40 25])))
(select {name: name age: age from: employees})
┌─────────┬────────────────────────────┐
│ name │ age │
├─────────┼────────────────────────────┤
│ Alice │ 25 │
│ Bob │ 30 │
│ Charlie │ 35 │
│ David │ 40 │
│ Eve │ 25 │
├─────────┴────────────────────────────┤
│ 5 rows (5 shown) 2 columns (2 shown) │
└──────────────────────────────────────┘
Inserting with Dictionary¶
Insert rows using a Dictionary for clearer column mapping:
;; Insert using dictionary for explicit column mapping
(set employees (insert employees
(dict [name age] (list 'Frank 45))))
(select {name: name age: age from: employees})
┌─────────┬────────────────────────────┐
│ name │ age │
├─────────┼────────────────────────────┤
│ Alice │ 25 │
│ Bob │ 30 │
│ Charlie │ 35 │
│ David │ 40 │
│ Eve │ 25 │
│ Frank │ 45 │
├─────────┴────────────────────────────┤
│ 6 rows (6 shown) 2 columns (2 shown) │
└──────────────────────────────────────┘
Inserting from Another Table¶
Insert rows from another Table:
(set new_employees (table [name age] (list ['Kate 'Leo] [65 70])))
(set employees (insert employees new_employees))
In-Place Insertion¶
To modify the table in place without reassigning, pass the table name as a quoted Symbol:
Important: Persisting Changes
By default, insert returns a new table and does not modify the original. To persist changes, you have two options:
-
Reassign the result:
-
Use in-place insertion with quoted symbol: