MySQL based learning 6

Subqueries

Sub-query is a query in other SQL (add, delete, change, check) in the nest


The subquery returns a single single

The query returns a single separate sub apparent, that is a single separate value that can be used when using the filter value among, but have parentheses.

Example:
the query "Computer Engineering" What professional.

SELECT
	MajorName
FROM
	major
WHERE
	DepartmentID = (SELECT
	DepartmentID 
FROM 
	department
WHERE
	DepartmentName = '计算机工程系');

In the whereinside, we DepartmentIDjudge equal, not a direct value, but with departmenta value of screening out, thus again screened. This is what we call sub-query returns a single separate

which

(SELECT
	DepartmentID 
FROM 
	department
WHERE
	DepartmentName = '计算机工程系')

Here, in line with DepartmentName = '计算机工程系'screened, out of a screening of a value , then for wherescreening.

Sub-query returns multiple records

Sub-query returns multiple records, and sub-query returns a single column on a single usage is essentially the same, they are selected data, and then for use.

Example:
Query CourseID10101, ExamScore 98 points student student information.

SELECT *
FROM
	student
WHERE
	StuID = 
	(SELECT 
		StuID 
	FROM 
		score
	WHERE 
		ExamScore = 98 AND 
		CourseID = 10101);

** We want to find among the data subject, but the figure is clearly not the two tables, but also need to filter the data table you want to get, so we used the sub-query returns multiple records , the above code, back braces code inside, is screened StuID, so then screened studen table. CourseID10101, ExamScore 98 into screening out the data, so in the table, we were ExamScore = 98 AND CourseID = 10101.
In sub-query returns multiple records , we will use the following operators: IN, ANY | SOME, ALL

  • in: the representative of the collection, which is present in this collection.
  • any: <any value represents one less than the inside, <any representative of a value greater than the inside, = any, and the same effect as in (a line)
  • all: <all represents all values ​​less than the inside, <all representatives of all values ​​greater than the inside, = all representatives meaningless (all ones)

Sub-query returns multiple records query multiple columns

Recording a plurality of sub-query returns a query multiple columns can be filtered out using the data as a table, may be used as a condition, may vary depending on the circumstances.
Example:
Query Monday classes have a class, the class number, the number of classes.

SELECT
	a1.ClassName,
	a2.ClassroomID,
	a2.Weekday,
	a2.Part
FROM
	class a1,
	(SELECT 
		ClassID,
		ClassroomID,
		Weekday,
		Part 
	FROM 
		teachingcourse 
	WHERE Weekday = 'mon') a2
WHERE
	a1.ClassID = a2.ClassID;

Here a2 is finished screening criteria table, screening conditions have classes on Monday, and then establish a connection class table, check out the data you want.

Should the connection table and the table is not very familiar with, Tell me what can be the first reference >> ---- the Join multi-table query

index

Importance:
information search, retrieval software system is the most widely used operating, and the speed of query speed will directly affect the type of software systems can and user experience.
You can use the index and table partitioning improve query performance. The index is a major factor effective means to improve data retrieval for any DBMS (database management system), indexes are optimized.
The amount of data is small, the effect is not obvious, when the amount of data, the effect is obvious.
Popular terms: database index is like a dictionary, if there is no index, then you have to rummage want to find page after page, if they have an index, just as with the catalog, speeds up the search.

Clustered index

A dictionary, for example, you are looking for the eighth lesson, you should the turn of the fifth class, you need to continue to next turn, if turn the eleventh class, you need to turn back.

  • "Cluster index" default primary key, if not the primary key, a unique non-empty index will be selected in place, should or not, is implicitly defined as a primary key "clustered indexes."
  • "Clustered index" is not selected, his index number (Lesson 8) and corresponding records (content) exist together.

Advantages and disadvantages

  • Advantages: The primary key to increment, do as the scope of the primary key query
  • Disadvantage: usually by primary key, if the primary key is uuid

Non-clustered index

Or in the dictionary, for example, you want to find a word for the green, you turn to the dictionary directory is sorted according to the first letter of the word, we need to find G, to find any green. There will be a corresponding number of pages.

Data Structure Index Classification

B+TREE

Features:

  • General index
  • Unique index: general index field values ​​must be unique +
  • Primary key index comes
  • Joint index (multi-column)

Data structure for index is b + tree, b is an equilibrium balance tree, is derived based on a binary tree structure.

Can understand the image of the site ----> images of understanding binary self-balancing binary search trees (AVL trees)
Image of understanding self-balancing binary search trees (AVL trees)

HASH index

Feature

  • Soon, total memory, not suitable range to find the other.

Can understand the image of the site ----> images of understanding hash table)
Image of understanding hash table

Other types
  • r-tree: for geometry
  • Full-text indexing: for large text, fuzzy than%

Indexing

create index statement creates an index

format:

CREATE [unique|fulltext|spatial] 
INDEX 
	索引名 [using btree|hash] 
ON 
	表名(字段名[(length)] [asc|desc] );

Which is optional:

  • [Unique | fulltext | spatial]: type index
  • [Using btree | hash]: index data types
  • length: indication index column before how many characters to create indexes
  • [Asc | desc]: indicates the sort order of the index field values, the default is omitted when ascending asc

Example:
to score table StuiID index
CREATE INDEX idx_stuid ON score(StuID);

The question is, after the completion of the establishment of the index, how to view your index, or how to build their own check not index it?

The syntax is as follows:
SHOW INDEX FROM 表名;

For example, we now want to query studen table inside index:
Index query
This is a method of indexing a field name, there is the establishment of two index field names together, where we need to pay special attention to the order of field names , otherwise, it will cause Failed to query other issues.
Syntax is as follows:

CREATE [unique|fulltext|spatial] 
INDEX 
	索引名 [using btree|hash] 
ON 
	表名(字段名[(length)],字段名[(length)] [asc|desc] );

Because creating an index and single, as here, is not the same details, remember we must remember the order of questions.
Should a (field name 1, field name 2) two field names indexed, then:

  1. By Question 1 field names separate investigation.
  2. The problem with the check by field name 1, field name 2 pairs of columns.
  3. The problem can not be solved by the field name 2 separate investigation.

Indexing has been learned, but also learned how to check there is no index, the next time the query to see whether the index.

直接在select前面添加explain

Yes, that's so simple, remember keyword explain it.
Example:
explain keywords
We can see from the figure out, do not use the index query, which is why?
Because fuzzy search, preceded percent do not use indexes. Remember this too, or if there are supposed to interview.

alter table statement creates an index

format:

ALTER TABLE 
	表名 
ADD INDEX [索引名] [索引类型](索引字段);

Therein has many forms, such as primary keys, uniqueness, foreign keys, etc., in the following format:

主键:
ADD [constraint 约束名] PRIMARY KEY [索引类型](主键字段) 
唯一性
ADD [constraint 约束名] UNIQUE [索引名] [索引类型](唯一键字段)
外键
ADD [constraint 约束名] FOREIGN KEY [索引名] (外键字段) REFERENCE...


A writing lesson plans from teachers to teach: the tomato fever

Last note: I am also a beginner program, it is inevitable wrong place, we hope after reading and found out mistakes can comment, thank you

Published 12 original articles · won praise 101 · views 10000 +

Guess you like

Origin blog.csdn.net/lolly1023/article/details/105282863