Beta Draft: 2016-08-16
Table of Contents
This section explains how to use X DevAPI SQL CRUD functions to work with relational tables.
The following example code compares the operations previously shown for collections and how they can be used to work with relational tables using SQL. The simplified X DevAPI syntax is demonstrated using SQL in a Session and showing how it is similar to working with documents.
MySQL Shell JavaScript Code
// Working with Relational Tables
var mysqlx = require('mysqlx').mysqlx;
// Connect to server using a connection URL
var mySession = mysqlx.getSession( {
host: 'localhost', port: 33060,
dbUser: 'mike', dbPassword: 's3cr3t!'} )
var myDb = mySession.getSchema('test');
// Accessing an existing table
var myTable = myDb.getTable('my_table');
// Insert SQL Table data
myTable.insert(['name', 'birthday', 'age']).
values('Sakila', mysqlx.dateValue(2000, 5, 27), 16).execute();
// Find a row in the SQL Table
var myResult = myTable.select(['_id', 'name', 'birthday']).
where('name like :name AND age < :age').
bind('name', 'S%').bind('age', 20).execute();
// Print result
print(myResult.fetchOne());
MySQL Shell Python Code
# Working with Relational Tables
import mysqlx
# Connect to server using a connection URL
mySession = mysqlx.getSession( {
'host': 'localhost', 'port': 33060,
'dbUser': 'mike', 'dbPassword': 's3cr3t!'} )
myDb = mySession.getSchema('test')
# Accessing an existing table
myTable = myDb.getTable('my_table')
# Insert SQL Table data
myTable.insert(['name','birthday','age']) \
.values('Sakila', mysqlx.dateValue(2000, 5, 27), 16).execute()
# Find a row in the SQL Table
myResult = myTable.select(['_id', 'name', 'birthday']) \
.where('name like :name AND age < :age') \
.bind('name', 'S%') \
.bind('age', 20).execute()
# Print result
print myResult.fetchAll()
Node.js JavaScript Code
// Working with Relational Tables
var mysqlx = require('mysqlx');
// Connect to server using a connection URL
mysqlx.getSession({
host: 'localhost', port: 33060,
dbUser: 'mike', dbPassword: 's3cr3t!'
}).then(function (session) {
var db = session.getSchema('test');
// Accessing an existing table
var myTable = db.getTable('my_table');
// Insert SQL Table data
myTable.insert(['name', 'birthday', 'age']).
values('Sakila', mysqlx.dateValue(2000, 5, 27), 16).execute();
// Find a row in the SQL Table
var myResult = myTable.select(['_id', 'name', 'birthday']).
where('name like :name AND age < :age').
bind('name', 'S%').bind('age', 20).execute(function (row) {
console.log(row);
});
C# Code
{
// Working with Relational Tables
// Connect to server using a connection
var db = MySQLX.GetSession("server=localhost;port=33060;user=mike;password=s3cr3t!;")
.GetSchema("test");
// Accessing an existing table
var myTable = db.GetTable("my_table");
// Insert SQL Table data
myTable.Insert("name", "age")
.Values("Sakila", "19").Execute();
// Find a row in the SQL Table
var myResult = myTable.Select("_id, name, age")
.Where("name like :name AND age < :age")
.Bind(new { name = "S%", age = 20 }).Execute();
// Print result
PrintResult(myResult.FetchAll());
}
Java Code
// Working with Relational Tables
import com.mysql.cj.api.x.*;
import com.mysql.cj.x.MysqlxSessionFactory;
// Connect to server using a connection URL
String url = "mysqlx://localhost:33060/test?user=mike&password=s3cr3t!"
XSession mySession = new MysqlxSessionFactory().getSession(url);
Schema myDb = mySession.getSchema("test");
// Accessing an existing table
Table myTable = db.getTable("my_table");
// Insert SQL Table data
myTable.insert("name", "birthday").values("Sakila", "2000-05-27").execute();
// Find a row in the SQL Table
RowResult myResult = myTable.select("_id, name, birthday")
.where("name like :name AND age < :age")
.bind("name", "S*").bind("age", 20).execute();
// Print result
System.out.println(myResult.fetchAll());
C++ Code
// Working with Relational Tables
#include <mysqlx.h>
// Connect to server using a connection URL
XSession mySession(33060, "mike", "s3cr3t!");
Schema myDb = mySession.getSchema("test");
// Accessing an existing table
Table myTable = myDb.getTable("my_table");
// Insert SQL Table data
myTable.insert("name", "birthday", "age")
.values("Sakila", "2000-5-27", 16).execute();
// Find a row in the SQL Table
RowResult myResult = myTable.select("_id", "name", "birthday")
.where("name like :name AND age < :age")
.bind("name", "S%").bind("age", 20).execute();
// Print result
Row row = myResult.fetchOne();
cout << " _id: " << row[0] << endl;
cout << " name: " << row[1] << endl;
cout << "birthday: " << row[2] << endl;
The following SQL CRUD functions are available in X DevAPI.
The Table.insert() function is used to store
data in a relational table in the database. It is executed by
the execute() function.
The following example shows how to use the Table.insert() function. The example assumes that the test schema exists and is assigned to the variable db, and that an empty table called my_table exists.
MySQL Shell JavaScript Code
// Accessing an existing table
var myTable = db.getTable('my_table');
// Insert a row of data.
myTable.insert(['id', 'name']).
values(1, 'Mike').
values(2, 'Jack').
execute();
MySQL Shell Python Code
# Accessing an existing table
myTable = db.getTable('my_table')
# Insert a row of data.
myTable.insert(['id', 'name']).values(1, 'Mike').values(2, 'Jack').execute()
Node.js JavaScript Code
// Accessing an existing table
var myTable = db.getTable('my_table');
// Insert a row of data.
myTable.insert(['id', 'name']).
values(1, 'Mike').
values(2, 'Jack').
execute();
C# Code
{
// Assumptions: test schema assigned to db, empty my_table table exists
// Accessing an existing table
var myTable = db.GetTable("my_table");
// Insert a row of data.
myTable.Insert("id", "name")
.Values(1, "Mike")
.Values(2, "Jack")
.Execute();
}
Java Code
// Accessing an existing table
Table myTable = db.getTable("my_table");
// Insert a row of data.
myTable.insert("id", "name")
.values(1, "Mike")
.values(2, "Jack")
.execute();
C++ Code
// Accessing an existing table
var myTable = db.getTable("my_table");
// Insert a row of data.
myTable.insert("id", "name")
.values(1, "Mike")
.values(2, "Jack")
.execute();
Table.select() and
collection.find() use different methods for
sorting results. Table.select() follows the
SQL language naming and calls the sort method
orderBy().
Collection.find() does not. Use the method
sort() to sort the results returned by
Collection.find(). Proximity with the SQL
standard is considered more important than API uniformity here.