Pending completion
Deleting data
It's time! We've created, read and updated, all that is left now in part 1 is to DELETE.
We'll cover:
How to delete fields inside records
How to delete one record, a range of records, or the entire table
Starting where we left off in our previous lesson on deleting fields.
Deleting fields
SurrealQL does have a REMOVE FIELD statement, but that only works for schema definitions, as we'll explore in part 3.
For schemaless tables, we use the UPDATE statement to either:
SETthe field to NONEUNSETthe field
NONE means something does not exist, whereas NULL means the field exists but it doesn't contain any data. Therefore we are using NONE to say that the field should not exist, or in other words, be deleted.
The reason we have this logic, instead of only having UNSET is because UNSET doesn't work in the CONTENT, MERGE or PATCH clauses, where we need to use NONE or the remove operation for PATCH.
Deleting data
Now that we know how to delete fields, let's delete entire records.
This is where we use the DELETE statement, which only deletes the data inside a table, but not the table itself, similar to DELETE and TRUNCATE TABLE in most SQL dialects.
You can directly delete one record or the entire table. Just make sure that it is the right table, we wouldn't want to DELETE the person table if we wanted to DELETE the product table.
You can also DELETE a range of records, either by using record ranges or the WHERE clause.
Whenever you can, always use record IDs as that is the most efficient way.
Deleting tables
In order to delete the table itself, and any data it might have, we use the REMOVE TABLE statement, which is similar to DROP TABLE in most SQL dialects.
Summary
In summary, to delete a field inside a record, use the
UPDATEstatement to either:SET field_name = NONEUNSET field_name
REMOVE FIELDonly works for schemafull tables as we'll see in part 3Use the
DELETEstatement toDELETEone record, a range of records, or the entire table dataDELETEthe data inside a table, but not the table itself, similar toTRUNCATE TABLEin most SQL dialects
Use
REMOVE TABLEto delete the table itself and its data, similar toDROP TABLEin most SQL dialects