【Database statement】SQL common statement and some database knowledge

(1) Common database statements

Create a test environment
Online database http://sqlfiddle.com/
Primary introductory knowledge for the background: ["Learn MySQL with Sun Xinghua" relational database tutorial [End of primary chapter] - 哔哩哔哩] https://b23.tv /nStbOCd

〇, sql classification

DDL (Data Definition Language
): Data Definition Language, used to define database objects: libraries, tables, columns, etc.;
DML (Data Manipulation Language): Data Manipulation Language, used to define database records (data);
DCL (Data Control Language): Data Control Language, used to define access rights and security levels;
DQL (important) (Data Query Language): Data Query Language, used to query records (data). Note: the sql statement ends with;

1. The most basic CRUD

Link: Database Common Statements
CRUD

select 'A'||name||'A' ana, concat('A',name) an, lastname, firstname from persons
insert into persons values ('gates', 'bill', 'xuanwumen 10', 'beijing')
update person set firstname = 'fred' where lastname = 'wilson'
delete from person where lastname = 'wilson'
--子查询
select a.name,a.age from (select t.name,t1.age from workerid t left join workerinfo t1 on t.id=t1.id) a --表
Select * from People where PeopleAddress = (select PeopleAddress from People where PeopleName = '赵云') --值

1 Field deduplication
If there is historical data in a table, such as
(my male programmer 100,000 last year)
(my male programmer 200,000 this year)
for the name field, if you do not want it to appear repeatedly in the result, then

select distinct name, sex from worker;

2 plus paging conditions
1, oracle wording
rownum usage skills

select * from table1 where rownum <=20;

The oracle way of writing, in particular, rownum<=1 can be used to check whether there is data in the table. When writing a simple single-table query, you cannot use > (a value greater than or equal to 1) >= (a value greater than 1) or = (a value greater than 1) for rownum.
Because rownum is a pseudo-column, it is used as the incremental record row value of the returned results that meet the query conditions. Only the results that meet the query conditions will be counted as one row and the returned rownum will be increased by 1, that is to say, the rownum>1 condition is used to find out The result rownum cannot be greater than 1 and is discarded. As written below, the result of count is 0.

select count(*)from people where rownum >1;

Of course, >0 returns the correct number of rows.
If you want to check column 5, you can use the following method

select object_id,object_name
from (select object_id,object_name, rownum as rn from t_test1)
where rn = 5;

To query columns 6 to 10, use the following method

select * from
(
select a.*, rownum as rn from css_bl_view a
where capture_phone_num = '(1) 925-4604800'
) b
where b.rn between 6 and 10;

It is also necessary to pay attention to the use of order by: for the order by primary key, it is to sort first and then calculate ROWNUM. But if the order by is not the primary key, Oracle will first fetch the records satisfying the rownum condition according to the physical storage location (rowid), that is, the first 5 pieces of data in the physical location, and then sort the data according to the Order By field. So we need to do some processing on the writing, as follows

select object_id,object_name
from (select object_id,object_name from t_test1
order by object_name)
where rownum <= 5;
--主键是object_id,排序用非主键object_name时

2. Mysql writing method - use limit
MySql pagination query

# 放在查询语句的末尾
LIMIT 【位置偏移量off,】 行数n
--位置偏移量off值可不写,则取前n行。相当于每n行为1页时,取第1页。

Note that off is the number of skipped rows, which can be 0, and the query returns results starting from row off+1.
20 lines per page, take page 3:

SELECT 
  employee_id,
  last_name 
FROM
  employees 
LIMIT 40, 20 ;

Pagination query formula:
(current page number-1) * the number of entries per page, the number of entries per page
takes the 32nd and 33rd rows

SELECT 
  * 
FROM
  employees 
LIMIT 31, 2 ;

New way of writing MySql8.x

SELECT 
  * 
FROM
  employees 
LIMIT 2 OFFSET 31 ;

3 fields are concatenated into one field
concat(field 1, field 2, ...) as new field
use || : col1||'arbitrary string'||col2 ...
4 fields left fill
lpad (field a, 8, '*') Fill several asterisks to make the string length 8
5 Intercept
substr (field a, 3, 3) Intercept 3
substr (field a, 3) from the third to the end

2. Add conditions and operations, query by group or window

In practical applications, we often add statements or functions such as sorting, grouping, pointer query, and calculation to achieve the purpose of querying the required data.
SQL Server: Eight, conditional query: where, comparison operator, subquery, conditional statement

