After MySQL creates a database, it then creates a table and adds some fields. What if I want to add more fields in the future?
Answer: Use alter table (modify table)!
ALTER TABLE syntax:
ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification] ...
alter_specification:
ADD [COLUMN] column_definition [FIRST | AFTER col_name ]
| ADD [COLUMN] (column_definition,...)
| ADD INDEX [index_name] [index_type] (index_col_name,...)
| ADD [CONSTRAINT [symbol]]
PRIMARY KEY [index_type] (index_col_name,...)
| ADD [CONSTRAINT [symbol]]
UNIQUE [index_name] [index_type] (index_col_name,...)
| ADD [FULLTEXT|SPATIAL] [index_name] (index_col_name,...)
| ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
[reference_definition]
| ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
| CHANGE [COLUMN] old_col_name column_definition
[FIRST|AFTER col_name]
| MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]
| DROP [COLUMN] col_name
| DROP PRIMARY KEY
| DROP INDEX index_name
| DROP FOREIGN KEY fk_symbol
| DISABLE KEYS
| ENABLE KEYS
| RENAME [TO] new_tbl_name
| ORDER BY col_name
| CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
| [DEFAULT] CHARACTER SET charset_name [COLLATE collation_name]
| DISCARD TABLESPACE | IMPORT TABLESPACE | table_options
| partition_options
| ADD PARTITION partition_definition
| DROP PARTITION partition_names
| COALESCE PARTITION number
| REORGANIZE PARTITION partition_names INTO (partition_definitions)
| ANALYZE PARTITION partition_names
| CHECK PARTITION partition_names
| OPTIMIZE PARTITION partition_names
| REBUILD PARTITION partition_names
| REPAIR PARTITION partition_names
http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#alter-table
My example:
Add a field:
alter table book add name varchar(20);
I think 20 is too small, so I changed it to 50
alter table book change name name varchar(50);
Add a few fields:
alter table book add authors varchar(100),add category varchar(20),add
price double(10,2);
Delete a column:
alter table book drop cover;
Add 1 column at a certain position:
alter table book add cover varchar(100) after(first) publishdate;
Modify the value of a field or fields in a record:
update book set column_name1="" where column_name2="";
Delete a record:
delete from table_name where where_contion;
Modify the order of records in a table:
alter table book order by bookid (default is ascending order descending order is desc);