当前位置:网站首页>MySQL quick learning notes-2021-08-31
MySQL quick learning notes-2021-08-31
2022-07-26 10:44:00 【ZhuMou】
Catalog
1.DB,DBMS,SQL Basic concepts of
0. Abstract :
a. The database is briefly recorded (DB), Database management system (DBMS) and SQL Basic concepts and interrelations of
b. Brief introduction SQL Basic classification of statements , It involves some new concepts that will be learned in the future
c. Briefly introduces the database (DB) The basic unit of —— surface (table)
d. Introduced some preliminary SQL Statement and database related operations
e. Simple query and conditional query . Notice the difference
f. Fuzzy query
1.DB,DBMS,SQL Basic concepts of
database (DB): Press A certain format Storing data Of file Set .
Database management system (DBMS): For managing databases 、 Software for adding, deleting, modifying and checking data in the database .( The two functions are juxtaposed , The former is similar to the organization of documents , The latter passes SQL Realization )
SQL(structed query language): Structured query language , Used to operate the database ( Additions and deletions ).
The relationship between the three is :DBMS adopt SQL Yes DB To operate .
2.DB,table
The most basic unit in a database is a table (table), It is more intuitive to organize data in the form of tables .
The table consists of rows (row) And column (colnum) form . Rows can also be called records record, Columns can also be called fields field. Rows often store data of different aspects of an individual ( Zhang San male 20 year Chinese ), Columns often store the same type ( Broad sense of , Non narrow data types ) The data of . The properties of the field are : data type , Field names and constraints ( Such as uniqueness constraints and scope constraints ).
3.SQL Sentence classification
| DQL | Data query language (select keyword )(Data Query Language) |
| DML | Data operation language (insert,delete,update)(Data Management Language) The object of operation is data , Add, delete and modify data . |
| DDL | Data definition language (create,drop,alter) Structure of operation table , Such as deleting columns . For the structure of the table , Not data , Although it may cause data changes .(Data Definition Language) |
| TCL | Transaction control language (Transaction Control Language)(commit Transaction submission ;rollback Transaction rollback ) |
| DCL | Data control language (Data Control Language)(grant to grant authorization ,revoke Revoke authority ) |
4. Common command :
Note that some operations require running the command prompt as an administrator .
Sign in mysql
| net start mysql | Turn on mysql service , Then you can log in |
| net stop mysql | close mysql service , Login is not allowed after |
| mysql -uroot -pkeyword | User name root password keyword Sign in . This will cause the password to appear . |
mysql -uroot -p enter( enter ) keyword | Input p Enter enter , Enter the password again , Password display can be avoided . |
| ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_keyword'; | modify root Password of the account |
| exit | sign out mysql Account |
The above command only needs to add a semicolon or \g, Others do not need to add .
Management database ( Type in document management )
| show databases; | Display all databases of this user ( name ), This command is only allowed after the initial password has been changed |
| use DBname; | Use one of database,DBname For database name . Don't use 'DBname' or “DBname”. |
| create database DBname; | stay root Create database under user name DBname. |
| show tables; | After using a database (use DBname), Show all the tables in the database . |
| select * from tableName; | View a table , Only in use ( Choose use) The corresponding database can be viewed . |
| source loop | Import a loop( route ) Under the sql file ( database ),loop Chinese is not allowed in English . I thought before sql A file is something like excel Form file , But after reading the programming of IOT students , A new understanding has emerged :sql Be similar to C In language cpp, It's the source file , Different from my current command-line programming . |
| desc tableName; | Describe the structure of the table ( Describe the fields of the table and their corresponding data types ), yes describe tableName; Abbreviation . |
use database You don't need to add \g It can also be executed , If you add it, you won't report an error .
| select version(); | Query the current mysql edition |
| select database(); | Query the currently used database |
| \c | Cancel the input of this command . |
because SQL Statement does not encounter semicolon ; Don't execute , So you can use \c Cancel the input of this command , This will make the command that is not entered invalid ( Or ignored ).
5. Simple query
The first thing you need to use A database . A simple query refers to a query field , Different from the following conditional query , The query is the record .
| select fieldName from tableName; | Display only tableName In the table fieldName Field ( Displays a single field in the specified table ) | Be careful , The field name is case sensitive . |
| select field1,field2,... from tableName; | Display multiple fields in the specified table ( Can be all ) | Be careful , The field name is case sensitive . |
| select * from tableName; | Display all fields in the specified table | Low efficiency , The actual development does not need . |
as keyword select fieldName as 'newName' from tableName; | as Keyword aliases the field name ( But the field name is not modified ).as It can be omitted and replaced by spaces . Single quotation marks '' You can omit it .( Single quotation marks are SQL Standard for including strings ) But when it comes to Chinese aliases , Single quotation marks cannot be omitted . | as Not only can you give fieldName names , You can also alias a table . However, it should be noted that the use of these aliases must be after the definition of these aliases . |
| select fieldName_expr from tableName; | You can directly perform arithmetic operations on field names to form expressions ( Of course, the field must be numeric ), In this way, the displayed column will be the calculated column ( Include field name , It can be used as Change to more intuitive ). This does not modify the data in the table , It's just a change in the display . |
These sentences need practice to be remembered . If you don't know how to import a database, you might as well create one yourself , Reference resources :
https://blog.csdn.net/weixin_42419856/article/details/81355719
Here is an example : surface detail.
select * from detail;
Show :
+------------+------+------+------------+--------+----------+
| ID | name | age | date | scores | address |
+------------+------+------+------------+--------+----------+
| 2019213224 | Ying | 21 | 2001-07-20 | 653 | Zhejiang |
| 2019213225 | Chen | 22 | 2000-04-24 | 655 | Beijing |
| 2019213221 | Zhao | 19 | 2002-05-14 | 645 | Tianjin |
| 2019213220 | Qian | 19 | 2002-01-14 | 675 | Hubei |
| 2019213226 | Sun | 20 | 2001-04-14 | 673 | Xinjiang |
+------------+------+------+------------+--------+----------+
select name,id from detail; The result is :
+------+------------+
| name | id |
+------+------------+
| Ying | 2019213224 |
| Chen | 2019213225 |
| Zhao | 2019213221 |
| Qian | 2019213220 |
| Sun | 2019213226 |
+------+------------+
You can find ,sql Statements are not only case insensitive keywords , Even field names are indistinguishable . This poses a problem , If the field names are fieldname and FIELDNAME Words , How to distinguish between , Or you can't create similar fields at all ? This is reserved for subsequent learning table Say it later .
select name,scores + 100 from detail;
The result is :
+------+--------------+
| name | scores + 100 |
+------+--------------+
| Ying | 753 |
| Chen | 755 |
| Zhao | 745 |
| Qian | 775 |
| Sun | 773 |
+------+--------------+
This statement will not only cause the corresponding value under the field to change , It will also cause the field name to change , have access to as Keywords are simplified .
6. Conditions of the query
Conditional query is for records ( That is to say, yes ), Only the specified fields of qualified records will be displayed . The following are conditional query statements and common operators .
| SELECT field1,field2... FROM tableName WHERE conditionExpr; | where Keywords are followed by logical expressions , It consists of field names and operators . It is different from the previous arithmetic expression when performing arithmetic operations on fields . Only the logic is 1( That is, to meet the conditions ) The record will show . |
| = | Not assignment , It's equal to , amount to C In language ==. Pay attention to differences . |
| <> or != | It's not equal to |
| > and < | Greater than and less than |
| >= and <= | Greater than or equal to and less than or equal to |
| between...and | fieldName BETWEEN a AND b amount to fieldName>=a and filedName<=b. It must be guaranteed that a Small b Big , Otherwise, what you find out is empty sets. |
| and | Logic and , Priority over or, If you don't want to care about the priority, add () |
| or | Logic or |
| is null | The specified field of the record is required to be empty .sql Medium NULL It's not a value , So it can't be used fieldName = null Judge . This is different from Java and C in NULL by 0 Fact . |
| is not null | The specified field of the record is required to be non empty . |
in(value1, value2,...) | The specified field of the record is required to be value list Some value in |
| not in(value1, value2,...) | Be careful in hinder () Enumeration rather than interval |
It's still the table above detail:
mysql> select id, name
-> from detail
-> where scores>660
-> \g // Such input helps distinguish between keywords and identifiers .
The result is :
+------------+------+
| id | name |
+------------+------+
| 2019213220 | Qian |
| 2019213226 | Sun |
+------------+------+
Other operators are similar .
7. Fuzzy query
Fuzzy query is also aimed at records , It is a kind of conditional query . Its characteristics are :conditionExpr The data type of the field in is String.sql Medium String Use single quotation marks ' ' The enclosed , Not double quotes " ".
| like | keyword like |
| % | Replace any number of characters .'%T' Said to T a null-terminated string . If you want to express simple %, Use \%. |
| _ | Replace any character .'_T%' Indicates that the second character is T String . If you want to express simple _, Use \_. |
| select field1, field2,...from tableName where stringField like '....'; | stringField It refers to that the field data type is String Field name ,like Search keywords for fuzziness ,'...' citing _ and % The string formed . |
mysql> select id,name
-> from detail
-> where scores like '65_'
-> \g
The result is :
+------------+------+
| id | name |
+------------+------+
| 2019213224 | Ying |
| 2019213225 | Chen |
+------------+------+
This description where The fields in the following conditional statements can be string Type of , It can also be int Type of . There may be implicit type conversion in .
边栏推荐
猜你喜欢

