這篇文章帶給大家的內容是介紹MySQL表格與表格之間有什麼關係?表與表的多種關係。有一定的參考價值,有需要的朋友可以參考一下,希望對你們有幫助。
表與表之間的關係
表1 foreign key 表2 则表1的多条记录对应表2的一条记录,即多对一 利用foreign key的原理我们可以制作两张表的多对多,一对一关系多对多: 表1的多条记录可以对应表2的一条记录 表2的多条记录也可以对应表1的一条记录 一对一: 表1的一条记录唯一对应表2的一条记录,反之亦然 分析时,我们先从按照上面的基本原理去套,然后再翻译成真实的意义,就很好理解了
1、先確定關係
2、找到多的一方,把關聯欄位寫在多的一方
一對多
# 多對一或一對多(左邊表的多筆記錄對應右邊表格的唯一一筆記錄)
需要注意的:
#1.先建被關聯的表,保證被關聯表的欄位必須唯一。
2.在建立關聯表,關聯欄位一定保證是要有重複的。
範例:
這是一個書和出版社的一個例子,書要關聯出版社(多個書可以是一個出版社,一個出版社也可以有很多書)。
誰關聯誰是誰要依照誰的標準。
建立表格
书要关联出版社 被关联的表 create table press(id int primary key auto_increment, name char(20)); 关联的表 create table book( book_id int primary key auto_increment, book_name varchar(20), book_price int, press_id int, constraint Fk_pressid_id foreign key(press_id) references press(id) on delete cascade on update cascade );
- ##插入資料
insert into press(name) values('新华出版社'), ('海燕出版社'), ('摆渡出版社'), ('大众出版社'); insert into book(book_name,book_price,press_id) values('Python爬虫',100,1), ('Linux',80,1), ('操作系统',70,2), ('数学',50,2), ('英语',103,3), ('网页设计',22,3);
- 執行結果
一對一
範例一:
使用者和管理員(只有管理員才可以登錄,一個管理員對應一個使用者)#管理員關聯使用者- 建立表格 #
先建被关联的表 create table user( id int primary key auto_increment, #主键自增name char(10) ); 再建关联表 create table admin( id int primary key auto_increment, user_id int unique, password varchar(16), foreign key(user_id) references user(id) on delete cascade on update cascade );
- 插入資料
insert into user(name) values('susan1'),('susan2'),('susan3'),('susan4')('susan5'),('susan6'); insert into admin(user_id,password) values(4,'sds156'),(2,'531561'),(6,'f3swe');
- 運行結果
範例二:
學生表與客戶表- #建立表格
create table customer( id int primary key auto_increment, name varchar(10), qq int unique, phone int unique ); create table student1( sid int primary key auto_increment, course char(20), class_time time, cid int unique, foreign key(cid) references customer(id) on delete cascade on update cascade );
- 插入資料
insert into customer(name,qq,phone) values('小小',13564521,11111111),('嘻哈',14758254,22222222),('王维',44545522,33333333),('胡军',545875212,4444444),('李希',145578543,5555555),('李迪',754254653,8888888),('艾哈',74545145,8712547),('啧啧',11147752,7777777); insert into student1(course,class_time,cid) values('python','08:30:00',3),('python','08:30:00',4),('linux','08:30:00',1),('linux','08:30:00',7);
- # 執行結果
##多對多
書和作者(我們可以再建立一張表,用來存book和author兩張表的關係)
要把book_id和author_id設定成聯合唯一
聯合唯一:unique(book_id,author_id) 聯合主鍵:alter table t1 add primary key(id,avg)多對多:一個作者可以寫多本書,一本書也可以有多個作者,雙向的一對多,即多對
關聯方式:foreign key 一張新的表格
#範例:
建立表格
========书和作者,另外在建一张表来存书和作者的关系 #被关联的 create table book1( id int primary key auto_increment, name varchar(10), price float(3,2) ); #========被关联的 create table author( id int primary key auto_increment, name char(5) ); #========关联的 create table author2book( id int primary key auto_increment, book_id int not null, author_id int not null, unique(book_id,author_id), foreign key(book_id) references book1(id) on delete cascade on update cascade, foreign key(author_id) references author(id) on delete cascade on update cascade );
插入資料
- #
insert into book1(name,price) values('九阳神功',9.9), ('葵花宝典',9.5), ('辟邪剑谱',5), ('降龙十巴掌',7.3); insert into author(name) values('egon'),('e1'),('e2'),('e3'),('e4'); insert into author2book(book_id,author_id) values(1,1),(1,4),(2,1),(2,5),(3,2),(3,3),(3,4),(4,5);
多對多關係範例
使用者表,使用者群組,主機表
建立三張表
-- 用户表 create table user (id int primary key auto_increment,username varchar(20) not null,password varchar(50) not null); insert into user(username,password) values('egon','123'),('root',147),('alex',123),('haiyan',123),('yan',123); -- 用户组表 create table usergroup(id int primary key auto_increment,groupname varchar(20) not null unique); insert into usergroup(groupname) values('IT'),('Sale'),('Finance'),('boss'); -- 主机表 CREATE TABLE host(id int primary key auto_increment,ip CHAR(15) not NULL UNIQUE DEFAULT '127.0.0.1'); insert into host(ip) values('172.16.45.2'),('172.16.31.10'),('172.16.45.3'),('172.16.31.11'),('172.10.45.3'), ('172.10.45.4'),('172.10.45.5'),('192.168.1.20'),('192.168.1.21'),('192.168.1.22'),('192.168.2.23'),('192.168.2.223'), ('192.168.2.24'),('192.168.3.22'),('192.168.3.23'),('192.168.3.24');#########建立關係######## #
-- 建立user和usergroup的关系表 create table user2usergroup( id int not NULL UNIQUE auto_increment, user_id int not null, group_id int not NULL, PRIMARY KEY(user_id,group_id), foreign key(user_id) references user(id) ON DELETE CASCADE on UPDATE CASCADE , foreign key(group_id) references usergroup(id) ON DELETE CASCADE on UPDATE CASCADE ); insert into user2usergroup(user_id,group_id) values(1,1),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4); -- 建立user和host的关系 create table user2host( id int not null unique auto_increment, user_id int not null, host_id int not null, primary key(user_id,host_id), foreign key(user_id) references user(id), foreign key(host_id) references host(id) ); insert into user2host(user_id,host_id) values(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9),(1,10),(1,11),(1,12),(1,13),(1,14),(1,15),(1,16),(2,2),(2,3),(2,4),(2,5),(3,10),(3,11),(3,12);
以上是MySQL表與表之間有什麼關係?表與表的多種關係的詳細內容。更多資訊請關注PHP中文網其他相關文章!

