Add a column in a table with a first letter must be 0?

4.1k views Asked by At

I want to add a column S_order_no in a table Sales_order with a constraint that the first letter must start with 0. So that if we try to write the first letter other than 0 it throws an error. Is it possible?. If yes please explain with an example.

sales_order table

2

There are 2 answers

1
Whoiskp On

You can try:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Sales_order(
    S_order_no varchar(6) primary key,
    S_order_date Date,
    CONSTRAINT CHK_order_no CHECK (left(S_order_no, 1) = '0')
);
1
Dilkhush Gorana On
CREATE TABLE Sales_order(
    S_order_no varchar(6) primary key check(S_order_no Like 'O%'),
    S_order_date Date,
   
);