当前位置:网站首页>Multi table operation - one to one, one to many and many to many
Multi table operation - one to one, one to many and many to many
2022-07-01 23:44:00 【Soup key TJ】
Catalog
Demonstration of practical operation
one-on-one
Multi table introduction
- Multi table concept
- To put it bluntly, multiple data tables , There can be some association between tables , This association is achieved through foreign key constraints
- Classification of multiple tables
- one-on-one
- One to many
- Many to many
one-on-one
- Applicable scenario
- People and ID cards ;
- A person has only one ID card , An ID card can only correspond to one person
- Principle of building table
- Create a foreign key in any table , To associate the primary key of another table
Example operation
-- establish person surface CREATE TABLE person( id INT PRIMARY KEY auto_increment, -- Primary key id name VARCHAR(20) -- full name ); -- Add data INSERT INTO person VALUES (NULL,' Zhang San '),(NULL,' Li Si '); -- establish card surface CREATE TABLE card( id INT PRIMARY KEY auto_increment, -- Primary key id number VARCHAR(20) UNIQUE NOT NULL, -- ID number pid INT UNIQUE, -- Foreign key column CONSTRAINT ou FOREIGN KEY (pid) REFERENCES person(id) ); -- Add data INSERT INTO card VALUES (NULL,'12345',1),(NULL,'56789',2);
One to many
Introduce
- Applicable scenario
- Such as :
- Users and orders ;
- A user can have multiple orders
- Commodity classification and commodity ;
- There can be multiple products under one category
- Principle of building table
- On the more side , Create foreign key constraints
- To associate a party's primary key
Example operation
- The user is satisfied with the order
-- establish user surface CREATE TABLE user( id INT PRIMARY KEY auto_increment, -- Primary key id name VARCHAR(20) -- full name ); -- Add data INSERT INTO user VALUES (NULL,' Zhang San '),(NULL,' Li Si '); -- establish orderlist surface CREATE TABLE orderlist( id INT PRIMARY KEY auto_increment, -- Primary key id number VARCHAR(20), -- The order no. uid INT, -- Foreign key column CONSTRAINT ou FOREIGN KEY (uid) REFERENCES user(id) ); -- Add data INSERT INTO orderlist VALUES (NULL,'hm001',1),(NULL,'hm002',1),(NULL,'hm003',2),(NULL,'hm004',2);
- Commodity classification vs. commodity
-- establish category surface CREATE TABLE category( id INT PRIMARY KEY auto_increment, -- Primary key id name VARCHAR(20) -- Category name ); -- Add data INSERT INTO category VALUES (NULL,' Mobile phone digital '),(NULL,' Computer office '); -- establish product surface CREATE TABLE product( id INT PRIMARY KEY auto_increment, -- Primary key id number VARCHAR(30), -- Name of commodity uid INT, -- Foreign key column CONSTRAINT ou FOREIGN KEY (uid) REFERENCES category(id) ); -- Add data INSERT INTO product VALUES (NULL,'Sony xperia 1',1),(NULL,'Google pixel 6 pro',1),(NULL,'ROG Mob ',2),(NULL,'MacBook pro',2);
Many to many
Introduce
- Applicable scenario
- Such as :
- Students and courses
- A student can choose multiple courses
- A course can also be chosen by multiple students
- Principle of building table
- You need to use the third middle table , The middle table contains at least two columns
- These two columns act as foreign keys in the middle table , Associate the primary keys of the other two tables respectively
Demonstration of practical operation
-- establish student class CREATE TABLE student( id INT PRIMARY KEY auto_increment, -- Primary key id name VARCHAR(20) -- The student's name ); -- Add data INSERT INTO student VALUES (NULL,' Zhang San '),(NULL,' Li Si '); -- establish course surface CREATE TABLE course( id INT PRIMARY KEY auto_increment, -- Primary key id name VARCHAR(20) -- Course name ); -- Add data INSERT INTO course VALUES (NULL,' Chinese language and literature '),(NULL,' mathematics '); -- Create middle table CREATE TABLE stu_course( id INT PRIMARY KEY auto_increment, -- Primary key id sid INT, -- Used for and student In the table id Make foreign key constraints cid INT, -- Used for and course In the table id Make foreign key constraints CONSTRAINT sc_1 FOREIGN KEY (sid) REFERENCES student(id), -- Add a foreign key constraint CONSTRAINT sc_2 FOREIGN KEY (cid) REFERENCES course(id) -- Add a foreign key constraint ); -- Add data INSERT INTO stu_course VALUES (NULL,1,1),(NULL,1,2),(NULL,2,1),(NULL,2,2);
边栏推荐
- Practical application and extension of plain framework
- ADO. Net SqlDataAdapter object
- Matplotlib common settings
- Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
- Redis 主从同步
- Which securities company is the best to open a stock account? Is there a security guarantee
- 第六章 数据流建模
- from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- Is there a piece of code that makes you convinced by human wisdom
猜你喜欢
随机推荐
安全协议重点
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
Development trend and future direction of neural network Internet of things
Redis RDB snapshot
Daily three questions 6.28
Postgresql源码(58)元组拼接heap_form_tuple剖析
神经网络物联网的未来趋势与发展
物联网开发零基础教程
ShanDong Multi-University Training #3
2021 robocom world robot developer competition - preliminary competition of higher vocational group
Zero foundation tutorial of Internet of things development
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
[es practice] safe operation mode on ES
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
What professional classification does the application of Internet of things technology belong to
【必会】BM41 输出二叉树的右视图【中等+】
2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
Linux基础 —— CentOS7 离线安装 MySQL
SQL optimization
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板