搜尋
首頁資料庫mysql教程MySQL必知必会之15-20_MySQL

bitsCN.com

15.联结表

SQL最强大的功能之一就是能在数据检索查询的执行中联结表。联结是利用SQL的SELECT能执行的最重要的操作。

关系表的设计是要保证把信息分解成多个表,一类数据一个表,各表通过一定的关系互相关联。

外键:外键为某个表的一列,它包含另一个表的主键值,定义了两个表之间的关系。

可伸缩性:能够适应不断增加的工作量而不失败。

15.1 联结

联结是一种机制,用来在一条select语句中关联表,因此称之为联结。使用特殊的语法,可以联结多个表返回一组输出,联结在运行时关联表中正确的行。

15.2 创建联结

SELECT vend_name,prod_name,prod_price FROMvendors,products WHERE vendo.vend_id = products.vend_id ORDER BYvend_name,prod_name;

+-------------+----------------+------------+

| vend_name | prod_name | prod_price |

+-------------+----------------+------------+

| ACME | Bird seed | 10.00 |

| ACME | Carrots | 2.50 |

| ACME | Detonator | 13.00 |

| ACME | Safe | 50.00 |

| ACME | Sling | 4.49 |

| ACME | TNT (1 stick) | 2.50 |

| ACME | TNT (5 sticks) | 10.00 |

| Anvils R Us | .5 ton anvil | 5.99 |

| Anvils R Us | 1 ton anvil | 9.99 |

| Anvils R Us | 2 ton anvil | 14.99 |

| Jet Set | JetPack 1000 | 35.00 |

| Jet Set | JetPack 2000 | 55.00 |

| LT Supplies | Fuses | 3.42 |

| LT Supplies | Oil can | 8.99 |

+-------------+----------------+------------+

14 rows in set (0.05 sec)

应该保证所有联结都有WHERE子句,否则MySQL将返回比想要的数据多得多的数据。在引用的列可能出现二义性时,必须使用完全限定列名(用一个点分隔的表名和列名)。

15.3 内部联结

基于两个表之间相等测试的联结称为等值联结,也叫内部联结。可以使用稍微不同的语法来实现这种类型的联结。

SELECT vend_name,prod_name,prod_price FROMvendors INNER JOIN products ON vendors.vend_id = products.vend_id;

这里两个表之间的关系式FROM子句的组成部分,以INNER JOIN指定。在使用这种语法时,联结条件用特定的ON子句而不是WHERE子句给出。传递给ON的实际条件与传递给WHERE的相同。

15.4联结多个表

SELECT cust_name,cust_contact FROMcustomers,orders,orderitems WHERE customers.cust_id = orders.cust_id ANDorderitems.order_num = orders.order_num AND prod_id = 'TNT2';

+----------------+--------------+

| cust_name | cust_contact |

+----------------+--------------+

| Coyote Inc. | Y Lee |

| Yosemite Place | Y Sam |

+----------------+--------------+

2 rows in set (0.02 sec)

这里实现了14章中子查询的功能。

16.创建高级联结

16.1使用表别名

SELECT cust_name,cust_contact FROM customersAS c,orders AS o,orderitemsAS oi WHERE c.cust_id = o.cust_id AND oi.order_num=o.order_numAND prod_id = 'TNT2';

表别名布局能用于WHERE子句还可以用于SELECT的列表,ORDER BY子句以及语句的其他部分。

16.2使用不同类型的联结

自联结,自燃联结和外部联结

16.2.1 自联结

SELECT p1.prod_id,p1.prod_name FROMproducts AS p1,products AS p2 WHERE p1.vend_id = p2.vend_id AND p2.prod_id ='DTNTR';

16.2.2 自然联结

自然联结排除多次出现,使每个列值返回一次。事实上,我们建立的每个内部联结都是自然联结。

16.2.3外部联结

mysql> SELECTcustomers.cust_id,orders.order_num FROM customers LEFT OUTER JOIN

orders ON customers.cust_id =orders.cust_id;

+---------+-----------+

| cust_id | order_num |

+---------+-----------+

| 10001 | 20005 |

| 10001 | 20009 |

| 10002 | NULL |

| 10003 | 20006 |

| 10004 | 20007 |

| 10005 | 20008 |

+---------+-----------+

6 rows in set (0.00 sec)

与内部联结关联两个表中的行不同的是外部联结还包括没有关联的行。在使用OUTER JOIN时,必须使用RIGHT或LEFT关键字指定包括其所有行的表(RIGHT指出OUTER JOIN右边的表,LEFT指出OUTER JOIN左边的表)。

16.3使用带聚集函数的联结

