Constraint sql что это пример
Перейти к содержимому

Constraint sql что это пример

  • автор:

Ограничения в SQL

Ограничение (constraints) — это ограничение типа значений, которое накладывается на один или несколько столбцов таблицы. Это позволяет поддерживать точность и целостность данных в таблице БД.

В SQL существует несколько различных типов ограничений, в том числе:

  • NOT NULL
  • PRIMARY KEY
  • UNIQUE
  • DEFAULT
  • FOREIGN KEY
  • CHECK

Давайте обсудим каждое из этих ограничений попродробнее.

Ограничение NOT NULL

Ограничение NOT NULL указывает, что столбец не может принимать значения NULL .

Если к столбцу применено ограничение NOT NULL , вы не сможете вставить новую строку в таблицу без добавления не-NULL-значения в этот столбец.

Пример

Следующая SQL-инструкция создает таблицу persons с четырьмя столбцами, из которых три столбца — id , name и phone — не могут иметь значение NULL .

Примечание. Нулевое значение ( NULL ) — это не ноль(0) и не строка символов нулевой длины (»). NULL означает, что записи нет.

Ограниение PRIMARY KEY

Ограничение PRIMARY KEY определяет столбец или набор столбцов, значения которых однозначно идентифицируют строку в таблице. То есть никакие две строки в таблице не могут иметь одинаковое значение первичного ключа. Также нельзя вводить значение NULL в столбец первичного ключа.

Пример

Следующая SQL-инструкция создает таблицу persons и указывает столбец id в качестве первичного ключа. Это означает, что в этом поле не допускаются значения NULL или дубликаты.

Примечание. Первичный ключ обычно состоит из одного столбца в таблице, однако несколько столбцов могут составлять первичный ключ. Например, адрес электронной почты сотрудника или присвоенный ID является логическим первичным ключом для таблицы сотрудников.

Ограничение UNIQUE

Ограничение UNIQUE означает, что в указанных столбцах обязательно должны быть уникальные значения.

Хотя и ограничение UNIQUE, и ограничение PRIMARY KEY обеспечивают уникальность значений, есть различия.

UNIQUE лучше PRIMARY KEY, когда вы хотите обеспечить уникальность столбца или комбинации столбцов, которые не являются первичным ключом.

Пример

Следующая SQL-инструкция создает таблицу persons и определяет столбец phone как уникальный. Это означает, что это поле не допускает дублирования значений.

Примечание. В одной таблице может быть задано ограничений UNIQUE , но только одно ограничение PRIMARY KEY . Кроме того, в отличие от ограничений PRIMARY KEY , ограничения UNIQUE допускают значения NULL .

Ограничение DEFAULT

Ограничение DEFAULT определяет значение по умолчанию для столбцов.

Значение столбца по умолчанию — это некоторое значение, которое будет вставлено в столбец базой данных, если оператор INSERT явно не назначит конкретное значение.

Пример

В следующей SQL-инструкции мы задаем значение по умолчанию для столбца country .

Примечание. Если вы определили столбец таблицы как NOT NULL , но присвоили ему значение по умолчанию, то в операторе INSERT вам не нужно явно присваивать значение этому столбцу, чтобы вставить новую строку в таблицу.

Ограничение FOREIGN KEY

Внешний ключ (foreign key) — это столбец или комбинация столбцов, которые используются для установления и обеспечения взаимосвязи между данными в двух таблицах.

Ниже — диаграмма, которая показывает связь между таблицами сотрудников ( employees ) и отделов ( departments ). Если вы внимательно посмотрите на нее, то заметите, что столбец dept_id таблицы сотрудников совпадает со столбцом первичного ключа таблицы отделов. Поэтому столбец dept_id таблицы сотрудников является внешним ключом для таблицы отделов.

В MySQL можно создать внешний ключ, задав ограничение FOREIGN KEY при создании таблицы следующим образом.

Пример

В следующей SQL-инструкции мы определяем столбец dept_id таблицы employees как внешний ключ, который ссылается на столбец dept_id таблицы departments .

Ограничение CHECK

Ограничение CHECK используется для ограничения значений, которые могут быть помещены в столбец.

Например, диапазон значений для столбца зарплаты salary можно ограничить, создав ограничение CHECK , которое допускает значения только от 30 000 до 100 000. Это предотвратит ввод зарплат за пределами обычного (условного) диапазона.

Пример

Примечание. MySQL не поддерживает ограничение CHECK.

Constraint sql что это пример

Constraints are the rules that we can apply on the type of data in a table. That is, we can specify the limit on the type of data that can be stored in a particular column in a table using constraints.

The available constraints in SQL are:

  • NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a column is specified as NOT NULL then we will not be able to store null in this particular column any more.
  • UNIQUE: This constraint when specified with a column, tells that all the values in the column must be unique. That is, the values in any row of a column must not be repeated.
  • PRIMARY KEY: A primary key is a field which can uniquely identify each row in a table. And this constraint is used to specify a field in a table as primary key.
  • FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in a another table. And this constraint is used to specify a field as Foreign key.
  • CHECK: This constraint helps to validate the values of a column to meet a particular condition. That is, it helps to ensure that the value stored in a column meets a specific condition.
  • DEFAULT: This constraint specifies a default value for the column when no value is specified by the user.

How to specify constraints?
We can specify constraints at the time of creating the table using CREATE TABLE statement. We can also specify the constraints after creating a table using ALTER TABLE statement.

Syntax:
Below is the syntax to create constraints using CREATE TABLE statement at the time of creating the table.

Let us see each of the constraint in detail.

1. NOT NULL –
If we specify a field in a table to be NOT NULL. Then the field will never accept null value. That is, you will be not allowed to insert a new row in the table without specifying any value to this field.
For example, the below query creates a table Student with the fields ID and NAME as NOT NULL. That is, we are bound to specify values for these two fields every time we wish to insert a new row.

2. UNIQUE
This constraint helps to uniquely identify each row in the table. i.e. for a particular column, all the rows should have unique values. We can have more than one UNIQUE columns in a table.
For example, the below query creates a table Student where the field ID is specified as UNIQUE. i.e, no two students can have the same ID. Unique constraint in detail.

3. PRIMARY KEY –
Primary Key is a field which uniquely identifies each row in the table. If a field in a table as primary key, then the field will not be able to contain NULL values as well as all the rows should have unique values for this field. So, in other words we can say that this is combination of NOT NULL and UNIQUE constraints.
A table can have only one field as primary key. Below query will create a table named Student and specifies the field ID as primary key.

4. FOREIGN KEY –
Foreign Key is a field in a table which uniquely identifies each row of a another table. That is, this field points to primary key of another table. This usually creates a kind of link between the tables.
Consider the two tables as shown below:

Orders

O_ID ORDER_NO C_ID
1 2253 3
2 3325 3
3 4521 2
4 8532 1

Customers

C_ID NAME ADDRESS
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESH GURGAON

As we can see clearly that the field C_ID in Orders table is the primary key in Customers table, i.e. it uniquely identifies each row in the Customers table. Therefore, it is a Foreign Key in Orders table.
Syntax:

(i) CHECK –
Using the CHECK constraint we can specify a condition for a field, which should be satisfied at the time of entering values for this field.
For example, the below query creates a table Student and specifies the condition for the field AGE as (AGE >= 18 ). That is, the user will not be allowed to enter any record in the table with AGE < 18. Check constraint in detail

(ii) DEFAULT –
This constraint is used to provide a default value for the fields. That is, if at the time of entering new records in the table if the user does not specify any value for these fields then the default value will be assigned to them.
For example, the below query will create a table named Student and specify the default value for the field AGE as 18.

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

SQL сдерживает

SQL CONSTRAINTS — это целостность, которая определяет некоторые условия, которые не позволяют столбцу оставаться истинным при вставке, обновлении или удалении данных в столбце. Ограничения могут быть указаны, когда таблица создается сначала с помощью оператора CREATE TABLE или во время изменения структуры существующей таблицы с помощью инструкции ALTER TABLE.