select * from People where PeopleAddress in('武汉','北京')
select * from People where PeopleSalary >= 10000 and PeopleSalary <= 20000
select * from People where PeopleSalary between 10000 and 20000
select * from People where PeopleSalary <> 10000 --不等于
select top 5 * from People order by PeopleSalary desc --前5行
select top 10 percent * from People order by PeopleSalary desc --前百分之10
select * from People where PeopleAddress is not null
//获取时间的月 select to_char(sysdate,'dd') as nowDay from dual

查询消费者总消费额
select customer,sum(cost) from costrecord 
where year>2021 and type='厨卫' 
group by customer 
order by viplevel 
查询消费者总消费次数
select customer,count(cost) from costrecord 
group by customer order by viplevel

group by (grouping), order by (sorting), where (condition)
explains the relationship between grouping group by, sorting order by and condition where

Writing method: group by must be located after where and before order by.
That is from > where > group by > having > order

  • group is generally used together with order by, execute group by first and then execute order by.
  • After using group by, if you want to filter again, you can use having, that is, having is filtered after grouping.
  • Used together with where: execute the filter condition first and then group.
  • Having operates on a set of data; where operates on rows.

Window function PARTITION BY() function introduction
partition by and group by partition by can only group and sort
certain fields on the basis of retaining all data, and will not aggregate similar data, which is used to filter the calculation range ; group by only retains The fields involved in the grouping and the result of the aggregate function

# 查询每种商品的id,name,同类型商品数量(ps:感觉这个应用的有点问题)
select id,name,count(*) over (partition by type) from product;
# 查询每个城市每个类型价格最高的商品名称
select
       name,
       price, --这里的列应该是address?
       type,
       max(price) over (partition by address,type) as 'max_price'
from product;

Window function details over (partition by _ order by _ )
1. Keyword meaning:
partition by: group by a certain field, similar to group by, but will not aggregate similar data, used to filter the calculation range
order by: after grouping Sort
ROWS according to a field: physical row dimension, filter calculation range.
RANGE: According to the value of the column, to filter the range of rows
CURRENT ROW: current row
UNBOUNDED PRECEDING: all rows above the current row
UNBOUNDED FOLLOWING: all rows below the current row
__ PRECEDING: __ row above the current row (__ can be A number, or an expression)
__ FOLLOWING: __ line below the current line (__ can be a number or an expression)
2. Use window functions to sort and match aggregate functions
(after grouping and sorting, calculate by window sum + rows/range)
ps: The following writing method is possible, but the interpretation of the meaning feels a little problematic, so I haven’t tried it myself.

-- 根据用户id分组,分数排序,当前行的sum()
sum(sroce) over(partition by user_id order by sroce rows current row) as sum1
 
 -- 根据用户id分组,分数排序,从 组内首行? 到当前行的sum() 
        如果解释没错,那在统计每个人截止不同时间的总收入时可用
sum(sroce) over(partition by user_id order by sroce rows between 1 preceding and current row) as sum4

-- 根据用户id分组,分数排序,当前行且 相邻上下? 分数相等的sum()
sum(sroce) over(partition by user_id order by sroce range current row) as sum2
 
--根据用户id分组,分数排序,当前行且 相邻上下? 当前分数-1 和 当前分数+1之间
sum(sroce) over(partition by user_id order by sroce range between 1 preceding and 1 following) as sum3

3. Use the window function to sort and get the serial number in the group.
The serial number in the group
- if there are two tied for the highest score in the group

--顺序排序,顺序为1,2,3
ROW_NUMBER() over (partition by user_id order by score) as rn1
--并列排序,跳过重复序号,顺序为1,1,3
RANK() over (partition by user_id order by score) as rn2
--并列排序,不跳过重复序号,顺序为1,1,2
DENSE_RANK() over (partition by user_id order by score) as rn3

When using the row_number() over() function, the execution of the grouping and sorting in over() is later than where and group by, but not later than the execution of order by.
Other references for window functions 1
Other references for window functions 2

Three, multi-table query

spell left join

select 
a.schoolid,b.schoolname,b.schoolorder,
a.teacherid,c.teachername,d.teachersalary
from
schoolteacher a 
left join school b on a.schoolid=b.schoolid
left join teacher c on a.teacherid=c.teacherid
left join teacherinfo d on a.teacherid=d.teacherid

Composition where (+) --oracle
left and right joins in oracle

--(+)在哪一边,则返回另一边所有的记录。(+)在右面是左连接
select 
a.schoolid,b.schoolname,b.schoolorder,
a.teacherid,c.teachername,d.teachersalary
from
schoolteacher a 
school b 
teacher c 
teacherinfo d 
where 
a.schoolid=b.schoolid(+) and
a.teacherid=c.teacherid(+) and
a.teacherid=d.teacherid(+)

Spelling union / union all
see the difference between union and union all

4. Query the tree data associated with the PID field

Query the current node and all child nodes:

