exit; 退出
show databases; 查看所有数据库
show tables; 查看某个数据库下所有表
use 数据库名; 选择某个数据库
create database 数据库名; 创建数据库
drop database 数据库名; 删除数据库
source sql文件路径; 导入SQL文件
desc 表名; 查看表结构【describe 表名;的缩写形式】
简单查询
select version(); 查看mysql数据库的版本号
select database();用于查看当前使用的数据库的名字
select * from 表名; 查看表数据【*代表查看表中所有的字段】
select 字段名1[,字段名2,....] from 表名; 查询某一字段【“[]”中的内容代表可有可无】
select 字段名1 as 别名1[,字段名2 as 别名2,.....] from 表名; 给字段起别名
条件查询:
select
字段1[,字段2,...]
from
表名
where
条件;
条件包括:
=、!=
> 、>=、<、<=
and 、 or 、not
between ... and ...
is null、is not null
in 、 not in
like
排序: select
字段名1[,字段名2,...]
from
表名
order by
排序的字段1[,排序的字段2,...] [asc / desc],
排序的字段1[,排序的字段2,...] [asc / desc]
...;
asc代表升序,desc代表降序。可以按照多个字段进行排序。
可以同时指定有的字段升序,有的字段降序排序。【默认是sac可以不写】
总结:
select
...
from
...
where
...
group by
...
having
...
order by
...
limit
...
以上关键字的顺序不能颠倒。
执行顺序是什么?
1.from
2.where
3.group by
4.select
5.having
6.order by
7.limit
创建表
create table 表名 (
字段名1 数据类型(长度) [约束 默认值 描述] #省略长度默认是该类型的长度最大值
[,字段名2 数据类型(长度)]
[,....]
);
删除表
drop table 表名;
drop table if exists 表名;
创建数据库
create database 数据库名;
删除数据库
drop database 数据库名;
drop database if exists 数据库名;
更新表数据
update 表名 set 字段名1=值1[,字段名2=值2,...] where 条件;
删除表中所有的数据:truncate table 表名;速度较快不支持回滚
show create table 表名;//用于查看表创建语句。
show engines; 查看MySQL支持的所有存储引擎。
创建索引对象:
create index 索引名称 on 表名(字段名); 【多个字段之间使用逗号分割】
删除索引对象:
drop index 索引名称 on 表名;
怎样创建视图对象?
create view 视图名 as 查询语句;
怎么删除视图对象?
drop view 视图名;