SQL CONSTRAINTS используются для реализации правил таблицы. Если какое-либо нарушение ограничений вызвало какое-либо действие, не выполняющееся над таблицей, действие отменяется ограничением.

Некоторые CONSTRAINTS могут использоваться вместе с оператором SQL CREATE TABLE.

Общая структура SQL CONSTRAINT определяется как:

За ключевым словом CONSTRAINT следует имя ограничения, за которым следует столбец или список столбцов.

Типы SQL-ограничений

SQL предоставляет следующие типы ограничений:

скованность Описание
НЕНУЛЕВОЙ Это ограничение подтверждает, что столбец не может хранить значение NULL.
УНИКАЛЬНАЯ Это ограничение гарантирует, что каждая строка для столбца должна иметь различное значение.
ОСНОВНОЙ КЛЮЧ Это ограничение является комбинацией ограничения NOT NULL и ограничения UNIQUE. Это ограничение гарантирует, что конкретный столбец или комбинация из двух или более столбцов для таблицы имеют уникальную идентификацию, которая помогает легче и быстрее найти конкретную запись в таблице.
ПРОВЕРЯТЬ Проверочное ограничение гарантирует, что значение, хранящееся в столбце, соответствует определенному условию.
ДЕФОЛТ Это ограничение предоставляет значение по умолчанию, если для этого столбца указано значение none.
ИНОСТРАННЫЙ КЛЮЧ Ограничение внешнего ключа используется для обеспечения ссылочной целостности данных. в одной таблице, чтобы сопоставить значения в другой таблице.

Синтаксис:

Параметры:

название Описание
table_name Имя таблицы, в которой хранятся данные.
column1, column2 Наименование столбцов таблицы.
тип данных Char, varchar, integer, decimal, date и многое другое.
размер Максимальная длина столбца таблицы.
ограничение Ограничение для столбца или таблицы.

Содержание:

  • SQL CREATE TABLE с ограничением для исключения значения NULL
  • SQL CREATE TABLE для проверки уникального значения
  • SQL CREATE TABLE для проверки уникального значения на нескольких столбцах
  • SQL CREATE TABLE с уникальным ограничением
  • SQL CREATE TABLE с ограничением CHECK
  • SQL CREATE TABLE с использованием ограничения по умолчанию
  • SQL CREATE TABLE с использованием значений по умолчанию и CHECK CONSTRAINT
  • SQL CREATE TABLE с оператором CHECK CONSTRAINT и IN
  • SQL CREATE TABLE с оператором CHECK CONSTRAINT и LIKE
  • SQL CREATE TABLE с оператором CHECK CONSTRAINT и OR
  • SQL CREATE TABLE с использованием оператора CHECK CONSTRAINT и AND OR
  • SQL CREATE TABLE с использованием CASCADE

SQL CREATE TABLE с ограничением для исключения значения NULL

В следующем разделе будет описано, как NOT NULL CONSTRAINT подтверждает, что столбец не может иметь значение NULL в инструкции CREATE TABLE.

Пример:

В следующем примере создается таблица. Вот имя поля и типы данных:

Имя поля Тип данных Размер Десятичные знаки НОЛЬ
agent_code голец 6 нет
имя агента голец 25 нет
рабочая область голец 25 нет

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

SQL CREATE TABLE для проверки уникального значения

В следующем разделе мы обсудим, как SQL UNIQUE CONSTRAINT гарантирует, что каждая строка для столбца имеет разные значения в операторе CREATE TABLE.

Пример:

В следующем примере создается таблица. Вот имя поля и типы данных:

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
ord_num десятичный 6 нет УНИКАЛЬНАЯ
ord_amount десятичный 12 2 да
ord_date Дата нет
cust_code голец 6 нет
agent_code голец 6 нет

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

SQL CREATE TABLE для проверки уникального значения на нескольких столбцах

В следующем примере создается таблица. Вот имя поля и типы данных:

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
agent_code голец 6 нет УНИКАЛЬНАЯ
имя агента голец 25 нет УНИКАЛЬНАЯ
рабочая область голец 25 нет
комиссия десятичный 5 2 да

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

SQL CREATE TABLE с уникальным ограничением

Ограничение SQL UNIQUE используется для обеспечения того, чтобы каждая строка столбца имела различное значение. На этой странице мы собираемся обсудить, как работает SQL UNIQUE CONSTRAINT, если он используется в конце инструкции CREATE TABLE вместо использования UNIQUE CONSTRAINT в определенных столбцах.

Пример :

В следующем примере создается таблица. Вот имя поля и типы данных:

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
cust_code голец 6 нет УНИКАЛЬНАЯ
CUST_NAME голец 25 нет
cust_city голец 25 нет
класс целое число да
agent_code голец 6 нет УНИКАЛЬНАЯ

можно использовать следующий оператор SQL :

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

SQL CREATE TABLE с ограничением CHECK

SQL CHECK CONSTRAINT гарантирует, что значение для определенного столбца или столбцов удовлетворяет заданному условию.

Задача ограничения CHECK заключается в ограничении значений для столбца таблицы.

Замечания :

  • SQL CHECK CONSTRAINT нельзя использовать в VIEW.
  • SQL CHECK CONSTRAINT нельзя использовать в подзапросе.
  • SQL CHECK CONSTRAINT также можно использовать в инструкциях ALTER TABLE и DROP TABLE.

В следующем примере создается таблица. Таблица содержит CHECK CONSTRAINT для столбца комиссии. Ограничение гарантирует, что «комиссия» должна быть меньше 1. Вот имя поля и типы данных:

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
agent_code голец 6 нет УНИКАЛЬНАЯ
имя агента голец 25 нет УНИКАЛЬНАЯ
рабочая область голец 25 нет
комиссия целое число ПРОВЕРЯТЬ

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

Выход :

SQL CREATE TABLE с использованием ограничения по умолчанию

SQL DEFAULT CONSTRAINT предоставляет значение по умолчанию, если для столбца не задано значение none.

Пример:

Включить DEFAULT CONSTRAINT в столбец 'working_area', который гарантирует, что —

1. «Working_area» должно быть «Mumbai», если для этого столбца не указано ни одного,

во время создания таблицы, имена полей и типы данных которой —

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
agent_code голец 6 нет УНИКАЛЬНАЯ
имя агента голец 25 нет УНИКАЛЬНАЯ
рабочая область голец 25 да ДЕФОЛТ
комиссия десятичный 8 2 да

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

Выход :

SQL CREATE TABLE с использованием значений по умолчанию и CHECK CONSTRAINT

В следующем разделе мы обсудили использование SQL DEFAULT CONSTRAINT и использование SQL CHECK CONSTRAINT при создании таблицы.

Пример:

Чтобы включить CHECK CONSTRAINT для «комиссии» и DEFAULT CONSTRAINT для «рабочей_области», это гарантирует, что —

1. «Комиссия» должна быть больше .1 и меньше .3,

2. «Working_area» должно быть «Mumbai», если для этого столбца не указано ни одного,

во время создания таблицы, которая содержит следующие имена полей и типы данных —

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
agent_code голец 6 нет УНИКАЛЬНАЯ
имя агента голец 25 нет УНИКАЛЬНАЯ
рабочая область голец 25 да ДЕФОЛТ
комиссия десятичный 8 2 да ПРОВЕРЯТЬ

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

Выход :

SQL CREATE TABLE с оператором CHECK CONSTRAINT и IN

В следующем разделе мы обсудили, как использовать оператор SQL IN вместе с SQL CHECK CONSTRAINT.

Условие CHECK CONSTRAINT может быть определено с использованием любого из базовых операторов сравнения, таких как ( >, <, =,> =, <=, <> ), а также операторов BETWEEN, IN, LIKE и NULL.

