How to check a string while entering it in MySQL DB for characters in the beginning by DEFAULT? -


i want create table want list books on different subject. want include first letter of topic in book id. example, books on mathematics contain 'm', literature contain 'c'. had idea use create table book (id varchar(5) default 'm%', book_name varchar(10)) but, it's showing error.

static analysis:

2 errors found during analysis.

a comma or closing bracket expected. (near "'m%'" @ position 46) blockquote

unexpected beginning of statement. (near "10" @ position 71)

sql query:

create table book (id varchar(5) default 'm%', book_name varchar(10)) 

mysql said: documentation

1064 - have error in sql syntax; check manual corresponds mariadb server version right syntax use near 'like 'm%', book_name varchar(10))' @ line 1

the query usage of default wrong. can have fixed text in default. not sure if want text ending m in id or book_name, looking @ query assuming id.

you need use constraint control values allowed id. correct query constraint below

create table book (id varchar(5), book_name varchar(10), constraint check_book_name check (id '%m')) 

next thing is

i want include first letter of topic in book id.

to can add constraint check can control through insert statement.

my suggestion formulate data set. create table. based on dataset add constraint on table. add pre-insert validations. pre-insert validation along app or code executing insert statement.


Comments

Popular posts from this blog

xcode - CocoaPod Storyboard error: -

c# - AutoMapper - What's difference between Condition and PreCondition -