当前位置:网站首页>MySQL learning notes -- addition, deletion, modification and query on a single table
MySQL learning notes -- addition, deletion, modification and query on a single table
2022-06-25 02:51:00 【Laugh and say threethousand】
notes : Learning from this sister's blog https://blog.csdn.net/shine_a?type=blog
One ,sql and mysql The difference between
SQL Is a language developed for operating databases . It can operate the tables in the database , Such as modifying data , Find data .
For example, the following is SQL sentence .
-- increase
INSERT INTO < Table name > ( Field name )VALUES ( value );
-- Delete
DELETE FROM < Table name > WHERE < filter >;
-- Change
UPDATE < Table name > SET < Field name = value > WHERE < filter >;
-- check
SELECT < Field name > FROM < Table name > WHERE < filter >;
MySQL Is the computer software used to manage the database , This software also has Oracle、SQL Server etc. .
Two , install mysql
download MySQL:
https://dev.mysql.com/downloads/windows/installer/8.0.html
Open the URL above , The following interface will appear , Let's pick the first one Windows (x86, 32-bit), MSI Installer install .
install Mysql Will bring it mysql workbench Database graphical tools .

3、 ... and , Create database , And create a new table
-- Create database
CREATE database Xiyouji;
-- Select database
USE Xiyouji;
-- Create a people table Roles:
CREATE TABLE Roles(
Sid INT ,
Sname VARCHAR(10),
Sex VARCHAR(10)
);
-- Insert statement
INSERT INTO Roles VALUES
(1 , ' Pig eight quit ' , ' male '),
(2 , ' Tang Sanzang ' , ' male '),
(3 , ' The Monkey King ' , ' male '),
(4 , ' Sha Wujing ' , ' male '),
(5 , ' White dragon horse ' , ' male ');

Four , Use the command line to access the database
- SQL sentence Ignore case ,SELECT And select It's the same , For clarity of structure , all SQL Keywords are capitalized .
- SQL Statements ignore indents and line breaks , It's all made up of semicolons ; Division . Let's indent 、 Line breaks and so on are just to make the code look more beautiful , Structure is clearer .
Connect to database :mysql -u root -p
Show existing databases :show databases;
Select database :use [ Database name ]

5、 ... and ,select sentence
SELECT Statement is used to select data from the database , The results are stored in the result set .
SELECT < Field name > FROM < Table name >;
for example , We started with the “xiyouji” Look up the task table in the database “roles” All data for ,
1,select *
-- Select database
USE xiyouji;
-- Select the table data to look up
SELECT * FROM ROLES;

2,select Multiple fields
SELECT Sname,Sex FROM ROLES;

3,select Multiple fields
These fields may not be the existing column names in the table , It can also be Mathematical expression 、 Text . We use it AS Keyword to set column aliases (AS It can be omitted )
SELECT Sname as ' full name ',Sex FROM ROLES;

4,SELECT Later fields , It can be used DISTINCT keyword , De duplication of data .
SELECT DISTINCT Sname FROM ROLES;

6、 ... and ,where Clause
SELECT < Field name > FROM < Table name > WHERE < filter >;
1, Comparison operator
WHERE clause , The most commonly used is the comparison operator , Include greater than (>)、 Less than (<)、 be equal to (=)、 Greater than or equal to (>=)、 Less than or equal to (<=)、 It's not equal to (!= perhaps <>). Note that it's an equal sign, not two equal signs .
SELECT * FROM ROLES WHERE Sname=' The Monkey King ';

2,AND,OR Operator
If AND and OR Use at the same time , it is to be noted that AND Priority over OR, But it's best to put parentheses , It's clearer .
SELECT * FROM ROLES WHERE Sname=' The Monkey King 'OR Sname=' Sha Wujing ';

3,IN Operators and NOT IN Operator
SELECT * FROM ROLES WHERE Sname IN (' Pig eight quit ',' The Monkey King ');

4,BETWEEN … AND Operator
BETWEEN … AND An interval consists of two end points .
SELECT * FROM ROLES WHERE Sid BETWEEN 2 AND 4;

5,LIKE Operators and NOT LIKE Operator
Fuzzy matching , Use this , Two wildcards are required .
%: Match any character
_: Match a character
SELECT * FROM ROLES WHERE SNAME LIKE '% enlightenment %';

