Home >Backend Development >PHP Tutorial >Database foreign keys, database_PHP tutorial

Database foreign keys, database_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:55:321080browse

Database foreign key, database

Foreign key

Created by:

Method 1: Use foreign key (field name) references table name (primary key) when creating the table;

Method 2: Modify the table structure add foreign key (field name) references table name (primary key);

Delete:

alter table table name drop foreign key foreign key name;

If you want to view the foreign key name: show create table table name;

Foreign key constraints:

A certain field in table B is the primary key field of table A.

Table A is the master table and table B is the slave table:

The master table is constrained by the slave table.

1. Table A itself cannot be deleted at will

2. Changes to the records in table A cannot be deleted or updated at will, and there are certain restrictions behind the words

Leave the constraint blank

When a record in the main table is deleted, the foreign key field call in the record in the slave table is null

Syntax:

on delete set null

Cascading Constraints

When the records in the main table are updated, the foreign key fields in the records in the slave table are updated synchronously.

The slave table is constrained by the master table

1. Data from the slave table: The value of the foreign key field in the slave table must be a value in the primary key field in the master table.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1116657.htmlTechArticleDatabase foreign key, database foreign key creation: Method 1: Use foreign key (field name) references when creating a table Table name (primary key); Method 2: Modify the table structure add foreign key (field name...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn