库:database
create 创建库 drop 删除库 show databases 查看库 use 库名 使用库
表:table
create 创建表 drop 删除表 alter 修改表 show tables 查看表 desc 查看表结构
show columns from students 查看表结构
insert into students (id,name,age)values(1,'user7',70) 插入数据模板
CREATE TABLE students ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT); 创建表模板
show columns from students #查看表结构
select * from db1.student #查看当前表
insert into students(id,name,age,gender)values(1,'user7',70,'M') #插入数据模板
CREATE TABLE student (
ID INT,
NAME VARCHAR(50),
AGE INT,
GENDER VARCHAR(10)
);
#创建表模板
reset slave 清空从服务器的数据
select 查询
insert into values(,,,)向表中插入值
update set 修改数据表
运算符
比较运算符:= > < != <> is not null
逻辑运算符: and or not
分组:group by having
多表联合查询:
用逗号分隔
语法:多表查询语法
select 字段1,字段2... from 表1,表2... [where 条件];
注意: 如果不加条件直接进行查询,则会出现, 笛卡尔乘积
注意: 多表查询时,一定要找到两个表中相互关联的字段,并且作为条件使用
内连接:
select 字段列表 from 表1 inner join 表2 ON 表1.字段 = 表2.字段;
外链接:
左连接:
select 字段列表 from 表1 left join 表2 ON 表1.字段 = 表2.字段;
右链接:
select 字段列表 from 表1 right join 表2 ON 表1.字段 = 表2.字段;
子查询:就是嵌套另一个sql语句
union :连接俩个查询语句
模糊匹配:like
关键字:
去重复:distinct
分页:limit
升序:order by(默认) asc
倒序:desc
多少到多少:salary between 值 and 值
函数:
流程控制函数:
if 三目表达式
IF(expr1,expr2,expr3)
如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 否则返回值则为 expr3。IF() 的返回值为数字值或字符串值,具体情况视其所在语境而定。
分组函数:
avg(平均) max(最大) min(最小) sum(求和) count(总数)
约束:
not null (非空) unique(唯一)
数据类型:
tinyint int/integer decimal timestamp char varchar