Edgedb Tuple Usage
[tr] Türkçe Oku 2023-04-11
Edgedb is a modern database that supports the tuple data type. Tuple data type is used to represent combinations of different data types and this data structure can contain named or unnamed elements.
The Tuple data type is used to store combinations of multiple different data types. In edgedb, the tuple data type is defined using the keyword tuple. The following example defines an unnamed tuple
instance, which contains a str
and an int64
element:
SELECT ('Ali Veli', 35);
In this example, the expression ('Ali Veli', 35)
represents a tuple data type. This tuple contains the name of a person “Ali Veli” as a str
type and their age of 35 as an int64
type.
The tuple data type also supports named elements. Named tuple elements allow elements to be stored in a named way, making it easier to write more readable code. The following example defines a named tuple instance, which includes a str element named name and an int64 element named age:
SELECT (name := 'Ali Veli', age := 35);
In this example, the expression (name := 'Ali Veli', age := 35)
represents a named tuple data type. This tuple contains the name of a person named “Ali Veli” as a str type and their age as an int64 type.
The tuple data type is frequently used in database operations. edgedb provides a set of specialized processor functions to handle tuples. These functions can be used to sort elements in a tuple, search for specific elements in a tuple, filter elements in a tuple, aggregate elements in a tuple, and perform many other operations on tuples.
The following examples demonstrate tuple operations in edgedb:
SELECT (35, 'Ali Veli') ORDER BY 1 DESC;
SELECT User FILTER User(name='Hatice')
SELECT (name := 'Hatice', age := 25, gender := 'Female')
FILTER age > 20 AND gender = 'Female';
SELECT User{name, age} FILTER User.age >= 18;
SELECT sum(User.age) FILTER User.age >= 18;
SELECT sum((10, 20), (30, 40)).0 AS total_sum;
These examples show some tuple operations. The first example is used to sort the elements inside a tuple. The second example is used to search for a specific element or elements in a named tuple. The third example is used to filter the elements inside a named tuple, and the last example is used to aggregate the elements inside a named tuple.
The tuple data type can also be used together with other data types. For example, the tuple data type can be stored in an array data type in Edgedb. The following example demonstrates storing a tuple in an array data type:
SELECT ['Ali Veli', 35];
In this example, the expression ['Ali Veli', 35]
represents an array data type, and this array contains a str
and an int64
element. The tuple in this example is stored among the elements of the array.
The tuple data type can also be used in conjunction with many other data types in edgedb. For example, tuples can be used in join operations in edgedb and are particularly useful for combining result sets that contain multiple data types. Tuples can also be used in other places such as function parameters and returned results in edgedb.