Database Reference
In-Depth Information
Because this simple statement drops the table and all of its data, be very careful when using it.
Do not code this statement on the wrong table!
The DBMS will not drop a table that is the parent in a FOREIGN KEY constraint. It will not
do so even if there are no children, or even if you have coded DELETE CASCADE. Instead, to
drop such a table, you must first either drop the foreign key constraint or drop the child table.
Then you can delete the parent table. As mentioned earlier, parent tables must be first in and
last out.
The following statements are needed to drop the CUSTOMER table:
/* *** EXAMPLE CODE - DO NOT RUN *** */
/* *** SQL-DROP-TABLE-CH07-02 *** */
DROP TABLE CUSTOMER_ARTIST_INT;
DROP TABLE TRANS;
DROP TABLE CUSTOMER;
Alternatively, you could drop CUSTOMER with:
/* *** EXAMPLE CODE - DO NOT RUN *** */
/* *** SQL-ALTER-TABLE-CH07-05 *** */
ALTER TABLE CUSTOMER_ARTIST_INT
DROP CONSTRAINT Customer_Artist_Int_CustomerFK;
ALTER TABLE TRANS
DROP CONSTRAINT TransactionCustomerFK;
/* *** SQL-DROP-TABLE-CH07-03 *** */
DROP TABLE CUSTOMER;
The SQL TRUNCATE TABLE Statement
The SQL TRUNCATE TABLE statement was officially added in the SQL:2008 standard, so it
is one of the latest additions to SQL. It is used to remove all data from a table, while leaving
the table structure itself in the database. The SQL TRUNCATE TABLE statement does not use
an SQL WHERE clause to specify conditions for the data deletion— all the data in the table is
always removed when TRUNCATE it used.
The following statement could be used to remove all the data in the CUSTOMER_
ARTIST_INT table:
/* *** EXAMPLE CODE - DO NOT RUN *** */
/* *** SQL-TRUNCATE-TABLE-CH07-01 *** */
TRUNCATE TABLE CUSTOMER_ARTIST_INT;
The TRUNCATE TABLE statement cannot be used with a table that is referenced by a
foreign key constraint because this could create foreign key values that have no corresponding
primary key value. Thus, while we can use TRUNCATE TABLE with the CUSTOMER_ARTIST_
INT table, we cannot use it with the CUSTOMER table.
The SQL CREATE INDEX Statement
An index is a special data structure that is created to improve database performance. SQL
Server automatically creates an index on all primary and foreign keys. A developer can also
direct SQL Server to create an index on other columns that are frequently used in WHERE
clauses or on columns that are used for sorting data when sequentially processing a table for
queries and reports. Indexing concepts are discussed in Appendix G.
SQL DDL includes an SQL CREATE INDEX statement to create indexes, an SQL ALTER
INDEX statement to modify existing database indexes, and an SQL DROP INDEX statement
to remove indexes for the database.
 
Search WWH ::




Custom Search