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.
Asked
Active
Viewed 3,731 times
2

wchiquito
- 16,177
- 2
- 34
- 45

Tavish Barua
- 41
- 1
- 4
-
2See: [CHECK constraint in MySQL is not working](http://stackoverflow.com/questions/2115497). – wchiquito Apr 09 '17 at 06:21
2 Answers
0
CREATE TABLE Sales_order(
S_order_no varchar(6) primary key check(S_order_no Like 'O%'),
S_order_date Date,
);

blackgreen
- 34,072
- 23
- 111
- 129
-
2Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jul 26 '20 at 09:06
-1
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')
);

Whoiskp
- 444
- 4
- 7
-
2No! MySQL does not support CHECK and the syntax is absolutely not the same for all DB engines. – juergen d Apr 09 '17 at 06:30