2

Есть ли возможность добавить сразу несколько ограничений в таблицу командой ALTER TABLE?

Знаю, что подобное возможно в SQL Server:

ALTER TABLE l_customer_order 
    ADD CONSTRAINT pk_l_customer_order 
        PRIMARY KEY (customer_order_id_hk),
    CONSTRAINT fk_customer_id_hk 
        FOREIGN KEY (customer_id_hk) 
        REFERENCES h_customers(customer_id_hk)
        ON DELETE CASCADE,
    CONSTRAINT fk_order_id_hk 
        FOREIGN KEY (order_id_hk) 
        REFERENCES h_orders(order_id_hk)
        ON DELETE CASCADE;

Но получаю ошибку:

ORA-01735: invalid ALTER TABLE option


Свободный перевод вопроса Adding multiple constraints in ORACLE using ALTER TABLE от участника @Maeaex1

1

1 ответ 1

5

Заключите все добавляемые ограничения в круглые скобки:

create table t (c1 int, c2 int, c3 int);
    
alter table t
    add ( 
        constraint pk primary key ( c1 ),
        constraint ck check ( c2 > 0 )
    )
/
select constraint_name 
from user_constraints
where table_name = 'T';

CONSTRAINT_NAME
----------------
CK
PK

Прим.ред.: Отсутвие скобок на синтаксической диаграмме скорее всего ошибка в документации.


Свободный перевод ответа от участника @Chris Saxon

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.