Sie sind auf Seite 1von 10

Syntax:

ALTER {DATABASE | SCHEMA} [db_name]


alter_specification ...
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME

alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name

ALTER DATABASE enables you to change the overall characteristics of a


database. These characteristics are stored in the db.opt file in the
database directory. To use ALTER DATABASE, you need the ALTER privilege
on the database. ALTER SCHEMA is a synonym for ALTER DATABASE.

The database name can be omitted from the first syntax, in which case
the statement applies to the default database.

National Language Characteristics

The CHARACTER SET clause changes the default database character set.
The COLLATE clause changes the default database collation.
http://dev.mysql.com/doc/refman/5.5/en/charset.html, discusses
character set and collation names.

You can see what character sets and collations are available using,
respectively, the SHOW CHARACTER SET and SHOW COLLATION statements. See
[HELP SHOW CHARACTER SET], and [HELP SHOW COLLATION], for more
information.

If you change the default character set or collation for a database,


stored routines that use the database defaults must be dropped and
recreated so that they use the new defaults. (In a stored routine,
variables with character data types use the database defaults if the
character set or collation are not specified explicitly. See [HELP
CREATE PROCEDURE].)

Upgrading from Versions Older than MySQL 5.1

The syntax that includes the UPGRADE DATA DIRECTORY NAME clause updates
the name of the directory associated with the database to use the
encoding implemented in MySQL 5.1 for mapping database names to
database directory names (see
http://dev.mysql.com/doc/refman/5.5/en/identifier-mapping.html). This
clause is for use under these conditions:

o It is intended when upgrading MySQL to 5.1 or later from older


versions.

o It is intended to update a database directory name to the current


encoding format if the name contains special characters that need
encoding.

o The statement is used by mysqlcheck (as invoked by mysql_upgrade).

For example, if a database in MySQL 5.0 has the name a-b-c, the name
contains instances of the - (dash) character. In MySQL 5.0, the
database directory is also named a-b-c, which is not necessarily safe
for all file systems. In MySQL 5.1 and later, the same database name is
encoded as a@002db@002dc to produce a file system-neutral directory
name.

When a MySQL installation is upgraded to MySQL 5.1 or later from an


older version,the server displays a name such as a-b-c (which is in the
old format) as #mysql50#a-b-c, and you must refer to the name using the
#mysql50# prefix. Use UPGRADE DATA DIRECTORY NAME in this case to
explicitly tell the server to re-encode the database directory name to
the current encoding format:

ALTER DATABASE `#mysql50#a-b-c` UPGRADE DATA DIRECTORY NAME;

After executing this statement, you can refer to the database as a-b-c
without the special #mysql50# prefix.

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-database.html

Syntax:
ALTER
[DEFINER = { user | CURRENT_USER }]
EVENT event_name
[ON SCHEDULE schedule]
[ON COMPLETION [NOT] PRESERVE]
[RENAME TO new_event_name]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
[DO event_body]

The ALTER EVENT statement changes one or more of the characteristics of


an existing event without the need to drop and recreate it. The syntax
for each of the DEFINER, ON SCHEDULE, ON COMPLETION, COMMENT, ENABLE /
DISABLE, and DO clauses is exactly the same as when used with CREATE
EVENT. (See [HELP CREATE EVENT].)

Any user can alter an event defined on a database for which that user
has the EVENT privilege. When a user executes a successful ALTER EVENT
statement, that user becomes the definer for the affected event.

ALTER EVENT works only with an existing event:

mysql> ALTER EVENT no_such_event


> ON SCHEDULE
> EVERY '2:3' DAY_HOUR;
ERROR 1517 (HY000): Unknown event 'no_such_event'

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-event.html

Syntax:
ALTER FUNCTION func_name [characteristic ...]

characteristic:
COMMENT 'string'
| LANGUAGE SQL
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }

This statement can be used to change the characteristics of a stored


function. More than one change may be specified in an ALTER FUNCTION
statement. However, you cannot change the parameters or body of a
stored function using this statement; to make such changes, you must
drop and re-create the function using DROP FUNCTION and CREATE
FUNCTION.

You must have the ALTER ROUTINE privilege for the function. (That
privilege is granted automatically to the function creator.) If binary
logging is enabled, the ALTER FUNCTION statement might also require the
SUPER privilege, as described in
http://dev.mysql.com/doc/refman/5.5/en/stored-programs-logging.html.

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-function.html

Syntax:
ALTER LOGFILE GROUP logfile_group
ADD UNDOFILE 'file_name'
[INITIAL_SIZE [=] size]
[WAIT]
ENGINE [=] engine_name

This statement adds an UNDO file named 'file_name' to an existing log


