基本SQL语句使用方法

结构:
增: create database 库名 charset 字符集;
create table 表名称(字段名 类型 约束 ,字段名 类型 约束)

not null 非空
primary key 主键 唯一且非空
unque 唯一约束 唯一可为空
foreign key 外键约束 可为空,非空值与主键对应

删: drop database 库名;
drop table 表名;
改: alter table 表名 add 字段名 类型 约束;
drop 字段名;
change 旧字段名 新字段名 类型;
modify 字段名 类型 约束;
remove 新表名
查: desc 表名;查看表结构
show tables;查看所有表
show datebases;查看所有库
show create datebase 库名称; 查看指定库属性
show create table 表名称;查看指定表属性

增 insert into 表名 values(“”,“”~~)

删 delete from 表名 where ~~

改 update 表名 set 字段=新值 where ~~

查 select * from * where ~~


跳x行取y行

Select * from * limit(x,y)
Select * from * limit y offset x

内连接:
Select * from 表A inner join 表B on(条件)
左连接 Select * from 表A left outer join 表B on(条件)
右连接 Select * from 表A right outer join 表B on(条件)


联合查

Select concat(str ,str) 直接
Select concat_ws(sub,str ,str) sub为隔离符号
Select group_concat(str,str) 逗号隔离

常用查询

Select database(); 查询当前数据库;
Select user(); 查询当前用户;
Select version 或@@ version 查询数据库版本;

字符处理
Select left(str,x);左边取x位
Select right(str,x); 右边取x位
Select lower (str );转小写
Select upper (str ); 转大写
Select length(str);输出长度
Substr (str,x,y); str 的x位开始取y位
ltrim rtrim trim 去空格
reverse(str); 翻转

取日期
currdata();当前日期
currtime (); 当前时间
date()取日期
time() 取时间
hour()取小时
minute()取分钟
month()取月份
now()当前日期
dayofweek() 取星期几

数据查询语言 DQL
数据操纵语言 DML
数据定义语言 DDL
数据控制语言 DCL

注释:#~ 单行注释 /* */多行注释 -- 单行注释 必须—后加空格

原文地址:https://www.cnblogs.com/mutudou/p/11767682.html