mysql命令简录

最近在学mysql,特来记下学过的mysql命令,做复习之用,防止忘记,有点杂,书籍《mysql必知必会》。

mysqld initialize

初始化mysql

mysqld install

安装mysql

mysqladmin --version

查看版本

mysql

登录mysql

mysql -u root -p

使用root用户登录

mysqladmin -u root password "new_password";

更改root密码

net start mysql,net stop mysql,sc delete mysql

启动,停止,卸载mysql服务

show databases

查看数据库

show tables

查看该数据库下的表

describe 表名

查看该表下的具体列

use 数据库名

使用数据库

create database 数据库名称;

创建数据库

drop database 数据库名称;

删除数据库

create table table_name(column_name column_type);

创建表

drop table table_name;

删除表

alter table old_name rename to new_name 或 rename table old_name to new_name

更改表名

alter table table_name add column_name column_type;

为表增加列

alter table table_name change column_name new_column_name new_column_name_type;

修改列

alter table table_name drop column_name;

删除列

insert  表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

插入数据

insert  表1 [(列名1, 列名2, 列名3, ...)] select [(列名1, 列名2, 列名3, ...)] from 表2;

从表2中检索出的数据将全部插入到表1对应列。

update  [ignore] 表名  set  字段 = 值  [where 条件];

更新数据,没有where子句则会更新整个表,添加ignore使得update操作失败后,之前更新的数据会保存,未添加则不会保存。

delete from  表名  [where 条件];

删除数据,删除的是一行,并非某列,若删除列需使用update,没有where子句则会删除整个表的内容,但并不会删除表本身。

truncate table 表名;

删除整个表数据,会比使用delete快,实际上truncate 是删除整个表,然后在重新创建一个表。而不是逐行删除。

select * from  表名  [where 条件];

查询全部数据

select  字段名称[,字段名称]  from  表名  [where 条件];

查询指定字段数据

select  distinct 字段名称[,字段名称]  from  表名  [where 条件];

查询指定字段数据,和上面不同的是使用distinct关键字会返回唯一的行数据,比如:

查询students表中的sex字段,用select  sex  from students;会返回每个学生的性别:男,男,女,女···等等,而使用select  distinct sex  from  students;则返回:男,女。也就是说每个值只会返回一次。

select * from  表名  [limit x,y]; 或 select * from  表名  [limit y offset x];

查询全部数据,使用limit关键字限制显示行数,x为起始位置,可选,默认0(第一行为行0),y为显示行数。

select * from  表名  order by 列名,列名···,desc;

对查询的数据按order by 后的列按列顺序以字典序排列,只有当前列的值相同时才会用到后面的列来排序,在列名后加上desc,则按字母降序排列。desc只作用于在它前面的列,如果需要联合limit使用,order by 应放在from后面,在之后才是limit,如果在加上wherewhere应该放在order by前面。

where关键字

where用来过滤select查询的数据,比如:select * from  students  where age = 20;为查询students表中所有年龄等于20的学生,类似的有:<><=>=!=,特殊的:<>为不等于,等同!=between x and y,为x~y之间的,包含x,y。

使用is null来检测值为null数据,如:select * from  students  where tel is null; 为查询students表中所有 电话的值为null的学生。null不等同于0、空字符串、仅包含空格。

where字句允许使用andor来联结表达式,特别的,and的优先级比or高,所以,必要时,可以使用圆括号来确定运算顺序。如:select * from  students  where age = 20 and sex = ‘男’; 为查询符合年龄等于20且性别为男的所有学生。

in关键字,如:select * from  students  where age in(20,21);这和select * from  students  where age = 20 or age = 21; 的作用是相同的。

not关键字,作用为否定后跟条件,如:select * from  students  where age not in(20,21);为查询除了年龄为20和21的所有学生。

通配符%和_