file group logfile_group. An ALTER LOGFILE GROUP statement has one and
only one ADD UNDOFILE clause. No DROP UNDOFILE clause is currently
supported.

*Note*:

All NDB Cluster Disk Data objects share the same namespace. This means
that each Disk Data object must be uniquely named (and not merely each
Disk Data object of a given type). For example, you cannot have a
tablespace and an undo log file with the same name, or an undo log file
and a data file with the same name.

The optional INITIAL_SIZE parameter sets the UNDO file's initial size
in bytes; if not specified, the initial size defaults to 134217728 (128
MB). Prior to MySQL NDB Cluster 7.2.14, this value was required to be
specified using digits (Bug #13116514, Bug #16104705, Bug #62858); in
MySQL NDB Cluster 7.2.14 and later, you may optionally follow size with
a one-letter abbreviation for an order of magnitude, similar to those
used in my.cnf. Generally, this is one of the letters M (megabytes) or
G (gigabytes).

On 32-bit systems, the maximum supported value for INITIAL_SIZE is


4294967296 (4 GB). (Bug #29186)

The minimum allowed value for INITIAL_SIZE is 1048576 (1 MB). (Bug


#29574)

*Note*:

WAIT is parsed but otherwise ignored. This keyword currently has no


effect, and is intended for future expansion.

The ENGINE parameter (required) determines the storage engine which is


used by this log file group, with engine_name being the name of the
storage engine. Currently, the only accepted values for engine_name are
"NDBCLUSTER" and "NDB". The two values are equivalent.

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-logfile-group.html

Syntax:
ALTER PROCEDURE proc_name [characteristic ...]

characteristic:
COMMENT 'string'
| LANGUAGE SQL
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
This statement can be used to change the characteristics of a stored
procedure. More than one change may be specified in an ALTER PROCEDURE
statement. However, you cannot change the parameters or body of a
stored procedure using this statement; to make such changes, you must
drop and re-create the procedure using DROP PROCEDURE and CREATE
PROCEDURE.

You must have the ALTER ROUTINE privilege for the procedure. By
default, that privilege is granted automatically to the procedure
creator. This behavior can be changed by disabling the
automatic_sp_privileges system variable. See
http://dev.mysql.com/doc/refman/5.5/en/stored-routines-privileges.html.

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-procedure.html

Syntax:
ALTER SERVER server_name
OPTIONS (option [, option] ...)

Alters the server information for server_name, adjusting any of the


options permitted in the CREATE SERVER statement. The corresponding
fields in the mysql.servers table are updated accordingly. This
statement requires the SUPER privilege.

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-server.html

Syntax:
ALTER [ONLINE|OFFLINE] [IGNORE] TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]

alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| ADD [COLUMN] (col_name column_definition,...)
| ADD {INDEX|KEY} [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] PRIMARY KEY
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
UNIQUE [INDEX|KEY] [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD FULLTEXT [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD SPATIAL [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| 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 new_col_name column_definition
[FIRST|AFTER col_name]
| [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
| CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
| {DISABLE|ENABLE} KEYS
| {DISCARD|IMPORT} TABLESPACE
| DROP [COLUMN] col_name
| DROP {INDEX|KEY} index_name
| DROP PRIMARY KEY
| DROP FOREIGN KEY fk_symbol
| FORCE
| MODIFY [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| ORDER BY col_name [, col_name] ...
| RENAME [TO|AS] new_tbl_name
| ADD PARTITION (partition_definition)
| DROP PARTITION partition_names
| TRUNCATE PARTITION {partition_names | ALL}
| COALESCE PARTITION number
| REORGANIZE PARTITION [partition_names INTO (partition_definitions)]
| ANALYZE PARTITION {partition_names | ALL}
| CHECK PARTITION {partition_names | ALL}
| OPTIMIZE PARTITION {partition_names | ALL}
| REBUILD PARTITION {partition_names | ALL}
| REPAIR PARTITION {partition_names | ALL}
| PARTITION BY partitioning_expression
| REMOVE PARTITIONING

index_col_name:
col_name [(length)] [ASC | DESC]

index_type:
USING {BTREE | HASH}

index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'

table_options:
table_option [[,] table_option] ...

table_option:
AUTO_INCREMENT [=] value
| AVG_ROW_LENGTH [=] value
| [DEFAULT] CHARACTER SET [=] charset_name
| CHECKSUM [=] {0 | 1}
| [DEFAULT] COLLATE [=] collation_name
| COMMENT [=] 'string'
| CONNECTION [=] 'connect_string'
| {DATA|INDEX} DIRECTORY [=] 'absolute path to directory'
| DELAY_KEY_WRITE [=] {0 | 1}
| ENGINE [=] engine_name
| INSERT_METHOD [=] { NO | FIRST | LAST }
| KEY_BLOCK_SIZE [=] value
| MAX_ROWS [=] value
| MIN_ROWS [=] value
| PACK_KEYS [=] {0 | 1 | DEFAULT}
| PASSWORD [=] 'string'
| ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
| TABLESPACE tablespace_name [STORAGE {DISK|MEMORY|DEFAULT}]
| UNION [=] (tbl_name[,tbl_name]...)

ALTER TABLE changes the structure of a table. For example, you can add
or delete columns, create or destroy indexes, change the type of
existing columns, or rename columns or the table itself. You can also
change characteristics such as the storage engine used for the table or
the table comment.

o To use ALTER TABLE, you need ALTER, CREATE, and INSERT privileges for
the table. Renaming a table requires ALTER and DROP on the old table,
ALTER, CREATE, and INSERT on the new table.

o Following the table name, specify the alterations to be made. If none


are given, ALTER TABLE does nothing.

o The syntax for many of the permissible alterations is similar to


clauses of the CREATE TABLE statement. column_definition clauses use
the same syntax for ADD and CHANGE as for CREATE TABLE. For more
information, see [HELP CREATE TABLE].

o The word COLUMN is optional and can be omitted.

o Multiple ADD, ALTER, DROP, and CHANGE clauses are permitted in a


single ALTER TABLE statement, separated by commas. This is a MySQL
extension to standard SQL, which permits only one of each clause per
ALTER TABLE statement. For example, to drop multiple columns in a
single statement, do this:

ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;

o If a storage engine does not support an attempted ALTER TABLE


operation, a warning may result. Such warnings can be displayed with
SHOW WARNINGS. See [HELP SHOW WARNINGS]. For information on
troubleshooting ALTER TABLE, see
http://dev.mysql.com/doc/refman/5.5/en/alter-table-problems.html.

o For usage examples, see


http://dev.mysql.com/doc/refman/5.5/en/alter-table-examples.html.

o With the mysql_info() C API function, you can find out how many rows
were copied by ALTER TABLE, and (when IGNORE is used) how many rows
were deleted due to duplication of unique key values. See
http://dev.mysql.com/doc/refman/5.5/en/mysql-info.html.

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-table.html

Syntax:
ALTER TABLESPACE tablespace_name
{ADD|DROP} DATAFILE 'file_name'
[INITIAL_SIZE [=] size]
[WAIT]
ENGINE [=] engine_name

This statement can be used either to add a new data file, or to drop a
data file from a tablespace.

The ADD DATAFILE variant enables you to specify an initial size using
an INITIAL_SIZE clause, where size is measured in bytes; the default
value is 134217728 (128 MB). Prior to MySQL NDB Cluster 7.2.14, this
value was required to be specified using digits (Bug #13116514, Bug
#16104705, Bug #62858); in MySQL NDB Cluster 7.2.14 and later, you may
optionally follow size with a one-letter abbreviation for an order of
magnitude, similar to those used in my.cnf. Generally, this is one of
the letters M (megabytes) or G (gigabytes).

*Note*:

All NDB Cluster Disk Data objects share the same namespace. This means
that each Disk Data object must be uniquely named (and not merely each
Disk Data object of a given type). For example, you cannot have a
tablespace and an data file with the same name, or an undo log file and
a tablespace with the same name.

On 32-bit systems, the maximum supported value for INITIAL_SIZE is


4294967296 (4 GB). (Bug #29186)
INITIAL_SIZE is rounded, explicitly, as for CREATE TABLESPACE.

Once a data file has been created, its size cannot be changed; however,
you can add more data files to the tablespace using additional ALTER
TABLESPACE ... ADD DATAFILE statements.

Using DROP DATAFILE with ALTER TABLESPACE drops the data file
'file_name' from the tablespace. You cannot drop a data file from a
tablespace which is in use by any table; in other words, the data file
must be empty (no extents used). See
http://dev.mysql.com/doc/refman/5.5/en/mysql-cluster-disk-data-objects.
html. In addition, any data file to be dropped must previously have
been added to the tablespace with CREATE TABLESPACE or ALTER
TABLESPACE.

Both ALTER TABLESPACE ... ADD DATAFILE and ALTER TABLESPACE ... DROP
DATAFILE require an ENGINE clause which specifies the storage engine
used by the tablespace. Currently, the only accepted values for
engine_name are NDB and NDBCLUSTER.

WAIT is parsed but otherwise ignored, and so has no effect in MySQL


5.5. It is intended for future expansion.

When ALTER TABLESPACE ... ADD DATAFILE is used with ENGINE = NDB, a
data file is created on each Cluster data node. You can verify that the
data files were created and obtain information about them by querying
the INFORMATION_SCHEMA.FILES table. For example, the following query
shows all data files belonging to the tablespace named newts:

mysql> SELECT LOGFILE_GROUP_NAME, FILE_NAME, EXTRA


-> FROM INFORMATION_SCHEMA.FILES
-> WHERE TABLESPACE_NAME = 'newts' AND FILE_TYPE = 'DATAFILE';
+--------------------+--------------+----------------+
| LOGFILE_GROUP_NAME | FILE_NAME | EXTRA |
+--------------------+--------------+----------------+
| lg_3 | newdata.dat | CLUSTER_NODE=3 |
| lg_3 | newdata.dat | CLUSTER_NODE=4 |
| lg_3 | newdata2.dat | CLUSTER_NODE=3 |
| lg_3 | newdata2.dat | CLUSTER_NODE=4 |
+--------------------+--------------+----------------+
2 rows in set (0.03 sec)

See http://dev.mysql.com/doc/refman/5.5/en/files-table.html.

ALTER TABLESPACE is useful only with Disk Data storage for NDB Cluster.
See
http://dev.mysql.com/doc/refman/5.5/en/mysql-cluster-disk-data.html.

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-tablespace.html

Syntax:
ALTER
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]

This statement changes the definition of a view, which must exist. The
syntax is similar to that for CREATE VIEW and the effect is the same as
for CREATE OR REPLACE VIEW. See [HELP CREATE VIEW]. This statement
requires the CREATE VIEW and DROP privileges for the view, and some
privilege for each column referred to in the SELECT statement. ALTER
VIEW is permitted only to the definer or users with the SUPER
privilege.

URL: http://dev.mysql.com/doc/refman/5.5/en/alter-view.html

MySQL supports foreign keys, which let you cross-reference related data
across tables, and foreign key constraints, which help keep this
spread-out data consistent. The essential syntax for a foreign key
constraint definition in a CREATE TABLE or ALTER TABLE statement looks
like this:

[CONSTRAINT [symbol]] FOREIGN KEY


[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]

reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT

URL: http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html

Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...

create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name

CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.

URL: http://dev.mysql.com/doc/refman/5.5/en/create-database.html

Syntax:
CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
DO event_body;

schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]

interval:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
This statement creates and schedules a new event. The event will not
run unless the Event Scheduler is enabled. For information about
checking Event Scheduler status and enabling it if necessary, see
http://dev.mysql.com/doc/refman/5.5/en/events-configuration.html.

CREATE EVENT requires the EVENT privilege for the schema in which the
event is to be created. It might also require the SUPER privilege,
depending on the DEFINER value, as described later in this section.

The minimum requirements for a valid CREATE EVENT statement are as


follows:

o The keywords CREATE EVENT plus an event name, which uniquely


identifies the event in a database schema.

o An ON SCHEDULE clause, which determines when and how often the event
executes.

o A DO clause, which contains the SQL statement to be executed by an


event.

This is an example of a minimal CREATE EVENT statement:

CREATE EVENT myevent


ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;

The previous statement creates an event named myevent. This event


executes once---one hour following its creation---by running an SQL
statement that increments the value of the myschema.mytable table's
mycol column by 1.

The event_name must be a valid MySQL identifier with a maximum length


of 64 characters. Event names are not case sensitive, so you cannot
have two events named myevent and MyEvent in the same schema. In
general, the rules governing event names are the same as those for
names of stored routines. See
http://dev.mysql.com/doc/refman/5.5/en/identifiers.html.

An event is associated with a schema. If no schema is indicated as part


of event_name, the default (current) schema is assumed. To create an
event in a specific schema, qualify the event name with a schema using
schema_name.event_name syntax.

URL: http://dev.mysql.com/doc/refman/5.5/en/create-event.html

The CREATE FUNCTION statement is used to create stored functions and


user-defined functions (UDFs):

o For information about creating stored functions, see [HELP CREATE


PROCEDURE].

o For information about creating user-defined functions, see [HELP


CREATE FUNCTION UDF].

URL: http://dev.mysql.com/doc/refman/5.5/en/create-function.html

Syntax:
CREATE [ONLINE|OFFLINE] [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
[index_type]
ON tbl_name (index_col_name,...)
[index_option] ...

index_col_name:
col_name [(length)] [ASC | DESC]

index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'

index_type:
USING {BTREE | HASH}

CREATE INDEX is mapped to an ALTER TABLE statement to create indexes.


See [HELP ALTER TABLE]. CREATE INDEX cannot be used to create a PRIMARY
KEY; use ALTER TABLE instead. For more information about indexes, see
http://dev.mysql.com/doc/refman/5.5/en/mysql-indexes.html.

URL: http://dev.mysql.com/doc/refman/5.5/en/create-index.html

Das könnte Ihnen auch gefallen