Which of these is not a valid constraint in SQL?
Explanation
UNIQUE, PRIMARY KEY, NOT NULL, and CHECK are valid constraints in SQL with they respective functions.
EXISTS is an operator that is used to test the existence of some record in a subquery. It returns true if the subquery returns one or more records.
Example:
SELECT ...
FROM ...
WHERE EXISTS (SELECT ... FROM ... WHERE ...)
Theory
  • SQL UNIQUE Constraint

    The UNIQUE constraint uniquely identifies each record in a database table.
    The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.
    A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
    Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table. Read more: SQL UNIQUE Constraint
  • SQL PRIMARY KEY Constraint

    The PRIMARY KEY constraint uniquely identifies each record in a database table.
    Primary keys must contain UNIQUE values.
    A primary key column cannot contain NULL values.
    Most tables should have a primary key, and each table can have only ONE primary key.
    Read more: SQL UNIQUE Constraint
  • CHECK Constraint

    The CHECK Constraint enables a condition to check the value being entered into a record. If the condition evaluates to false, the record violates the constraint and isn't entered into the table.

    Example

    The following SQL creates a new table called CUSTOMERS. Here, we add a CHECK with AGE column, so that you can not have any CUSTOMER below 18 years:
    CREATE TABLE CUSTOMERS(
           ID   INT              NOT NULL,
           NAME VARCHAR (20)     NOT NULL,
           AGE  INT              NOT NULL CHECK (AGE >= 18),  
           PRIMARY KEY (ID)
    );
    
    If CUSTOMERS table has already been created, then to add a CHECK constraint to AGE column, you would write a statement similar to the following:
    ALTER TABLE CUSTOMERS
       MODIFY AGE INT NOT NULL CHECK (AGE >= 18 );
    
    Naming the constraint in multiple columns as well:
    ALTER TABLE CUSTOMERS
       ADD CONSTRAINT myCheckConstraint CHECK(AGE >= 18);
    
  • NOT NULL Constraint

    By default, a column can hold NULL values. If you do not want a column to have a NULL value, then you need to define such constraint on this column specifying that NULL is now not allowed for that column.
    A NULL is not the same as no data, rather, it represents unknown data.

    Example

    The following SQL creates a new table called CUSTOMERS, two of which, ID and NAME, specify not to accept NULLs:
    CREATE TABLE CUSTOMERS(
           ID   INT              NOT NULL,
           NAME VARCHAR (20)     NOT NULL,
           ADDRESS  CHAR (25) ,
           PRIMARY KEY (ID)
    );
    
    If CUSTOMERS table has already been created, then to add a NOT NULL constraint to SALARY column in Oracle and MySQL:
    ALTER TABLE CUSTOMERS
       MODIFY SALARY  DECIMAL (18, 2) NOT NULL;
    
    Read more: NOT NULL Constraint

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback