新睿云

> 云数据库MySQL > MySQL18条常用命令,不论是新手还是大神你们都离不开“它”

MySQL18条常用命令,不论是新手还是大神你们都离不开“它”

作者/来源:新睿云小编 发布时间:2019-11-18

MySQL18条常用命令,不论是新手还是大神你们都离不开“它”

MySQL绝对可以算免费社区类型做的最好的了,当然Mysql自然也有商业版本的,要想知道双方有什么不同可以参考《MySQL免费版与商业版具体有哪些区别呢?

此篇文章我要介绍一下MySQL实用的命令,这些命名是我们必须掌握的,可以说使用命令足有90%以上。无论您是DBA还是开发人员,都不妨碍您从各个角度看待理解本文。

1、mysql -u root -p命令来连接到Mysql服务器;

mysqladmin -u root password "new_password"命令来创建root用户的密码。

2、查看当前有哪些DB:show databases;

添加DB:create database mx;(mx数据库名)

删除DB:drop database mx;

使用DB:use mx;

3、创建数据表table

creat table table_name(colum_name data_type,colum_name data_type,..colum_name data_type,);

查看表字段:describe table_name;

4、增加列

alter table 【table_name】add 【column_name】 【data_type】[not null][default];

删除列

alter table 【table_name】drop 【column_name】;

5、修改列信息

alter table 【table_name】change 【old_column_name】 【new_column_name】【data_type】

只改列名:data_type和原来一样,old_column_name != new_column_name

只改数据类型:old_column_name == new_column_name, data_type改变

列名和数据类型都改了

6、修改表名

alter table 【table_name】rename【new_table_name】;

7、查看表数据

select * from table_name;

select col_name,col_name2,...from table_name;

8、插入数据

insert into 【table_name】 value(值1,值2,...);

insert into 【table_name】 (列1,列2...)value(值1,值2,...);

9、where语言

select * from table_name where col_name 运算符 值;

组合条件 and、or

where后面可以通过and与or运算符组合多个条件筛选

select * from table_name where col1 = xxx and col2 = xx or col > xx

10、null的判断 - is /is not

select * from table_name where col_name is null;

select * from table_name where col_name is not null:

11、distinct(精确的)

select distinct col_name from table_name;

12、order by排序

按单一列名排序:

select * from table_name [where 子句] order by col_name [asc/desc];

按多列排序:

select * from table_name [where 子句] order by  col1_name [asc/desc], col2_name [asc/desc]...;

不加asc或者desc时,默认为asc

13、limit限制

select * from table_name [where 子句] [order by子句] limit [offset,] rowCount;

offset:查询结果的起始位置,第一条记录的其实是0

rowCoun:从offset位置开始,获取的记录条数

注:limit rowCount = limit 0,rowCount

14、insert into与select组合使用

insert into 【表名1】 select 列1, 列2 from 【表名2】;

insert into 【表名1】 (列1, 列2) select 列3, 列4 from 【表名2】;

15、updata语法

修改单列

updata 表名 set 列名 = xxx [where 字句];

修改多列

updata 表名 set 列名1 = xxx, 列名2 = xxx...[where 字句];

16、in语法

select * from 表名 where 列名 in (value1,value2...);

select * from 表名 where 列名 in (select 列名 from 表名);

17、between语法

select * from 表名 where 列名 between 值1 and 值2;

select * from 表名 where 列名 not between 值1 and 值2;

18、like语法

select * from 表名 where 列名 [not] like pattern;

pattern:匹配模式 , 比如 'abc'  '%abc'  'abc%'  '%abc%'

'%' 是一个通配符,理解上可以把它当成任何字符串

例如:'%abc'   能匹配  'erttsabc'

18条基本命令,或许看起来很简单,但使用的时候您真的能灵活掌握吗?万层高楼平地起,如果有不熟悉的命令赶紧从MySQL基础看起,再复习一遍总的不是坏事,或许您需要《postgresql和mysql:性能、语法、功能有哪些差异?

热门标签
new year
在线咨询
咨询热线 400-1515-720
投诉与建议
{{item.description}}

—您的烦恼我们已经收到—

我们会将处理结果发送至您的手机

请耐心等待