GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Documentation
  • Privacy Statement
  • License Agreement
  • Contact Us

This view

  • Original page
  • New address

GeezerVison is showing docs.voltactivedata.com — 4.6. Modifying the Schema

  • Volt Active Data
  • Developer Hub
  • Books
    • VoltDB Tutorial
    • Using VoltDB
    • VoltDB Guide to Performance and Customization
    • VoltDB Administrator's Guide
    • Volt Kubernetes Administrator's Guide
    • VoltDB Quick Reference
  • VoltDB V16
    • VoltDB V16
    • VoltDB V15.3 LTS
    • VoltDB V14.3 LTS
    • VoltDB V13.3 LTS
    • VoltDB V12.3 LTS
    • VoltDB V11.4
    • VoltDB V10.2
    • VoltDB V9.3
4.6. Modifying the Schema

Using VoltDB

  • About This Book
  • 1. Overview▶
    • 1.1. What is VoltDB?
    • 1.2. Who Should Use VoltDB
    • 1.3. How VoltDB Works
      • 1.3.1. Partitioning
      • 1.3.2. Serialized (Single-Threaded) Processing
      • 1.3.3. Partitioned vs. Replicated Tables
      • 1.3.4. Ease of Scaling to Meet Application Needs
    • 1.4. Working with VoltDB Effectively
  • 2. Installing VoltDB▶
    • 2.1. Operating System and Software Requirements
    • 2.2. Installing VoltDB
      • 2.2.1. Upgrading From Older Versions
    • 2.3. Setting Up Your Environment
    • 2.4. What is Included in the VoltDB Distribution
  • 3. Starting the Database▶
    • 3.1. Initializing and Starting a VoltDB Database
    • 3.2. Initializing and Starting a VoltDB Database on a Cluster
    • 3.3. Stopping a VoltDB Database
    • 3.4. Saving the Data
    • 3.5. Restarting a VoltDB Database
    • 3.6. Updating Nodes on the Cluster
    • 3.7. Defining the Cluster Configuration
      • 3.7.1. Determining How Many Sites per Host
      • 3.7.2. Configuring Paths for Runtime Features
      • 3.7.3. Verifying your Hardware Configuration
  • 4. Designing the Database Schema▶
    • 4.1. How to Enter DDL Statements
    • 4.2. Creating Tables and Primary Keys
    • 4.3. Analyzing Data Volume and Workload
    • 4.4. Partitioning Database Tables
      • 4.4.1. Choosing a Column on which to Partition Table Rows
      • 4.4.2. Specifying Partitioned Tables
      • 4.4.3. Design Rules for Partitioning Tables
    • 4.5. Replicating Database Tables
      • 4.5.1. Choosing Replicated Tables
      • 4.5.2. Specifying Replicated Tables
    • 4.6. Modifying the Schema
      • 4.6.1. Effects of Schema Changes on Data and Clients
      • 4.6.2. Viewing the Schema
      • 4.6.3. Modifying Tables
      • 4.6.4. Adding and Dropping Indexes
      • 4.6.5. Modifying Partitioning for Tables and Stored Procedures
  • 5. Designing Stored Procedures to Access the Database▶
    • 5.1. How Stored Procedures Work
      • 5.1.1. VoltDB Stored Procedures are Transactional
      • 5.1.2. VoltDB Stored Procedures are Deterministic
    • 5.2. The Anatomy of a VoltDB Stored Procedure
      • 5.2.1. The Structure of the Stored Procedure
      • 5.2.2. Passing Arguments to a Stored Procedure
      • 5.2.3. Creating and Executing SQL Queries in Stored Procedures
      • 5.2.4. Interpreting the Results of SQL Queries
      • 5.2.5. Returning Results from a Stored Procedure
      • 5.2.6. Rolling Back a Transaction
    • 5.3. Installing Stored Procedures into the Database
      • 5.3.1. Compiling, Packaging, and Loading Stored Procedures
      • 5.3.2. Declaring Stored Procedures in the Schema
      • 5.3.3. Partitioning Stored Procedures in the Schema
  • 6. Designing VoltDB Client Applications▶
    • 6.1. Connecting to the VoltDB Database
      • 6.1.1. How Connections To The Cluster Work
      • 6.1.2. Specifying Multiple Servers
    • 6.2. Invoking Stored Procedures
    • 6.3. Invoking Stored Procedures Asynchronously
    • 6.4. Defining Options on a Per-Call Basis
    • 6.5. Closing the Connection
    • 6.6. Handling Errors
      • 6.6.1. Interpreting Execution Errors
      • 6.6.2. Handling Timeouts
      • 6.6.3. Writing Handlers to Interpret Other Errors
    • 6.7. Compiling and Running Client Applications
      • 6.7.1. Starting the Client Application
      • 6.7.2. Running Clients from Outside the Cluster
  • 7. Simplifying Application Development▶
    • 7.1. Using Default Procedures
    • 7.2. Shortcut for Defining Simple Stored Procedures
    • 7.3. Verifying Expected Query Results
    • 7.4. Scheduling Stored Procedures as Tasks
    • 7.5. Directed Procedures: Distributing Transactions to Every Partition
  • 8. Using VoltDB with Other Programming Languages▶
    • 8.1. C++ Client Interface
      • 8.1.1. Writing VoltDB Client Applications in C++
      • 8.1.2. Creating a Connection to the Database Cluster
      • 8.1.3. Invoking Stored Procedures
      • 8.1.4. Invoking Stored Procedures Asynchronously
      • 8.1.5. Interpreting the Results
    • 8.2. JSON HTTP Interface
      • 8.2.1. How the JSON Interface Works
      • 8.2.2. Using the JSON Interface from Client Applications
      • 8.2.3. How Parameters Are Interpreted
      • 8.2.4. Interpreting the JSON Results
      • 8.2.5. Error Handling using the JSON Interface
      • 8.2.6. Managing Client Sessions
    • 8.3. JDBC Interface
      • 8.3.1. Using JDBC to Connect to a VoltDB Database
      • 8.3.2. Using JDBC to Query a VoltDB Database
    • 8.4. Python Client Interface
      • 8.4.1. Creating a Connection to the Database Cluster
      • 8.4.2. Invoking Stored Procedures
      • 8.4.3. Invoking Stored Procedures Asynchronously
      • 8.4.4. Interpreting the Results
      • 8.4.5. Closing the Connections to the Database Cluster
  • 9. Integrating Volt Active Data with AI Agents and Chatbots▶
    • 9.1. Volt Agent Skills
      • 9.1.1. Installing Volt Skills
      • 9.1.2. Using Volt Skills Effectively
    • 9.2. The VoltDB MCP Server
      • 9.2.1. Installing the VoltDB MCP Server
      • 9.2.2. Configuring the MCP Server
      • 9.2.3. How to Use the VoltDB MCP Server
  • 10. Using VoltDB in a Cluster▶
    • 10.1. Starting a Database Cluster
    • 10.2. Defining the Cluster Configuration
    • 10.3. Elastic Scaling to Resize the Cluster
      • 10.3.1. Adding Nodes with Elastic Scaling
      • 10.3.2. Removing Nodes with Elastic Scaling
      • 10.3.3. Configuring How VoltDB Rebalances Nodes During Elastic Scaling
  • 11. Availability▶
    • 11.1. How K-Safety Works
    • 11.2. Enabling K-Safety
      • 11.2.1. What Happens When You Enable K-Safety
      • 11.2.2. Calculating the Appropriate Number of Nodes for K-Safety
    • 11.3. Recovering from System Failures
      • 11.3.1. What Happens When a Node Rejoins the Cluster
      • 11.3.2. Where and When Recovery May Fail
    • 11.4. Avoiding Network Partitions
      • 11.4.1. K-Safety and Network Partitions
      • 11.4.2. Network Fault Protection
  • 12. Active(N) Database Replication▶
    • 12.1. How Database Replication Works
      • 12.1.1. Starting Database Replication
      • 12.1.2. Database Replication, Availability, and Disaster Recovery
      • 12.1.3. Database Replication and Completeness
    • 12.2. Using Active(N) Cross Datacenter Replication
      • 12.2.1. Designing Your Schema for Active Replication
      • 12.2.2. Configuring the Database Clusters
      • 12.2.3. Starting the Database Clusters
      • 12.2.4. Loading a Matching Schema and Starting Replication
      • 12.2.5. Stopping Replication
      • 12.2.6. Example XDCR Configurations
      • 12.2.7. Understanding Conflict Resolution
    • 12.3. Updating the Schema During Replication
      • 12.3.1. Safely Updating the Schema While the Clusters are Paused
      • 12.3.2. Adding and Removing Tables Without Pausing
      • 12.3.3. Using Dynamic Schema Change to Add, Remove, and Modify Table Columns Without Pausing
    • 12.4. Monitoring Database Replication
    • 12.5. Tuning Active(N) to Meet Your Application Needs
      • 12.5.1. The XDCR Workflow
      • 12.5.2. Detecting and Correcting Bottlenecks in the XDCR Workflow
  • 13. Security▶
    • 13.1. How Security Works in VoltDB
    • 13.2. Enabling Authentication and Authorization
    • 13.3. Defining Users and Roles
    • 13.4. Assigning Access to Stored Procedures
    • 13.5. Assigning Access by Function (System Procedures, SQL Queries, and Default Procedures)
    • 13.6. Using Built-in Roles
    • 13.7. Encrypting VoltDB Communication Using TLS/SSL
      • 13.7.1. Creating the TLS/SSL Certificates
      • 13.7.2. Using Certificate Revocation Lists (CRLs)
      • 13.7.3. Configuring TLS/SSL on the VoltDB Server
      • 13.7.4. Configuring Mutual TLS/SSL on the Server and Clients
      • 13.7.5. Using the VoltDB Command Line Utilities with TLS/SSL
      • 13.7.6. Implementing TLS/SSL in the Java Client Applications
      • 13.7.7. Configuring Database Replication (XDCR) With TLS/SSL
      • 13.7.8. Updating TLS/SSL Certificates and Certificate Revocation Lists (CRLs)
    • 13.8. Integrating LDAP Security with VoltDB
      • 13.8.1. Configuring LDAP Security in VoltDB
      • 13.8.2. Configuring VoltDB Security in LDAP
    • 13.9. Integrating Kerberos Security with VoltDB
      • 13.9.1. Installing and Configuring Kerberos
      • 13.9.2. Configuring Java Security
      • 13.9.3. Configuring the VoltDB Servers and Clients
      • 13.9.4. Accessing the Database from the Command Line and the Web
  • 14. Saving & Restoring a VoltDB Database▶
    • 14.1. Performing a Manual Save and Restore of a VoltDB Cluster
      • 14.1.1. How to Save the Contents of a VoltDB Database
      • 14.1.2. How to Restore the Contents of a VoltDB Database Manually
      • 14.1.3. Changing the Cluster Configuration Using Save and Restore
    • 14.2. Scheduling Automated Snapshots
    • 14.3. Tuning the Snapshot Process
    • 14.4. Managing Snapshot Files
    • 14.5. Special Notes Concerning Save and Restore
  • 15. Command Logging and Recovery▶
    • 15.1. How Command Logging Works
    • 15.2. Controlling Command Logging
    • 15.3. Configuring Command Logging for Optimal Performance
      • 15.3.1. Log Size
      • 15.3.2. Log Frequency
      • 15.3.3. Synchronous vs. Asynchronous Logging
      • 15.3.4. Hardware Considerations
  • 16. Streaming Data: Import, Export, and Migration▶
    • 16.1. How Data Streaming Works in VoltDB
      • 16.1.1. Understanding Import
      • 16.1.2. Understanding Export
      • 16.1.3. Understanding Migration
    • 16.2. The Business Case for Streaming Data
      • 16.2.1. Extract, Transform, Load (ETL)
      • 16.2.2. Change Data Capture
      • 16.2.3. Streaming Data Validation
      • 16.2.4. Caching
      • 16.2.5. Archiving
    • 16.3. VoltDB Export Connectors
      • 16.3.1. How Export Works
      • 16.3.2. The File Export Connector
      • 16.3.3. The HTTP Export Connector
      • 16.3.4. The JDBC Export Connector
      • 16.3.5. The Kafka Export Connector
      • 16.3.6. The Elasticsearch Export Connector
    • 16.4. VoltDB Import Connectors
      • 16.4.1. Bulk Loading Data Using VoltDB Standalone Utilities
      • 16.4.2. Streaming Import Using Built-in Import Features
      • 16.4.3. The Kafka Importer
    • 16.5. VoltDB Import Formatters
  • A. Supported SQL DDL Statements▶
    • ALTER STREAM
    • ALTER TABLE
    • ALTER TASK
    • ALTER VIEW
    • CREATE INDEX
    • CREATE PROCEDURE AS
    • CREATE PROCEDURE FROM CLASS
    • CREATE ROLE
    • CREATE STREAM
    • CREATE TABLE
    • CREATE TASK
    • CREATE VIEW
    • DR TABLE
    • DROP INDEX
    • DROP PROCEDURE
    • DROP ROLE
    • DROP STREAM
    • DROP TABLE
    • DROP TASK
    • DROP VIEW
    • PARTITION TABLE
  • B. Supported SQL Statements▶
    • DELETE
    • INSERT
    • MIGRATE
    • SELECT
    • TRUNCATE TABLE
    • UPDATE
    • UPSERT
  • C. SQL Functions▶
    • ABS()
    • APPROX_COUNT_DISTINCT()
    • AREA()
    • ARRAY_ELEMENT()
    • ARRAY_LENGTH()
    • ASTEXT()
    • AVG()
    • BIN()
    • BIT_SHIFT_LEFT()
    • BIT_SHIFT_RIGHT()
    • BITAND()
    • BITNOT()
    • BITOR()
    • BITXOR()
    • CAST()
    • CEILING()
    • CENTROID()
    • CHAR()
    • CHAR_LENGTH()
    • COALESCE()
    • CONCAT()
    • CONTAINS()
    • COS()
    • COT()
    • COUNT()
    • CSC()
    • CURRENT_DATE()
    • CURRENT_TIMESTAMP()
    • DATEADD()
    • DATEDIFF()
    • DAY(), DAYOFMONTH()
    • DAYOFWEEK()
    • DAYOFYEAR()
    • DECODE()
    • DEGREES()
    • DISTANCE()
    • DWITHIN()
    • EXP()
    • EXTRACT()
    • FIELD()
    • FLOOR()
    • FORMAT()
    • FORMAT_CURRENCY()
    • FORMAT_TIMESTAMP()
    • FROM_UNIXTIME()
    • HEX()
    • HOUR()
    • INET6_ATON()
    • INET6_NTOA()
    • INET_ATON()
    • INET_NTOA()
    • ISINVALIDREASON()
    • ISVALID()
    • IS_VALID_TIMESTAMP()
    • LATITUDE()
    • LEFT()
    • LN(), LOG()
    • LOG10()
    • LONGITUDE()
    • LOWER()
    • MAKEVALIDPOLYGON()
    • MASK_CCN()
    • MASK_EMAIL()
    • MASK_INNER()
    • MASK_OUTER()
    • MASK_PAN()
    • MASK_PAN_RELAXED()
    • MASK_PARTIAL()
    • MASK_TEXT()
    • MAX()
    • MAX_VALID_TIMESTAMP()
    • MIGRATING()
    • MIN()
    • MIN_VALID_TIMESTAMP()
    • MINUTE()
    • MOD()
    • MONTH()
    • NOW()
    • NUMINTERIORRINGS()
    • NUMPOINTS()
    • OCTET_LENGTH()
    • OVERLAY()
    • PI()
    • POINTFROMTEXT()
    • POLYGONFROMTEXT()
    • POSITION()
    • POWER()
    • QUARTER()
    • RADIANS()
    • REGEXP_POSITION()
    • REPEAT()
    • REPLACE()
    • RIGHT()
    • ROUND()
    • SEC()
    • SECOND()
    • SET_FIELD()
    • SIGN()
    • SIN()
    • SINCE_EPOCH()
    • SPACE()
    • SQRT()
    • STR()
    • SUBSTRING()
    • SUM()
    • TAN()
    • TIME_WINDOW()
    • TO_TIMESTAMP()
    • TRIM()
    • TRUNCATE()
    • UPPER()
    • VALIDPOLYGONFROMTEXT()
    • WEEK(), WEEKOFYEAR()
    • WEEKDAY()
    • YEAR()
  • D. VoltDB CLI Commands▶
    • csvloader
    • jdbcloader
    • kafkaloader
    • sqlcmd
    • voltadmin
    • voltdb
  • E. YAML Configuration Properties▶
    • E.1. Understanding YAML Syntax
      • E.1.1. YAML Files
      • E.1.2. Dot Notation
    • E.2. YAML Properties for Configuring Volt Databases on Bare Metal
  • F. VoltDB Datatype Compatibility▶
    • F.1. Java and VoltDB Datatype Compatibility
  • G. System Procedures▶
    • @AdHoc
    • @AdjustLogging
    • @Explain
    • @ExplainProc
    • @ExplainView
    • @GetPartitionKeys
    • @GetYamlConfiguration
    • @Metrics
    • @Note
    • @Pause
    • @Ping
    • @Quiesce
    • @Resume
    • @SetYamlConfiguration
    • @Shutdown
    • @SnapshotDelete
    • @SnapshotRestore
    • @SnapshotSave
    • @SnapshotScan
    • @Statistics
    • @StopNode
    • @SwapTables
    • @SystemCatalog
    • @SystemInformation
    • @UpdateClasses
    • @UpdateLogging

