Skip to content
Advertisement

Can I allow only certain specific strings to be inputted for a table in Oracle 10G?

CREATE TABLE trial (
    phone_number NUMBER(5) PRIMARY KEY;
    name VARCHAR2(20)
)

In this table, I want a constraint such that, name inputted should be only ‘trial’,’experiment’ or ‘test’.

Advertisement

Answer

You can add the CHECK constraint as follows:

ALTER TABLE trial  
ADD CONSTRAINT name_CHK CHECK (NAME IN ('trial','experiment','test'));

If you don’t want the null values in the name then you can also use NOT NULL constraint on the name column.

While creating the table, you can use the Constraint inside it as follows:

CREATE TABLE trial (
    phone_number NUMBER(5) PRIMARY KEY;
    name VARCHAR2(20) not null,
    CONSTRAINT name_CHK CHECK (NAME IN ('trial','experiment','test')
);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement