菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

MySQL 教程

MySQL HOME MySQL Intro MySQL RDBMS

MySQL SQL

MySQL SQL MySQL SELECT MySQL WHERE MySQL AND, OR, NOT MySQL ORDER BY MySQL INSERT INTO MySQL NULL Values MySQL UPDATE MySQL DELETE MySQL LIMIT MySQL MIN and MAX MySQL COUNT, AVG, SUM MySQL LIKE MySQL Wildcards MySQL IN MySQL BETWEEN MySQL Aliases MySQL Joins MySQL INNER JOIN MySQL LEFT JOIN MySQL RIGHT JOIN MySQL CROSS JOIN MySQL Self Join MySQL UNION MySQL GROUP BY MySQL HAVING MySQL EXISTS MySQL ANY, ALL MySQL INSERT SELECT MySQL CASE MySQL Null Functions MySQL Comments MySQL Operators

MySQL 数据库

MySQL Create DB MySQL Drop DB MySQL Create Table MySQL Drop Table MySQL Alter Table MySQL Constraints MySQL Not Null MySQL Unique MySQL Primary Key MySQL Foreign Key MySQL Check MySQL Default MySQL Create Index MySQL Auto Increment MySQL Dates MySQL Views

MySQL References

MySQL Data Types MySQL Functions

MySQL Examples

MySQL Examples MySQL Editor MySQL Quiz MySQL Exercises MySQL Certificate

MySQL CHECK 约束


MySQL CHECK 约束

The CHECK constraint is used to limit the value range that can be placed in a column.(CHECK 约束用于限制可以放入列中的值的范围。)

If you define a CHECK constraint on a column it will allow only certain values for this column.(如果在列上定义 CHECK 约束,它将只允许该列的特定值。)

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.(如果在表上定义 CHECK 约束,它可以基于行中其他列的值来限制某些列中的值。)


CHECK on CREATE TABLE

The following SQL creates a CHECK constraint on the "Age" column when the "Persons" table is created. The CHECK constraint ensures that the age of a person must be 18, or older(以下 SQL 在创建 "Persons" 表时,在 "Age" 列上创建了一个 CHECK 约束。CHECK 约束确保一个人的年龄必须是 18 岁或以上。)

CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    CHECK (Age>=18)
);

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax(要允许命名 CHECK 约束,以及为多个列定义 CHECK 约束,请使用以下 SQL 语法)

CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    City varchar(255),
    CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);


CHECK on ALTER TABLE

To create a CHECK constraint on the "Age" column when the table is already created, use the following SQL(要在表已创建后,在 "Age" 列上创建 CHECK 约束,请使用以下 SQL)

ALTER TABLE Persons
ADD CHECK (Age>=18);

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax(要允许命名 CHECK 约束,以及为多个列定义 CHECK 约束,请使用以下 SQL 语法)

ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');

DROP a CHECK Constraint (删除 CHECK 约束)

To drop a CHECK constraint, use the following SQL(要删除 CHECK 约束,请使用以下 SQL)

ALTER TABLE Persons
DROP CHECK CHK_PersonAge;

×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持