Edgedb Constraints

[tr] Türkçe Oku

2023-04-11

Edgedb is a relational database management system (RDBMS) that provides various constraint mechanisms to securely store and manage your data while preserving the integrity of the data recorded in your database. Constraints are a set of rules used to maintain the integrity of the data stored in your database.

There are five different types of constraint:

1. Primary Key Constraints: Primary key constraints ensure that each row is uniquely identified.
This constraint specifies that there can only be one primary key column in a table, and each value in this column can only be used once. Edgedb defines primary key constraints with the `required` attribute

2. Unique Constraints: Unique constraints ensure that all values in a column are unique. 
This constraint specifies that only one unique constraint can be defined for a column in a table. Edgedb defines unique constraints with the `unique` attribute.

3. Check constraints: Check constraints specify that a column must meet a certain condition. This constraint allows for multiple check constraints to be defined for a column in a table. In edgedb, check constraints are defined using the expr attribute.

4. Foreign key constraints: Foreign key constraints define the relationship between two tables. Only one foreign key constraint can be defined in a table, and this constraint defines the relationship between the primary key columns of the two tables. In edgedb, foreign key constraints are defined using the link attribute.

5. Matching constraints: Matching constraints define the match between two columns. Only one matching constraint can be defined in a table. In edgedb, matching constraints are defined using the match attribute.

They can be used when creating tables or adding constraints to existing tables. For example, let’s say we want to create a table named Person and add a primary key constraint to this table:

CREATE TYPE GenderEnum extending enum<('male', 'female', 'other')>;

CREATE TABLE Person {
  id -> uuid,
  name -> str,
  age -> int64,
  gender -> GenderEnum,
  PRIMARY KEY (id)
};

In this example, the id column in the Person table is defined as the primary key. We also created a custom type named GenderEnum and linked the gender column to this type.

To add unique constraints, we simply need to add the unique attribute next to the column:

CREATE TABLE Person {
  id -> uuid,
  name -> str,
  age -> int64,
  gender -> GenderEnum,
  email -> str unique,
  PRIMARY KEY (id)
};

In this example, the email column is defined as unique.

To add check constraints, we simply need to add the expr attribute next to the column:

CREATE TABLE Person {
  id -> uuid,
  name -> str,
  age -> int64,
  gender -> GenderEnum,
  email -> str unique,
  phone -> str expr,
  PRIMARY KEY (id)
};

In this example, the phone column has a check constraint added using the expr attribute. The expr attribute is an expression language used in edbql that is a subset of the Python language.

To add foreign key constraints, we simply need to add the link attribute next to the column:

CREATE TABLE Post {
  id -> uuid,
  title -> str,
  content -> str,
  author -> uuid,
  PRIMARY KEY (id),
  FOREIGN KEY (author) REFERENCES Person
};

In this example, the author column in the Post table is defined as a foreign key that matches the id column in the Person table using the link attribute.

Finally, to add matching constraints, we simply need to add the match attribute next to the column:

CREATE TABLE Message {
  id -> uuid,
  sender -> str,
  recipient -> str,
  message -> str,
  PRIMARY KEY (id),
  MATCH (sender, recipient)
};

In this example, the sender and recipient columns in the Message table are defined as a matching constraint.

Constraints are important to ensure the correct and secure storage of your data. Edgedb offers a range of constraint types including primary key, unique, check, foreign key, and matching constraints. These constraints can help you make your database scalable, secure, and efficient.

The use of constraints is important to optimize your database design. Adding the correct constraints can ensure the integrity of your data and improve the performance of your database. Edgedb facilitates the use of constraints and helps you improve your database design.



More posts like this

Edgedb Computed Links

2023-04-12 | #edgedb #edgedb-links

Computed Links is a structure used to define relationships between two objects. These links are created by using a defined query or function to calculate the relationship between associated objects. Computed Links is a structure used to define relationships between two objects. These connections are created by using a defined query or function to calculate the relationship between the associated objects. This structure is very useful for ensuring consistency of the data held in the database.

Continue reading 


Edgedb Exclusive Constraint

2023-04-12 | #edgedb #edgedb-constraints

The term exclusive constraint in EdgeDB refers to a type of constraint that ensures a specific property is unique or within a certain range. For example, you can create an exclusive constraint in a User object that ensures the email property is unique, thus ensuring that each user is only saved once. In the following example, we are defining an exclusive constraint that ensures uniqueness in the email property: type User { required property id -> uuid; required property name -> str; exclusive constraint unique_email on (.

Continue reading 


Edgedb Object Type

2023-04-11 | #edgedb #edgedb-types

EdgeDB can be defined as an object-oriented database management system. Data is defined as object types, and these types are defined with attributes and methods. In this article, a detailed explanation will be provided about the object type in EdgeDB. The object type is one of the object types in EdgeDB. An EdgeDB object can be thought of as a class, and these objects have attributes and methods. The object type defines the characteristics of an object type by keeping these attributes and methods together.

Continue reading 