二叉树的遍历 递归+迭代

解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
![[leetcode daily question 2021/2/14]765. Lovers hold hands](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode daily question 2021/2/14]765. Lovers hold hands

在神州IV开发板上为STemWin 5.22加入触屏驱动
![[paper after dinner] deep mining external perfect data for chestx ray disease screening](/img/d6/41c75d292c26b2e7e116767a51eb5e.png)
[paper after dinner] deep mining external perfect data for chestx ray disease screening

RT thread learning notes (III) -- building a compilation environment with scons

$router和$route的区别

一文详解Nodejs中fs文件模块与path路径模块

在神州IV开发板上成功移植STemWin V5.22
![[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
随机推荐
10 let operator= return a reference to *this
剑指Offer(八):跳台阶
IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
剑指Offer(五十二):正则化表达式
Flutter CachedNetworkImage圆角
[paper after dinner] deep mining external perfect data for chestx ray disease screening
[dectectron2] follow the official demo
剑指Offer(十):矩形覆盖
Flutter TextField设置高度并且自动换行,圆角边框去除下划线
Common classes (understand)
RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)
第7期:内卷和躺平,你怎么选
鹏哥C语言第七节课总结
Write to esp8266 burning brush firmware
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
Analysis of the transaction problem of chained method call
剑指Offer(四十九):把字符串转换成整数
A semicolon is missing
Dry goods likeshop takeout order system is open source, 100% open source, no encryption
algorithm