Adding constraint to an existing table column
-
27-02-2021 - |
Question
I have a table that doesnt have any constraint on them like this
CREATE TABLE SomeTable
(
Id int NOT NULL,
Type varchar(50),
)
insert into SomeTable values (12,'Exchange-Student');
Now I know that I can do alter
into add constraint
and then check
if type is of my desired value.
I just dont know how to implement it correctly.
For example lets say that I want it to be restricted to 3
types 'Exchange-Studen'
, 'Independent student'
and 'program Student'
How would I implement that ?
Solution
To alter ypour existing table use
ALTER TABLE SomeTable
ADD CONSTRAINT `Student_type2` CHECK (Type IN
('Exchange-Student','Independent student','program Student'))
Or ad following to the CREATE TABLE seperated by comma
CONSTRAINT `Student_type2` CHECK (Type IN
('Exchange-Student','Independent student','program Student')
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange