Computer Science

SQL CHECK

SQL CHECK is a constraint used in SQL databases to limit the values that can be inserted into a column. It ensures that only valid data is entered into the database by defining a condition that must be met before data can be added or updated. This helps to maintain data integrity and prevent errors.

Written by Perlego with AI-assistance

4 Key excerpts on "SQL CHECK"

  • Book cover image for: Database Systems For Next-generation Applications: Principles And Practice
    • W Kim, Yahiko Kambayashi, I S Paik(Authors)
    • 1993(Publication Date)
    • World Scientific
      (Publisher)
    ENGINEERING DATABASES A SURVEY OF INTEGRITY CONSTRAINT CHECKING METHODS IN RELATIONAL DATABASES Tok Wang LING and Sin Yeung LEE Department of Information Systems and Computer Science National University of Singapore Lower Kent Ridge Road, Singapore 0511, Republic of Singapore. email: [email protected], [email protected] ABSTRACT Constraint validation, an essential feature of any database system, is a process of guaranteeing and maintaining a set of semantic invariants across database state transitions. This process has been studied by many authors and various simplification methods have been proposed. This survey describes recent developments in the field of integrity constraint checking methods. We first give a review on different characteristics of methods for enforcing integrity constraints, followed by a survey of different methods to perform and simplify integrity checking in relational databases, and the corresponding ways to express integrity constraints. 1.0 INTRODUCTION An integrity constraint is a formula which governs the database state [Eswa75, Hamm78]. When a database state is consistent with all the specified integrity constraints, we say that the database is consistent. Otherwise, when the database state does not obey some integrity constraints, the database state is said to be inconsistent. One of the major functions of a database management system is to ensure that the database is consistent. However, when a database is changed from the current database state to a new database state, there is no guarantee that the new state will obey the integrity constraints. A test is therefore necessary to ensure that the update does not create an inconsistent database state. Since the checking is done as often as transactions are performed, the test must be efficient. In other words, the integrity checking problem is basically the minimization of computation effort as well as database accesses for integrity checking.
  • Book cover image for: Beginning Microsoft SQL Server 2012 Programming
    • Paul Atkinson, Robert Vieira(Authors)
    • 2012(Publication Date)
    • Wrox
      (Publisher)
    CHECK constraints is that they are not restricted to a particular column. They can be related to a column, but they can also be essentially table related in that they can check one column against another, as long as all the columns are within a single table and the values are for the same row being updated or inserted. They can also check that any combination of column values meets a criterion.
    The constraint is defined using the same rules that you would use in a WHERE clause. Examples of the criteria for a CHECK constraint include:
    GOAL SQL
    Limit Month column to appropriate numbers BETWEEN 1 AND 12
    Proper SSN formatting LIKE '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]'
    Limit to a specific list of Shippers IN ('UPS', 'Fed Ex', 'USPS')
    Price must be positive UnitPrice >= 0
    Reference another column in the same row ShipDate >= OrderDate
    This really only scratches the surface, and the possibilities are virtually endless. Almost anything you could put in a WHERE clause, you can also put in your constraint. What’s more, CHECK constraints are very fast performance-wise as compared to the alternatives (rules and triggers).
    Still building on the Accounting database, try adding a modification to the Customers table to check for a valid date in the DateInSystem field (you can’t have a date in the system that’s in the future):
    ALTER TABLE Customers ADD CONSTRAINT CN_CustomerDateInSystem CHECK (DateInSystem <= GETDATE ());
    Now try to insert a record that violates the CHECK constraint; you’ll get an error:
    INSERT INTO Customers (CustomerName, Address1, Address2, City, State, Zip, Contact, Phone, FedIDNo, DateInSystem) VALUES ('Customer1', 'Address1', 'Add2', 'MyCity', 'NY', '55555', 'No Contact', '553-1212', '930984954', '12-31-2049');
    Code snippet Chap06.sql
    Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted with the CHECK constraint "CN_CustomerDateInSystem". The conflict occurred in database "Accounting", table "dbo.Customers", column 'DateInSystem'. The statement has been terminated.
  • Book cover image for: Relational Database Design and Implementation
    There are additional constraints that you can place on columns in a table beyond primary and foreign key constraints. These include requiring unique values and predicates in CHECK clauses.
    Requiring Unique Values
    If you want to ensure that the values in a non-primary key column are unique, then you can use the UNIQUE keyword. UNIQUE verifies that all non-null values are unique. For example, if you were storing social security numbers in an employee table that used an employee ID as the primary key, you could also enforce unique social security numbers with
    The UNIQUE clause can also be placed at the end of the CREATE TABLE statement, along with the primary key and foreign key specifications. In that case, it takes the form
    Check Clauses
    The CHECK clause to which you were introduced earlier in the chapter, in the “Domains” section, can also be used with individual columns to declare column-specific constraints. To add a constraint, you place a CHECK clause after the column declaration, using the keyword VALUE in a predicate to indicate the value being checked.
    For example, to verify that a column used to hold true-false values is limited to T and F, you could write a CHECK clause as

    Modifying Database Elements

    With the exception of tables, database elements are largely unchangeable. When you want to modify them, you must delete them from the database and create them from scratch. In contrast, just about every characteristic of a table can be modified without deleting the table, using the ALTER TABLE statement.

    Adding Columns

    To add a new column to a table, use the ALTER TABLE statement with the following syntax:
    For example, if Antique Opticals wanted to add a telephone number column to the producer table, they would use
    To add more than one column at the same time, simply separate the clauses with commas:

    Adding Table Constraints

    You can add table constraints, such as foreign keys, at any time. To do so, include the new constraint in an ALTER TABLE statement:
  • Book cover image for: Database
    eBook - PDF

    Database

    Principles Programming Performance

    404 6.1 Integrity Constraints SQL-92 Integrity Constraints 405 The SQL-92 standard has a number of new integrity features, most of which are not yet available on commercial database products. One important mod-ification is in the optional check clause of the Create Table statement: check (search_condition) In SQL-92 this form accepts any valid search_condition form. In the X/ OPEN clause of Definition 6.1.1, and in most current database products, the search_condition of the check clause can refer only to column values in the same table, on the same row for which an Update or Insert statement is in process, with no Subselects or set functions allowed. This is quite a strong limitation, a form known technically as a restriction predicate, and it should be obvious that there are many business rules we might wish to impose that cannot be provided without more powerful search_conditions. I EXAMPLE 6.1.7 We want to ensure that any customers with di sent of 0.0 (new customers who have not yet had a credit check) cannot place total orders in excess of $2000.00. In SQL-92 we can impose this requirement by placing the following check on the orders table: create table orders( . . . constraint newcustlimit check (orders, ci d not in (select cid from customers c where c.discnt = 0.0) or (select sum(x.dolIars) from orders x where x.cid = orders.cid) <= 2000.00 I . . . ) ■ The search_condition of Example 6.1.7 is stated as a condition that should always hold, and has a qualifier, o r d e r s , by which we refer to col-umn value on the row(s) being inserted or updated in the o r d e r s table on which the check clause is defined. This is a natural result of the following definition of the check constraint. Given a table t with a check clause, if we represent the associated search_condition by y, then a row modification to the table t will fail if and only if, after modification, the following state-ment is false: not exists t.* where not y
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.