mysql> SELECTcustomers.cust_name,customers.cust_id,COUNT(orders.order_num) AS N

num_ord FROM customers INNER JOIN orders ONcustomers.cust_id = orders.cust_id G

ROUP BY customers.cust_id;

+----------------+---------+----------+

| cust_name | cust_id | Nnum_ord |

+----------------+---------+----------+

| Coyote Inc. | 10001 | 2 |

| Wascals | 10003 | 1 |

| Yosemite Place | 10004 | 1 |

| E Fudd | 10005 | 1 |

+----------------+---------+----------+

4 rows in set (0.00 sec)

17.组合查询

组合查询也通常称为并(union)或复合查询

有两种基本情况,其中需要使用组合查询:
1)在单个查询中从不同的表返回类似结构的数据

2)对单个表执行多个查询,按单个查询返回数据

17.1创建组合查询

SELECT vend_id,prod_id,prod_price FROM productsWHERE prod_price

将UNION换位UNION ALL可以可以包含不同SELECT子句选中的重复的列

17.2 UNION规则

1)UNION必须由两条或两条以上的SELECT语句组成,语句之间用关键字UNION分隔。

2)UNION中的 每个查询必须包含相同的列、表达式或聚集函数

3)列数据类型可以不完全相同,但必须兼容

17.3对组合查询结果排序

ORDER BY必须出现在最后一条SELECT语句之后

18.全文本搜索

在进行全文本搜索之前,首先应将表中的一列或多列设为FULLTEXT。然后使用Match()和Against()执行全文搜索,其中Match()指定被搜索的列,Against()指定要使用的搜索表达式。

18.1进行全文本搜索

SELECT note_text FROM productnotes WHERE Match(note_text)Against('rabbit');

+-------------------------------------------------------------------------------

----------------------------------------+

| note_text

|

+-------------------------------------------------------------------------------

----------------------------------------+

| Customer complaint: rabbit has been ableto detect trap, food apparently less

effective now. |

| Quantity varies, sold by the sack load.

All guaranteed to be bright and orange, andsuitable for use as rabbit bait. |

+-------------------------------------------------------------------------------

----------------------------------------+

2 rows in set (0.05 sec)

上面输出的两行结果的输出顺序是进过排序后得到的,也就是包含词rabbit作为第3个词的等级比作为第20个词的行高。下面演示全文本搜索如何排序工作。

mysql> SELECT note_text,Match(note_text)Against('rabbit') AS rank FROM productn

otes;

+-------------------------------------------------------------------------------

-----------------------------------------------------------------------------+--

----------------+

| note_text

| r

ank |

+-------------------------------------------------------------------------------

-----------------------------------------------------------------------------+--

----------------+

| Customer complaint:

Sticks not inpidually wrapped, too easyto mistakenly detonate all at once.

Recommend inpidual wrapping. | 0 |

| Can shipped full, refills not available.

Need to order new can if refill needed.

| 0 |

| Safe is combination locked, combinationnot provided with safe.

This is rarely a problem as safes aretypically blown up or dropped by customers

. | 0 |

| Quantity varies, sold by the sack load.

All guaranteed to be bright and orange, andsuitable for use as rabbit bait.

| 1.59055435657501 |

| Included fuses are short and have beenknown to detonate too quickly for some

customers.

Longer fuses are available (item FU1) andshould be recommended. |

0 |

| Matches not included, recommend purchaseof matches or detonator (item DTNTR).

|

0 |

| Please note that no returns will beaccepted if safe opened using explosives.

|

0 |

| Multiple customer returns, anvils failingto drop fast enough or falling backw

ards on purchaser. Recommend that customerconsiders using heavier anvils. |

0 |

| Item is extremely heavy. Designed for dropping,not recommended for use with s

lings, ropes, pulleys, or tightropes. |

0 |

| Customer complaint: rabbit has been ableto detect trap, food apparently less

effective now. | 1

.64080536365509 |

| Shipped unassembled, requires commontools (including oversized hammer).

|

0 |

| Customer complaint:

Circular hole in safe floor can apparentlybe easily cut with handsaw.

| 0 |

| Customer complaint:

Not heavy enough to generate flying starsaround head of victim. If being purcha

sed for dropping, recommend ANV02 or ANV03instead. | 0 |

| Call from inpidual trapped in safeplummeting to the ground, suggests an esc

ape hatch be added.

Comment forwarded to vendor. | 0 |

+-------------------------------------------------------------------------------

-----------------------------------------------------------------------------+--

----------------+

14 rows in set (0.03 sec)

