Skip to content
Advertisement

How do you Alter a column in Oracle SQL to only allow it to take in two values, either a Y or a N?

Most of the results I have seen were for MySQL. I understand that Alter Table is to be used, as well as MODIFY, but after that I am stuck

My table in question is this:

CREATE TABLE store_reps
(
rep_id NUMBER(5) PRIMARY KEY,
LastN VARCHAR2(15),
FirstN VARCHAR2(10),
comm CHAR(1) DEFAULT 'Y'
);

For the comm column, I want to set it where it can only allow either “Y” or “N”. I hope this is enough to help solve the problem.

Advertisement

Answer

You are looking for a check constraint:

CREATE TABLE store_reps (
    rep_id NUMBER(5) PRIMARY KEY,
    LastN VARCHAR2(15),
    FirstN VARCHAR2(10),
    comm CHAR(1) DEFAULT 'Y',
    CONSTRAINT chk_store_reps_comm CHECK (comm IN ('Y', 'N'))
);

Or after the table is created:

ALTER TABLE store_reps
    ADD CONSTRAINT chk_store_reps_comm CHECK (comm IN ('Y', 'N'));
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement