MySQL constraint (Constraints): One, non-null constraint

Source: Internet
Author: User

nonempty constraint NOT NULL constraint:

Forcing a column to not be a NULL value, the Constraint force field always contains a value. This means that if you do not add a value to the field, you cannot insert a new record or update the record.

1. Create a not null constraint on the ID column,the name column when the "Persons" table is created:

CREATE TABLE Persons (
ID int not NULL,
P_name varchar () NOT NULL,
Deparment varchar (20),
Address varchar (20),
Telnum varchar (20)
)
DESC Persons;

  

Results:

Insert

Insert into Persons (id,p_name,deparment,address,telnum) VALUES (' 1 ', ' Zhang San ', ' marketing ', ' x y ', ' "') ' SELECT * from Persons

Results:

Insert a null value to see the effect:

3. Delete the NOT NULL constraint after the table is created (via the ALTER table statement):

ALTER TABLE Persons MODIFY p_name varchar (20); DESC Persons;

  

4. Increase the NOT NULL constraint after the table is created (via the ALTER table statement):

ALTER TABLE Persons MODIFY p_name varchar (a) not null;desc Persons;

  

MySQL constraint (Constraints): One, non-null constraint not a nulls constraint

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.