Zero basic learning, basic MySQL operation

1. Enter the command line

1. Enter MySQL command line mode, Windows, Linux open the terminal and enter the following command

mysql -u root -p

Insert picture description here

If you are prompted that mysql is not an internal command, please refer to my other article to solve the problem that mysql is not an internal command

2. Basic grammar

2.1 Basic database management

1. View the database

 show databases;

2. Select the database

use mysql;
# use 数据库名称;

3. View the data sheet

show tables;

4. View the table structure

show columns from help_topic;
# show columns from 表名称; 

2.2 Basic database operations

1. Create a database

create database test;
#create database 数据库名称;

2. Delete the database

drop database test;
#drop database 数据库名称;

2.3 Data table operation

1. Create a data table

#基础语法
create table table_name(column_name column_type);

2. Delete the data table

#基础语法
drop table table_name;

3. Insert data

insert into table_name(column_name1,column_name2,...)
values( value1,value2,...)
#column_name表示数据表的列名,value表示对应的列插入的数据

4. Query data

select * from table_name;#查找表中的全部数据
select column_name,column2_name from table_name;#查找表中的几列数据
select * from table_name where condition1[and[or]]congition2;#精确查找

5. Modify the data

update table_name set field1=new_values1,field2=new_values2 
[where Clause]

6. Delete data

delete from table_name [where Clause]

7. Example
1) Create a database

create database school;

Insert picture description here

2) Use school database

use school;

Insert picture description here

3) Create data table class

create table `class`(
`id` INT UNSIGNED AUTO_INCREMENT,
`number` INT(10) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`class_name` VARCHAR(50) NOT NULL,
PRIMARY KEY(`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Insert picture description here

4. Insert data

insert into class(number,name,class_name)
values(1903210086,'贾明','计科1903');

Insert multiple data at once

insert into class(number,name,class_name)
values(1903210087,'张三','软工1902'),
(1903210088,'麻子','计科1903'),
(1903210089,'李四','软工1902');

Insert picture description here

5. Query data

select * from class;
#查询表中全部数据
select id,name,class_name from class;
#查询表中的序号,姓名,班级
select * from class where name='张三';
#查询表中张三的信息
select * from class where name='麻子' or class_name='计科1903';
#查找姓名是麻子或者班级是计科1903班的学生
select * from class where name='麻子'and class_name='计科1903' or number=1903210086;
#查找姓名是麻子并且班级是计科1903班或者学号是1903210086的学生

Insert picture description here
Insert picture description here6. Modify the data

update class set class_name='大数据1901' where name='贾明'

Insert picture description here7. Delete the statement

delete from class where name='贾明';

Insert picture description here

2.4 like sentence

In MySQL, you can use the select command to read data, and you can also use the where clause in the select statement to find the required data. When searching, sometimes it is necessary to get all the records containing a certain character in a certain field. At this time, the like word is needed.
In the like sentence, use% to represent all characters. If there is no %, then the like sentence has the same effect as "=". Like sentences can also use and and or to specify one or more conditions.

The data used in the experiment comes from MySQL

select * from class where number like '%87';
select * from class where number like '%89' or name like '张%' and class_name like '软工1902';

Insert picture description here

2.5 order by

MySQL can use the order by word to sort, use any field as the sort condition, you can also add where...like words to set the query conditions, use ASC or DESC to set the results in ascending or descending order.

select * from class order by number desc;

Insert picture description here

2.6 group by

The GROUP BY statement groups the results according to one or more columns. The group by syntax is as follows:

select column_name,function(column_name) from table_name
[where column_name operator value]
gropu by column_name;
#其中function表示mysql中的一些函数,常见的有SUM,AVG,COUNT等 

Example
Group the records in the class table according to class_name and count the number

select class_name,count(class_name) from class 
group by class_name;

Insert picture description here

3. Matters needing attention

1. When creating a database, the
column name symbol uses backquotes, not English quotation marks. The
column name symbol uses backquotes, not English quotation marks. The
column name symbol uses backquotes, not English quotation marks.

Tell the important thing three times

2. The punctuation marks in English are used in the whole operating environment.

4. The author chats

For more details, please refer to the MySQL development documentation . We criticize and correct the inadequacies in the syntax of the MySQL development documentation , and continue to update.

Guess you like

Origin blog.csdn.net/cly_32/article/details/112156960