docs.intersystems.com
InterSystems IRIS Data Platform 2019.2  /  InterSystems SQL Reference

InterSystems SQL Reference
DROP TABLE
Previous section           Next section
InterSystems: The power behind what matters   
Search:  


Deletes a table and (optionally) its data.
Synopsis
DROP TABLE table 
     [RESTRICT | CASCADE] [%DELDATA | %NODELDATA]
Arguments
table The name of the table to be deleted. The table name can be qualified (schema.table), or unqualified (table). An unqualified table name takes the system-wide default schema name. Schema search path values are not used.
RESTRICT
CASCADE
Optional — RESTRICT only allows a table with no dependent views or integrity constraints to be deleted. CASCADE allow a table with dependent views or integrity constraints to be deleted; any referencing views or integrity constraints will also be deleted as part of the table deletion. (See restriction on CASCADE below.)
%DELDATA
%NODELDATA
Optional — These keywords specify whether to delete data associated with a table when deleting the table. The default is to delete table data.
Description
The DROP TABLE command deletes a table and its corresponding persistent class definition. If the table is the last item in its schema, deleting the table also deletes the schema and its corresponding persistent class package.
By default, DROP TABLE deletes both the table definition and the table’s data (if any exists). The %NODELDATA keyword allows you to specify deletion of the table definition but not the table’s data.
In order to delete a table, the following conditions must be met:
You can use the $SYSTEM.SQL.DropTable() method to delete a table in the current namespace. You specify the SQL table name. Unlike DROP TABLE, this method can delete a table that was defined without [DdlAllowed]. The second argument specifies whether the table data should also be deleted; by default, data is not deleted.
  DO $SYSTEM.SQL.DropTable("Sample.MyTable",1,.SQLCODE,.%msg)
  IF SQLCODE '= 0 {WRITE "SQLCODE ",SQLCODE," error: ",%msg}
You can use the $SYSTEM.OBJ.Delete() method to delete one or more tables in the current namespace. You must specify the persistent class name that projects the table (not the SQL table name). You can specify multiple class names using wildcards. The second argument specifies whether the table data should also be deleted; by default, data is not deleted.
Privileges
The DROP TABLE command is a privileged operation. Prior to using DROP TABLE it is necessary for your process to have either %DROP_TABLE administrative privilege or a DELETE object privilege for the specified table. Failing to do so results in an SQLCODE –99 error (Privilege Violation). You can determine if the current user has DELETE privilege by invoking the %CHECKPRIV command. You can determine if a specified user has DELETE privilege by invoking the $SYSTEM.SQL.CheckPriv() method. You can use the GRANT command to assign %DROP_TABLE privileges, if you hold appropriate granting privileges.
In embedded SQL, you can use the $SYSTEM.Security.Login() method to log in as a user with appropriate privileges:
   DO $SYSTEM.Security.Login("_SYSTEM","SYS")
   &sql(      )
