The basic operation of the data in the seventh part of the table

1. Full text search

Introduction: Although the LIKE keyword can use wildcards to match text, regular expressions can match column values, and thus search for the information we need. But they have several important limitations. One is performance. Wildcard and regular expression matching usually require MySQL to try to match all rows in the table. If the number of search rows continues to increase, these searches may be very time-consuming. Second, clear control. Using wildcards and regular expression matching, it is difficult to explicitly control what is matched and what is not matched. For example, specifying a word must match, a word must not match, and a word can only match if the first word does match Or it may not match. Third, the result of intelligence. Although searching based on wildcards and regular expressions provides a very flexible search, neither of them can provide an intelligent way to select results. For example, a search for a particular word will return all lines that contain the word, without distinguishing between lines that contain a single match and lines that contain multiple matches. Similarly, a search for a particular word will not find lines that do not contain the word but contain other related words. Because of this, full text search can solve these limitations. When using full text search, MySQL does not need to view each row separately, and does not need to analyze and process each word separately. In this way, MySQL can quickly and effectively decide which words match, which words do not match, how often they match, and so on. In order to perform a full-text search, the searched column must be indexed, and it must be re-indexed continuously as the data changes.

1. Enable full-text search : generally enable full-text search when creating a table. After indexing, use two functions Match () and Against () to perform full-text search, where Match () specifies the searched column, Against () Specify the search expression to use.

Example 1: Create a table productnotes and list the columns it contains, and index the note_text column to perform a full text search.

SQL statement:

CREATE TABLE productnotes

(

note_id int NOT NULL AUTO_INCREMENT,

prod_id char(10) NOT NULL,

note_data datatime NOT NULL,

note_test text NULL,

PRIMARY KEY(note_id),

FULLTEXT(note_text)

)ENGINE=MyISAM;

Example 2: Retrieve the specified column note_text, and specify the word rabbit as the search text, and return the row containing rabbit.

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against('rabbit');

SQL语句:SELECT note_text FROM productnotes WHERE note_text LIKE '%rabbit%';

2. Query expansion: Query expansion is used to try to relax the range of full-text search results returned.

Example: Find all comments that mention anvils, and other lines that may be relevant to your search, even if they do not contain the word anvils.

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against('anvils' WITH QUERY EXPANSION) ;

3. Boolean text search: Provide full text query about the details, even if the FULLTEXT index is not defined, it can also be used.

Example 1: Full text search for all lines containing the word heavy

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against('heavy' IN BOOLEAN MODE);

Example 2: Matches lines that contain heavy but do not contain any words beginning with rope

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against('heavy - rope*' IN BOOLEAN MODE);

 

Example 3 : Search for lines that contain rabbit and bait.

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against('+rabbit +bait' IN BOOLEAN MODE);

Example 4 : Without specifying an operator, search for lines that match at least one of rabbit and bait

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against('rabbit bait' IN BOOLEAN MODE);

Example 5: Search for the phrase rabbit bait instead of matching the two words rabbit and bait.

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against(' "rabbit bait" ' IN BOOLEAN MODE);

Example 6: Match rabbit and carrot, increase the level of the former, and decrease the level of the latter.

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against('>rabbit <carrot' IN BOOLEAN MODE);

示例7:搜索匹配词safe和combination,降低后者的等级。

SQL语句:SELECT note_text FROM productnotes WHERE Match(note_text) Against('+safe +(<combination)' IN BOOLEAN MODE);

二、插入数据

使用INSERT语句插入数据到数据库表中。插入可以用以下几种方式

  • 插入完整的行;
  • 插入行的一部分;
  • 插入多行;
  • 插入某些查询的结果。

1.插入完整的行

示例:插入一个新客户到customers表。

SQL语句:INSERT INTO customers VALUES(NULL,'pe','199 main','Los');

SQL语句:INSERT INTO customers(cust_name,cust_addr,cust_city,cust_nn) VALUES(NULL,'pe','199 main','Los');

2.插入多行

示例:插入多行信息到customers表。

SQL语句:INSERT INTO customers(cust_name,cust_addr,cust_city,cust_nn) VALUES(NULL,'pe','199 main','Los'),(NULL,'OO','19 main','Ls');

3.插入检索出的数据

示例:将表custnew的信息合并到customers表。

SQL语句:INSERT INTO customers(cust_id,cust_contact,cust_email,cust_name,cust_addr,cust_city) SELECT cust_id,cust_contact,cust_email,cust_name,cust_addr,cust_city FROM custnew;

三、更新和删除数据

使用UPDATE语句更新或者修改表中的数据,DELETE语句删除表中的数据。其中,更新操作包括对表中特定的行更新以及更新表中所有行,在更新表中特定行时,不可省略WHERE子句。

1.更新操作

UPDATE操作包括三部分,即要更新的表、列名和它们的新值、以及确定要更新行的过滤条件。

示例1:更新客户1005的信息

SQL语句:UPDATE customers SET cust_email = '[email protected]' WHERE cust_id = 1005;

SQL语句:UPDATE customers SET cust_email='[email protected]', cust_name = 'zhang san' WHERE cust_id = 1005;

示例2:删除cust_email列的值,并将它设置为NULL

SQL语句:UPDATE customers SET cust_email = NULL WHERE cust_id = 1005;

2.删除操作

删除操作包括两种方式,即从表中删除特定的行和删除所有行。

示例1:删除表customers中的一行

SQL语句:DELETE FROM customers WHERE cust_id = 1006;

示例2:删除表customers中的所有行

SQL语句:DELETE FROM customers;

SQL语句:TRUNCATE TABLE customers;

DELETE和TRUNCATE区别:

参考博客https://www.cnblogs.com/zhizhao/p/7825469.html

注意事项:

使用UPDATE或DELETE时需要遵循的习惯

  • 除非确实打算更新和删除每一行,否则绝对不要使用不带WHERE子句的UPDATE或DELETE语句;
  • 保证每个表都有主键,尽可能像WHERE子句那样使用它,可以指定各主键、多个值或值的范围;
  • 在对UPDATE或DELETE语句使用WHERE子句前,应该先用SELECT进行测试,保证它过滤的是正确的记录,以防编写的WHERE子句不正确;
  • 使用强制实施引用完整性的数据库,这样MySQL将不允许删除具有与其它表相关联的数据的行。

Guess you like

Origin www.cnblogs.com/wzw0625/p/12683111.html