当前位置:网站首页>SQL language
SQL language
2022-07-04 13:27:00 【Game programming】
SQL Language
elements of grammar
notes
# Use '#' notes -- Use '--' notes /* Multiline comment */Create use database
CREATE DATABASE db01;USE db01;Create table
CREATE TABLE table01 ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, col1 INT NOT NULL DEFAULT 1, col2 VARCHAR(45) NULL, col3 DATE NULL);Modify table
# Add new column ALTER TABLE table01 ADD col CHAR(20);# Modify the column ALTER TABLE Table name CHANGE Original field name new field name Field type constraint condition ALTER TABLE table01 CHANGE col col4 CHAR(32) NOT NULL DEFAULT 'xxx';# Delete column ALTER TABLE table01 DROP COLUMN col4;# Delete table DROP TABLE table01;Additions and deletions
# Add data INSERT INTO table01(col1, col2)VALUES(val1, val2);# Table data migration CREATE TABLE newtable ASSELECT * FROM table01;# to update UPDATE table01SET col = valWHERE Conditions ;# Delete DELETE FROM table01WHERE Conditions ;Inquire about
Basics
# duplicate removal SELECT DISTINCT * FROM table01;# Limit return SELECT * FROM table01 LIMIT 5, 2; # from 5 Start ( barring 5) Return two rows of data , namely 6,7# Sort ASC Ascending ,DESC Descending SELECT * FROM table01ORDER BY col1 DESC, col2 ASC;# Screening SELECT * FROM table01 WHERE Conditions ;Calculated field
| Letter Count | say bright |
|---|---|
| AVG() | Returns the average of a column |
| COUNT() | Returns the number of rows in a column |
| MAX() | Returns the maximum value of a column |
| MIN() | Returns the minimum value of a column |
| SUM() | Returns the sum of values in a column |
# give an example : Calculate the weight removed col1 The mean value of the column is named avg_colSELECT AVG(DISTINCT col1) AS avg_colFROM table01;Text processing
have access to LIKE + Regular expressions Filter text
SELECT *FROM table01WHERE col1 LIKE '[^AB]%'; -- Don't to A and B Any text at the beginning You can also use some functions for text processing :
| function | explain |
|---|---|
| LEFT() | The character on the left |
| RIGHT() | The character on the right |
| LOWER() | Convert to lowercase characters |
| UPPER() | Convert to uppercase characters |
| LTRIM() | Remove the space on the left |
| RTRIM() | Remove the space on the right |
| LENGTH() | length |
| SOUNDEX() | Convert a string into an alphanumeric pattern that describes its phonetic representation |
grouping
Grouping is to put rows with the same data value in the same group . You can use the summary function to process the same grouped data .
WHERE Filter line ,HAVING Filter grouping , Row filtering should precede group filtering .
SELECT col1, COUNT(*) AS numFROM table01WHERE col1 > 2GROUP BY col1HAVING num >= 2;We also need to pay attention to the following points
- GROUP BY Clause appears in WHERE After Clause ,ORDER BY Before clause ;
In addition to summary fields ,SELECT Every field in the statement must be in GROUP BY Given in Clause ;
NULL Your guild is divided into separate groups ;
majority SQL Implementation does not support GROUP BY Columns have variable length data types .
Subquery
Only one field of data can be returned in the subquery . The results of subqueries can be used as WHRER Filter condition of statement :
SELECT *FROM table01WHERE col1 IN (SELECT col1 FROM table02);Link query
Join is used to join multiple tables , Use JOIN keyword , And conditional statements use ON instead of WHERE.
Equivalent connection :
SELECT A.value, B.valueFROM tablea AS A INNER JOIN tableb AS BON A.key = B.key;It can also be used indefinitely INNER JOIN, Instead, use normal queries and in WHERE Join the columns to be joined in the two tables by equivalent method .
SELECT A.value, B.valueFROM tablea AS A, tableb AS BWHERE A.key = B.key; Natural join :
Natural join is to join columns with the same name through equivalence test , Columns with the same name can have more than one .
SELECT A.value, B.valueFROM tablea AS A NATURAL JOIN tableb AS B; External connection :
It is divided into left outer connection , Right outer connection and all outer connection , The left outer join is to keep the unrelated rows in the left table , The right outer connection is the same .
SELECT A.value, B.valueFROM tablea AS A LEFT OUTER JOIN tableb as bON A.col1 = B.col1;Combination query
Use UNION To combine two queries , If the first query returns M That's ok , The second query returns N That's ok , The result of the combined query is generally M+N That's ok . Each query must contain the same columns 、 Expressions and aggregation functions .
By default, the same line will be removed , If you need to keep the same company , Use UNION ALL.
SELECT *FROM table01WHERE col1 = 1UNIONSELECT *FROM table01WHERE col1 =2;Use view query
View is a virtual table , It doesn't contain data itself , It can't be indexed . The operation on the view is the same as that on the normal table .
Here is a simple view creation :
CREATE VIEW myview ASSELECT sentence ;Rights management
stay mysql in , The account information is saved in mysql In this database
USE mysql;SELECT * FROM user;Account operation
# Create an account CREATE USER myuser IDENTIFIED BY ' password ';# Change user name RENAME myuser TO newname;# Delete user DROP USER newname;# View permissions SHOW GRANTS FOR myuser;# to grant authorization GRANT SELECT, INSERT ON mydatabase.* TO myuser;# Delete permission REVOKE SELECT, INSERT ON mydatabase.* FROM myuser;# Change password SET PASSWROD FOR myuser = Password('new_password');author :S++
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- n++也不靠谱
- 「小技巧」给Seurat对象瘦瘦身
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- When MDK uses precompiler in header file, ifdef is invalid
- 老掉牙的 synchronized 锁优化,一次给你讲清楚!
- 【云原生 | Kubernetes篇】深入了解Ingress(十二)
- iptables基础及Samba配置举例
- Annual comprehensive analysis of China's mobile reading market in 2022
- MDK在头文件中使用预编译器时,#ifdef 无效的问题
- 实时云交互如何助力教育行业发展
猜你喜欢

ASP.NET Core入门一

CANN算子:利用迭代器高效实现Tensor数据切割分块处理

诸神黄昏时代的对比学习

CTF竞赛题解之stm32逆向入门

干货整理!ERP在制造业的发展趋势如何,看这一篇就够了

MDK在头文件中使用预编译器时,#ifdef 无效的问题

Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?

数据库锁表?别慌,本文教你如何解决

Oracle 被 Ventana Research 评为数字创新奖总冠军

Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
随机推荐
Valentine's Day confession code
Use fail2ban to prevent password attempts
Comprehensive evaluation of modular note taking software: craft, notation, flowus
WPF double slider control and forced capture of mouse event focus
【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
It is six orders of magnitude faster than the quantum chemical method. An adiabatic artificial neural network method based on adiabatic state can accelerate the simulation of dual nitrogen benzene der
一文掌握数仓中auto analyze的使用
Reading cognitive Awakening
聊聊支付流程的设计与实现逻辑
7、 Software package management
[cloud native | kubernetes] in depth understanding of ingress (12)
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
Dgraph: large scale dynamic graph dataset
How real-time cloud interaction helps the development of education industry
After installing vscode, the program runs (an include error is detected, please update the includepath, which has been solved for this translation unit (waveform curve is disabled) and (the source fil
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
MySQL three-level distribution agent relationship storage
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
用fail2ban阻止密码尝试攻