AMI SQL: Schema and Data Modification¶
This page covers AMI SQL commands for modifying data in, and managing the schema of, AMI's in-memory tables.
Data Modification¶
INSERT...VALUES¶
Overview
This clause enables the insertion of raw values into an existing table. Many rows may be inserted in a single call. Each values_expr represents a single row of data to be inserted. To insert multiple rows, append additional values_expr.
In order to specify columns in which to insert values, provide an ordered list of column names after the table name. The same number of values must be supplied in the values_expr and in the same order. All other columns will default to null.
If a list of column names is not provided, all columns will be populated. Each values_expr should contain one value for each column in the target table. Values should be in the same natural order as the columns in the target table.
The forloop_expr allows for the insertion of values_expr to be repeated for each cycle of the for loop. The values can reference the for loop's declared variable. Multiple for loops can be nested inside one another.
Syntax
tblname: String name of the table to insert into.
colN: String name of the column to contain the Nth value from values_expr.
Examples
INSERT...FROM¶
Overview
This clause causes the insertion of the result of a projecting-clause (select_clause, analyze_clause, prepare_clause, execute_clause) into an existing table. Each row from the result set of the select_clause will result in the insertion of a new record into the target table. Multiple tables may be inserted in a single call using the UNION clause.
To specify columns in which to insert values, provide an ordered list of column names after the table name. The result set from the preceding select_clause must have the same number of columns. The columns should be in the same order. All other columns will default to null.
If a list of column names is not provided, all columns will be populated. Each values_expr should contain one value for each column in the target table. Values should be in the same natural order as the columns in the target table.
If the BYNAME keyword is included, then source and target columns are aligned based on column name. Column Names in the target table that do not existing in the query clause will be populated with null
Syntax
tblname: String name of the table to insert into.
colN: String name of the column to contain the Nth value from results of select clause*.
Example
SYNC¶
Overview
This clause causes the target table to reflect the result of a projecting-clause (select_clause, analyze_clause, prepare_clause, execute_clause), meaning that rows will be inserted, deleted and updated accordingly on the target table such that the target table will properly mirror the result of the projecting-clause.
In other words, the two examples will result in the same final state for the my_target table, but from a trigger stand point they are significantly different:
Note
SYNC is only supported for same-Center tables.
In the first example, the truncate instruction will cause a delete trigger to be fired for every row originally in the my_target table and then the subsequent insert instruction will cause an insert trigger to be fired for every row inserted into the my_target table.
The following rules apply for how the target table is mutated:
- Records in the projecting-clause that do not have a matching record in the target table are inserted into the target table
- Records in the target table that do not have a matching record in the projecting-clause are deleted from the target table
- Records in the target table that have a "perfect" matching record in the projecting-clause remain untouched (no triggers fired)
- Records in the target table that have a "non-perfect" matching record in the projecting-clause are updated
An ON clause can be used to tell the sync tool how to determine if a source and target row are considered equivalent. When an equivalent row between the target table and projecting-clause is identified, this will result in an update. Rows that don't have a match will result in a delete and/or insert.
Matching Rules:
- If all cells between two rows are equivalent, that is considered a "Perfect match"
- If an
ONclause is specified: Two row are considered a "non-perfect" match if all of the cells specified in the ON clause are a match, but other cells are not (if anONclause is not specified then there can not be non-perfect matches)
In order to specify columns in which to sync values, provide an ordered list of column names after the table name (note that only these columns will be considered for the perfect-matching criteria). The result set from the preceding select_clause must have the same number of columns. The columns should be in the same order. All other columns will default to null for inserts and will be left unaltered for updates.
If a list of column names is not provided, all columns will be populated. Each values_expr should contain one value for each column in the target table. Values should be in the same natural order as the columns in the target table.
Syntax
tblname: String name of the table to insert into.
colN: String name of the column to contain the Nth value from results of select clause.
onColN: String name of the column to be used in determining if two records are equivalent.
Examples
DELETE FROM¶
Overview
This clause enables the deletion of specific rows or all rows from a table and also calls the onDelete triggers if applicable.
When it comes to deleting all rows from the table, this is similar to using truncate clause but consumes more memory.
To delete only specific rows, provide a WHERE clause which must evaluate to a Boolean value where true indicates that the row should be deleted. Otherwise, to delete all rows, do not provide a WHERE clause.
Specify the maximum number of rows to delete using the LIMIT clause (maxdelete). Additionally, you may provide a number of rows to skip when deleting (skipdelete). Note the limit is applied after the where clause, meaning that the skipdelete indicates how many records to skip deleting that would otherwise be deleted based on the where clause.
The JOIN .. ON clause allows you to use the values from another table to determine which rows to delete from the target table. The JOIN operates similar to the SELECT's JOIN clause. Note that LEFT JOIN and LEFT ONLY are also supported. Keep in mind that you can only delete rows from the left table.
Syntax
tblname: String name of the table to delete from.
Examples
TRUNCATE¶
Overview
This command deletes all rows from a table. You can specify multiple tables, separated by commas.
Syntax
Examples
UPDATE¶
Overview
This clause can be used to update specific rows or all rows in a table. Following the SET statement, provide an assignment (target=expr) clause for each column to be updated, where target is the name of an existing column.
To update only specific rows, provide a WHERE clause which evaluates to a Boolean value, where true indicates that the row should be updated. Otherwise, to update all rows, do not provide a WHERE clause.
You can specify the maximum number of rows to update using the LIMIT clause (maxupdate). Additionally, you may provide a number of rows to skip when updating (skipupdate). Note the limit is applied after the where clause, meaning that the skipupdate indicates how many records to skip updating that would otherwise be updated based on the where clause.
The JOIN .. ON clause allows you to use the values from another table to update values in the target table. The JOIN operates similar to the SELECT's JOIN clause. Note that LEFT JOIN and LEFT ONLY are also supported. Keep in mind that you can only update values on the left table.
Syntax
tblname: String name of the table to update.
colN: String name of the column to assign value from expression.
Schema Modification¶
CREATE TABLE¶
Overview
This command is used to create a new, empty (no rows) table with a specified set of columns (names and types). If another table already exists with the tblname, the existing table will be implicitly dropped and replaced with the newly created table.
At least one column must be specified, and each column must have a unique name.
Syntax
tblname: String name of the table to create.
colN: String name of the Nth column to create.
Example
CREATE TABLE AS¶
Overview
This command is used to create a new table which has the same schema definition as the result set of a select_clause. If another table already exists with the tblname, the existing table will be implicitly dropped and replaced with the newly created table.
All rows from the select_clause's result set will be inserted into the table.
Syntax
tblname: String name of the table to create.
Example
ALTER TABLE¶
Overview
This command alters the schema (column names and types) of an existing table. There are 4 alter commands:
-
ADDto add columns -
RENAME ... TOto rename columns -
DROPto delete columns -
MODIFYto change column type - A best effort cast will be applied. If no appropriate casting exists, the values will become null.
When multiple alterations are supplied, they are evaluated left to right. New columns will be added to the end (right-most) of the table.
Syntax
tblname: String name of the table to alter.
Examples
By default, values in newly added columns will be null. To specify a non-null default value, use the equal operator followed by any additional logic.
Examples
RENAME TABLE¶
Overview
This command renames an existing table. If another table already exists with the new name, the existing table will be implicitly dropped and replaced with the renamed table.
Syntax
tblname: String name of the table to rename.
newname: String name of the new name for the table.
Example
DROP TABLE¶
Overview
This command drops an existing table, along with its schema and all rows. You can specify multiple tables, separated by commas.
Syntax
tblname: String name of the table to drop.
Example