6,REGEXP Regular expressions
Mainly used to search string , Than LIKE Operators are applicable to more scenarios .
^: start
$: ending
[abc]: contain abc
[a-c]: contain a-c
|: or
-- Find names that contain “ ning ” Of the students
WHERE Sname REGEXP ' ning ';
-- The search name is “ ning ” The first student
WHERE Sname REGEXP '^ ning ';
-- The search name is “ ning ” The students at the end
WHERE Sname REGEXP ' ning $';
-- The search name is “ ning ” or “ Twist “ The first student
WHERE Sname REGEXP '^[ Ning Ning ]';
-- Find student number Sid yes 2 To 5( Include 2 and 5) Of the students
WHERE Sid REGEXP '[2-5]';
-- Find student number Sid yes 2 To 5 perhaps 7 To 9( Include 2 and 5,7 and 9) Of the students
WHERE Sid REGEXP '[2-5]|[7-9]';
Contains the word Wu and the word ma :
SELECT * FROM ROLES WHERE SNAME REGEXP '[ Wuma ]';

7,IS NULL Operators and IS NOT NULL Operator
SELECT * FROM ROLES WHERE SNAME IS NULL;

7、 ... and ,ORDER BY Sort clause
SELECT < Field name > FROM < Table name > ORDER BY < Field name >;
You can sort not only numbers but also words , The field name after it can be the name of the column , The column name can be followed by a keyword .ASC It's in ascending order ( Default ),DESC It's in descending order .
SELECT * FROM ROLES WHERE SNAME REGEXP '[ Wuma ]' ORDER BY SID DESC;

8、 ... and ,LIMIT Clause is used to limit the number of records returned
SELECT < Field name > FROM < Table name > LIMIT < Limit the number of rows >;
1、 Take the first few records
We can go through LIMIT n Take the first... In the result set n Bar record .
SELECT * FROM ROLES LIMIT 3;

2, Skip the first few records
Can pass LIMIT m,n Skip the first... In the result set m Before records n Bar record .
When we want to skip 3 Data , Take... Directly 45 Two lines :
SELECT * FROM ROLES LIMIT 3,2;

Nine ,INSERT Insert statement
INSERT INTO < Table name > ( Name )VALUES ( value );
1, You can do without specifying the column name , The inserted values must be completely inserted in the existing field order .
insert into roles values(9,' Princess Iron Fan ',' Woman ');

2, Column names can be specified , The order of the following values , If the order of the column names corresponds to .
insert into roles(Sid,Sname) values(10,' Zixia fairy ');

3、 Insert multiple rows , Multiple rows of data can be separated by commas between brackets .
insert into roles values(11,' Which zha ',' male '),(12,' Erlang God ',' male '),(14,' Wheezing dog ',' male ');

Ten ,UPDATE UPDATE statement
UPDATE < Table name > SET < Field name = value > WHERE < filter >;
update roles set Sex=' Woman ' where Sname=' Which zha ';

11、 ... and ,DELETE Delete statements
DELETE FROM < Table name > WHERE < filter >;
DELETE FROM ROLES WHERE Sname=' Zixia fairy ';

边栏推荐
- 微信小程序获取扫描二维码后携带的参数
- Dirvish Chinese document of vim
- 分布式事务解决方案和代码落地
- UnityShader入门精要——表面着色器
- [i.mx6ul] u-boot migration (VI) network driver modification lan8720a
- Network planning | [four network layers] knowledge points and examples
- 14 BS object Node name Name attrs string get node name attribute content
- Error log format and precautions
- Can automate - 10k, can automate - 20K, do you understand automated testing?
- 14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
猜你喜欢

Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups

使用ShaderGraph制作边缘融合粒子Shader的启示
![Network planning | [four network layers] knowledge points and examples](/img/c3/d7f382409e99eeee4dcf4f50f1a259.png)
Network planning | [four network layers] knowledge points and examples

DSPACE的性能渲染问题

jwt

Getting started with unityshader - Surface Shader

3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?

mysql学习笔记--单张表上的增删改查

Yarn: unable to load file c:\users\xxx\appdata\roaming\npm\yarn PS1 because running scripts is prohibited on this system

How transformers Roberta adds tokens
随机推荐
F - spices (linear basis)
Groovy之高级用法
Software testing weekly (issue 77): giving up once will breed the habit of giving up, and the problems that could have been solved will become insoluble.
Summary of knowledge points of computer level III (database) test preparation topics
记一次beego通过go get命令后找不到bee.exe的坑
mysql命令备份
保险也能拼购?个人可以凑够人数组团购买医疗保险的4大风险
Sumati gamefi ecological overview, element design in the magical world
对进程内存的实践和思考
leecode学习笔记-机器人走到终点的最短路径
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
NPM package publishing tutorial
AOSP ~ 默认属性值
JS regular matching numbers, upper and lower case letters, underscores, midlines and dots [easy to understand]
Resolution of cross reference in IDA
After reciting the eight part essay, I won the hemp in June
Folding screen will become an important weapon for domestic mobile phones to share the apple market
VSCode中如何实现点击DOM自动定位到相应代码行
DSPACE设置斑马线和道路箭头
It's 2022, and you still don't know what performance testing is?