SELECT * FROM YW_XYZB 
CONNECT BY PRIOR ID = PARENT_ID 
START WITH ID = '***';

Query the current node and all parent nodes:

SELECT * FROM YW_XYZB 
CONNECT BY PRIOR PARENT_ID = ID 
START WITH ID = '***'

5. Convert the return value according to the condition (case when)

case when then else end

select name , 
(case when age>65 then 2 when age<35 then 0 else 1 end) as type 
from workerinfo;

Recommended reading: SQL- case when then else end usage experience summary

SELECT a.*,
	CASE
	WHEN a.age BETWEEN 0 and 20 THEN '青年'
	WHEN a.age BETWEEN 20 and 40 THEN '中年'
	ELSE '非人类'
	END AS '描述'
	FROM c_20170920 a

other references

select country,
	sum( case when sex = '1' then
	population else 0 end),  --男性人口
	sum( case when sex = '2' then
	population else 0 end)   --女性人口
	from table_a
	group by country;

Comparative analysis of Oracle's unique decode case when and decode

#Other query conditions

1. Fuzzy query like, any number of characters in front of the num field in the lookup table, the middle character a, and one character after it (several _ means how many characters there are)

select num from table1 where num like '%a_';

2. Specify the range to query in, and search in the given value

select * from table1 where id in ('11','13','a1');

3. The empty value is null
4. The middle value is between 1and 5. Whether it includes the boundary depends on the database
5. <> is not equal to and or not and or not (suggested writing)
6. The Chinese day of the week can be sorted by order by instr ('Friday, Thursday, Wednesday', day) In this way, instr returns the position of param2 in param1, counting from 1, and returning 0 if it cannot be found.

#Build table, change table, build view and add comments

create or replace
comment on

#Build mode and change permissions

??
grant

#Import and export library

imp/impdp and exp/expdp

# Commonly used system tables in the database

user_tables user_tab_columns user_tab_cols
user_tab_comments user_col_comments

Get the table name and table comment owned by the current user

select a.table_name as name,b.comments as remark
from (select table_name from user_tables ) a
inner join (select table_name,comments from user_tab_comments) b
on a.table_name=b.table_name
order by a.table_name

# some functions

SYSTIMESTAMP SYSDATE TRUNC ROUND dbms_random(oracle) to_char to_date

#Temporary tables

with queryname1 as (sql query statement) select * from queryname1;

# trigger process/package

slightly

#MySql Install & Reset & Password Reset

MySql install 5.x
MySql install 8.x
mysql reset database
mysql reset database 2
Mysql forgot password and password reset

(2) Basic knowledge of database

1. Database Design Principles (Paradigm)

Detailed Explanation of the Six Paradigms of the Database
Regarding whether the paradigms should be followed:
https://www.bilibili.com/video/BV1J94y1y75k/?spm_id_from=333.337.search-card.all.click&vd_source=6debe81be6c46d072d7be3a28fc4790e insert image description here
Each paradigm is progressive in turn, and if it meets the latter, it must be in line with the previous.

"Dependence" in this context refers to the existence of a discrete function y=f(x1, x2).

  • 1NF is an atomic constraint on attributes, and attributes are indivisible
  • 2NF is the uniqueness constraint of the record. In terms of implementation, there must be a primary key (consisting of one or more attributes) in the table, and non-primary attributes must depend on the candidate key
  • 3NF means that each non-main attribute does not depend on other non-main attributes, non-main attributes cannot be information derived from other non-main attributes, and must be directly related to the main attribute (removing the redundancy of non-main attribute information)
  • BCNF is a non-key attribute and cannot have partial functional dependencies on the candidate code, that is, it cannot determine the value of the non-key attribute by only taking part of the attributes in a candidate code (removing the redundancy of the main attribute information in the table). "Partial functional dependency" means that "a certain attribute value can depend on a part of the attributes of the attribute group instead of the whole".
    Regarding the improvement of BCNF to 3NF:
    the result of following BCNF is that the candidate codes in the table are uniquely determined and used as the primary key of the table, and the subset candidate codes cannot be split from the candidate codes for non-candidate codes to rely on. Otherwise, in the primary key composed of multiple attributes, there must be a one-to-one correspondence between the attributes, or the value of the primary attribute is meaningless in the business described in the table. This one-to-one relationship or redundant primary attributes are described in this table. The business data must be redundant information, and only one of the attributes should be kept. It is also possible that the data of this one-to-one correspondence cannot be fully reflected in the table data due to insufficient data volume. No matter how you look at it, it should be a separate relational table, and the primary key of the business table should be a collection of attributes as small as possible.
    Extending the situation of "one-to-one correspondence of attributes in the primary key" just discussed, one can think of one-to-many correspondence. I think this "many" should not be an attribute in the primary key but a non-primary attribute.
  • 4NF removes many-to-many. I thought about a situation, if the various orientations in "shopping orientation" K have non-trivial multi-valued dependencies with "occupation" A, "interest" B, "age" C, etc., and it is a many-to-many relationship, then Should not appear in a table.
  • 5NF really didn't understand.
    Recommended naming convention:
    insert image description here