Пример:

Чтобы включить две проверки ограничения, которые —

1. Первый столбец находится в столбце «working_area», который гарантирует, что works_area должен быть либо «London», либо «Brisban», либо «Chennai», либо «Mumbai»,

2. Второй столбец — «комиссия», который гарантирует, что комиссия должна быть меньше 1,

В следующей таблице имена полей и типы данных:

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
agent_code голец 6 нет УНИКАЛЬНАЯ
имя агента голец 25 нет УНИКАЛЬНАЯ
рабочая область голец 25 нет ПРОВЕРЯТЬ
комиссия целое число ПРОВЕРЯТЬ

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

SQL CREATE TABLE с оператором CHECK CONSTRAINT и LIKE

В следующем разделе мы обсудим, как можно использовать оператор SQL LIKE с CHECK CONSTRAINT.

Пример:

Чтобы включить CHECK CONSTRAINT в столбец 'ord_date', который гарантирует, что формат 'ord_date' должен быть похож на '- / — / —-', например, ('18 / 05/1998 ') на время создания таблицы со следующими именами полей и типов данных —

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
ord_num десятичный 6 нет УНИКАЛЬНАЯ
ord_amount десятичный 12 2 да
ord_date голец 10 нет ПРОВЕРЯТЬ
cust_code голец 6 нет
agent_code голец 6 нет

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

Выход :

SQL CREATE TABLE с оператором CHECK CONSTRAINT и OR

В следующей теме мы обсуждаем использование оператора OR вместе с CHECK CONSTRAINT.

Пример:

Чтобы включить CHECK CONSTRAINT в столбцы 'Commission' и 'working_area', который гарантирует, что значение 'Commission' должно быть меньше .20, а значение 'working_area' должно быть 'London' во время создания следующей таблицы, которая состоит из имен полей и типы данных —

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
agent_code голец 6 нет УНИКАЛЬНАЯ
имя агента голец 25 нет УНИКАЛЬНАЯ
рабочая область голец 25 да ПРОВЕРЯТЬ
комиссия десятичный 8 2 да ПРОВЕРЯТЬ

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

Выход :

SQL CREATE TABLE с использованием оператора CHECK CONSTRAINT и оператора AND, OR

В следующей теме мы обсудим использование операторов OR и AND вместе с CHECK CONSTRAINT. Условие начнет работать во время вставки записей в таблицу.

Пример:

Включить CHECK CONSTRAINT в столбцы 'Commission' и 'working_area', который гарантирует, что —

1. «Комиссия» должна быть меньше .14, а «working_area» должно быть «Лондон»,

2. или «комиссия» должна быть меньше .15, а «working_area» должно быть «Мумбаи»,

3. или «комиссия» должна быть меньше .13, а «working_area» должно быть «Нью-Йорк»

во время создания таблицы, имена полей и типы данных которых

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
agent_code голец 6 нет УНИКАЛЬНАЯ
имя агента голец 25 нет УНИКАЛЬНАЯ
рабочая область голец 25 да ПРОВЕРЯТЬ
комиссия десятичный 8 2 да ПРОВЕРЯТЬ

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

Выход :

SQL CREATE TABLE с использованием CASCADE

Опция CASCADE с ON DELETE позволяет удалять строки из дочерней таблицы, когда соответствующие строки удаляются из родительской таблицы.

DELETE CASCADE работает по ссылке внешнего ключа и удаляет дочерние записи, связанные с родительскими записями.

Пример:

Чтобы создать таблицу, которая содержит следующие имя поля и типы данных —

Имя поля Тип данных Размер Десятичные знаки НОЛЬ скованность
tranno десятичный нет
Идентификатор компании VARCHAR 6 да ИНОСТРАННЫЙ КЛЮЧ
код изделия VARCHAR 10 да ОСНОВНОЙ КЛЮЧ
coname VARCHAR 35 да
имя элемента VARCHAR 35 да
iqty целое число да

Таблица содержит PRIMARY KEY для «itemcode» и FOREIGN KEY для столбца «company_id», который ссылается на столбец «company_id» таблицы «company».

можно использовать следующий оператор SQL:

Код SQL:

Чтобы увидеть структуру созданной таблицы:

Код SQL:

Выводы указанного оператора SQL, показанного здесь, взяты с использованием Oracle Database 10g Express Edition.

