[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-08-30 UTC。"],[],[],null,["# Create and manage check constraints\n\nA `CHECK` constraint lets you specify that the values of one or more columns\nmust satisfy a boolean expression. In this page, we describe how to add and\nmanage this type of constraint in GoogleSQL-dialect databases and PostgreSQL-dialect databases.\n\nAdd a check constraint to a new table\n-------------------------------------\n\nIn the following `CREATE TABLE` snippet, we create a table to store information\nabout concerts.\nTo require that the end time of a concert is later than its start time, we\ninclude a check constraint. \n\n### GoogleSQL\n\n CREATE TABLE Concerts (\n ConcertId INT64,\n StartTime Timestamp,\n EndTime Timestamp,\n CONSTRAINT start_before_end CHECK(StartTime \u003c EndTime),\n ) PRIMARY KEY (ConcertId);\n\n### PostgreSQL\n\n CREATE TABLE Concerts (\n ConcertId BIGINT,\n StartTime TIMESTAMPTZ,\n EndTime TIMESTAMPTZ,\n CONSTRAINT start_before_end CHECK(StartTime \u003c EndTime),\n PRIMARY KEY (ConcertId)\n );\n\nThe constraint definition begins with the `CONSTRAINT` keyword. We've explicitly\nnamed the constraint `start_before_end` in this example to help you find it in\nerror messages and whenever we need to refer to it. If no name is given,\nSpanner provides one, with the generated name beginning with\nthe prefix `CK_`. Constraint names are scoped to the schema, along with the\nnames for tables and indexes, and must be unique within the schema. The check\nconstraint definition consists of the keyword `CHECK` followed by an expression\nin parentheses. The expression can only reference columns of this table. In this\nexample, it references **StartTime** and **EndTime**, and the check constraint\nmakes sure that the start time of a concert is always less than the end time.\n\nThe value of the check constraint expression is evaluated when a new row is\ninserted or when the `StartTime` or `EndTime` of an existing row are updated. If\nthe expression evaluates to `TRUE` or `NULL`, the data change is allowed by the\ncheck constraint. If the expression evaluates to `FALSE`, the data change is not\nallowed.\n\n- The following restrictions apply to a check constraint `expression` term.\n\n - The expression can only reference columns in the same table.\n\n - The expression must reference at least one non-generated column, whether\n directly or through a generated column which references a non-generated\n column.\n\n - The expression can't reference columns that have set the\n `allow_commit_timestamp` option.\n\n - The expression can't contain [subqueries](/spanner/docs/reference/standard-sql/subqueries).\n\n - The expression can't contain non-deterministic functions, such as\n [`CURRENT_DATE()`](/spanner/docs/reference/standard-sql/date_functions#current_date)\n and\n [`CURRENT_TIMESTAMP()`](/spanner/docs/reference/standard-sql/timestamp_functions#current_timestamp).\n\nAdd a check constraint to an existing table\n-------------------------------------------\n\nUsing the following `ALTER TABLE` statement, we add a constraint to make sure\nthat all concert ids are greater than zero. \n\n ALTER TABLE Concerts\n ADD CONSTRAINT concert_id_gt_0 CHECK (ConcertId \u003e 0);\n\nOnce again, we've given the constraint a name, **concert_id_gt_0** . Adding a\n`CHECK` constraint to an existing table starts the enforcement of the constraint\nimmediately for new data and starts a long-running operation to validate that\nexisting data conforms to the new constraint. Because this validation is\nperformed as a long-running operation, ongoing transactions on the table are not\nimpacted. For more information, see [Schema update performance](/spanner/docs/schema-updates#performance). If there are\nany violations on existing data, the constraint is rolled back.\n\nRemove a check constraint\n-------------------------\n\nThe following DDL statement drops a `CHECK` constraint from the `Concerts`\ntable. \n\n ALTER TABLE Concerts\n DROP CONSTRAINT concert_id_gt_0;\n\nModify a check constraint expression\n------------------------------------\n\nModifying the expression of a `CHECK` constraint is not allowed. Instead, you\nneed to drop the existing constraint and create a new constraint with the new\nexpression.\n\nView properties of a check constraint\n-------------------------------------\n\nSpanner's [INFORMATION_SCHEMA](/spanner/docs/information-schema) contains information about\nthe check constraints on your database. The following are some examples of the\nquestions you can answer by querying the information schema.\n\n*What check constraints are defined in my database?* \n\n SELECT tc.CONSTRAINT_NAME, tc.TABLE_NAME, tc.CONSTRAINT_TYPE\n FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS as tc\n WHERE tc.CONSTRAINT_TYPE = 'CHECK';\n\n*What is the current state of the check constraints in my database?*\n\nIf you have added a check constraint to an existing table, you might want to\nview its current state to determine, for example, whether all existing data has\nbeen validated against the constraint. If `SPANNER_STATE` returns\n`VALIDATING_DATA` in the following query, it means\nSpanner is still in the process of validating existing data\nagainst that constraint. \n\n SELECT cc.CONSTRAINT_NAME, cc.SPANNER_STATE\n FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS as cc;\n\nWhat's next\n-----------\n\n- Learn more about Spanner's [INFORMATION SCHEMA](/spanner/docs/information-schema)."]]