4.6. Modifying the Schema

You can use DDL to add, modify, or remove schema objects as the database is running. For a list of all valid DDL you can use, see Appendix A, Supported SQL DDL Statements. You can do the following types of schema changes:

  • Modifying Tables — You can add, modify (alter), and remove (drop) table columns. You can also add and drop table constraints. Finally, you can drop entire tables.

  • Adding and Dropping Indexes — You can add and remove (drop) named indexes.

  • Modifying Partitioning for Tables and Stored Procedures — You can un-partition stored procedures and re-partition stored procedures on a different column, For tables you can change a table between partitioned and replicated, and repartition a table on a different column,

  • Modify roles and users — To learn about modifying roles and users, see Chapter 13, Security.

VoltDB safely handles sqlcmd DDL entered by different users on different nodes of the cluster because it manages sqlcmd commands as transactions, just like stored procedures. To demonstrate the DDL statements to modify the schema, the following sections use a new table, Airport, added to the fight reservation as shown below:

CREATE TABLE Airport (
   AirportID integer NOT NULL,
   Name varchar(15) NOT NULL,
   City varchar(25),
   Country varchar(15),
   PRIMARY KEY (AirportID)
);

4.6.1. Effects of Schema Changes on Data and Clients

You can make many schema changes on empty tables with few restrictions. However, be aware that if a table has data, some schema changes are not allowed and other schema changes may modify or even remove data. When working with test data in your database, you can use TRUNCATE TABLE to empty the data from a table you are working on. Note that all DDL examples in this chapter assume the tables are empty.