MySQL和SQLite的主要區別在於設計理念和使用場景:1.MySQL適用於大型應用和企業級解決方案,支持高性能和高並發;2.SQLite適合移動應用和桌面軟件,輕量級且易於嵌入。

MySQL中的索引是數據庫表中一列或多列的有序結構,用於加速數據檢索。 1)索引通過減少掃描數據量提升查詢速度。 2)B-Tree索引利用平衡樹結構,適合範圍查詢和排序。 3)創建索引使用CREATEINDEX語句,如CREATEINDEXidx_customer_idONorders(customer_id)。 4)複合索引可優化多列查詢,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。 5)使用EXPLAIN分析查詢計劃,避

在MySQL中使用事務可以確保數據一致性。 1)通過STARTTRANSACTION開始事務,執行SQL操作後用COMMIT提交或ROLLBACK回滾。 2)使用SAVEPOINT可以設置保存點,允許部分回滾。 3)性能優化建議包括縮短事務時間、避免大規模查詢和合理使用隔離級別。

選擇PostgreSQL而非MySQL的場景包括:1)需要復雜查詢和高級SQL功能,2)要求嚴格的數據完整性和ACID遵從性,3)需要高級空間功能,4)處理大數據集時需要高性能。 PostgreSQL在這些方面表現出色,適合需要復雜數據處理和高數據完整性的項目。

MySQL數據庫的安全可以通過以下措施實現:1.用戶權限管理:通過CREATEUSER和GRANT命令嚴格控制訪問權限。 2.加密傳輸:配置SSL/TLS確保數據傳輸安全。 3.數據庫備份和恢復:使用mysqldump或mysqlpump定期備份數據。 4.高級安全策略:使用防火牆限制訪問,並啟用審計日誌記錄操作。 5.性能優化與最佳實踐:通過索引和查詢優化以及定期維護兼顧安全和性能。

如何有效監控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。 1.使用mysqladmin查看連接數。 2.用SHOWGLOBALSTATUS查看查詢數。 3.PMM提供詳細性能數據和圖形化界面。 4.MySQLEnterpriseMonitor提供豐富的監控功能和報警機制。

MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显著差异,选择时需考虑项目规模和未来扩展性。

在需要高可用性、高級安全性和良好集成性的企業級應用場景下,應選擇SQLServer而不是MySQL。 1)SQLServer提供企業級功能,如高可用性和高級安全性。 2)它與微軟生態系統如VisualStudio和PowerBI緊密集成。 3)SQLServer在性能優化方面表現出色,支持內存優化表和列存儲索引。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),