How to create conditional structure in SQL?

Asked

Viewed 71 times

1

I need to create conditions for the attribute nome and idade, which are:

  • For the name: Reject nomes containing the word %mentiroso%.
  • For Age: Reject idades < 1 || > 130.

Follow the creation of my table:

create table exemplo(id int PRIMARY KEY,
              nome char(30) not null....,
              idade ....,
              sexo enum('m', 'M', 'f', 'F'));

1 answer

0

In the definition of your table use the CHECK clause.

To check age:

int idade CHECK (idade > 1 AND idade <=130),

For the name:

nome char(30) CHECK (nome NOT LIKE '%mentiroso%'),
  • didn’t work out :(

  • What went wrong ?

  • https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.