We can think of the effects of schema changes on data in three severity levels:

  • Schema change completes without damage to data

  • Schema change fails to complete to avoid damage to data

  • Schema change destroys data

VoltDB error messages and the documentation can help you avoid schema change attempts that fail to complete. For example, you cannot drop a table that has referencing procedures or views.

Obviously you need to be most aware of which schema changes cause data to be destroyed. In particular, removing objects from the schema will also remove the data they contain. Note that schema objects cannot be renamed with DDL, but objects can be replaced by performing a DROP and then ADD. However, it is important to realize that as a result of a DROP operation, such as DROP TABLE, the data associated with that table will be deleted before the new definition is added.

Plan and coordinate changes with client development. Stored procedures and ad hoc queries provide an API that clients use to access the database correctly. Changes to the schema can break the stored procedure calls client applications have developed, so use well-planned schedules to communicate database schema changes to others. Client applications depend on many schema definition features including (but not limited to):

  • Table names

  • Column names

  • Column data types

  • Primary key definitions

  • Table partitions

  • Stored procedure names

  • Stored procedure partitioning

Plan and test carefully before making schema changes to a production database. Be aware that clients may experience connection issues during schema changes, especially for changes that take longer to complete, such as view or index changes.

Schema changes not only affect data, but the existence of data in the database affects the time it takes to process schema changes. For example, when there are large amounts of data, some DDL statements can block processing, resulting in a noticeable delay for other pending transactions. Examples include adding indexes, creating new table columns, and modifying views.