2. My experience and understanding of the design and use of relational data tables

According to my experience (non-universal division), when designing tables, there are code tables, relationship tables, record tables, etc.
Code table: The code table in the narrow sense generally stores information that can extract the code value (the table is composed of code, name, and serial number), and the basic situation table stores basically fixed and unchanged related information. For example, the warehouse with code 001 is located in Beijing with code 0101. District, capacity 1023 and so on. The code table in the broad sense can combine the code table in the narrow sense with the basic situation table, and use the basically fixed physical descriptions such as position and capacity as the extended columns of the code table to reduce the number of tables.
Relationship table: store variable association relationships. Describe the association relationship between code table attributes (associate multiple code table main attributes into a table, highlighting an association relationship, rather than using them as the main attribute/candidate code of another table as data identification). When the basic attributes do not change, the corresponding relationship may change, but generally the number of entries in this relationship will not change too much. The association relationship generally reflects the business characteristics. For example, the warehouse manager relationship table reflects the business characteristic that the warehouse is managed by people, and the blog label relationship table reflects the business characteristic of domain division.
Record table: use multi-attribute or single-attribute as the primary key (the only acceptable candidate code for a table), record the relevant current state or change state, as time goes by, non-primary key data will change (status table), or add new rows Record new data (state change table).

More specific analysis:
code table:
such as warehouse code table. Generally, the full name of the warehouse will not be repeated or changed, but it may still change; and the name is longer and the name of the repeated storage warehouse takes up more space. Therefore, the code table is designed, and codes are used to refer to warehouses in other tables. Latitude and longitude, capacity (100 square meters and 101 square meters are directly stored values, large warehouses and small warehouses are code values ​​for storing warehouse capacity levels) can be considered as fixed associated information/physical attributes of warehouses, and can be placed in the warehouse code table to avoid too much table many.
For example, the manager code table. What we are most concerned about is the name of the administrator, but it may be repeated, and the identity card information is needed to further clarify the data. The ID card can be directly used as the code, and the name of the manager depends on the ID card. In this case, even if an administrator is in charge of a warehouse for life, it is not appropriate to put the corresponding relationship between the ID card and the name in the warehouse table. Administrator code list to save. If you only care about the name of the manager and not the ID card, but the manager is not a lifelong responsibility system, then this variable correspondence should also be counted as a relationship table rather than as fixed related information in the warehouse code table. If you only care about the name of the manager and it is fixed for life, then it can be considered that the name of the manager is an inherent attribute of the warehouse and directly placed in the code table).
In addition, there are material coding tables, gender coding tables (97 genders-sweaty soybeans), etc. There are cases where the codes are meaningful (such as ID cards) and meaningless (such as random codes).

Relational table:
The corresponding relationship between the primary keys of multiple code tables reflects certain business logic, rather than changes in time and space. The affiliation of may change over time due to delegation and appointment. Although the warehouse and administrator may be one-to-one, considering business reform and administrator change, it is better to store the corresponding relationship through the warehouse administrator relationship table instead of putting the administrator in the warehouse code as an associated attribute .

Record table:
embodies the nature of time and space. For example, the current quantity and space occupied by a certain material in a certain warehouse, the current number of workers in a certain warehouse, check-in status, or the number of materials in and out of the warehouse today, etc. A value that is used for frequent insert or update changes. These values ​​often correspond to candidate keys composed of multiple main attributes.

3. Understand the operation of the database

https://www.bilibili.com/video/BV12t4y1H7by/?spm_id_from=333.788&vd_source=6debe81be6c46d072d7be3a28fc4790e

What happened when querying:
insert image description here
insert image description here
performance tuning, according to server performance, determine the optimal parameters through testing:
insert image description here
insert image description here

Through multi-scenario testing, it is predicted that the number of database servers will need to be increased in order to separate reads and writes and shard storage to reduce the pressure on a single server when the business grows. The performance of a single MySQL table exceeding 1kW rows will drop sharply.

Fourth, the difference between document and relational database

https://www.bilibili.com/video/BV1aL411G7Z9/?spm_id_from=333.999.0.0&vd_source=6debe81be6c46d072d7be3a28fc4790e
Several types of databases
insert image description here

#other:

index?

Guess you like

Origin blog.csdn.net/starfire_hit/article/details/128662908