MySQL Basics (Basic Grammar, Additions, Deletes, Checks, and Modifications)

Hello everyone, this article brings you the basic syntax knowledge of MySQL data, including adding, deleting, checking and modifying, creating tables and databases, and so on.
If you are interested, remember to click to follow, so you won't get lost next time!

basic sentence

database creation
create database if not exists  db_name;

(Adding if exists means delete if it exists, and do nothing if it does not exist/if not exists means create if it does not exist, and do nothing if it exists)

Databases using the utf8mb4 character set
create database if not exists  name character set utf8mb4;
delete database
drop database if not exists db_name;
create table
create table table_name;
View tables and structures
desc table_name;
delete table
drop table table_name;
Insertion of a single row of data
insert into table_name values(100,1000,'名字',null);
Insertion of multiple rows of data
insert into tbale_name (id,sn,name)values
(101,2001,'名字')(102,2002,'名字');
Query all information
select * from table_name;
Query some information
select id,name from tbale_name;
The query field is an expression
select id,name, english,10 from table_name;
select id,name, english+10 from table_name;
select id,name,english+math+chinese from table_name;

The operation of the first step will create 10 constants listed in the following display

reference alias
select id,english+math+chinese 成绩 from table_name;
select id,english+math+chinese as 成绩 from table_name;

The effect of both is the same as english+math+chinese is quoted as an alias result

deduplicated statement
select distinct math from table_name;

distinct for deduplication, deduplication can be performed in one column (for example, if there are repeated 22 in the data, more than 22 will be removed, leaving one 22) or deduplication can be performed in two columns as shown below

select distinct math,english from table_name;

But the prerequisite is that there are the same elements in the two columns, otherwise it will not be possible to deduplicate as shown below

select distinct math,name from table_name;
sort statement

– asc is ascending (default)

select name,math from table_name order by math ; 
select name,math from table_name order by math asc; 

– desc is descending order

select name,math from table_name order by math desc;

The keyword order by can specify multiple fields to sort, but it will sort by the first field first. If the content of the first field is the same, then it will be sorted by the second field. So only the first field is the same to sort the second field.

select name,math from table_name order by math,chinese desc;
Conditional filtering used by where

Indicates that the query score is below 60

select name,math from table_name where math<60 ;

Indicates query xxx

select name,math from table_name where name='xxx' ;

Indicates that the query score is between 70-80. In a field between start value and end value start value <= a field <= end value

select name,math from table_name where math between 70 and 80 ;

Conditional filtering of fields

select *from table_name where chinese>50 or math<70 and english >70;

Priority (and>or) Satisfy Chinese>50 and english >70 first, if not satisfied, then proceed to math<70 and english >70 and and && are similar to or and || are similar to is not null and != null in java

in(value1, value2...)
select name, math from exam_result where math in (58, 59, 98, 99);

Field X in (value 1, value 2...) For each piece of data, as long as the X field is one of the multiple values ​​behind in, it can be displayed.

fuzzy query like

– Sun Wukong and Sun Quan are found in the query (% means multiple characters are matched)

select name from exam_result where name like '孙%';

– Query Sun Quan (_ means match a character)

select name from exam_result where name like'孙_';
limit paging query

**limit n;** means starting from 0 and filtering n-1 pieces of data (a total of n pieces of data)

select*from exam_result limit 5;

**limit s,n ** means to take n pieces of data from the subscript s (counting s)

select*from exam_result limit 1,5

limit n offset s; means to filter n pieces of data starting from s (compared with the second method, this method is recommended, and memory can remember the second method)

selevt*from exam_result order by id limit 3 offset 6(下标);
modify (updata)

– modify statement

update table_name set math=80 where name='abc';

– Multiple field modification

UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '曹孟德';

– Support fuzzy query modification

UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '%悟%';

– Add 30 points to the mathematics scores of the three bottom three students in the total score

UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT 3;

– Update the Chinese scores of all students to double the original

UPDATE exam_result SET chinese = chinese * 2;
delete
delete from  table_name where name='abc' ;
time type

create table

create table data_test (start datatime );

create table data_test2 (start timestamp );

insert into table_name values('2022-5-21');

insert into table_name values('2022-5-21 15:42:41');

Inquire

select * from table_name where start<'2022-5-22'

Do not use the form

select * from table_name where start<999999

The above is the basic knowledge article this time, and there will be improvement articles and JDBC operation articles in the follow-up, you can pay attention to it.

Guess you like

Origin blog.csdn.net/qq_53699052/article/details/126824373