标签:password 聚合 drop 包含 esc 多表 lte 均值 nts
create
、alter
、drop
insert
、update
、delete
select
(面试和工作都是最重要的)revoke
、grant
net start mysql
net stop mysql
mysql -u username -p password [-hMySQL的ip地址 -PMySQL的端口]
create database databaseName;
show database;
-- 查看创建数据库的详细信息
show create database databaseName;
use database;
-- 查看在哪个数据库里
select database();
drop database databaseName;
MySQL | 说明 | 对应Java数据类型 |
---|---|---|
int | 整型 | int/Integer |
varchar | 可变长度的字符串 | String |
date | 日期类型 | java.sql.Date |
datetime | 日期时间类型,yyyy-MM-dd HH:mm:ss,范围1000-9999 | java.sql.Timestamp, java.sql.Date |
timestamp | 时间戳类型,yyyy-MM-dd HH:mm:ss ,范围:1970-2038 | java.sql.Timestamp |
decimal | 小数类型,常用金钱 | java.math.BigDecimal |
DCL
:数据库控制语言,管理数据库用户、权限等
create user 'userName' @ 'hostName' identified by 'password'
set password for 'userName' @ 'hostName' = password('newPwd')
drop user 'userName' @ 'hostName'
grant [select|update|delete|alter|drop...], ... on databaseName.tableName to 'userName' @ 'hostName'
show grants for 'userName' @ 'hostName'
revoke [select|update|delete|alter|drop...], ... on databaseName.tableName from 'userName' @ 'host Name';
create table tableName (
fieldName fieldType [constraint],
...
fieldName fieldType [constraint]
);
-- 创建表结构相同的新表
create table newTable like oldTable;
-- 查看所有表
show tables;
-- 查看表的结构
desc tableName;
-- 查看表创建信息
show create table tableName;
drop table tableName;
drop table if exists tableName
-- 重命名表
rename table oldTable to newTable;
-- 添加字段
alter table tableName add fieldName fieldType [constraint]
-- 修改字段名称
alter table tableName change fieldName newFieldName newFieldType [constraint]
-- 修改字段类型
alter table tableName modify fieldName fieldType [constraint]
-- 删除字段
alter table tableName drop fieldName
insert into tableName(fieldName1, fieldName2, ...) values (fieldValue1, fieldValue2, ...)
-- 对应表的所有字段,且值的顺序必须和字段顺序保持一致
insert into tableName values (fieldValue1, fieldValue2, ...)
-- 一条一条删,效率低
delete from tableName [where condition]
-- 一次性删除,效率高,但是不能加where条件
truncate tableName
update tableName set fieldName1=fieldValue1, fieldName2=fieldValue2, ... [where condition]
-- 查询全部
select * from tableName;
-- 查询指定列
select field1, field2, ... from tableName;
-- 查询并运算
select field1 + value1, field2 - value2, ... from tableName;
-- 查询运算做空值处理
select ifnull(field, 为空的值) + value1, ... from tableName;
-- 查询并起别名
select field1 alias1, field2 as alias2, ... from tableName;
-- 查询并去重
select distinct field1, field2, ... from tableName;
select * from tableName where condition
-- 比较: >, >=, <, <=, !=, <>
-- 范围:包含头和尾
field between head and tail
-- 集合
field in (value1, value2, ...)
-- 模糊查询:%表示任意个字符,_表示一个任意字符
field like '%queryValue%'
field like concat('%', 'queryValue', '%')
-- 语法
order by sortField1 sortRule1, sortField2 sortRule2, ...
-- 规则:ASC-升序(默认),DESC-降序
select 聚合函数 from tableName [where condition]
-- 统计个数
count(*)
-- 求和
sum(field)
-- 平均值
avg(field)
-- 最大值
max(field)
-- 最小值
min(field)
注意:聚合函数会忽略null值
select 分组字段, 聚合函数1, 聚合函数2, ... from tableName [where condition order by 排序字段] group by 分组字段
-- 分组之后进行过滤
select 分组字段, 聚合函数1, 聚合函数2, ... from tableName [where condition order by 排序字段] group by 分组字段 having count(*) > 6
说明:
分组字段
和聚合函数
limit index, size
分页通常位于sql语句的最后
select * | field, distinct field2, ... from [tableName inner|left[outer]|right[outer] join] tableName [on condition] where condition group by groupField having groupFilter union [all|distinct] order by sortField sortRule limit index, size;
-- 执行顺序
from --> on --> join --> where --> group by --> having --> select --> distinct --> union --> order by --> limit
select * from table1, table2 where relationCondition;
select * from table1 inner join table2 on relationCondition;
-- 左外连接:显示左表全部数据
select * from leftTable left[outer] rightTable on relationCondition;
-- 右外连接:显示右表全部数据
select * from leftTable right[outer] rightTalbe on relationCondition;
-- 单行单列-查询结果为一个值
select * from table where name=(select name from anotherTalbe where id='1');
-- 多行单列-查询结果是一个集合
select * from table where id in (select id from anotherTable where name like '%queryValue%');
-- 多行多列-查询结果是一张虚拟表
select * from table1, (select * from anotherTable) table2 where table1.id = table2.id;
标签:password 聚合 drop 包含 esc 多表 lte 均值 nts
原文地址:https://www.cnblogs.com/fztomaster/p/11470651.html