name | sorce |
---|---|
1 | 61 |
2 | 79 |
3 | 100 |
wangfei | 80 |
wanhong | 90 |
fenggong | 95 |
一、基础显示相关
显示可用的数据库:show databases;
使用数据库A(database):Use A
;
显示数据库A的表:Show tables; 因为已经在使用A了
查看表结构:desc 表名;
Show tables from 库名;
Show Create database 数据库名称;
二、创建(库/表操作)
【可选】
创建数据库:Create database 【if not exists 】 数据库名称;
创建表: Create table 表名(列名1 列名类型,列名2 列名类型);
删除数据库: drop database 数据库名称;
修改字符集: alter database 数据库名称 character set 字符集名称;
修改表名: alter table 表名 rename to 新的表名;
添加一列: alter table 表名 add 列名 数据类型;
修改列名: alter table 表名 modify 列名 新数据类型;
修改列名: alter table 表名 drop 列名 ;
删除表 : drop table 表名;
加数据: Insert into 表名(列名1,…列名n)values (值1,…值n);
加数据: Insert into 表名values (值1,值2,…值n);
删除数据
:Delete from 表名 where 条件;
修改数据
:Update 表名 set 列名1=值1,列名2=值2,where 条件;
三、查询相关
1、基础格式
select
字段列表from
表名列表where
条件列表
group by分组字段
having分组之后的条件
order by排序
limit分页限定
2、检索多个列:select 列名1,列名2 from 表名;
3、distinct
去重
:select distinct 列名 from 表名;
4、limit
限定行数
:select 列名 from 表名 limit 5;显示前5行
select 列名 from 表名 limit 5,5;从第5行开始显示5行
5、order by
排序
:select 列名1,列名2,列名3 from 表名 order by 列名1,列名2;先按照1,再按照2排序
降序:
SELECT * FROM employees ORDER BY salary DESC;
先降序,再升序:
SELECT * FROM employees ORDER BY salary DESC,employee_id ASC;
6、where
1、 select 名称 from 表名 where sorce
between
60 and 100;包括首尾
2、select name,sorce from 表名 where name=1
or
name=2and
sorce>10;
select name,sorce from 表名 where(
name=1 or name=2)
and sorce>10;
AND
在计算次序中优先级更高,在WHERE子句中使用圆括号
7、in\not
IN操作符用来指定条件范围,范围中的每个条件都可以进行匹配
select sorce from 表名 where name
in
(1,2);
select sorce from 表名 where namenot in
(1,2);
8、like(通配符)
通配符(wildcard) 用来匹配值的一部分的特殊字符。
8.1百分号(%)
通配符
%表示任何字符
出现任意次数
select sorce from 表名 where name like “wan%”;
select sorce from 表名 where name like “%ong%”;
8.2 下划线(_)
通配符
下划线只匹配单个
字符
SELECT * FROM student WHERE NAME LIKE ‘
___
’;查名字是三个字的