CREATE TABLE
Syntax
1044
ALTER TABLE
tbl_name
DROP FOREIGN KEY
fk_symbol
;
If the
FOREIGN KEY
clause included a
CONSTRAINT
name when you created the foreign key, you can
refer to that name to drop the foreign key. Otherwise, the
fk_symbol
value is generated internally
when the foreign key is created. To find out the symbol value when you want to drop a foreign key, use
a
SHOW CREATE TABLE
statement, as shown here:
mysql>
SHOW CREATE TABLE ibtest11c\G
*************************** 1. row ***************************
Table: ibtest11c
Create Table: CREATE TABLE `ibtest11c` (
`A` int(11) NOT NULL auto_increment,
`D` int(11) NOT NULL default '0',
`B` varchar(200) NOT NULL default '',
`C` varchar(175) default NULL,
PRIMARY KEY (`A`,`D`,`B`),
KEY `B` (`B`,`C`),
KEY `C` (`C`),
CONSTRAINT `0_38775` FOREIGN KEY (`A`, `D`)
REFERENCES `ibtest11a` (`A`, `D`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `0_38776` FOREIGN KEY (`B`, `C`)
REFERENCES `ibtest11a` (`B`, `C`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB CHARSET=latin1
1 row in set (0.01 sec)
mysql>
ALTER TABLE ibtest11c DROP FOREIGN KEY `0_38775`;
You cannot add a foreign key and drop a foreign key in the same
ALTER TABLE
statement. A separate
statements is required for each operation.
If an
ALTER TABLE
statement results in changes to column values (for example, because a column
is truncated), MySQL's foreign key constraint checks do not notice possible violations caused by
changing the values.
Foreign Keys and Other MySQL Statements
Table and column identifiers in a
FOREIGN KEY ... REFERENCES ...
clause can be quoted within
backticks (
`
). Alternatively, double quotation marks (
"
) can be used if the
ANSI_QUOTES
[535]
SQL
mode is enabled. The setting of the
lower_case_table_names
[466]
system variable is also taken
into account.
You can view a child table's foreign key definitions as part of the output of the
SHOW CREATE TABLE
statement:
SHOW CREATE TABLE
tbl_name
;
You can also obtain information about foreign keys by querying the
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
table.
mysqldump
produces correct definitions of tables in the dump file, including the foreign keys for child
tables.
To make it easier to reload dump files for tables that have foreign key relationships,
mysqldump
automatically includes a statement in the dump output to set
foreign_key_checks
[451]
to 0. This
avoids problems with tables having to be reloaded in a particular order when the dump is reloaded. It is
also possible to set this variable manually:
mysql>
SET foreign_key_checks = 0;
mysql>
SOURCE dump_file_name;
mysql>
SET foreign_key_checks = 1;
This enables you to import the tables in any order if the dump file contains tables that are not correctly
ordered for foreign keys. It also speeds up the import operation. Setting
foreign_key_checks
[451]
to 0 can also be useful for ignoring foreign key constraints during
LOAD DATA
and
ALTER TABLE
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...