MySQL foreign key insert_mysql

Source: Internet
Author: User
Add a foreign key for the added Data Table: syntax: altertable table name addconstraintFK_IDforeignkey (your foreign key field name) REFERENCES external table name (corresponding table's primary key field name); example: altertabletb_activeaddconstraint:

Syntax: alter table name add constraint FK_ID foreign key (your foreign key field name) REFERENCES external table name (corresponding table's primary key field name );

Example: alter table tb_active add constraint FK_ID foreign key (user_id) REFERENCES tb_user (id)

// FK_ID is the foreign key name/* create table 'TB _ activity' ('id' int (11) not null AUTO_INCREMENT, 'title' varchar (100) character set utf8 COLLATE utf8_unicode_ci not null, 'content' text character set utf8 COLLATE utf8_unicode_ci not null, 'User _ id' int (11) not null, primary key ('id '), KEY 'user _ id' ('User _ id'), KEY 'user _ id_2 '('User _ id '), CONSTRAINT 'fk _ id' foreign key ('User _ id') REFERENCES 'TB _ user' ('id') ENGINE = InnoDB default charset = latin1 */

Delete foreign key

Syntax: alter table table-name drop foreign key key-id;

For example, alter table 'TB _ active' drop foreign key 'fk _ id'

Automatic key update and deletion:

The foreign key can ensure the integrity of the newly inserted record. However, what if the deleted record is named in the REFERENCES clause? What happens in a secondary table that uses the same value as a foreign key?

Obviously, those records should also be deleted, otherwise there will be a lot of meaningless isolated records in the database, MYSQL can forward to foreign key... the REFERENCES modifier adds an on delete or on update clause to simplify the task. it tells the database how to handle isolated tasks in this case.

Keyword meaning

CASCADE deletes all records that have reference relationships with deleted key values.

Set null: modify all records that have reference relationships with the deleted key value and replace them with NULL values (only for fields marked as not null)

RESTRICT rejects the deletion request until the secondary table that uses the deleted key value is manually deleted without reference (this is the default setting and the safest setting)

No action, NO ACTION

Note that when you set MYSQL to perform automatic operations through the on update and on delete rules, if the key relationship is not set properly, it may cause serious data damage,

For example, if a series of tables are connected through a foreign key relationship and the on delete cascade rule, any changes to the primary table will lead to the deletion of some records that will be associated with the original deletion without warning. Therefore, we need to check these rules before the operation, check again after the operation.

Add foreign key

Alter table locstock add foreign key locstock_ibfk2 (stockid) references product (stockid)

Locstock is the table name, locstock_ibfk2 is the name of the foreign key column in the first brackets of the foreign key name, product is the table name, and the second column is the name of the column associated with the foreign key.

Delete foreign key

Alter table locstock drop foreign key locstock_ibfk2

View the table's foreign keys

Show create table locstock

[CONSTRAINT symbol] foreign key [id] (index_col_name ,...)

REFERENCES tbl_name (index_col_name ,...)

[On delete {RESTRICT | CASCADE | set null | no action}]

[On update {RESTRICT | CASCADE | set null | no action}]

All tables must be of the InnoDB type and cannot be temporary tables.

In the referenced table, an index is required, and the foreign key column is listed in the same order as the first column. If such an index does not exist, it must be automatically created in the reference table.

In the referenced table, there must be an index. the referenced columns are listed in the same order as the first column.

The index prefix of the foreign key column is not supported. One of the consequences is that BLOB and TEXT columns are not included in an external key, because the index of these columns must always contain a prefix length.

If CONSTRAINTsymbol is given, it must be unique in the database. If it is not given, InnoDB automatically creates this name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.