添加/删除数据库:alter/drop database name
使用数据库:use name
整型:tinyint(0~255,1字节),smallint,mediumint,int,bigint
浮点型:float,double,decimal(M,D)(m表示数据长度,D表示小数点后长度)
日期:year(1901~2155),date(1000-01-01~9999-12-31) (year(now()))当前时间年份
datetime(1000-01-01 00:00:00),timestamp(1970001080001)
字符串:char(存储空间大小固定),varchar(可变长度)
text:用于存储大文本数据
二进制:varbinary(M)允许长度为0~M的变长二进制字符串,字节数为值二点长度加1
blog(M)一般用与存储图片等
create table语句创建表
查看表结构:desc name/show create table name\G
修改字段位置:alter table name 字段名1 字段类型1 first/after 字段名2 字段类型2
添加字段:alter table name add 字段名 数据类型
删除字段:alter table name drop 字段名
修改引擎:alter table name engine=存储引擎名
复制表:create table 表名 select * from 源表名 where false
复制表部分字段:create table 表名 as(字段1,....字段N from 源表名);
删除表:drop table 表名
(replace(有重复行会替换))insert into name(字段名) values
(字段对应值逗号隔开)
插入其它表数据:
insert into 目标数据表(字段列表)
select uname,usex,upwd(一一对应)
from user
where ..........
修改数据:update name set 字段名1=值1,字段名n=值n [where .....]
删除数据 delete from name [where ......]
select from where
group by 列名[asc/desc] [having ....]
order by [asc/desc]
limit 数量(3)(3,3)
运算符:
比较运算符,逻辑运算符
between and :where 表达式 between 初始值 and 终止值
in:where 表达式 in(值1,值n)
like:where 列名 like '字符串' (s%查询以s开头的任意字符串)(_s查询以s开头且长度为2的字符串)
(使用like是要注意加转义字符\)
聚合函数:sum,avg,max,min
count函数:select count(*)统计字段数
select count(distinct 字段名) 统计去重的字段数
group_concat(字段名)函数
内连接,左连接(显示左表中不满足的条件,输出为null),右连接(左连接相反)
交叉连接:select .... from 表一 cross join 表二
联合查询:union关键字连接
子查询,使用in关键字的子查询
排序dense_rank() over(order by .... desc) as ... from ....;