# A guide to SQL Check Constraint

**URL:** <https://forum.freecodecamp.org/t/a-guide-to-sql-check-constraint/290662>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2019, 3:19am UTC](https://forum.freecodecamp.org/t/a-guide-to-sql-check-constraint/290662 "2019-06-30T03:19:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2019, 3:19am UTC](https://forum.freecodecamp.org/t/a-guide-to-sql-check-constraint/290662/1 "2019-06-30T03:19:34Z")

</div>

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 single column it allows 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.

### SQL 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 you can not have any person below 18 years:

**MySQL:**

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

```

**SQL Server / Oracle / MS Access:**

```auto
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:

**MySQL / SQL Server / Oracle / MS Access:**

```auto
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')
);

```

### SQL CHECK on ALTER TABLE

To create a CHECK constraint on the “Age” column when the table is already created, use the following SQL:

**MySQL / SQL Server / Oracle / MS Access:**

```auto
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:

**MySQL / SQL Server / Oracle / MS Access:**

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

```

### DROP a CHECK Constraint

To drop a CHECK constraint, use the following SQL:

**SQL Server / Oracle / MS Access:**

```auto
ALTER TABLE Persons
DROP CONSTRAINT CHK_PersonAge;

```

**MySQL:**

```auto
ALTER TABLE Persons
DROP CHECK CHK_PersonAge; 

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 4:01pm UTC](https://forum.freecodecamp.org/t/a-guide-to-sql-check-constraint/290662/2 "2025-01-08T16:01:08Z")

</div>

Closed on January 8, 2025, 4:01pm UTC


