Developing By Me

Project Development and Design

CHECK

    The CHECK constraint is a bit different than the others. This constraint allows the DBA to limit what information can be inserted into the field. For example if it is a number for a lottery drawing then the numbers must be between 1 and 46. If the number is outside of this range SQL will not accept the information and throw an error. This is not used often however it is useful for specialized data limitations that should be filtered prior to going into the database. The following is the syntax to add a CHECK constraint.

 

SQL Syntax Description SQL Database
CREATE TABLE table1 (id INT NOT NULL,CHECK (id > 0)) Creates a table with a CHECK CONSTRAINT MySQL
CREATE TABLE table1 (id INT NOT NULL CHECK (id > 0)) Creates a table with a CHECK CONSTRAINT SQL Server, Oracle, PostgreSQL
CREATE TABLE table1 (id INT NOT NULL,city VARCHAR(255), CONSTRAINT index_name CHECK (id > 0 AND city = ‘Some City’)

)

Creates a table with a CHECK CONSTRAINT MySQL, SQL Server, Oracle, PostgreSQL

 

%d bloggers like this: