Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Introduction to the NOT NULL Constraint in SQL

Tech 1

In SQL, the NOT NULL constraint ensures that a column does not accept NULL values.

This constraint is typically used when creating or modifying tables to ensure data integrity and accuracy.

Here are some common uses of NOT NULL:

  1. Specifying NOT NULL Constraint When Creatign a Table: When creating a new table, you can add NOT NULL after the column definition to specify that the column cannot contain NULL values.

    CREATE TABLE customers (
        customer_id INT NOT NULL,
        first_name VARCHAR(50) NOT NULL,
        last_name VARCHAR(50) NOT NULL,
        email VARCHAR(100)
    );
    

    In this example, the columns customer_id, first_name, and last_name cannot contain NULL values, whereas the email column has no such constraint and can be NULL.

  2. Adding NOT NULL Constraint When Modifying a Table: If an existing table needs to have a NOT NULL constraint added to a specific column, you can use the ALTER TABLE statement.

    ALTER TABLE customers
    MODIFY email VARCHAR(100) NOT NULL;
    

    This statement changes the constraint of the email column from allowing NULL to disallowing NULL.

  3. Using NOT NULL with DEFAULT Values in Column Definitions: You can combine NOT NULL with the DEFAULT keyword to provide a default value for a column.

    CREATE TABLE orders (
        order_id INT NOT NULL,
        customer_id INT NOT NULL,
        order_date DATETIME NOT NULL DEFAULT NOW()
    );
    

    In this case, if a new order is inserted without specifying the order_date, the current date will be autmoatically used as the default value.

  4. Handling NOT NULL Constraints in INSERT Statements: When inserting data into a table that contains NOT NULL constraints, values must be provided for those columns.

    INSERT INTO orders (order_id, customer_id)
    VALUES (1, NULL);
    
    INSERT INTO orders (order_id, customer_id)
    VALUES (1, 1);
    

    Attempting to insert a record missing values for order_id or customer_id will result in a error because these columns have NOT NULL constraints.

  5. Handling NOT NULL Constraints in UPDATE Statements: Even though a column has a NOT NULL constraint, you can still update it using an UPDATE statement, as long as the new value is not NULL.

    UPDATE customers
    SET first_name = 'Jane'
    WHERE customer_id = 1;
    

The NOT NULL constraint is an essential part of database design. It helps maintain data integrity and prevents incomplete data from being stored in the database. Properly using NOT NULL constraints during database design enhances data reliability and application robustness.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.