4.6.2. Viewing the Schema

The Volt Management Center provides a web browser view of database information, including the DDL schema source. Use a web browser to view the Volt Management Center (VMC) service on port 8080 (for example, http://vmcsvc:8080).

You can also use the sqlcmd show directive to see a list of the current database tables and all procedures. For additional details about the schema, execute the @SystemCatalog system procedure. Use any of the following arguments to @SystemCatalog to obtain details about a component of the database schema:

  • TABLES

  • COLUMNS

  • INDEXINFO

  • PRIMARYKEYS

  • PROCEDURES

  • PROCEDURECOLUMNS

For example:

$ sqlcmd
1> SHOW TABLES;
2> SHOW PROCEDURES;
3> EXEC @SystemCatalog COLUMNS;

4.6.3. Modifying Tables

After creating a table in a database with CREATE TABLE, you can use ALTER TABLE to make the following types of table changes:

  • Altering a Table Column's Data Definition

  • Adding and Dropping Table Columns

  • Adding and Dropping Table Constraints

To drop an entire table, use the DROP TABLE DDL statement.

4.6.3.1. Altering a Table Column's Data Definition

You can make the following types of alterations to a table column's data definition:

$ sqlcmd
1> ALTER TABLE Airport ALTER COLUMN Name VARCHAR(25);            1
2> ALTER TABLE Airport ALTER COLUMN Country SET DEFAULT 'USA';   2
3> ALTER TABLE Airport ALTER COLUMN Name SET NOT NULL;           3

The examples are described as follows:

1

Change a column's data type. In our example we decided we needed more than 15 characters for the Airport Name so we changed it to 25 characters.

If the table has no existing data, you can make any data type changes. However, if the table already contains data, the new type must be larger than the old one. This restriction prevents corrupting existing data values that might be larger than the size of the new data type (See also Table A.1, “Supported SQL Datatypes”.)

2

Set or drop the column's DEFAULT value. In our example we assume the application is to be used mostly for US domestic travel so we can set a default value for the Airport Country of 'USA'.

To remove a default, redefine the column data definition, for example:

ALTER TABLE Airport ALTER COLUMN Country VARCHAR(15);

3

Change whether the column is NULL or NOT NULL. In our example we set the AirportID to be not null because this is a required field.

If the table has existing data, you cannot change a column to not null.

4.6.3.2. Adding and Dropping Table Columns

$ sqlcmd
1> ALTER TABLE Airport ADD COLUMN AirportCode VARCHAR(3)        1
2> BEFORE AirportID;  
3> ALTER TABLE Airport DROP COLUMN AirportID;                   2

The examples are described as follows:

1

Add table columns. In our example, we have decided not to use the integer AirportID for airport identification but to instead add an AirportCode, which uses a unique three-letter code for any airport as defined by the International Air Transport Association's airport codes.

You cannot rename or overwrite a column but you can drop and add columns. When adding a column, you must include the new column name and the data type. Options you may include are:

  • DEFAULT value — If a table contains data, the values for the new column will be automatically filled in with the default value.

  • NOT NULL — If the table contains data, you must include a default value if you specify a NOT NULL column.

  • One of the following index type constraints including PRIMARY KEY, UNIQUE, or ASSUMEUNIQUE.

    Note, we recommend that you not define the UNIQUE or ASSUMEUNIQUE constraint directly on a column definition when adding a column or creating a table. If you do, the constraint has no name so you cannot drop the constraint without dropping the entire column. Instead, we recommend you apply UNIQUE or ASSUMEUNIQUE by adding the constraint (see Section 4.6.3.3, “Adding and Dropping Table Constraints”) or by adding an index with the constraint (see Section 4.6.4, “Adding and Dropping Indexes”). Defining these constraints this way names the constraint, which makes it easier to drop later if necessary.

  • BEFORE column-name — Table columns cannot be reordered but the BEFORE clause allows you to place a new column in a specific position with respect to the existing columns of the table.

2

Drop table columns. In our example we drop the AirportID column because we are replacing it with the AirportCode column.

You cannot remove a column that has a reference to it. You have to remove all references to the column first. References to a column may include:

  • A stored procedure

  • An index

  • A view

4.6.3.3. Adding and Dropping Table Constraints

You cannot alter a table constraint but you can add and drop table constraints. If the table contains existing data, you cannot add UNIQUE, ASSUMEUNIQUE, or PRIMARY KEY constraints.

$ sqlcmd                                                                    
1> ALTER TABLE Airport ADD CONSTRAINT                        1
2>    uniquecode UNIQUE (Airportcode);
3> ALTER TABLE Airport ADD PRIMARY KEY (AirportCode);        2

The examples are described as follows:

1

Add named constraints UNIQUE or ASSUMEUNIQUE. In our example, we add the UNIQUE constraint to the AirportCode column. To drop a named constraint, include the name using the format in the following example:

ALTER TABLE Airport DROP CONSTRAINT uniquecode;

2

Add unnamed constraint PRIMARY KEY. In our example, we add the PRIMARY KEY constraint to the new AirportCode column.

When adding a table constraint, it must not conflict with the other columns of the table. For example, only one primary key is allowed for a table so you cannot add the PRIMARY KEY constraint to an additional column.

To drop the PRIMARY KEY, include the type of constraint using the following format:

ALTER TABLE Airport DROP PRIMARY KEY;

4.6.4. Adding and Dropping Indexes

Use CREATE INDEX to create an index on one or more columns of a table. Use DROP INDEX to remove an index from the schema. The following example modifies the flight reservation schema by adding an index to the Flight table to improve performance when looking up flights.

$ sqlcmd
1> CREATE INDEX flightTimeIdx ON Flight (departtime);

The CREATE INDEX statement explicitly creates an index. VoltDB creates an index implicitly when you specify the table constraints UNIQUE, PRIMARY KEY, or ASSUMEUNIQUE. Use the ALTER TABLE statement to add or drop these table constraints along with their associated indexes, as shown in Section 4.6.3, “Modifying Tables”.

4.6.5. Modifying Partitioning for Tables and Stored Procedures

Any changes to the schema must be carefully coordinated with the design and development of stored procedures. This not only applies to column names, data types, and so on, but also to the partition plan.

How to partition tables and stored procedures using the PARTITION TABLE and CREATE PROCEDURE PARTITION ON statements is explained in Section 4.4, “Partitioning Database Tables” and Section 5.3.3, “Partitioning Stored Procedures in the Schema”.

You can change the partitioning of stored procedures, and you can change a table to a replicated table or repartition it on a different column. However, because of the intricate dependencies of partitioned tables and stored procedures, this can only be done by dropping and re-adding the tables and procedures. Also, you must pay close attention to the order in which objects are dropped and added.

The following DDL examples demonstrate some partitioning modifications to a table and stored procedures.

  • Un-partitioning a Stored Procedure

  • Changing a Partitioned Table to a Replicated Table

  • Re-partitioning a Table to a Different Column

  • Updating a Stored Procedure

  • Removing a Stored Procedure from the Database

The following DDL is added to the Flight reservation schema to help demonstrate the DDL partition changes described in this section.

$ sqlcmd
1> PARTITION TABLE Airport ON COLUMN Name;
2> CREATE PROCEDURE FindAirportCodeByName 
3>    PARTITION ON TABLE Airport COLUMN Name
4>    AS SELECT TOP 1 AirportCode FROM Airport WHERE Name=?;
5> 
6> CREATE PROCEDURE FindAirportCodeByCity AS
7>    SELECT TOP 1 AirportCode FROM Airport WHERE City=?;

The stored procedures are tested with the following sqlcmd directives:

$ sqlcmd
1> exec FindAirportCodeByName 'Logan Airport';
2> exec FindAirportCodeByCity 'Boston';

4.6.5.1. Un-partitioning a Stored Procedure

In the simplest case, you can un-partition a single-partitioned stored procedure by dropping and re-creating that procedure without including the PARTITION ON clause. You would do this if you are planning to change the partitioning of the table itself, or if you are modifying the SQL statements within the procedure to require access to multiple partitions. In this example we drop the single-partitioned FindAirportCodeByName procedure and re-create it as multi-partitioned assuming it will need to search all partitions to find an airport code by name.

$ sqlcmd
1> DROP PROCEDURE FindAirportCodeByName;
2> CREATE PROCEDURE FindAirportCodeByName AS
3>    SELECT TOP 1 AirportCode FROM Airport WHERE Name=?;

4.6.5.2. Changing a Partitioned Table to a Replicated Table

Important

You cannot change the partitioning of a table that has data in it. To change a partitioned table to a replicated one, you drop and re-create the table, which deletes any data that might be in the table.

Before executing the following steps, save the existing schema so you can easily re-create the table. The Volt Management Center provides a view of the existing database schema DDL source, which you can download and save.

$ sqlcmd
1> DROP PROCEDURE FindAirportCodeByName;                     1
2> DROP PROCEDURE FindAirportCodeByCity;
3> DROP TABLE Airport IF EXISTS CASCADE;                     2
4> CREATE TABLE AIRPORT (                                    3
5>    AIRPORTCODE varchar(3) NOT NULL,
6>    NAME varchar(25),
7>    CITY varchar(25),
8>    COUNTRY varchar(15) DEFAULT 'USA',
9>    CONSTRAINT UNIQUECODE UNIQUE (AIRPORTCODE),
10>    PRIMARY KEY (AIRPORTCODE)
11> );
12> CREATE PROCEDURE FindAirportCodeByName AS                4
13>    SELECT TOP 1 AirportCode FROM Airport WHERE Name=?;
14> CREATE PROCEDURE FindAirportCodeByCity AS
15>    SELECT TOP 1 AirportCode FROM Airport WHERE City=?;

The example is described as follows:

1

Drop all stored procedures that reference the table. You cannot drop a table if stored procedures reference it.

2

Drop the table. Options you may include are:

  • IF EXISTS — Use the IF EXISTS option to avoid command errors if the named table is already removed.

  • CASCADE — A table cannot be removed if it has index or view references. You can remove the references explicitly first or use the CASCADE option to have VoltDB remove the references along with the table.

3

Re-create the table. By default, a newly created table is a replicated table.

4

Re-create the stored procedures that access the table. If the stored procedure is implemented with Java and changes are required, modify and reload the code before re-creating the stored procedures. For more, see Section 5.3, “Installing Stored Procedures into the Database”.

4.6.5.3. Re-partitioning a Table to a Different Column

Important

You cannot change the partitioning of a table that has data in it. In order to re-partition a table you have to drop and re-create the table, which deletes any data that might be in the table.

Follow these steps to re-partition a table:

  1. Un-partition the table by following the instructions in Section 4.6.5.2, “Changing a Partitioned Table to a Replicated Table”. The sub-steps are summarized as follows:

    1. Drop all stored procedures that reference the table.

    2. Drop the table.

    3. Re-create the table.

    4. Re-create the stored procedures that access the table.

  2. Partition the table on the new column. In our example, it makes sense to partition the Airport table on the AirportCode column, where each row must be unique and non null.

    $ sqlcmd
    1> PARTITION TABLE Airport ON COLUMN AirportCode;
  3. Re-partition stored procedures that should be single-partitioned. See Section 4.6.5.4, “Updating a Stored Procedure”.

4.6.5.4. Updating a Stored Procedure

This section describes how to update a stored procedure that has already been declared in the database with the CREATE PROCEDURE statement. The steps to update a stored procedure are summarized as follows:

  1. If the procedure is implemented in Java, update the procedure's code, recompile, and repackage the jar file. For details, see Section 5.3, “Installing Stored Procedures into the Database”.

  2. Ensure all tables and columns the procedure accesses are in the database schema.

  3. Update the procedure in the database.

    • If the procedure is implemented in Java, use the sqlcmd load classes directive to update the class in the database. For example:

      $ sqlcmd
      1> load classes GetAirport.jar;
    • If the procedure is implemented with SQL, use the CREATE PROCEDURE AS command to update the SQL.

  4. If required, re-partition the stored procedure. You partition procedures using the PARTITION ON clause in the CREATE PROCEDURE statement. If you need to re-partition the procedure, either changing the partitioning column or switching from replicated to partitioned or vice versa, perform the following steps:

    1. Use DROP PROCEDURE to remove the stored procedure.

    2. Use CREATE PROCEDURE to re-declare the stored procedure, including the new partitioning scheme.

    In our example so far, we have three stored procedures that are adequate to access the Airport table, so no additional procedures need to be partitioned:

    • VoltDB automatically defined a default select stored procedure, which is partitioned on the AirportCode column. It takes an AirportCode as input and returns a table structure containing the AirportCode, Name, City, and Country.

    • The FindAirportCodeByName stored procedure should remain multi-partitioned because it needs to search in all partitions.

    • The FindAirportCodeByCity stored procedure should also remain multi-partitioned because it needs to search in all partitions.

4.6.5.5. Removing a Stored Procedure from the Database

If you've decided a stored procedure is no longer needed, use the following steps to remove it from the database:

  1. Drop the stored procedure from the database.

    $ sqlcmd
    1> DROP PROCEDURE GetAirport;
  2. Remove the code from the database. If the procedure is implemented with Java, use the sqlcmd remove classes directive to remove the procedure's class from the database.

    2> remove classes myapp.procedures.GetAirport;
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.