field constraints in MySQL null, NOT NULL, default, Auto_increment

Source: Internet
Author: User
Tags modifier modifiers

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 itif 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.