Previous Next JavaScript must be enabled to correctly display this content

## Using SQL command to insert data

The INSERT statement is used to construct a new row and add it to a specified table.

Optional column(s) may be specified after the table name. This list contains the column names for a subset of the table’s columns. The subset must include all the primary key columns. If no columns list is present, the default columns list is the one containing all the columns of the table, in the order, they are specified in the CREATE TABLE statement.

The columns in the columns list correspond one-to-one to the expressions (or DEFAULT keywords) listed after the VALUES clause (an error is raised if the number of expressions/DEFAULTs is not the same as the number of columns). These expressions/DEFAULTs compute the value for their associated column in the new row. An error is raised if an expression returns more than one item. If an expression returns no result, NULL is used as the result of that expression. If instead of an expression, the DEFAULT keyword appears in the VALUES list, the default value of the associated column is used as the value of that column in the new row. The default value is also used for any missing columns when the number of columns in the columns list is less than the total number of columns in the table.

Example 1: Inserting a row into `BaggageInfo` table providing all column values:

    INSERT INTO BaggageInfo VALUES(
    1762392196147,
    "Birgit Naquin",
    "M",
    "165-742-5715",
    "QD1L0T",
    [ {
        "id" : "7903989918469",
        "tagNum" : "17657806240229",
        "routing" : "JFK/MAD",
        "lastActionCode" : "OFFLOAD",
        "lastActionDesc" : "OFFLOAD",
        "lastSeenStation" : "MAD",
        "flightLegs" : [ {
          "flightNo" : "BM495",
          "flightDate" : "2019-03-07T07:00:00Z",
          "fltRouteSrc" : "JFK",
          "fltRouteDest" : "MAD",
          "estimatedArrival" : "2019-03-07T14:00:00Z",
          "actions" : [ {
            "actionAt" : "MAD",
            "actionCode" : "Offload to Carousel at MAD",
            "actionTime" : "2019-03-07T13:54:00Z"
          }, {
            "actionAt" : "JFK",
            "actionCode" : "ONLOAD to MAD",
            "actionTime" : "2019-03-07T07:00:00Z"
          }, {
            "actionAt" : "JFK",
            "actionCode" : "BagTag Scan at JFK",
            "actionTime" : "2019-03-07T06:53:00Z"
          }, {
            "actionAt" : "JFK",
            "actionCode" : "Checkin at JFK",
            "actionTime" : "2019-03-07T05:03:00Z"
          } ]
        } ],
        "lastSeenTimeGmt" : "2019-03-07T13:51:00Z",
        "bagArrivalDate" : "2019-03-07T13:51:00Z"
      } ]
    )

Example 2: Skipping some data while doing an INSERT statement by specifying the DEFAULT clause.

You can skip the data of some columns by specifying "DEFAULT".

    INSERT INTO BaggageInfo VALUES(
    1762397286805,
    "Bonnie Williams",
    DEFAULT,
    DEFAULT,
    "CZ1O5I",
    [ {
        "id" : "79039899129693",
        "tagNum" : "17657806216554",
        "routing" : "SFO/ORD/FRA",
        "lastActionCode" : "OFFLOAD",
        "lastActionDesc" : "OFFLOAD",
        "lastSeenStation" : "FRA",
        "flightLegs" : [ {
          "flightNo" : "BM572",
          "flightDate" : "2019-03-02T05:00:00Z",
          "fltRouteSrc" : "SFO",
          "fltRouteDest" : "ORD",
          "estimatedArrival" : "2019-03-02T09:00:00Z",
          "actions" : [ {
            "actionAt" : "SFO",
            "actionCode" : "ONLOAD to ORD",
            "actionTime" : "2019-03-02T05:24:00Z"
          }, {
            "actionAt" : "SFO",
            "actionCode" : "BagTag Scan at SFO",
            "actionTime" : "2019-03-02T04:52:00Z"
          }, {
            "actionAt" : "SFO",
            "actionCode" : "Checkin at SFO",
            "actionTime" : "2019-03-02T03:28:00Z"
          } ]
        }, {
          "flightNo" : "BM582",
          "flightDate" : "2019-03-02T05:24:00Z",
          "fltRouteSrc" : "ORD",
          "fltRouteDest" : "FRA",
          "estimatedArrival" : "2019-03-02T13:24:00Z",
          "actions" : [ {
            "actionAt" : "FRA",
            "actionCode" : "Offload to Carousel at FRA",
            "actionTime" : "2019-03-02T13:20:00Z"
          }, {
            "actionAt" : "ORD",
            "actionCode" : "ONLOAD to FRA",
            "actionTime" : "2019-03-02T12:54:00Z"
          }, {
            "actionAt" : "ORD",
            "actionCode" : "OFFLOAD from ORD",
            "actionTime" : "2019-03-02T12:30:00Z"
          } ]
        } ],
        "lastSeenTimeGmt" : "2019-03-02T13:18:00Z",
        "bagArrivalDate" : "2019-03-02T13:18:00Z"
      } ]
    )

Example 3: Specifying column names and skipping columns in the insert statement.

