Rechercher dans le manuel MySQL
13.1.36 RENAME TABLE Syntax
RENAME TABLE
renames one or more
tables. You must have ALTER
and
DROP
privileges for the original
table, and CREATE
and
INSERT
privileges for the new
table.
For example, to rename a table named old_table
to new_table
, use this statement:
That statement is equivalent to the following
ALTER TABLE
statement:
RENAME TABLE
, unlike ALTER
TABLE
, can rename multiple tables within a single
statement:
Renaming operations are performed left to right. Thus, to swap two
table names, do this (assuming that a table with the intermediary
name tmp_table
does not already exist):
Metadata locks on tables are acquired in name order, which in some cases can make a difference in operation outcome when multiple transactions execute concurrently. See Section 8.11.4, “Metadata Locking”.
As of MySQL 8.0.13, you can rename tables locked with a
LOCK TABLES
statement, provided
that they are locked with a WRITE
lock or are
the product of renaming WRITE
-locked tables
from earlier steps in a multiple-table rename operation. For
example, this is permitted:
This is not permitted:
Prior to MySQL 8.0.13, to execute RENAME TABLE
,
there must be no tables locked with LOCK
TABLES
.
With the transaction table locking conditions satisfied, the rename operation is done atomically; no other session can access any of the tables while the rename is in progress.
If any errors occur during a RENAME TABLE
, the
statement fails and no changes are made.
You can use RENAME TABLE
to move a table from
one database to another:
Using this method to move all tables from one database to a different one in effect renames the database (an operation for which MySQL has no single statement), except that the original database continues to exist, albeit with no tables.
Like RENAME TABLE
, ALTER TABLE ...
RENAME
can also be used to move a table to a different
database. Regardless of the statement used, if the rename
operation would move the table to a database located on a
different file system, the success of the outcome is platform
specific and depends on the underlying operating system calls used
to move table files.
If a table has triggers, attempts to rename the table into a
different database fail with a Trigger in wrong
schema
(ER_TRG_IN_WRONG_SCHEMA
) error.
An unencrypted table can be moved to an encryption-enabled
database and vice versa. However, if the
table_encryption_privilege_check
variable is enabled, the
TABLE_ENCRYPTION_ADMIN
privilege is
required if the table encryption setting differs from the default
database encryption.
To rename TEMPORARY
tables, RENAME
TABLE
does not work. Use ALTER
TABLE
instead.
RENAME TABLE
works for views, except that views
cannot be renamed into a different database.
Any privileges granted specifically for a renamed table or view are not migrated to the new name. They must be changed manually.
RENAME TABLE
changes
internally generated foreign key constraint names and user-defined
foreign key constraint names that begin with the string
“tbl_name
TO
new_tbl_name
tbl_name
_ibfk_” to
reflect the new table name. InnoDB
interprets
foreign key constraint names that begin with the string
“tbl_name
_ibfk_” as
internally generated names.
Foreign key constraint names that point to the renamed table are automatically updated unless there is a conflict, in which case the statement fails with an error. A conflict occurs if the renamed constraint name already exists. In such cases, you must drop and re-create the foreign keys for them to function properly.
RENAME TABLE
changes
internally generated and user-defined tbl_name
TO
new_tbl_name
CHECK
constraint names that begin with the string
“tbl_name
_chk_” to reflect
the new table name. MySQL interprets CHECK
constraint names that begin with the string
“tbl_name
_chk_” as
internally generated names. Example:
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-rename-table.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.