CHECK


CHECK

This command checks that all values in a column meet some criteria. The CHECK command can be used in any attribute of a table to avoid the input of invalid values in a table.
Este comando permite restringir los valores de entrada en una columna. El comando CHECK se puede usar en cualquier atributo de esta tabla y se usa para evitar la entrada de valores incorrectos a una tabla.

Tip
When a CHECK is created, the database allows assigning a name to the restriction. It is very important to assign a name to all the CHECK commands when they are created (the CONSTRAINT command allows setting the name of a CHECK).
Cuando se crea un CHECK, la base de datos permiten asignarle un nombre a esta restricción. Es muy buena práctica asignar nombres a todos los comandos CHECK en el momento de crearlas (el comando CONSTRAINT permite fijar el nombre de un CHECK).

Tip
Even though it is possible to add a CHECK to a table once the table has data, it is recommended to define all CHECKS before inserting data into the table.
Aun cuando es posible agregar un CHECK a una tabla una vez que esta tiene datos, es recomendable definir los CHECKs apropiados antes de insertar datos a la tabla.

Tip
MySQL accepts the CHECK command for compatibily, but it DOES NOT perform any action when the data in inserted into the table.
MySQL acepta el comando CHECK por compatibilidad, pero NO impone ninguna restricción en la entrada de datos.

Problem 1
In the client table described in the SQL script shown below, indicate the set of possible values for
  1. client_id
  2. ssn
  3. zip
  4. phone
  5. gender
  6. age

my_company.sql
CREATE TABLE dbo.client
(
     client_id INT NOT NULL IDENTITY PRIMARY KEY
          CONSTRAINT client_id_ck CHECK(client_id>-1),
     last_name NVARCHAR(25) NOT NULL,
     first_name NVARCHAR (25) NOT NULL,
     ssn NCHAR(11) NOT NULL UNIQUE
          CONSTRAINT client_ssn_ck CHECK (ssn LIKE
          '[0123456789][0123456789][0123456789]-[0123456789][0123456789]-[0123456789][0123456789][0123456789][0123456789]'),
     address NVARCHAR (200) NULL,
     city NVARCHAR (50) NULL,
     state NCHAR(2) NULL,
     zip NVARCHAR(10) NULL
          CONSTRAINT client_zip_ck CHECK
          (
               zip LIKE
               '[0123456789][0123456789][0123456789][0123456789][0123456789]'
               OR zip LIKE
               '[0123456789][0123456789][0123456789][0123456789][0123456789]-[0123456789][0123456789][0123456789][0123456789]'
          ),     
     phone NCHAR(12) NOT NULL
          CONSTRAINT client_phone_ck CHECK (phone LIKE
          '[0123456789][0123456789][0123456789]-[0123456789][0123456789][0123456789]-[0123456789][0123456789][0123456789][0123456789]'),
     gender NCHAR(1) NOT NULL
          CONSTRAINT client_gender_ck CHECK (gender = 'F' OR gender = 'M'),
     age INT CONSTRAINT client_age_ck CHECK (age BETWEEN 0 AND 200)
)
GO

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home