GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||
GeezerVison is showing docs.voltdb.com — 4.6. Modifying the Schema Using VoltDB4.6. Modifying the SchemaYou 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:
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 ClientsYou 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:
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):
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 SchemaThe 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:
For example: $ sqlcmd 1> SHOW TABLES; 2> SHOW PROCEDURES; 3> EXEC @SystemCatalog COLUMNS; 4.6.3. Modifying TablesAfter creating a table in a database with CREATE TABLE, you can use ALTER TABLE to make the following types of table changes: To drop an entire table, use the DROP TABLE DDL statement. 4.6.3.1. Altering a Table Column's Data DefinitionYou can make the following types of alterations to a table column's data definition: $ sqlcmd 1> ALTER TABLE Airport ALTER COLUMN Name VARCHAR(25); The examples are described as follows:
4.6.3.2. Adding and Dropping Table Columns$ sqlcmd 1> ALTER TABLE Airport ADD COLUMN AirportCode VARCHAR(3) The examples are described as follows:
4.6.3.3. Adding and Dropping Table ConstraintsYou 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 The examples are described as follows:
4.6.4. Adding and Dropping IndexesUse 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 ProceduresAny 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. 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 ProcedureIn 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 TableImportantYou 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; The example is described as follows:
4.6.5.3. Re-partitioning a Table to a Different ColumnImportantYou 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:
4.6.5.4. Updating a Stored ProcedureThis 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:
4.6.5.5. Removing a Stored Procedure from the DatabaseIf you've decided a stored procedure is no longer needed, use the following steps to remove it from the database:
| ||||||||||||||||||||||
| 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. |