How to restrict a column value in SQLite / MySQL

后端 未结 5 1975
日久生厌
日久生厌 2020-12-14 18:03

I would like to restrict a column value in a SQL table. For example, the column values can only be \"car\" or \"bike\" or \"van\". My question is how do you achieve this in

5条回答
  •  庸人自扰
    2020-12-14 18:19

    You would use a check constraint. In SQL Server it works like this

    ALTER TABLE Vehicles
    ADD CONSTRAINT chkVehicleType CHECK (VehicleType in ('car','bike','van'));
    

    I'm not sure if this is ANSI standard but I'm certain that MySQL has a similar construct.

提交回复
热议问题