13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Altering Tables After Creation<br />

263<br />

Table 10.5<br />

Syntax<br />

DISABLE KEYS<br />

ENABLE KEYS<br />

RENAME [AS] new_table_name<br />

ORDER BY col_name<br />

CONVERT TO CHARACTER SET cs<br />

COLLATE c<br />

[DEFAULT] CHARACTER SET cs<br />

COLLATE c<br />

DISCARD TABLESPACE<br />

IMPORT TABLESPACE<br />

table_options<br />

Possible Changes with the ALTER TABLE Statement<br />

Description<br />

Turns off index updating.<br />

Turns on index updating.<br />

Renames a table.<br />

Re-creates the table with the rows in a particular<br />

order. (Note that after you begin<br />

changing the table, the rows will no longer<br />

be in order.)<br />

Converts all text-based columns to the<br />

specified character set <strong>and</strong> collation.<br />

Sets the default character set <strong>and</strong> collation.<br />

Deletes the underlying tablespace file for an<br />

InnoDB table. (See Chapter 13 for more<br />

details on InnoDB.)<br />

Re-creates the underlying tablespace file for<br />

an InnoDB table. (See Chapter 13 for more<br />

details on InnoDB.)<br />

Allows you to reset the table options. Uses<br />

the same syntax as CREATE TABLE.<br />

Let’s look at a few of the more common uses of ALTER TABLE.<br />

You may frequently realize that you haven’t made a particular column “big enough”<br />

for the data it has to hold. For example, previously in the customers table, you allowed<br />

names to be 50 characters long. After you start getting some data, you might notice that<br />

some of the names are too long <strong>and</strong> are being truncated.You can fix this problem by<br />

changing the data type of the column so that it is 70 characters long instead:<br />

alter table customers<br />

modify name char(70) not null;<br />

Another common occurrence is the need to add a column. Imagine that a sales tax on<br />

books is introduced locally <strong>and</strong> that Book-O-Rama needs to add the amount of tax to<br />

the total order but keep track of it separately.You can add a tax column to the orders<br />

table as follows:<br />

alter table orders<br />

add tax float(6,2) after amount;<br />

Getting rid of a column is another case that comes up frequently.You can delete the column<br />

you just added as follows:<br />

alter table orders<br />

drop tax;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!