In MySQL, each field definition contains additional constraints or modifiers that can be used to increase the constraints on the input data. Today, let's take a look at MySQL field constraints:
NULLAnd
Not NULLmodifiers,
DEFAULTModifier,
auto_incrementModifier.
null and NOT NULL modifiers: You can add this null or NOT NULL modifier after each field to specify whether the field can be empty (null), or you must fill in the data (not NULL). MYSQL Specifies the null modifier by default, and if a field is specified as not null,mysql it is not allowed to insert a null value into the field (which is said to be null) because it is "prescribed". CREATE TABLE friends (ID int (3) not null,name varchar (8) Not null,pass varchar (a) not null); INSERT into Friendsvalues (NULL, ' simaopig ', ' Simaopig '), but in the self-increment column and timestamp field, this rule does not apply. Inserting a null value into these fields will result in the insertion of the next automatically incremented value or the current timestamp.
DEFAULT modifier: You can set a default value for a field by using the default modifier. When you insert a record, your old man forgets to pass the value of the field, and MySQL automatically sets the default value for you on that field. CREATE table IM (id int (3) not null,name varchar (+) NOT null default ' QQ '); INSERT into IM (ID, name) VALUES (2, ' MSN '); INSERT into IM (ID) VALUES (3); SELECT * from im LIMIT 0, 30; If the default modifier is not specified in a field, MySQL automatically sets the defaults based on whether the field is null or NOT NULL.
If the specified field can be null, MySQL sets the default value to NULL for it。
if it is a NOT null field, MySQL inserts 0 for the numeric type, the string type inserts an empty string, the timestamp type is inserted into the current date and time, and the enum type is inserted into the first of the enumeration group.
auto_increment modifier:The Auto_increment modifier applies only to the Int field, indicating that MySQL should automatically generate a number for that field (each time it adds 1 above the last value generated). This is useful for primary keys (described later). Because it allows developers to use MySQL to create unique identifiers for each record. CREATE TABLE items (id int (5) NOT NULL auto_increment PRIMARY KEY, label varchar (255) is not NULL); INSERT into items (label) VALUES (' xxx '); Insert into items values (' yyy '); INSERT into items (label) values (' zzz '); select * from items;
Field constraints in MySQL null, NOT NULL, default, Auto_increment