If you have data only for some columns of a table, you can specify the name of the columns in the INSERT clause and then specify the corresponding values in the "VALUES" clause.

    INSERT INTO BaggageInfo(ticketNo, fullName,confNo,bagInfo) VALUES(
    1762355349471,
    "Bryant Weber",
    "LI7N1W",
    [ {
        "id" : "79039899149056",
        "tagNum" : "17657806234185",
        "routing" : "MEL/LAX/MIA",
        "lastActionCode" : "OFFLOAD",
        "lastActionDesc" : "OFFLOAD",
        "lastSeenStation" : "MIA",
        "flightLegs" : [ {
          "flightNo" : "BM114",
          "flightDate" : "2019-03-01T12:00:00Z",
          "fltRouteSrc" : "MEL",
          "fltRouteDest" : "LAX",
          "estimatedArrival" : "2019-03-02T02:00:00Z",
          "actions" : [ {
            "actionAt" : "MEL",
            "actionCode" : "ONLOAD to LAX",
            "actionTime" : "2019-03-01T12:20:00Z"
          }, {
            "actionAt" : "MEL",
            "actionCode" : "BagTag Scan at MEL",
            "actionTime" : "2019-03-01T11:52:00Z"
          }, {
            "actionAt" : "MEL",
            "actionCode" : "Checkin at MEL",
            "actionTime" : "2019-03-01T11:43:00Z"
          } ]
        }, {
          "flightNo" : "BM866",
          "flightDate" : "2019-03-01T12:20:00Z",
          "fltRouteSrc" : "LAX",
          "fltRouteDest" : "MIA",
          "estimatedArrival" : "2019-03-02T16:21:00Z",
          "actions" : [ {
            "actionAt" : "MIA",
            "actionCode" : "Offload to Carousel at MIA",
            "actionTime" : "2019-03-02T16:18:00Z"
          }, {
            "actionAt" : "LAX",
            "actionCode" : "ONLOAD to MIA",
            "actionTime" : "2019-03-02T16:12:00Z"
          }, {
            "actionAt" : "LAX",
            "actionCode" : "OFFLOAD from LAX",
            "actionTime" : "2019-03-02T16:02:00Z"
          } ]
        } ],
        "lastSeenTimeGmt" : "2019-03-02T16:09:00Z",
        "bagArrivalDate" : "2019-03-02T16:09:00Z"
      } ]
    )

Example 4: Inserting a row into `stream_acct` table providing all column values:

    INSERT INTO stream_acct VALUES(
    1,
    "AP",
    "2023-10-18",
    {
       "firstName": "Adam",
       "lastName": "Phillips",
       "country": "Germany",
       "contentStreamed": [{
          "showName": "At the Ranch",
          "showId": 26,
          "showtype": "tvseries",
          "genres": ["action", "crime", "spanish"],
          "numSeasons": 4,
          "seriesInfo": [{
             "seasonNum": 1,
             "numEpisodes": 2,
             "episodes": [{
                "episodeID": 20,
                "episodeName": "Season 1 episode 1",
                "lengthMin": 85,
                "minWatched": 85,
                "date": "2022-04-18"
             },
             {
                "episodeID": 30,
                "lengthMin": 60,
                "episodeName": "Season 1 episode 2",
                "minWatched": 60,
                "date": "2022 - 04 - 18 "
             }]
          },
          {
             "seasonNum": 2,
             "numEpisodes": 2,
             "episodes": [{
                "episodeID": 40,
                "episodeName": "Season 2 episode 1",
                "lengthMin": 50,
                "minWatched": 50,
                "date": "2022-04-25"
             },
             {
                "episodeID": 50,
                "episodeName": "Season 2 episode 2",
                "lengthMin": 45,
                "minWatched": 30,
                "date": "2022-04-27"
             }]
          },
          {
             "seasonNum": 3,
             "numEpisodes": 2,
             "episodes": [{
                "episodeID": 60,
                "episodeName": "Season 3 episode 1",
                "lengthMin": 50,
                "minWatched": 50,
                "date": "2022-04-25"
             },
             {
                "episodeID": 70,
                "episodeName": "Season 3 episode 2",
                "lengthMin": 45,
                "minWatched": 30,
                "date": "2022 - 04 - 27 "
             }]
          }]
       },
       {
          "showName": "Bienvenu",
          "showId": 15,
          "showtype": "tvseries",
          "genres": ["comedy", "french"],
          "numSeasons": 2,
          "seriesInfo": [{
             "seasonNum": 1,
             "numEpisodes": 2,
             "episodes": [{
                "episodeID": 20,
                "episodeName": "Bonjour",
                "lengthMin": 45,
                "minWatched": 45,
                "date": "2022-03-07"
             },
             {
                "episodeID": 30,
                "episodeName": "Merci",
                "lengthMin": 42,
                "minWatched": 42,
                "date": "2022-03-08"
             }]
          }]
       }]
    });

Example 5: Insert data into the JSON collection table created for a shopping application.

    INSERT into storeAcct(contactPhone, firstName, lastName, address, cart) values("1817113382", "Adam", "Smith", {"street" : "Tex Ave", "number" : 401, "city" : "Houston", "state" : "TX",   "zip" : 95085}, [{"item" : "handbag", "quantity" : 1, "priceperunit" : 350},{"item" : "Lego", "quantity" : 1, "priceperunit" : 5500}])

In the above example, you insert the shopper's data by supplying the `contactPhone` as the primary key followed by other details of the shoppers. The shopper's details are stored as a single document. Notice that in JSON collection tables, you do not supply a column name for the document itself and you only provide the JSON fields in the document.

You can add another row to the same table with additional fields.

    INSERT into  storeAcct(contactPhone, firstName, lastName, gender, address, notify, cart, wishlist) values("1917113999", "Sharon", "Willard", "F", {"street" : "Maine", "number" : 501, "city" : "San Jose", "state" : "San Francisco",   "zip" : 95095},"yes", [{"item" : "wallet", "quantity" : 2, "priceperunit" : 950},{"item" : "wall art", "quantity" : 1, "priceperunit" : 9500}], [{"item" : "Tshirt", "priceperunit" : 500},{"item" : "Jenga", "priceperunit" : 850}])

In the above statement, you insert the shopper data with additional fields such as `gender`, `notify`, and `wishlist` as compared with the first inserted row. The `wishlist` field is a JSON array that includes the details of the items wishlisted by the shopper.