Упражнения по SQL

  • Упражнения по SQL, практика, решение
  • SQL Получить данные из таблиц [33 Упражнения]
  • Булевы и реляционные операторы SQL [12 упражнений]
  • Подстановочные знаки SQL и специальные операторы [22 упражнения]
  • Агрегатные функции SQL [25 упражнений]
  • Вывод запроса форматирования SQL [10 упражнений]
  • SQL-запросы к нескольким таблицам [7 упражнений]
  • ФИЛЬТРАЦИЯ И СОРТИРОВКА в базе данных персонала [38 упражнений]
  • SQL СОЕДИНЯЕТ
    • SQL СОЕДИНЯЕТСЯ [29 упражнений]
    • SQL присоединяется к базе данных HR [27 упражнений]
    • ПОДПИСИ SQL [39 упражнений]
    • SQL ПОДПИСИ по базе данных HR [55 упражнений]
    • href = «/ sql-упражнения / база данных фильмов-упражнение / основные упражнения-на-фильме-базе данных.php»> ОСНОВНЫЕ запросы к базе данных фильмов [10 упражнений]
    • ПОДПИСКИ на фильм База данных [16 упражнений]
    • ПРИСОЕДИНЯЕТСЯ к базе данных фильма [24 упражнения]
    • Вступление
    • ОСНОВНЫЕ запросы по футболу базы данных [29 упражнений]
    • ПОДПИСКИ по футбольной базе данных [33 упражнения]
    • ПРИСОЕДИНЯЕТСЯ к запросам по футбольной базе данных [61 упражнений]
    • Вступление
    • ОСНОВНЫЕ, ПОДПИСИ И СОЕДИНЕНИЯ [39 упражнений]
    • ОСНОВНЫЕ запросы к базе данных сотрудников [115 упражнений]
    • БРОНИРОВАНИЕ на сотрудника База данных [77 Упражнения]

    Хотите улучшить вышеуказанную статью? Вносите свои заметки / комментарии / примеры через Disqus.

    Предыдущая: Внешний ключ
    Далее: Изменить таблицу

    Constraint sql что это пример

    Use a constraint to define an integrity constraint— a rule that restricts the values in a database. Oracle Database lets you create six types of constraints and lets you declare them in two ways.

    The six types of integrity constraint are described briefly here and more fully in «Semantics» :

    A NOT NULL constraint prohibits a database value from being null.

    A unique constraint prohibits multiple rows from having the same value in the same column or combination of columns but allows some values to be null.

    A primary key constraint combines a NOT NULL constraint and a unique constraint in a single declaration. It prohibits multiple rows from having the same value in the same column or combination of columns and prohibits values from being null.

    A foreign key constraint requires values in one table to match values in another table.

    A check constraint requires a value in the database to comply with a specified condition.

    A REF column by definition references an object in another object type or in a relational table. A REF constraint lets you further describe the relationship between the REF column and the object it references.

    You can define constraints syntactically in two ways:

    As part of the definition of an individual column or attribute. This is called inline specification.

    As part of the table definition. This is called out-of-line specification.

    NOT NULL constraints must be declared inline. All other constraints can be declared either inline or out of line.

    Constraint clauses can appear in the following statements:

    Oracle Database does not enforce view constraints. However, you can enforce constraints on views through constraints on base tables.

    You can specify only unique, primary key, and foreign key constraints on views, and they are supported only in DISABLE NOVALIDATE mode. You cannot define view constraints on attributes of an object column.

    View Constraints for additional information on view constraints and «DISABLE Clause» for information on DISABLE NOVALIDATE mode

    External Table Constraints

    You can specify only NOT NULL , unique, primary key, and foreign key constraints on external tables. Unique, primary key, and foreign key constraints are supported only in RELY DISABLE mode.

    DISABLE Clause for information on RELY and DISABLE .

    You must have the privileges necessary to issue the statement in which you are defining the constraint.

    To create a foreign key constraint, in addition, the parent table or view must be in your own schema or you must have the REFERENCES privilege on the columns of the referenced key in the parent table or view.

    ( global_partitioned_index::= , local_partitioned_index::= —part of CREATE INDEX , index_attributes::= . The INDEXTYPE IS . clause is not valid when defining a constraint.)

    This section describes the semantics of constraint . For additional information, refer to the SQL statement in which you define or redefine a constraint for a table or view.

    Oracle Database does not support constraints on columns or attributes whose type is a user-defined object, nested table, VARRAY , REF , or LOB, with two exceptions:

    NOT NULL constraints are supported for a column or attribute whose type is user-defined object, VARRAY , REF , or LOB.

    NOT NULL , foreign key, and REF constraints are supported on a column of type REF .

    Specify a name for the constraint. The name must satisfy the requirements listed in «Database Object Naming Rules» . If you omit this identifier, then Oracle Database generates a name with the form SYS_C n . Oracle stores the name and the definition of the integrity constraint in the USER_ , ALL_ , and DBA_CONSTRAINTS data dictionary views (in the CONSTRAINT_NAME and SEARCH_CONDITION columns, respectively).

    Oracle Database Reference for information on the data dictionary views

    NOT NULL Constraints

    A NOT NULL constraint prohibits a column from containing nulls. The NULL keyword by itself does not actually define an integrity constraint, but you can specify it to explicitly permit a column to contain nulls. You must define NOT NULL and NULL using inline specification. If you specify neither NOT NULL nor NULL , then the default is NULL .

    NOT NULL constraints are the only constraints you can specify inline on XMLType and VARRAY columns.

    To satisfy a NOT NULL constraint, every row in the table must contain a value for the column.

    Oracle Database does not index table rows in which all key columns are null except in the case of bitmap indexes. Therefore, if you want an index on all rows of a table, then you must either specify NOT NULL constraints for at least one of the index key columns or create a bitmap index.

    Restrictions on NOT NULL Constraints

    NOT NULL constraints are subject to the following restrictions:

    You cannot specify NULL or NOT NULL in a view constraint.

    You cannot specify NULL or NOT NULL for an attribute of an object. Instead, use a CHECK constraint with the IS [ NOT ] NULL condition.

    A unique constraint designates a column as a unique key. A composite unique key designates a combination of columns as the unique key. When you define a unique constraint inline, you need only the UNIQUE keyword. When you define a unique constraint out of line, you must also specify one or more columns. You must define a composite unique key out of line.

    To satisfy a unique constraint, no two rows in the table can have the same value for the unique key. However, the unique key made up of a single column can contain nulls. To satisfy a composite unique key, no two rows in the table or view can have the same combination of values in the key columns. Any row that contains nulls in all key columns automatically satisfies the constraint. However, two rows that contain nulls for one or more key columns and the same combination of values for the other key columns violate the constraint.

    Unique constraints are sensitive to declared collations of their key columns. See Collation Sensitivity of Constraints for more details.

    When you specify a unique constraint on one or more columns, Oracle implicitly creates an index on the unique key. If you are defining uniqueness for purposes of query performance, then Oracle recommends that you instead create the unique index explicitly using a CREATE UNIQUE INDEX statement. You can also use the CREATE UNIQUE INDEX statement to create a unique function-based index that defines a conditional unique constraint. See «Using a Function-based Index to Define Conditional Uniqueness: Example» for more information.

    When you specify an enabled unique constraint on an extended data type column, you may receive a «maximum key length exceeded» error when Oracle tries to create the index to enforce uniqueness for the enabled constraint. See «Creating an Index on an Extended Data Type Column» for information on how to work around this issue.

    Restrictions on Unique Constraints

    Unique constraints are subject to the following restrictions:

    None of the columns in the unique key can be of LOB, LONG , LONG RAW , VARRAY , NESTED TABLE , OBJECT , REF , TIMESTAMP WITH TIME ZONE, or user-defined type. However, the unique key can contain a column of TIMESTAMP WITH LOCAL TIME ZONE .

    A composite unique key cannot have more than 32 columns.

    You cannot designate the same column or combination of columns as both a primary key and a unique key.

    You cannot specify a unique key when creating a subview in an inheritance hierarchy. The unique key can be specified only for the top-level (root) view.

    When you specify a unique constraint for an external table, you must specify the RELY and DISABLE constraint states. See External Table Constraints for more information.

    Primary Key Constraints

    A primary key constraint designates a column as the primary key of a table or view. A composite primary key designates a combination of columns as the primary key. When you define a primary key constraint inline, you need only the PRIMARY KEY keywords. When you define a primary key constraint out of line, you must also specify one or more columns. You must define a composite primary key out of line.

    To satisfy a primary key constraint:

    No primary key value can appear in more than one row in the table.

    No column that is part of the primary key can contain a null.

    When you create a primary key constraint:

    Oracle Database uses an existing index if it contains a unique set of values before enforcing the primary key constraint. The existing index can be defined as unique or nonunique. When a DML operation is performed, the primary key constraint is enforced using this existing index.

    If no existing index can be used, then Oracle Database generates a unique index.

    When you drop a primary key constraint:

    If the primary key was created using an existing index, then the index is not dropped.

    If the primary key was created using a system-generated index, then the index is dropped.

    When you designate an extended data type column as an enabled primary key, you may receive a «maximum key length exceeded» error when Oracle tries to create the index to enforce uniqueness for the enabled constraint. See «Creating an Index on an Extended Data Type Column» for information on how to work around this issue.

    Primary key constraints are sensitive to declared collations of their key columns. See Collation Sensitivity of Constraints for more details.

    Restrictions on Primary Key Constraints

    Primary constraints are subject to the following restrictions:

    A table or view can have only one primary key.

    None of the columns in the primary key can be LOB, LONG , LONG RAW , VARRAY , NESTED TABLE , BFILE , REF , TIMESTAMP WITH TIME ZONE , or user-defined type. However, the primary key can contain a column of TIMESTAMP WITH LOCAL TIME ZONE .

    The size of the primary key cannot exceed approximately one database block.

    A composite primary key cannot have more than 32 columns.

    You cannot designate the same column or combination of columns as both a primary key and a unique key.

    You cannot specify a primary key when creating a subview in an inheritance hierarchy. The primary key can be specified only for the top-level (root) view.

    When you specify a primary key constraint for an external table, you must specify the RELY and DISABLE constraint states. See External Table Constraints for more information.

    Foreign Key Constraints

    A foreign key constraint (also called a referential integrity constraint ) designates a column as the foreign key and establishes a relationship between that foreign key and a specified primary or unique key, called the referenced key . A composite foreign key designates a combination of columns as the foreign key.

    The table or view containing the foreign key is called the child object, and the table or view containing the referenced key is called the parent object. The foreign key and the referenced key can be in the same table or view. In this case, the parent and child tables are the same. If you identify only the parent table or view and omit the column name, then the foreign key automatically references the primary key of the parent table or view. The corresponding column or columns of the foreign key and the referenced key must match in order, data types, and declared collations.

    Foreign key constraints are sensitive to declared collations of the referenced primary or unique key columns. See Collation Sensitivity of Constraints for more details.

    You can define a foreign key constraint on a single key column either inline or out of line. You must specify a composite foreign key and a foreign key on an attribute out of line.

    To satisfy a composite foreign key constraint, the composite foreign key must refer to a composite unique key or a composite primary key in the parent table or view, or the value of at least one of the columns of the foreign key must be null.

    You can designate the same column or combination of columns as both a foreign key and a primary or unique key. You can also designate the same column or combination of columns as both a foreign key and a cluster key.

    You can define multiple foreign keys in a table or view. Also, a single column can be part of more than one foreign key.

    Restrictions on Foreign Key Constraints

    Foreign key constraints are subject to the following restrictions:

    None of the columns in the foreign key can be of LOB, LONG , LONG RAW , VARRAY , NESTED TABLE , BFILE , REF , TIMESTAMP WITH TIME ZONE , or user-defined type. However, the primary key can contain a column of TIMESTAMP WITH LOCAL TIME ZONE .

    The referenced unique or primary key constraint on the parent table or view must already be defined.

    A composite foreign key cannot have more than 32 columns.

    The child and parent tables must be on the same database. To enable referential integrity constraints across nodes of a distributed database, you must use database triggers. See CREATE TRIGGER.

    If either the child or parent object is a view, then the constraint is subject to all restrictions on view constraints. See «View Constraints» .

    You cannot define a foreign key constraint in a CREATE TABLE statement that contains an AS subquery clause. Instead, you must create the table without the constraint and then add it later with an ALTER TABLE statement.

    When a table has a foreign key, and the parent of the foreign key is an index-organized table, a session that updates a row that contains the foreign key can hang when another session is updating a non-key column in the parent table.

    When you specify a foreign key constraint for an external table, you must specify the RELY and DISABLE constraint states. See External Table Constraints for more information.

    Oracle Database Development Guide for more information on using constraints

    Foreign key constraints use the references_clause syntax. When you specify a foreign key constraint inline, you need only the references_clause . When you specify a foreign key constraint out of line, you must also specify the FOREIGN KEY keywords and one or more columns.

    ON DELETE Clause

    The ON DELETE clause lets you determine how Oracle Database automatically maintains referential integrity if you remove a referenced primary or unique key value. If you omit this clause, then Oracle does not allow you to delete referenced key values in the parent table that have dependent rows in the child table.

    Specify CASCADE if you want Oracle to remove dependent foreign key values.

    Specify SET NULL if you want Oracle to convert dependent foreign key values to NULL . You cannot specify this clause for a virtual column, because the values in a virtual column cannot be updated directly. Rather, the values from which the virtual column are derived must be updated.

    Restriction on ON DELETE

    You cannot specify this clause for a view constraint.

    A check constraint lets you specify a condition that each row in the table must satisfy. To satisfy the constraint, each row in the table must make the condition either TRUE or unknown (due to a null). When Oracle evaluates a check constraint condition for a particular row, any column names in the condition refer to the column values in that row.

    The syntax for inline and out-of-line specification of check constraints is the same. However, inline specification can refer only to the column (or the attributes of the column if it is an object column) currently being defined, whereas out-of-line specification can refer to multiple columns or attributes.

    Oracle does not verify that conditions of check constraints are not mutually exclusive. Therefore, if you create multiple check constraints for a column, design them carefully so their purposes do not conflict. Do not assume any particular order of evaluation of the conditions.

    If the condition of a check constraint depends on NLS parameters, such as NLS_DATE_FORMAT , Oracle evaluates the condition using the database values of the parameters, not the session values. You can find the database values of the NLS parameters in the data dictionary view NLS_DATABASE_PARAMETERS . These values are associated with a database by the DDL statement CREATE DATABASE and never change afterwards.

    Conditions for additional information and syntax

    Restrictions on Check Constraints

    Check constraints are subject to the following restrictions:

    You cannot specify a check constraint for a view. However, you can define the view using the WITH CHECK OPTION clause, which is equivalent to specifying a check constraint for the view.

    The condition of a check constraint can refer to any column in the table, but it cannot refer to columns of other tables.

    Conditions of check constraints cannot contain the following constructs:

    Subqueries and scalar subquery expressions

    Calls to the functions that are not deterministic ( CURRENT_DATE , CURRENT_TIMESTAMP , DBTIMEZONE , LOCALTIMESTAMP , SESSIONTIMEZONE , SYSDATE , SYSTIMESTAMP , UID , USER , and USERENV )

    Calls to user-defined functions

    Dereferencing of REF columns (for example, using the DEREF function)

    Nested table columns or attributes

    The pseudocolumns CURRVAL , NEXTVAL , LEVEL , or ROWNUM

    Date constants that are not fully specified

    You cannot specify a check constraint for an external table.

    REF constraints let you describe the relationship between a column of type REF and the object it references.

    REF constraints use the ref_constraint syntax. You define a REF constraint either inline or out of line. Out-of-line specification requires you to specify the REF column or attribute you are further describing.

    For ref_column , specify the name of a REF column of an object or relational table.

    For ref_attribute , specify an embedded REF attribute within an object column of a relational table.

    Both inline and out-of-line specification let you define a scope constraint, a rowid constraint, or a referential integrity constraint on a REF column.

    If the scope table or referenced table of the REF column has a primary-key-based object identifier, then the REF column is a user-defined REF column .

    SCOPE REF Constraints

    In a table with a REF column, each REF value in the column can conceivably reference a row in a different object table. The SCOPE clause restricts the scope of references to a single table, scope_table . The values in the REF column or attribute point to objects in scope_table , in which object instances of the same type as the REF column are stored.

    Specify the SCOPE clause to restrict the scope of references in the REF column to a single table. For you to specify this clause, scope_table must be in your own schema, or you must have the READ or SELECT privilege on scope_table , or you must have the READ ANY TABLE or SELECT ANY TABLE system privilege. You can specify only one scope table for each REF column.

    Restrictions on Scope Constraints

    Scope constraints are subject to the following restrictions:

    You cannot add a scope constraint to an existing column unless the table is empty.

    You cannot specify a scope constraint for the REF elements of a VARRAY column.

    You must specify this clause if you specify AS subquery and the subquery returns user-defined REF data types.

    You cannot subsequently drop a scope constraint from a REF column.

    You cannot specify a scope constraint for an external table.

    Rowid REF Constraints

    Specify WITH ROWID to store the rowid along with the REF value in ref_column or ref_attribute . Storing the rowid with the REF value can improve the performance of dereferencing operations, but will also use more space. Default storage of REF values is without rowids.

    The function DEREF for an example of dereferencing

    Restrictions on Rowid Constraints

    Rowid constraints are subject to the following restrictions:

    You cannot define a rowid constraint for the REF elements of a VARRAY column.

    You cannot subsequently drop a rowid constraint from a REF column.

    If the REF column or attribute is scoped, then this clause is ignored and the rowid is not stored with the REF value.

    You cannot specify a rowid constraint for an external table.

    Referential Integrity Constraints on REF Columns

    The references_clause of the ref_constraint syntax lets you define a foreign key constraint on the REF column. This clause also implicitly restricts the scope of the REF column or attribute to the referenced table. However, whereas a foreign key constraint on a non- REF column references an actual column in the parent table, a foreign key constraint on a REF column references the implicit object identifier column of the parent table.

    If you do not specify a constraint name, then Oracle generates a system name for the constraint of the form SYS_C n .

    If you add a referential integrity constraint to an existing REF column that is already scoped, then the referenced table must be the same as the scope table of the REF column. If you later drop the referential integrity constraint, then the REF column will remain scoped to the referenced table.

    As is the case for foreign key constraints on other types of columns, you can use the references_clause alone for inline declaration. For out-of-line declaration you must also specify the FOREIGN KEY keywords plus one or more REF columns or attributes.

    Restrictions on Foreign Key Constraints on REF Columns

    Foreign key constraints on REF columns have the following additional restrictions:

    Oracle implicitly adds a scope constraint when you add a referential integrity constraint to an existing unscoped REF column. Therefore, all the restrictions that apply for scope constraints also apply in this case.

    You cannot specify a column after the object name in the references_clause .

    Collation Sensitivity of Constraints

    Starting with Oracle Database 12 c Release 2 (12.2), primary key, unique, and foreign key constraints are sensitive to declared collations of their key columns. A primary or unique key character column value from a new or updated row is compared with values in existing rows using the declared collation of the key column. For example, if the declared collation of the key column is the case-insensitive collation BINARY_CI , a new or updated row may be rejected if the new key column value differs from some existing key value only by case. The collation BINARY_CI treats character values differing only by case as equal.

    A foreign key character column value is compared to parent primary or unique key column values using the declared collation of the parent key column. For example, if the declared collation of the key column is the case-insensitive collation BINARY_CI , a new or updated child row may be accepted even if there is no identical parent key value for the corresponding foreign key value, provided there exists a value differing only by case.

    The declared collation of a foreign key column must be the same as the collation of the corresponding parent key column.

    Columns in a composite key of a constraint may have different declared collations.

    When the declared collation of a key column of a constraint is a pseudo-collation, the constraint uses a corresponding variant of the collation BINARY . Pseudo-collations cannot be used directly to compare values for a constraint, because constraints are static and cannot depend on session NLS parameters on which the pseudo-collations depend. Therefore:

    The pseudo-collations USING_NLS_COMP , USING_NLS_SORT , and USING_NLS_SORT_CS use the collation BINARY .

    The pseudo-collation USING_NLS_COMP_CI uses the collation BINARY_CI .

    The pseudo-collation USING_NLS_COMP_AI uses the collation BINARY_AI .

    When the effective collation used by a primary or unique key column is not BINARY , Oracle creates a hidden virtual column for this column. The expression of the virtual column calculates collation keys for character values of the original key column. The primary key or unique constraint is internally created on the virtual column instead of the original column. The virtual column is visible in the data dictionary views of the *_TAB_COLS family. For each of these hidden virtual columns, the COLLATED_COLUMN_ID of the *_TAB_COLS views contains the internal sequence number pointing to the corresponding original key column. The hidden virtual columns count to the 1000-column limit of a table.

    Specifying Constraint State

    You can specify how and when Oracle should enforce the constraint when you define the constraint.

    You can use constraint_state with both inline and out-of-line specification. Except for the clauses DEFERRABLE and INITIALLY , that may be specified in any order, you must specify the rest of the component clauses in the order shown, and each clause only once.

    The DEFERRABLE and NOT DEFERRABLE parameters indicate whether or not, in subsequent transactions, constraint checking can be deferred until the end of the transaction using the SET CONSTRAINT ( S ) statement. If you omit this clause, then the default is NOT DEFERRABLE .

    Specify NOT DEFERRABLE to indicate that in subsequent transactions you cannot use the SET CONSTRAINT [ S ] clause to defer checking of this constraint until the transaction is committed. The checking of a NOT DEFERRABLE constraint can never be deferred to the end of the transaction.

    If you declare a new constraint NOT DEFERRABLE , then it must be valid at the time the CREATE TABLE or ALTER TABLE statement is committed or the statement will fail.

    Specify DEFERRABLE to indicate that in subsequent transactions you can use the SET CONSTRAINT [ S ] clause to defer checking of this constraint until a COMMIT statement is submitted. If the constraint check fails, then the database returns an error and the transaction is not committed. This setting in effect lets you disable the constraint temporarily while making changes to the database that might violate the constraint until all the changes are complete.

    The optimizer does not consider indexes on deferrable constraints as usable.

    You cannot alter the deferrability of a constraint. Whether you specify either of these parameters, or make the constraint NOT DEFERRABLE implicitly by specifying neither of them, you cannot specify this clause in an ALTER TABLE statement. You must drop the constraint and re-create it.

    SET CONSTRAINT[S] for information on setting constraint checking for a transaction

    Restriction on [NOT] DEFERRABLE

    You cannot specify either of these parameters for a view constraint.

    The INITIALLY clause establishes the default checking behavior for constraints that are DEFERRABLE . The INITIALLY setting can be overridden by a SET CONSTRAINT ( S ) statement in a subsequent transaction.

    Specify INITIALLY IMMEDIATE to indicate that Oracle should check this constraint at the end of each subsequent SQL statement. If you do not specify INITIALLY at all, then the default is INITIALLY IMMEDIATE .

    If you declare a new constraint INITIALLY IMMEDIATE , then it must be valid at the time the CREATE TABLE or ALTER TABLE statement is committed or the statement will fail.

    Specify INITIALLY DEFERRED to indicate that Oracle should check this constraint at the end of subsequent transactions.

    This clause is not valid if you have declared the constraint to be NOT DEFERRABLE , because a NOT DEFERRABLE constraint is automatically INITIALLY IMMEDIATE and cannot ever be INITIALLY DEFERRED .

    The RELY and NORELY parameters specify whether a constraint in NOVALIDATE mode is to be taken into account for query rewrite. Specify RELY to activate a constraint in NOVALIDATE mode for query rewrite in an unenforced query rewrite integrity mode. The constraint is in NOVALIDATE mode, so Oracle does not enforce it. The default is NORELY .

    Unenforced constraints are generally useful only with materialized views and query rewrite. Depending on the QUERY_REWRITE_INTEGRITY mode, query rewrite can use only constraints that are in VALIDATE mode, or that are in NOVALIDATE mode with the RELY parameter set, to determine join information.

    Restriction on the RELY Clause

    You cannot set a nondeferrable NOT NULL constraint to RELY .

    Oracle Database Data Warehousing Guide for more information on materialized views and query rewrite

    Using Indexes to Enforce Constraints

    When defining the state of a unique or primary key constraint, you can specify an index for Oracle to use to enforce the constraint, or you can instruct Oracle to create the index used to enforce the constraint.

    You can specify the using_index_clause only when enabling unique or primary key constraints. You can specify the clauses of the using_index_clause in any order, but you can specify each clause only once.

    If you specify schema . index , then Oracle attempts to enforce the constraint using the specified index. If Oracle cannot find the index or cannot use the index to enforce the constraint, then Oracle returns an error.

    If you specify the create_index_statement , then Oracle attempts to create the index and use it to enforce the constraint. If Oracle cannot create the index or cannot use the index to enforce the constraint, then Oracle returns an error.

    If you neither specify an existing index nor create a new index, then Oracle creates the index. In this case:

    The index receives the same name as the constraint.

    If table is partitioned, then you can specify a locally or globally partitioned index for the unique or primary key constraint.

    Restrictions on the using_index_clause

    The following restrictions apply to the using_index_clause :

    You cannot specify this clause for a view constraint.

    You cannot specify this clause for a NOT NULL , foreign key, or check constraint.

    You cannot specify an index ( schema.index ) or create an index ( create_index_statement ) when enabling the primary key of an index-organized table.

    You cannot specify the parallel_clause of index_attributes .

    The INDEXTYPE IS . clause of index_properties is not valid in the definition of a constraint.

    CREATE INDEX for a description of index_attributes , the global_partitioned_index and local_partitioned_index clauses, and for a description of NOSORT and the logging_clause in relation to indexes

    Specify ENABLE if you want the constraint to be applied to the data in the table.

    If you enable a unique or primary key constraint, and if no index exists on the key, then Oracle Database creates a unique index. Unless you specify KEEP INDEX when subsequently disabling the constraint, this index is dropped and the database rebuilds the index every time the constraint is reenabled.

    You can also avoid rebuilding the index and eliminate redundant indexes by creating new primary key and unique constraints initially disabled. Then create (or use existing) nonunique indexes to enforce the constraint. Oracle does not drop a nonunique index when the constraint is disabled, so subsequent ENABLE operations are facilitated.

    ENABLE VALIDATE specifies that all old and new data also complies with the constraint. An enabled validated constraint guarantees that all data is and will continue to be valid.

    If any row in the table violates the integrity constraint, then the constraint remains disabled and Oracle returns an error. If all rows comply with the constraint, then Oracle enables the constraint. Subsequently, if new data violates the constraint, then Oracle does not execute the statement and returns an error indicating the integrity constraint violation.

    If you place a primary key constraint in ENABLE VALIDATE mode, then the validation process will verify that the primary key columns contain no nulls. To avoid this overhead, mark each column in the primary key NOT NULL before entering data into the column and before enabling the primary key constraint of the table.

    ENABLE NOVALIDATE ensures that all new DML operations on the constrained data comply with the constraint. This clause does not ensure that existing data in the table complies with the constraint.

    If you specify neither VALIDATE nor NOVALIDATE , then the default is VALIDATE .

    If you change the state of any single constraint from ENABLE NOVALIDATE to ENABLE VALIDATE , then the operation can be performed in parallel, and does not block reads, writes, or other DDL operations.

    Restriction on the ENABLE Clause

    You cannot enable a foreign key that references a disabled unique or primary key.

    Specify DISABLE to disable the integrity constraint. Disabled integrity constraints appear in the data dictionary along with enabled constraints. If you do not specify this clause when creating a constraint, then Oracle automatically enables the constraint.

    DISABLE VALIDATE disables the constraint and drops the index on the constraint, but keeps the constraint valid. This feature is most useful in data warehousing situations, because it lets you load large amounts of data while also saving space by not having an index. This setting lets you load data from a nonpartitioned table into a partitioned table using the exchange_partition_subpart clause of the ALTER TABLE statement or using SQL*Loader. All other modifications to the table (inserts, updates, and deletes) by other SQL statements are disallowed.

    Oracle Database Data Warehousing Guide for more information on using this setting

    DISABLE NOVALIDATE signifies that Oracle makes no effort to maintain the constraint (because it is disabled) and cannot guarantee that the constraint is true (because it is not being validated).

    You cannot drop a table whose primary key is being referenced by a foreign key even if the foreign key constraint is in DISABLE NOVALIDATE state. Further, the optimizer can use constraints in DISABLE NOVALIDATE state.

    Oracle Database SQL Tuning Guide for information on when to use this setting

    If you specify neither VALIDATE nor NOVALIDATE , then the default is NOVALIDATE .

    If you disable a unique or primary key constraint that is using a unique index, then Oracle drops the unique index. Refer to the CREATE TABLE enable_disable_clause for additional notes and restrictions.

    The behavior of VALIDATE and NOVALIDATE depends on whether the constraint is enabled or disabled, either explicitly or by default. Therefore, the VALIDATE and NOVALIDATE keywords are described in the context of «ENABLE Clause» and «DISABLE Clause» .

    Note on Foreign Key Constraints in NOVALIDATE Mode

    When a foreign key constraint is in NOVALIDATE mode, if existing data in the table does not comply with the constraint and the QUERY_REWRITE_INTEGRITY parameter is not set to ENFORCED , then the optimizer may use join elimination during queries on the table. In this case, a query may return table rows with noncompliant foreign key values even if the query contains a join condition that should filter out those rows.

    Handling Constraint Exceptions

    When defining the state of a constraint, you can specify a table into which Oracle places the rowids of all rows violating the constraint.

    Use the exceptions_clause syntax to define exception handling. If you omit schema , then Oracle assumes the exceptions table is in your own schema. If you omit this clause altogether, then Oracle assumes that the table is named EXCEPTIONS . The EXCEPTIONS table or the table you specify must exist on your local database.

    You can create the EXCEPTIONS table using one of these scripts:

    UTLEXCPT.SQL uses physical rowids. Therefore it can accommodate rows from conventional tables but not from index-organized tables. (See the Note that follows.)

    UTLEXPT1.SQL uses universal rowids, so it can accommodate rows from both conventional and index-organized tables.

    If you create your own exceptions table, then it must follow the format prescribed by one of these two scripts.

    If you are collecting exceptions from index-organized tables based on primary keys (rather than universal rowids), then you must create a separate exceptions table for each index-organized table to accommodate its primary-key storage. You create multiple exceptions tables with different names by modifying and resubmitting the script.

    Restrictions on the exceptions_clause

    The following restrictions apply to the exceptions_clause :

    You cannot specify this clause for a view constraint.

    You cannot specify this clause in a CREATE TABLE statement, because no rowids exist until after the successful completion of the statement.

    The DBMS_IOT package in Oracle Database PL/SQL Packages and Types Reference for information on the SQL scripts

    Oracle Database Performance Tuning Guide for information on eliminating migrated and chained rows

    Oracle does not enforce view constraints. However, operations on views are subject to the integrity constraints defined on the underlying base tables. This means that you can enforce constraints on views through constraints on base tables.

    Notes on View Constraints

    View constraints are a subset of table constraints and are subject to the following restrictions:

    You can specify only unique, primary key, and foreign key constraints on views. However, you can define the view using the WITH CHECK OPTION clause, which is equivalent to specifying a check constraint for the view.

    View constraints are supported only in DISABLE NOVALIDATE mode. You cannot specify any other mode. You must specify the keyword DISABLE when you declare the view constraint. You need not specify NOVALIDATE explicitly, as it is the default.

    The RELY and NORELY parameters are optional. View constraints, because they are unenforced, are usually specified with the RELY parameter to make them more useful. The RELY or NORELY keyword must precede the DISABLE keyword.

    Because view constraints are not enforced directly, you cannot specify INITIALLY DEFERRED or DEFERRABLE .

    You cannot specify the using_index_clause , the exceptions_clause clause, or the ON DELETE clause of the references_clause .

    You cannot define view constraints on attributes of an object column.

    External Table Constraints

    Starting with Oracle Database 12 c Release 2 (12.2), you can specify NOT NULL , unique, primary key, and foreign key constraints on external tables.

    NOT NULL constraints on external tables are enforced and prohibit columns from containing nulls.

    Unique, primary key, and foreign key constraints are supported on external tables only in RELY DISABLE mode. You must specify the keywords RELY and DISABLE when you create these constraints. These constraints are declarative and are not enforced. They can increase query performance and reduce resource consumption because more optimizer transformations can be taken into account. In order for the optimizer to utilize these RELY DISABLE constraints, the QUERY_REWRITE_INTEGRITY initialization parameter must be set to either trusted or stale_tolerated .

    Unique Key Example

    The following statement is a variation of the statement that created the sample table sh.promotions . It defines inline and implicitly enables a unique key on the promo_id column (other constraints are not shown):

    The constraint promo_id_u identifies the promo_id column as a unique key. This constraint ensures that no two promotions in the table have the same ID. However, the constraint does allow promotions without identifiers.

    Alternatively, you can define and enable this constraint out of line:

    The preceding statement also contains the using_index_clause , which specifies storage characteristics for the index that Oracle creates to enable the constraint.

    Composite Unique Key Example

    The following statement defines and enables a composite unique key on the combination of the warehouse_id and warehouse_name columns of the oe.warehouses table:

    The wh_unq constraint ensures that the same combination of warehouse_id and warehouse_name values does not appear in the table more than once.

    The ADD CONSTRAINT clause also specifies other properties of the constraint:

    The USING INDEX clause specifies storage characteristics for the index Oracle creates to enable the constraint.

    The EXCEPTIONS INTO clause causes Oracle to write to the wrong_id table information about any rows currently in the warehouses table that violate the constraint. If the wrong_id exceptions table does not already exist, then this statement will fail.

    Primary Key Example

    The following statement is a variation of the statement that created the sample table hr.locations . It creates the locations_demo table and defines and enables a primary key on the location_id column (other constraints from the hr.locations table are omitted):

    The loc_id_pk constraint, specified inline, identifies the location_id column as the primary key of the locations_demo table. This constraint ensures that no two locations in the table have the same location number and that no location identifier is NULL .

    Alternatively, you can define and enable this constraint out of line:

    NOT NULL Example

    The following statement alters the locations_demo table (created in «Primary Key Example» ) to define and enable a NOT NULL constraint on the country_id column:

    The constraint country_nn ensures that no location in the table has a null country_id .

    Composite Primary Key Example

    The following statement defines a composite primary key on the combination of the prod_id and cust_id columns of the sample table sh.sales :

    This constraint identifies the combination of the prod_id and cust_id columns as the primary key of the sales table. The constraint ensures that no two rows in the table have the same combination of values for the prod_id column and cust_id columns.

    The constraint clause ( PRIMARY KEY ) also specifies the following properties of the constraint:

    The constraint definition does not include a constraint name, so Oracle generates a name for the constraint.

    The DISABLE clause causes Oracle to define the constraint but not enable it.

    Foreign Key Constraint Example

    The following statement creates the dept_20 table and defines and enables a foreign key on the department_id column that references the primary key on the department_id column of the departments table:

    The constraint fk_deptno ensures that all departments given for employees in the dept_20 table are present in the departments table. However, employees can have null department numbers, meaning they are not assigned to any department. To ensure that all employees are assigned to a department, you could create a NOT NULL constraint on the department_id column in the dept_20 table in addition to the REFERENCES constraint.

    Before you define and enable this constraint, you must define and enable a constraint that designates the department_id column of the departments table as a primary or unique key.

    The foreign key constraint definition does not use the FOREIGN KEY clause, because the constraint is defined inline. The data type of the department_id column is not needed, because Oracle automatically assigns to this column the data type of the referenced key.

    The constraint definition identifies both the parent table and the columns of the referenced key. Because the referenced key is the primary key of the parent table, the referenced key column names are optional.

    Alternatively, you can define this foreign key constraint out of line:

    The foreign key definitions in both variations of this statement omit the ON DELETE clause, causing Oracle to prevent the deletion of a department if any employee works in that department.

    ON DELETE Example

    This statement creates the dept_20 table, defines and enables two referential integrity constraints, and uses the ON DELETE clause:

    Because of the first ON DELETE clause, if manager number 2332 is deleted from the employees table, then Oracle sets to null the value of manager_id for all employees in the dept_20 table who previously had manager 2332.

    Because of the second ON DELETE clause, Oracle cascades any deletion of a department_id value in the departments table to the department_id values of its dependent rows of the dept_20 table. For example, if Department 20 is deleted from the departments table, then Oracle deletes all of the employees in Department 20 from the dept_20 table.

    Composite Foreign Key Constraint Example

    The following statement defines and enables a foreign key on the combination of the employee_id and hire_date columns of the dept_20 table:

    The constraint fk_empid_hiredate ensures that all the employees in the dept_20 table have employee_id and hire_date combinations that exist in the employees table. Before you define and enable this constraint, you must define and enable a constraint that designates the combination of the employee_id and hire_date columns of the employees table as a primary or unique key.

    The EXCEPTIONS INTO clause causes Oracle to write information to the wrong_emp table about any rows in the dept_20 table that violate the constraint. If the wrong_emp exceptions table does not already exist, then this statement will fail.

    Check Constraint Examples

    The following statement creates a divisions table and defines a check constraint in each column of the table:

    Each constraint restricts the values of the column in which it is defined:

    check_divno ensures that no division numbers are less than 10 or greater than 99.

    check_divname ensures that all division names are in uppercase.

    check_office restricts office locations to Dallas, Boston, Paris, or Tokyo.

    Because each CONSTRAINT clause contains the DISABLE clause, Oracle only defines the constraints and does not enable them.

    The following statement creates the dept_20 table, defining out of line and implicitly enabling a check constraint:

    This constraint uses an inequality condition to limit an employee’s total commission, the product of salary and commission_pct , to $5000:

    If an employee has non-null values for both salary and commission, then the product of these values must not exceed $5000 to satisfy the constraint.

    If an employee has a null salary or commission, then the result of the condition is unknown and the employee automatically satisfies the constraint.

    Because the constraint clause in this example does not supply a constraint name, Oracle generates a name for the constraint.

    The following statement defines and enables a primary key constraint, two foreign key constraints, a NOT NULL constraint, and two check constraints:

    The constraints enable the following rules on table data:

    pk_od identifies the combination of the order_id and part_no columns as the primary key of the table. To satisfy this constraint, no two rows in the table can contain the same combination of values in the order_id and the part_no columns, and no row in the table can have a null in either the order_id or the part_no column.

    fk_oid identifies the order_id column as a foreign key that references the order_id column in the orders table in the sample schema oe . All new values added to the column order_detail . order_id must already appear in the column oe.orders.order_id .

    fk_pno identifies the product_id column as a foreign key that references the product_id column in the product_information table owned by oe . All new values added to the column order_detail.product_id must already appear in the column oe.product_information.product_id .

    nn_qty forbids nulls in the quantity column.

    check_qty ensures that values in the quantity column are always greater than zero.

    check_cost ensures the values in the cost column are always greater than zero.

    This example also illustrates the following points about constraint clauses and column definitions:

    Out-of-line constraint definition can appear before or after the column definitions. In this example, the out-of-line definition of the pk_od constraint precedes the column definitions.

    A column definition can contain multiple inline constraint definitions. In this example, the definition of the quantity column contains the definitions of both the nn_qty and check_qty constraints.

    A table can have multiple CHECK constraints. Multiple CHECK constraints, each with a simple condition enforcing a single business rule, are preferable to a single CHECK constraint with a complicated condition enforcing multiple business rules. When a constraint is violated, Oracle returns an error identifying the constraint. Such an error more precisely identifies the violated business rule if the identified constraint enables a single business rule.

    Case-Insensitive Constraints Example

    The following statements create two tables in a parent-child relationship. The parent table is a product description table and the child table is a product component description table. Unique constraints are defined to assure that product and description values are unambiguous. For illustrative purposes, the product and component ID are case-insensitive character values. (In real-world applications, primary key IDs are usually numeric or case-normalized.)

    Note that if you do not specify the data type or the collation for a foreign key column, then they are inherited from the parent key column.

    The following statements add a product and its components into the tables:

    Note the different case of the product ID in different component rows. Because the primary key on the product ID is declared as case-insensitive, all possible letter case combinations of the same ID are considered equal.

    The following statement demonstrates that it is not possible to enter another product with the same description differing only by case. It fails with the error ORA-00001: unique constraint ( schema .PRODUCT_DESCRIPTION_UNQ) violated .

    Similarly, the following statement demonstrates that the primary key contraint of the product table is case-insensitive and does not allow values differing only by case. It fails with the error ORA-00001: unique constraint ( schema .PRODUCT_PK) violated .

    The following statement demonstrates that it is not possible to enter another component with the same description differing only by case. It fails with the error ORA-00001: unique constraint ( schema .PRODUCT_COMPONENT_DESCR_UNQ) violated .

    Attribute-Level Constraints Example

    The following example guarantees that a value exists for both the first_name and last_name attributes of the name column in the students table:

    REF Constraint Examples

    The following example creates a duplicate of the sample schema object type cust_address_typ , and then creates a table containing a REF column with a SCOPE constraint:

    The following example creates the same table but with a referential integrity constraint on the REF column that references the object identifier column of the parent table:

    The following example uses the type department_typ and the table departments_obj_t , created in «Creating Object Tables: Examples» . A table with a scoped REF is then created.

    The following statement creates a table with a REF column which has a referential integrity constraint defined on it:

    Explicit Index Control Example

    The following statement shows another way to create a unique (or primary key) constraint that gives you explicit control over the index (or indexes) Oracle uses to enforce the constraint:

    This example also shows that you can create an index for one constraint and use that index to create and enable another constraint in the same statement.

    DEFERRABLE Constraint Examples

    The following statement creates table games with a NOT DEFERRABLE INITIALLY IMMEDIATE constraint check (by default) on the scores column:

    To define a unique constraint on a column as INITIALLY DEFERRED DEFERRABLE , issue the following statement:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *