SQL and RethinkDB share very similar terminology. Below is a table of terms and concepts in the two systems.
SQL | RethinkDB |
---|---|
database | database |
table | table |
row | document |
column | field |
table joins | table joins |
primary key | primary key (by default id ) |
index | index |
This is a list of queries for inserting data into a database.
SQL | ReQL |
---|---|
INSERT INTO users(user_id, age, name) VALUES ("f62255a8259f", 30, Peter) | r.table("users").insert({ "user_id": "f62255a8259f", "age": 30, "name": "Peter" }) |
This is a list of queries for selecting data out of a database.
This is a list of commands for updating data in the database.
SQL | ReQL |
---|---|
UPDATE users SET age = 18 WHERE age < 18 |
r.table("users").filter( r.row["age"] < 18 ).update({ "age": 18 }) |
UPDATE users SET age = age+1 |
r.table("users").update( { "age": r.row["age"]+1 } ) |
This is a list of queries for deleting data from the database.
SQL | ReQL |
---|---|
DELETE FROM users |
r.table("users").delete() |
DELETE FROM users WHERE age < 18 |
r.table("users") .filter( r.row["age"] < 18) .delete() |
This is a list of queries for performing joins between multiple tables.
SQL | ReQL |
---|---|
SELECT * FROM posts JOIN users ON posts.user_id = users.id |
r.table("posts").inner_join( r.table("users"), lambda post, user: post["user_id"] == user["id"] ).zip() Note: If you have an index (primary key or secondary index) built on the field of the right table, you can perform a more efficient join with eq_join. r.table("posts").eq_join( "id", r.table("users"), index="id" ).zip() |
SELECT posts.id AS post_id, user.name, users.id AS user_id FROM posts JOIN users ON posts.user_id = users.id SELECT posts.id AS post_id, user.name, users.id AS user_id FROM posts INNER JOIN users ON posts.user_id = users.id |
r.table("posts").inner_join( r.table("users"), lambda post, user: post["user_id"] == user["id"] ).map({ "post_id": r.row["left"]["id"], "user_id": r.row["right"]["id"], "name": r.row["right"]["name"] }) |
SELECT * FROM posts RIGHT JOIN users ON posts.user_id = users.id SELECT * FROM posts RIGHT OUTER JOIN users ON posts.user_id = users.id |
r.table("posts").outer_join( r.table("users"), lambda post, user: post["user_id"] == user["id"] ).zip() Note: You can perform more efficient r.table("posts").concat_map(lambda post: r.table("users") .get_all(post["id"],index="id") .do( lambda results: r.branch( results.count() == 0, [{"left": post}], results.map( lambda user: { "left": post "right": user } ) ) ) ).zip() |
SELECT * FROM posts LEFT JOIN users ON posts.user_id = users.id SELECT * FROM posts LEFT OUTER JOIN users ON posts.user_id = users.id |
r.table("users").outer_join( r.table("posts"), lambda user, post: post.user_id == user.id ).zip() r.table("users").concat_map(lambda user: r.table("posts") .get_all(user["id"],index="id") .do( lambda results: r.branch( results.count() == 0, [{"left": user}], results.map( lambda post: { "left": user "right": post } ) ) ) ).zip() |
This is a list of queries for performing data aggregation.
This is a list of queries for creating and dropping tables and databases.
Browse the following resources to learn more about ReQL:
© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/docs/sql-to-reql/python/