Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SQL CHECK Constraint in Create Table #1480

Open
seonghun-dev opened this issue May 4, 2024 · 0 comments
Open

Implement SQL CHECK Constraint in Create Table #1480

seonghun-dev opened this issue May 4, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@seonghun-dev
Copy link
Contributor

seonghun-dev commented May 4, 2024

Description

The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a column it will allow only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

Example

CREATE TABLE persons (
    name VARCHAR(40),
    age INTEGER CHECK (age >= 18)
);
ALTER TABLE persons ADD CHECK (age>=18)
ALTER TABLE persons DROP CHECK (age>=18)

The following tests should be passed:

test_case!(enum_data_type, async move {
    let test_cases = [
        (
            "CREATE TABLE persons (name VARCHAR(40), age INTEGER CHECK (age>=18))",
            Ok(Payload::Create),
        ),
        (
            "INSERT INTO persons VALUES ('minsu', 20)",
            Ok(Payload::Insert(1)),
        ),
        (
            "INSERT INTO persons VALUES ('younghee', 10)",
            Err(ValueError::NotEnumValue(
                "invalid input value for check age: \"10\"".to_string(),
            )),
        ),
        (
            "ALTER TABLE persons DROP CHECK (age>=18)",
            Ok(Payload::AlterTable),
        ),
        (
            "INSERT INTO persons VALUES ('kim', 10)",
            Ok(Payload::Insert(1)),
        )
    ];

    for (sql, expected) in test_cases {
        test!(expected, sql);
    }
});

Reference

https://www.w3schools.com/sql/sql_check.asp
https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
https://www.postgresql.org/docs/current/ddl-constraints.html

@panarch panarch added the enhancement New feature or request label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants