In SQL, the CHECK constraint is used to limit the values that can be inserted into a column. It ensures that the data inserted into a column meets a certain condition or set of conditions.
Here is an example of how to define a CHECK constraint in SQL:
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
grade CHAR(1) CHECK (grade IN ('A', 'B', 'C', 'D', 'F'))
);
In this example, we have defined a table called “students” with four columns: “id”, “name”, “age”, and “grade”. The grade column has a CHECK constraint that ensures that the value inserted into the column is one of the allowed values: A, B, C, D, or F.
Here is an example of how to insert data into the “students” table with a CHECK constraint:
INSERT INTO students (id, name, age, grade)
VALUES (1, 'John Doe', 18, 'A');
In this example, we are inserting a new row into the “students” table with values for the “id”, “name”, “age”, and “grade” columns. Since the grade column has a CHECK constraint, we must provide a value for the column that meets the specified condition. If we try to insert a row with a grade value that is not allowed by the CHECK constraint, the database will return an error.
In summary, the CHECK constraint is used to limit the values that can be inserted into a column in SQL. It is essential for maintaining data integrity and ensuring the accuracy and consistency of data in a database.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.