%为匹配任意字符任意次数,_为匹配单个任意字符。在搜索子句中使用通配符需使用like关键字,应尽可能少的使用通配符,如:select * from  students  where tel like "180%";为匹配电话号码开头为180的所有学生。

regexp正则表达式

使用方法和like类似,将like替换为regexp即可,注:正则表达式和通配符类似但不同。like匹配整个串,regexp匹配子串。

函数concat(),trim(),AS关键字

concat为连接函数,trim为去掉左右空格函数,如:select concat(trim(id),' ',name) from students;会把去掉左右多余空格的id值和一个空格字符加上name的值连接在一起成为一个单独的列显示出来。若在from的前面加上AS id_name则将这一列起一个别名为id_name,只有拥有别名才能被客户机引用。

支持+,-,*,/算术运算

mysql还支持列和列之间的算术运算。如:select id * age from students;

支持avg(),count(),max(),min(),sum()函数运算

如:select avg(age) from students;返回学生的平均年龄,其他函数使用方法类似。大多数的函数都会忽略掉null值。

group by 和 having子句

group by 为分组操作 ,和order by 类似,having 为过滤操作,和where使用类似。不同的是前者对应组,后者对应行。

子查询(select嵌套)

select可以嵌套使用,如:select order_num from orderitems where price = 200;(结果为20,27), select id from orders where order_num in(20,27); 等同于 select id from orders where order_num in(select order_num from orderitems where price = 200);

联结,SQL中最重要最强大的特性(内部联结 inner join···on, 外部联结 left/right outer join···on)

使用联结可以达到上述子查询相同的结果,如:select id from orders,orderitems where order.order_num = orderitems.order_num and orderitems.price = 200; select id from orders inner join orderitems on order.order_num = orderitems.order_num and orderitems.price = 200;

union关键字

使用union来进行组合查询,和where类似,但union可以在多个表中使用,在多表查询中unionwhere要方便,如:select id,age from students where id in(1,2,3,4,5) union select id,age from students where age < 20; 等同于 select id,age from students where id in(1,2,3,4,5) or age < 20;

create view 视图名 as select语句;

创建一个视图,视图提供一种mysql的select语句封装,用来简化数据处理。

drop view 视图名;

输出视图。

create or replace view 视图名 as select语句;

若视图已存在则不创建,否则创建一个视图。

call 存储过程名();

执行存储过程。

create procedure 存储过程名() begin ··· end;

创建一个存储过程。存储过程类似于批处理。

drop procedure 存储过程名;

输出存储过程。

declare a 数据类型;

定义变量a。

if 条件 then ··· endif;

if语句。

select语句 into a;

select的检索数据赋给变量a。

declare 游标名 cursor for select语句;

定义一个游标,用来检索后面的select语句所得的数据。游标只能在存储过程中使用。

open/close 游标名;

打开或关闭游标。

fetch 游标名 into a;

使用fetch来检索数据并存入变量a。

declare continue handler for 条件 set ···;

若条件成立则执行set后面的语句。

repeat ··· until 条件 end repeat;

循环语句,当条件成立时结束循环。

create trigger 触发器名 before/after insert/update/delete on 表名 for each row ···;

创建一个触发器,触发器只对表的插入、更新、删除支持。当出现上述操作时,触发器响应操作执行for each row 后的操作。

drop trigger 触发器名;

删除触发器。

show character set;

查看mysql支持的字符集。

show collation;

查看mysql支持的校对。

create user 用户名 identified by 密码;

创建用户。

drop 用户名;

删除用户。

rename user 用户名1 to 用户名2;

用户1重命名为用户2。

show grants for 用户名;

查看用户权限。

grant ··· all to 用户名;

授予用户在整个服务器的某一项权限。

revoke ··· all from 用户名;

撤销用户在整个服务器的某一项权限。

set password for 用户名  = password('123');

设置用户密码为123。

猜你喜欢

转载自blog.csdn.net/hunt_er/article/details/81604107