SQL Word Limit in a table

818 views Asked by At

I am trying to create a table for storing information about books related to certain modules and the specification for one of the fields is that it stores a maximum or 5 words regarding the content of each book. Is there a way of limiting the word count in SQL or would it be done using another method?

1

There are 1 answers

1
Kombajn zbożowy On

Depends on the database, but you might check using regular expressions. An example for Oracle:

CREATE TABLE books (
       id INTEGER PRIMARY KEY,
       name VARCHAR2(100),
       keywords VARCHAR2(100),
       CONSTRAINT ck_keywords CHECK (regexp_like (keywords, '^(\w+ ){0,4}\w+$'))
);

INSERT INTO books VALUES (1, 'Kombajn zbożowy', '');
INSERT INTO books VALUES (2, 'Weeheee', 'wee');
INSERT INTO books VALUES (3, 'Stack Overflow', 'programming databases questions answers');
INSERT INTO books VALUES (4, 'Blah', 'blah blah blah blah blah blah'); -- this will fail