如何查询Teradata指定表的检查约束名称及表达式?
Retrieve Check Constraint Details for a Teradata Table
Got it, let's tackle this! You're right that DBC.ConstraintName only gives you the names of check constraints—you need to pair it with another system table to get the actual constraint logic.
The Solution: Join Two System Tables
Teradata stores the check constraint expressions in DBC.CheckConstraints. Here's a straightforward query that combines both tables to pull exactly what you need:
SELECT cn.ConstraintName AS constraint_name, cc.CheckText AS constraint_expression FROM DBC.ConstraintName cn INNER JOIN DBC.CheckConstraints cc ON cn.ConstraintId = cc.ConstraintId WHERE cn.DatabaseName = '<your_database_name>' AND cn.TableName = '<your_table_name>' AND cn.ConstraintType = 'C'; -- 'C' filters for check constraints only
Quick Breakdown:
- Replace
<your_database_name>and<your_table_name>with the full qualified parts of your target table (omit the database name if it's your default schema, but including it avoids ambiguity). - The
ConstraintType = 'C'clause ensures you don't pull in other constraint types like primary keys or foreign keys. - You'll need SELECT privileges on both
DBC.ConstraintNameandDBC.CheckConstraints—if you don't have access, reach out to your Teradata admin for grants.
Example Output (Matching Your Sample Table)
For your table_with_check example, this query would return:
| constraint_name | constraint_expression |
|---|---|
| CHECK_CONSTR | S_BYTEINT > 10 |
内容的提问来源于stack exchange,提问作者Denis
相关产品推荐
相关产品推荐

