MySQL's three attribute constraints (gender defaults to male and female)

1. DEFAULT: Default value constraint

For example, when some data inserted is empty or no data is inserted, we can give a default value

CREATE TABLE students (
  no INT,
  name VARCHAR(32),
  sex CHAR(1) DEFAULT 'm',
  age INT(3),
  email VARCHAR(255)
);

2. Use ENUM type to limit field values

ENUMis a string object whose value is selected from the list of allowed values ​​defined when the column is created

CREATE TABLE student (
  name VARCHAR(32),
  sex ENUM('男', '女'),
  age INT(3),
  email VARCHAR(255)
);

3. CHECK check constraint
note: (CHECK constraint: used to limit the range of values ​​in the column. MySQL5.7 does not support this constraint, but the write statement will not report an error. MySQL8.0 version supports this constraint.)

create table test_user
(
id int primary key,
name VARCHAR(20),
sex VARCHAR(1),
check (sex='男'or sex='女')
);


 

Guess you like

Origin blog.csdn.net/nuhao/article/details/132928381