Insert Data

This document describes how to insert data into MatrixOne by using the SQL language.

Before you start

Make sure you have already Deployed standalone MatrixOne.

INSERT INTO Statement

It is possible to write the INSERT INTO statement in several ways:

  1. Specify both the column names and the values to be inserted:

    1. INSERT INTO tbl_name (a,b,c) VALUES (1,2,3);
  2. If you add values for all the table columns, you do not need to specify the column names in the SQL query. However, make sure the values’ order is in the same order as the columns in the table. Here, the INSERT INTO syntax would be as follows:

    1. INSERT INTO tbl_name VALUES (1,2,3);
  3. INSERTstatements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of comma-separated column values, with lists enclosed within parentheses and separated by commas. Example:

    1. INSERT INTO tbl_name (a,b,c) VALUES(1,2,3), (4,5,6), (7,8,9);

Demo Database

Below is a selection from the “Customers” table in the Northwind sample database:

  1. CREATE TABLE Customers (
  2. CustomerID INT AUTO_INCREMENT NOT NULL,
  3. CustomerName VARCHAR(40) NOT NULL,
  4. ContactName VARCHAR(30) NULL,
  5. Address VARCHAR(60) NULL,
  6. City VARCHAR(15) NULL,
  7. PostalCode VARCHAR(10) NULL,
  8. Country VARCHAR(15) NULL,
  9. PRIMARY KEY (CustomerID)
  10. );
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
89White Clover MarketsKarl Jablonski305 - 14th Ave. S. Suite 3BSeattle98128USA
90Wilman KalaMatti KarttunenKeskuskatu 45Helsinki21240Finland
91WolskiZbyszekul. Filtrowa 68Walla01-012Poland

INSERT INTO Example

The following SQL statement inserts a new record in the “Customers” table:

Example

  1. INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
  2. VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

The selection from the “Customers” table will now look like this:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
89White Clover MarketsKarl Jablonski305 - 14th Ave. S. Suite 3BSeattle98128USA
90Wilman KalaMatti KarttunenKeskuskatu 45Helsinki21240Finland
91WolskiZbyszekul. Filtrowa 68Walla01-012Poland
92CardinalTom B. ErichsenSkagen 21Stavanger4006Norway

Insert Data Only in Specified Columns

It is also possible to only insert data in specific columns.

Example

The following SQL statement will insert a new record, but only insert data in the “CustomerName”, “City”, and “Country” columns (CustomerID will be updated automatically):

  1. INSERT INTO Customers (CustomerName, City, Country)
  2. VALUES ('Cardinal', 'Stavanger', 'Norway');

The selection from the “Customers” table will now look like this:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
89White Clover MarketsKarl Jablonski305 - 14th Ave. S. Suite 3BSeattle98128USA
90Wilman KalaMatti KarttunenKeskuskatu 45Helsinki21240Finland
91WolskiZbyszekul. Filtrowa 68Walla01-012Poland
92CardinalnullnullStavangernullNorway

INSERT INTO…SELECT

With INSERT INTO SELECT, you can quickly insert many rows into a table from the result of a SELECT statement, which can select from one or many tables. The INSERT INTO SELECT statement requires that the data types in source and target tables match.

INSERT INTO SELECT Syntax

Copy all columns from one table to another table:

  1. INSERT INTO *table2*
  2. SELECT * FROM *table1
  3. *WHERE *condition*;

Copy only some columns from one table into another table:

  1. INSERT INTO *table2* (*column1*, *column2*, *column3*, ...)
  2. SELECT *column1*, *column2*, *column3*, ...
  3. FROM *table1*
  4. WHERE *condition*;

Northwind sample database

In this tutorial we will use the Northwind sample database.

  1. CREATE TABLE Customers (
  2. CustomerID INT AUTO_INCREMENT NOT NULL,
  3. CustomerName VARCHAR(40) NOT NULL,
  4. ContactName VARCHAR(30) NULL,
  5. Address VARCHAR(60) NULL,
  6. City VARCHAR(15) NULL,
  7. PostalCode VARCHAR(10) NULL,
  8. Country VARCHAR(15) NULL,
  9. PRIMARY KEY (CustomerID)
  10. );
  11. CREATE TABLE Suppliers (
  12. SupplierID INT AUTO_INCREMENT NOT NULL,
  13. SupplierName VARCHAR(40) NOT NULL,
  14. ContactName VARCHAR(30) NULL,
  15. Address VARCHAR(60) NULL,
  16. City VARCHAR(15) NULL,
  17. PostalCode VARCHAR(10) NULL,
  18. Country VARCHAR(15) NULL,
  19. PRIMARY KEY (SupplierID)
  20. );

Below is a selection from the “Customers” table:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico

And a selection from the “Suppliers” table:

SupplierIDSupplierNameContactNameAddressCityPostalCodeCountry
1Exotic LiquidCharlotte Cooper49 Gilbert St.LondonaEC1 4SDUK
2New Orleans Cajun DelightsShelley BurkeP.O. Box 78934New Orleans70117USA
3Grandma Kelly’s HomesteadRegina Murphy707 Oxford Rd.Ann Arbor48104USA

Example

The following SQL statement copies “Suppliers” into “Customers” (the columns that are not filled with data, will contain NULL):

  1. INSERT INTO Customers (CustomerName, City, Country)
  2. SELECT SupplierName, City, Country FROM Suppliers;

The selection from the “Customers” table will now look like this:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Exotic LiquidnullnullLondonanullUK
5New Orleans Cajun DelightsnullnullNew OrleansnullUSA
6Grandma Kelly’s HomesteadnullnullAnn ArbornullUSA

Constraints

MatrixOne doesn’t support INSERT ... ON DUPLICATE KEY UPDATE statement yet. You can need to perform this operation with UPDATE.