1. Foreign key setting method1. In MySQL, in order to associate two tables, two important functions are used: foreign key (FOREIGN KEY) and connection (JOIN). Foreign keys need to be defined when creating a table. Joins can connect two tables through fields with the same meaning and are used in the query stage. 2. Suppose there are two tables, Table A and Table B, which are associated through a common field id. We call this association R. If id is the primary key in table A, then table A is the primary table in this relationship R. Correspondingly, table B is the secondary table in this relationship. The id in table B is what table B uses to reference the data in table A, which is called a foreign key. So, the foreign key is the common field in the slave table that is used to reference data in the master table. Create the main table CREATE TABLE demo.importhead ( listnumber INT PRIMARY KEY, supplierid INT, stocknumber INT, importtype INT, importquantity DECIMAL(10 , 3 ), importvalue DECIMAL(10 , 2 ), recorder INT, recordingdate DATETIME); Create a secondary table CREATE TABLE demo.importdetails( listnumber INT, itemnumber INT, quantity DECIMAL(10,3), importprice DECIMAL(10,2), importvalue DECIMAL(10,2), -- Define foreign key constraints, specify the foreign key field and the referenced primary table field CONSTRAINT fk_importdetails_importhead FOREIGN KEY (listnumber) REFERENCES importhead (listnumber) ); By running this SQL statement, we define a foreign key constraint named fk_importdetails_importhead while creating the table. At the same time, we declare that the field listnumber of this foreign key constraint refers to the field listnumber in the table importhead. After the creation is complete, we can view it through SQL statements. Here we need to use the MySQL built-in database for storing system information: information_schema. We can view relevant information about foreign key constraints: The table where the foreign key constraint is located is importdetails, and the foreign key field is listnumber The referenced main table is importhead, and the referenced main table field is listnumber. In this way, by defining the foreign key constraint, we have established an association relationship between the two tables. 3. Connect There are two types of joins in MySQL, INNER JOIN and OUTER JOIN.
When defining foreign keys, you need to follow the following rules:
SummarizeThis is the end of this article about MySQL foreign key settings. For more relevant MySQL foreign key settings, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Pure CSS meteor shower background sample code
>>: Future-oriented all-round web design: progressive enhancement
Using fonts on the Web is both a fundamental skill...
The equal height layout described in this article...
I won't say much nonsense, let's just loo...
Table of contents 1. Props Parent >>> Ch...
1. Click Terminal below in IDEA and enter mvn cle...
This article uses examples to illustrate the func...
Table of contents Preface difficulty Cross-domain...
Table of contents Preface Global parameter persis...
A web designer's head must be filled with a lo...
Table of contents Preface Introduction to Dockerf...
Project scenario: Dark Horse Vue project manageme...
Problem Peeping In the server, assuming that the ...
The JD carousel was implemented using pure HTML a...
There is a big difference between the writing ord...
This article mainly introduces how to call desktop...