Previous Next JavaScript must be enabled to correctly display this content

## SQLAdvancedExamples Script

The `SQLAdvancedExamples` script creates the following table:

    CREATE TABLE Persons (
      id integer,
      firstname string,
      lastname string,
      age integer,
      income integer,
      lastLogin timestamp(4),
      address record(street string,
                     city string,
                     state string,
                     phones array(record(type enum(work, home),
                                         areacode integer,
                                         number integer
                                       )
                                )
                    ),
      connections array(integer),
      expenses map(integer),
      primary key (id)
    );

The script also imports the following table rows:

    {
      "id":1,
      "firstname":"David",
      "lastname":"Morrison",
      "age":25,
      "income":100000,
      "lastLogin" : "2016-10-29T18:43:59.8319",
      "address":{"street":"150 Route 2",
                 "city":"Antioch",
                 "state":"TN",
                 "zipcode" : 37013,
                 "phones":[{"type":"home", "areacode":423,
                            "number":8634379}]
                },
      "connections":[2, 3],
      "expenses":{"food":1000, "gas":180}
    }

    {
      "id":2,
      "firstname":"John",
      "lastname":"Anderson",
      "age":35,
      "income":100000,
      "lastLogin" : "2016-11-28T13:01:11.2088",
      "address":{"street":"187 Hill Street",
                 "city":"Beloit",
                 "state":"WI",
                 "zipcode" : 53511,
                 "phones":[{"type":"home", "areacode":339,
                 "number":1684972}]
                },
      "connections":[1, 3],
      "expenses":{"books":100, "food":1700, "travel":2100}
    }

    {
      "id":3,
      "firstname":"John",
      "lastname":"Morgan",
      "age":38,
      "income":100000000,
      "lastLogin" : "2016-11-29T08:21:35.4971",
      "address":{"street":"187 Aspen Drive",
                 "city":"Middleburg",
                 "state":"FL",
                 "phones":[{"type":"work", "areacode":305,
                            "number":1234079},
                          {"type":"home", "areacode":305,
                           "number":2066401}
                         ]
                },
      "connections":[1, 4, 2],
      "expenses":{"food":2000, "travel":700, "gas":10}
    }

    {
      "id":4,
      "firstname":"Peter",
      "lastname":"Smith",
      "age":38,
      "income":80000,
      "lastLogin" : "2016-10-19T09:18:05.5555",
      "address":{"street":"364 Mulberry Street",
                 "city":"Leominster",
                 "state":"MA",
                 "phones":[{"type":"work", "areacode":339,
                            "number":4120211},
                          {"type":"work", "areacode":339,
                           "number":8694021},
                          {"type":"home", "areacode":339,
                           "number":1205678},
                          {"type":"home", "areacode":305,
                           "number":8064321}
                         ]
                },
      "connections":[3, 5, 1, 2],
      "expenses":{"food":6000, "books":240, "clothes":2000, "shoes":1200}
    }

    {
      "id":5,
      "firstname":"Dana",
      "lastname":"Scully",
      "age":47,
      "income":400000,
      "lastLogin" : "2016-11-08T09:16:46.3929",
      "address":{"street":"427 Linden Avenue",
                 "city":"Monroe Township",
                 "state":"NJ",
                 "phones":[{"type":"work", "areacode":201,
                            "number":3213267},
                          {"type":"work", "areacode":201,
                           "number":8765421},
                          {"type":"home", "areacode":339,
                           "number":3414578}
                         ]
               },
      "connections":[2, 4, 1, 3],
      "expenses":{"food":900, "shoes":1000, "clothes":1500}
    }

You run the SQLAdvancedExamples script using the load command:

    > cd <installdir>/examples/sql
    > java -jar <KVHOME>/lib/sql.jar -helper-hosts <host>:<port> \
    -store <storename> load \
    -file <KVHOME>/examples/sql/SQLAdvancedExamples.cli

Note:

The Persons table schema models people that can be connected to other people in the table. All connections are stored in the "connections" column, which consists of an array of integers. Each integer is an ID of a person with whom the subject is connected. The entries in the "connections" array are sorted in descending order, indicating the strength of the connection. For example, looking at the record for person 3, we see that John Morgan has these connections: [1, 4, 2]. The order of the array elements specifies that John is most strongly connected with person 1, less connected with person 4, and least connected with person 2.

Records in the Persons table also include an "expenses" column, declared as an integer map. For each person, the map stores key-value pairs of string item types and integers representing money spent on the item. For example, one record has these expenses: {"food":900, "shoes":1000, "clothes":1500}, other records have different items. One benefit of modelling expenses as a map type is to facilitate the categories being different for each person. Later, we may want to add or delete categories dynamically, without changing the table schema, which maps readily support. An item to note about this map is that it is an integer map always contains key-value pairs, and keys are always strings.