WikiGalaxy

Personalize

SQL Insert Into

Introduction:

The SQL INSERT INTO statement is used to insert new records in a table. It is an essential part of SQL operations, allowing you to add data to your database.

Basic Syntax:

The basic syntax for the INSERT INTO statement is as follows:


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
      

Inserting Data into Specific Columns:

When inserting data, you can specify the columns you want to insert values into. This is useful when you do not need to insert data into every column.


INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
      

Inserting Multiple Rows:

To insert multiple rows at once, you can use a single INSERT INTO statement with multiple sets of values.


INSERT INTO table_name (column1, column2)
VALUES 
  (value1a, value2a),
  (value1b, value2b),
  (value1c, value2c);
      

Inserting Data from Another Table:

You can also insert data into a table by selecting data from another table. This is done using the INSERT INTO SELECT statement.


INSERT INTO table2 (column1, column2)
SELECT column1, column2
FROM table1
WHERE condition;
      

Handling Null Values:

If a column can accept NULL values, you can insert a NULL value by simply omitting it from the INSERT INTO statement or explicitly using NULL.


INSERT INTO table_name (column1, column2)
VALUES (value1, NULL);
      

Using Default Values:

If a column has a default value, you can insert a row without specifying a value for that column, and the default will be used.


INSERT INTO table_name (column1)
VALUES (value1);
      

Avoiding Duplicate Entries:

To avoid duplicate entries, you can use constraints like PRIMARY KEY or UNIQUE on the table columns.


// Example with UNIQUE constraint
CREATE TABLE example_table (
  id INT PRIMARY KEY,
  email VARCHAR(255) UNIQUE
);
      

Checking Insert Results:

After performing an INSERT INTO operation, you can use the SELECT statement to verify the inserted data.


SELECT * FROM table_name WHERE column1 = value1;
      
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025