MySQL Learning (II) - Using MySQL

MySQL Learning (II) - Using MySQL

2.1 connection

All MySQL with the client - the same server DBMS, asked to log in to the DBMS before being able to execute the command

First, make sure your computer has started mysql

User login actions:

/usr/local/mysql/bin/mysql -u root -p
#这里面前面是mysql的默认安装位置,-u root是使用root用户进行登录,-p是不显示密码进行输入

Select Database 2.2

When the beginning is not connected to a mysql database, it is necessary to select a database

Input:

USE crashcourse
#这是建立在你已经建立了名字为crashcourse数据库的基础上
#会显示Database changed

Creating a database

create database xxx
#创建一个叫xxx的数据库

2.3 understand the contents of the database

Showcase already existing database:

show databases;

Display a list already exists in the database

show tables;

Other commands on the show:

show columns from xxx;
#展示xxx数据库中的列表
#同样可以使用 describe xxx这样的快捷命令

show status;
#用于显示服务器状态

show create database;
show create table;
#用来显示创建特定数据库或表

2.4 to retrieve data

A data retrieval

select prod_name from xxx;
#从xxx表里面选择prod_name的列

Retrieve multiple columns of data

select prod_id,prod_name,prod_price from xxx;

Retrieve all columns

select * from xxx;

Different search lines:

Sometimes such as student management system subjects, needs to appear only once, this time on the need to retrieve and mysql have the ability to return a different item

#使用关键字distinct
select fistinct vend_id from xxx;

Limit your search requirements:

select prod_name from xxx limit 5;
#找出从第一行开始的前面五个

select prod_name from xxx limit 5,5;
#找出从第五行开始的五行数
#只有一个数,则是默认从第一行开始,若有两个数,则第一个数为起始位置
#数据库中有行0的概念,因此limit 1,1;是从第二行开始检索的

#从MySQL5后面还支持另外一种写法
limit 4 offset 3;
#表示从第三行开始取四行,等同于limit 3,4;

2.5 Sorting retrieve data

2.5.1 Sorting Data

Under normal conditions, SQL statements, are disordered, without sorting, the data will typically show a sequence that appears at the bottom

We can use the order by name clause to take one or more columns, sort accordingly

select prod_name from xxx order by prod_name;

2.5.2 sort by multiple columns

select prod_id,prod_price,prod_name
from xxx
order by prod_price,prod_name;
#上面这段的意思是,将id,price,name进行排序,而排序的规则是,先按照prod_name进行排序,如果出现相同,再按照prod_name进行排序,即第一个如果已经可以排序,则不会使用第二个进行排序

2.5.3 specify the direction of the sort

Sort in descending order can not be, it can be sorted in ascending order, this time on the need to use the desc keyword

select prod_id,prod_price,prod_name
from xxx
order by prod_price desc;
#如果还想使用上面的按多个列进行排序,且只有其中一项是倒序
select prod_id,prod_price,prod_name
from xxx
order by prod_price desc,prod_name;
#如果是对选择的每一列的排序都是要倒序的时候,则每一列都需要指定desc

Also, since there are desc , then of course there are ascending keywords asc , but because the default sort is ascending, it can not be considered

2.6 Filter data

2.6.1 Use the where clause

select prod_name,prod_price
from xxx
where prod_price = 2.5;
#这里返回的是表格的两列数据,其中使用了限定条件prod_price = 2.5

If you use order by and where , it should make the order by then located where

2.6.2 where clause operator

Operators Explanation
= equal
<> not equal to
!= not equal to
< Less than
<= Less than or equal
> more than the
>= greater or equal to
between Values ​​between two specified

2.6.3 Detection single value

select prod_name,prod_price
from xxx
where prod_name = 'xx'
#寻找prod_name里面xx对应的一行

2.6.4 null value detecting

select prod_name
from xxx
where prod_name is NULL;
发布了35 篇原创文章 · 获赞 3 · 访问量 1131

Guess you like

Origin blog.csdn.net/qq_44671752/article/details/104738589