1 relational database
-
- between the table and the table, the relationship can be established; called primary foreign key;
-
Primary key: uniquely identifies a record, can not be repeated, is not allowed to empty, is used to ensure data integrity
1: int type, self-energizing
2: varchar type, the UUID
. 3: Custom (current time stamp + + Encoding + int increment sequence); for distributed systems -
Foreign key: another table is the primary key, foreign key can be repeated, it can be a null value, and to establish contact with the other tables. So, if it comes to a foreign key, it must be to involve at least two tables.
-
Using the foreign key needs to satisfy the following conditions:
- Two tables must be InnoDB tables are, and they are not temporary tables.
Note: InnoDB database engine is. There are two common MySQL engine: InnoDB and MyISAM, which does not support foreign keys.
-
Establishing a corresponding relationship between foreign key columns must have similar InnoDB internal data types.
-
Establish a foreign key relationship must be established in the corresponding column of the index.
-
If the explicitly given CONSTRAINT symbol, that symbol must be unique within the database. If not explicitly given, InnoDB will automatically be created.
For example:
books and table books category table / student and class table / table departments and employees / users and borrow books and relationship
The relationship between 2 tables
2.1 one (one corresponding to the ID card)
1: A table primary key, foreign key table B;
2: a field of Table A, Table B is a foreign key, the cut is provided a unique index;
2.2-to-many (a plurality of books corresponding to books category)
1: a field of Table A, Table B is a foreign key;
2.3-many (teacher and class)
1: General described many to many relationship between two tables by a third party table