insert NULL values to database

Certainly! If you want to insert some NULL values into your database, you can modify the INSERT statement to include NULL in place of the actual values for any column that allows NULL values. Here’s an example based on your current Books table structure:

INSERT INTO Books (BookID, Title, Author, ISBN, Price, InStock)
VALUES (6, 'The Hobbit', 'J.R.R. Tolkien', NULL, 8.99, TRUE), (7, NULL, 'Agatha Christie', '9780007136834', 12.99, FALSE), (8, 'War and Peace', NULL, '9781400079988', NULL, TRUE);
Enter fullscreen mode
Exit fullscreen mode

In this example:

  • For the book with BookID 6, the ISBN is set to NULL.
  • For the book with BookID 7, the Title is set to NULL.
  • For the book with BookID 8, the Author and Price are set to NULL.

Remember, you can only insert NULL values into columns that are defined to accept NULL values in the database schema. If a column is defined as NOT NULL, attempting to insert a NULL value will result in an error.

Image description

DON’T allow to create database with NULL values

Certainly! Based on the SQL code you’ve provided, which creates a Customer table with customer_id and customer_name columns, here’s a simple practical example of how to insert data into this table:

INSERT INTO Customer (customer_id, customer_name)
VALUES (1, 'Alice Smith'), (2, 'Bob Johnson'), (3, 'Charlie Davis');
Enter fullscreen mode
Exit fullscreen mode

This SQL statement inserts three rows into the Customer table. Each row includes a unique customer_id and a customer_name. Remember that since both columns are marked as NOT NULL, you must provide a value for each column when inserting a new row.

Also, please note that in your CREATE TABLE statement, the NOT NULL constraints should be written as NOT NULL without spaces within the keywords. Here’s the corrected CREATE TABLE statement:

CREATE TABLE Customer ( customer_id INT NOT NULL, customer_name VARCHAR(255) NOT NULL
);
Enter fullscreen mode
Exit fullscreen mode

With this corrected table structure, the INSERT statement I provided above will work correctly to add new customers to your database.


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Wishlist urban safari pet.