这里,在SELECT而不是WHERE子句中使用Match()和Against()。这使所有行都被返回。Match()和Against()用来建立一个计算列,此列包含全文本搜索计算出的等级值。等级有MySQL根据行中词的数目、唯一词的数目、整个索引中词的总数以及包含该词的行的数目计算出来。

18.2使用查询扩展

查询扩展用来设法房款所返回的全文本搜素结果的范围。在使用查询扩展时,MySQL对数据和索引进行两边扫描来完成搜索:

1) 首先,进行一个基本的全文本搜索,找粗与搜索条件匹配的所有行

2) 其次,MySQL检查这些匹配行并进行选择所有有用的词(我们将会简要地解释MySQL如何判定什么有用,什么无用)

3) 再其次,MySQL再次进行全文本搜索,这次不仅使用原来的条件,而且还使用所有有用的词

mysql> select note_text FROMproductnotes WHERE Match(note_text) Against('anvils

' WITH QUERY EXPANSION);

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

| note_text

|

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

| Multiple customer returns, anvils failingto drop fast enough or falling backw

ards on purchaser. Recommend that customerconsiders using heavier anvils. |

| Customer complaint:

Sticks not inpidually wrapped, too easyto mistakenly detonate all at once.

Recommend inpidual wrapping. |

| Customer complaint:

Not heavy enough to generate flying starsaround head of victim. If being purcha

sed for dropping, recommend ANV02 or ANV03instead. |

| Please note that no returns will beaccepted if safe opened using explosives.

|

| Customer complaint: rabbit has been ableto detect trap, food apparently less

effective now. |

| Customer complaint:

Circular hole in safe floor can apparentlybe easily cut with handsaw.

|

| Matches not included, recommend purchaseof matches or detonator (item DTNTR).

|

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

7 rows in set (0.00 sec)

这里第一行包含词anvils,因此等级最高。第二行与anvils无关,但因为它包含第一行中的两个词(customer和recommend),所以也被检索出来。接下去的几行也会如此。

18.3布尔文本搜索

MySQL支持全文本搜索的另外一种形式,布尔搜索,可以提供如下内容的细节:

1) 要匹配的词

2) 要排斥的词(如果某行包含这种词,则不返回改行,即使它包含要匹配的词)

3) 排列提示(指定某些词比其他次更重要,更重的词等级更高)

4) 表达式分组

5) 另外一些内容

布尔方式不同于一般的全文本搜索的地方在于:即使没有定义FULLTEXT索引,也可以使用它。

布尔操作符与说明如下:

+ 包含,词必须存在

- 排除,词必须不出现

> 包含,而且增加等级值

() 把词组成子表达式

~ 取消一个词的排序值

* 词尾的通配符

“ ” 定义一个短语

SELECT note_text FROM productnotes WHERE Match(note_text)Against('heavy' IN BOOLEAN MODE);//输出结果如下

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

| note_text

|

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

| Item is extremely heavy. Designed fordropping, not recommended for use with s

lings, ropes, pulleys, or tightropes. |

| Customer complaint:

Not heavy enough to generate flying starsaround head of victim. If being purcha

sed for dropping, recommend ANV02 or ANV03instead. |

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

2 rows in set (0.00 sec)

这里使用了关键字IN BOOLEAN MODEN

mysql> SELECT note_text FROMproductnotes WHERE Match(note_text) Against('heavy

-rope*' IN BOOLEAN MODE);

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

| note_text

|

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

| Customer complaint:

Not heavy enough to generate flying starsaround head of victim. If being purcha

sed for dropping, recommend ANV02 or ANV03instead. |

+-------------------------------------------------------------------------------

---------------------------------------------------------------------------+

1 row in set (0.00 sec)

这里-rope*明确地指示MySQL排除包含rope*。

下面举几个例子

SELECT note_text FROM productnotes WHEREMatch(note_text) Against(‘+rabbit +bait’ IN BOOLEAN MODE);//这个搜索匹配包含词rabbit和bait的行

SELECT note_text FROM productnotes WHEREMatch(note_text) Against(‘rabbit bait’ IN BOOLEAN MODE);//没有指定操作符,这个搜索匹配rabbit和bait中的至少一个词的行

SELECT note_text FROM productnotes WHEREMatch(note_text) Against(‘”rabbit +bait”’ IN BOOLEAN MODE);//这个词匹配短语rabbitbait而不是匹配两个词rabbit和bait

SELECT note_text FROM productnotes WHEREMatch(note_text) Against(‘>rabbit

SELECT note_text FROM productnotes WHEREMatch(note_text) Against(‘+safe +(

19.插入数据

19.1插入完整的行

mysql> INSERT INTOcustomers(cust_name,cust_address,cust_city,cust_state,cust_zi

p,cust_country,cust_contact,cust_email)VALUES('Pep E. Lapew','100 main Street',

'LosAngeles','CA','90046','USA','NULL','NULL');

Query OK, 1 row affected (0.06 sec)

19.2插入多个行

mysql> INSERT INTOcustomers(cust_name,cust_address,cust_city,cust_state,cust_zi

p,cust_country) VALUES('Pep E. Lapew','100main Street',

'Los Angeles','CA','90046','USA'),(‘M.Martian’,’42 Galaxy Way’,’New York’,’NY’,’11213’,’USA’);

19.3插入检索出的数据

INSERT INTOcustomers(cust_name,cust_address,cust_city,cust_state,cust_zi

p,cust_country)SELECTcust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_countryFROM custnew;

20.更新和删除数据

20.1跟新数据

两种方式

1)更新表中所有行,不加WHERE

2)更新表中特定行,加WHERE

UPDATE语句由3个部分组成,分别是:

1) 要更新的表

2) 列名和他们的新值