You must have the %Service_Login:Use privilege to invoke the $SYSTEM.Security.Login method. For further information, refer to %SYSTEM.Security in the InterSystems Class Reference.
DROP TABLE cannot be used on a table created by defining a persistent class, unless the table class definition includes [DdlAllowed]. Otherwise, the operation fails with an SQLCODE -300 error with the %msg DDL not enabled for class 'Schema.tablename'>
Existing Object Privileges
Deleting a table does not delete the object privileges for that table. For example, the privilege granted to a user to insert, update, or delete data on that table. This has the following two consequences:
For these reasons, it is generally recommended that you use the REVOKE command to revoke object privileges from a table before deleting the table.
Table Containing Data
By default, DROP TABLE deletes the table definition and deletes the table’s data. This table data delete is an atomic operation; if DROP TABLE encounters data that cannot be deleted (for example, a row with a referential constraint) any data deletion already performed is automatically rolled back, with the result that no table data is deleted.
You can set the system-wide default for table data deletion using the $SYSTEM.SQL.SetDDLDropTabDelData() method. To determine the current setting, call $SYSTEM.SQL.CurrentSettings(), which displays the Does DDL DROP TABLE delete the table's data? setting.
The default is 1 (“Yes”). This is the recommended setting for this option. Set this option to 0 (“No”) if you want DROP TABLE to not delete the table’s data when it deletes the table definition.
The deletion of data can be overridden on a per-table basis. When deleting a table, you can specify DROP TABLE with the %NODELDATA option to prevent the automatic deletion of the table’s data. If the system-wide default is set to not delete table data, you can delete data on a per-table basis by specifying DROP TABLE with the %DELDATA option.
In most circumstances DROP TABLE automatically deletes the table’s data using a highly efficient kill extent operation. The following circumstances prevent the use of kill extent: the table has foreign keys that reference it; the class projecting the table is a subclass of a persistent class; the class does not use default storage; there is a ForEach = "row/object" trigger; there is a stream field that references a non-default global location. If any of these apply, DROP TABLE deletes the table’s data using a less-efficient delete record operation.
You can use the TRUNCATE TABLE command to delete the table’s data without deleting the table definition.
Lock Applied
The DROP TABLE statement acquires an exclusive table-level lock on table. This prevents other processes from modifying the table definition or the table data while table deletion is in process. This table-level lock is sufficient for deleting both the table definition and the table data; DROP TABLE does not acquire a lock on each row of the table data. This lock is automatically released at the end of the DROP TABLE operation.
Foreign Key Constraints
By default, you cannot drop a table if any foreign key constraints are defined on another table that references the table you are attempting to drop. You must drop all referencing foreign key constraints before dropping the table they reference. Failing to delete these foreign key constraints before attempting a DROP TABLE operation results in an SQLCODE -320 error.
This default behavior is consistent with the RESTRICT keyword option. The CASCADE keyword option is not supported for foreign key constraints.
To change this default foreign key constraint behavior, refer to the COMPILEMODE=NOCHECK option of the SET OPTION command.
Associated Queries
Dropping a table automatically purges any related cached queries and purges query information as generated by %SYS.PTools.StatsSQL. Dropping a table automatically purges any SQL runtime statistics (SQL Stats) information for any related query.
Nonexistent Table
To determine if a specified table exists in the current namespace, use the $SYSTEM.SQL.TableExists() method.
If you try to delete a nonexistent table, DROP TABLE issues an SQLCODE -30 error by default. However, this error-reporting behavior can be overridden by setting the system-wide configuration using the $SYSTEM.SQL.SetDDLNo30() method call. To determine the current setting, call $SYSTEM.SQL.CurrentSettings(), which displays a Suppress SQLCODE=-30 Errors: setting.
The default is “No” (0). This is the recommended setting for this option. Set this option to “Yes” (1) if you want a DROP TABLE for nonexistent table to perform no operation and not issue an error message.
Examples
The following embedded SQL example creates a table named SQLUser.MyEmployees and later deletes it. This example specifies that any data associated with this table not be deleted when the table is deleted:
  &sql(CREATE TABLE SQLUser.MyEmployees (
       NAMELAST     CHAR (30) NOT NULL,
       NAMEFIRST    CHAR (30) NOT NULL,
       STARTDATE    TIMESTAMP,
       SALARY       MONEY))
  WRITE !,"Created a table"
  /*
    &sql(SQL code populating SQLUser.MyEmployees table)
    &sql(SQL code using SQLUser.MyEmployees table)
  */
  NEW SQLCODE,%msg
  &sql(DROP TABLE SQLUser.MyEmployees %NODELDATA)
  IF SQLCODE=0 {WRITE !,"Table deleted"}
  ELSE {WRITE !,"SQLCODE=",SQLCODE,": ",%msg }
See Also


Previous section           Next section
Send us comments on this page
View this book as PDF   |  Download all PDFs
Copyright © 1997-2019 InterSystems Corporation, Cambridge, MA
Content Date/Time: 2019-08-22 06:48:58