Non-null constraint

Source: Internet
Author: User

When you define a data table, all fields are allowed to be null by default, and if you need to disallow fields to be empty, you need to display the specified when you create the table. Specifies that a field is empty by adding not null after the field definition, for example, the following SQL statement creates a table T_person, and the Set Fnumber field is not allowed to be empty:

MYSQL,MSSQLServer,DB2:CREATE TABLE T_Person (FNumber VARCHAR(20) NOT NULL ,FName VARCHAR(20),FAge INT)Oracle:CREATE TABLE T_Person (FNumber VARCHAR2(20) NOT NULL ,FName VARCHAR2(20),FAge NUMBER (10))

After creating the T_person table, we execute the following SQL statement to test:

INSERT INTO T_Person (FNumber, FName, FAge) VALUES ( NULL , "kingchou", 20)

Because the SET field Fnumber cannot be empty when defining the T_person table, and the Fnumber field is set to NULL in this SQL statement, the database will report the following error message after executing this SQL statement in the database:

    • You cannot insert a value of NULL into the column "Fnumber", the Table "Demo.dbo.T_Person", and the column does not allow null values. INSERT failed. The following SQL statement can be executed correctly:
INSERT INTO T_Person (FNumber, FName, FAge) VALUES ( "1" , "kingchou", 20)

Non-null constraints work not only on the data that is inserted through the INSERT statement, but also on updates with the UPDATE statement. Execute the following SQL statement to try to update the Fnumber field of the data you just inserted to null:

UPDATE T_Person SET FNumber = NULL

After executing this SQL statement in the database, the database will report the following error message:

    • You cannot insert a value of NULL into the column "Fnumber", the Table "Demo.dbo.T_Person", and the column does not allow null values. UPDATE failed.

Non-null constraint

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.