3) 确定要更新的过滤条件

mysql> UPDATE customers SET cust_name ='The Fudds',cust_email = 'elmer@fudd.com

' WHERE cust_id = 10005;

Query OK, 1 row affected (0.08 sec)

Rows matched: 1 Changed: 1 Warnings: 0

20.2删除数据

两种方式:

1) 从表中删除特定的行,不省略WHERE子句

2) 从表中删除所有行,省略WHERE子句

DELETE FROM customers WHERE cust_id = 10006;

DELETE删除的是表的内容而不是表本身

要删除所有行,不要使用DELETE,用TRUNCATETABLE;


==参考MySQL必知必会

bitsCN.com

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
將用戶添加到MySQL:完整的教程將用戶添加到MySQL:完整的教程May 12, 2025 am 12:14 AM

掌握添加MySQL用戶的方法對於數據庫管理員和開發者至關重要,因為它確保數據庫的安全性和訪問控制。 1)使用CREATEUSER命令創建新用戶,2)通過GRANT命令分配權限,3)使用FLUSHPRIVILEGES確保權限生效,4)定期審計和清理用戶賬戶以維護性能和安全。

掌握mySQL字符串數據類型:varchar vs.文本與char掌握mySQL字符串數據類型:varchar vs.文本與charMay 12, 2025 am 12:12 AM

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

MySQL:字符串數據類型和索引:最佳實踐MySQL:字符串數據類型和索引:最佳實踐May 12, 2025 am 12:11 AM

在MySQL中處理字符串數據類型和索引的最佳實踐包括:1)選擇合適的字符串類型,如CHAR用於固定長度,VARCHAR用於可變長度,TEXT用於大文本;2)謹慎索引,避免過度索引,針對常用查詢創建索引;3)使用前綴索引和全文索引優化長字符串搜索;4)定期監控和優化索引,保持索引小巧高效。通過這些方法,可以在讀取和寫入性能之間取得平衡,提升數據庫效率。

mysql:如何遠程添加用戶mysql:如何遠程添加用戶May 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

MySQL字符串數據類型的最終指南:有效的數據存儲MySQL字符串數據類型的最終指南:有效的數據存儲May 12, 2025 am 12:05 AM

tostorestringsefliceflicyInmySql,ChooSetherightDataTypeBasedyOrneOrneEds:1)USEcharforFixed-LengthStstringStringStringSlikeCountryCodes.2)UseVarcharforvariable-lengtthslikenames.3)USETEXTCONTENT.3)

mysql blob vs.文本:為大對象選擇正確的數據類型mysql blob vs.文本:為大對象選擇正確的數據類型May 11, 2025 am 12:13 AM

選擇MySQL的BLOB和TEXT數據類型時,BLOB適合存儲二進制數據,TEXT適合存儲文本數據。 1)BLOB適用於圖片、音頻等二進制數據,2)TEXT適用於文章、評論等文本數據,選擇時需考慮數據性質和性能優化。

MySQL:我應該將root用戶用於產品嗎?MySQL:我應該將root用戶用於產品嗎?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL字符串數據類型說明了:選擇適合您數據的合適類型MySQL字符串數據類型說明了:選擇適合您數據的合適類型May 11, 2025 am 12:10 AM

mySqlStringDatatAtatPessHouldBechoseBasedondatActarActeristicsAndusecases:1)USEcharforFixed lengthStstringStringStringSlikeCountryCodes.2)usevarcharforvariable-lengtthslikeLikenames.3)usebarnionororvarinyorvarinyorvarybinarydatalgebenedaTalgeextocrabextrapon.4)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版

SublimeText3 英文版

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

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具