查询语句的语法格式
Select 字段名 from 表名
可以与之有关的一些关键字
order by 后面跟字段名或者内置函数 asc (升序) desc(降序)
group by 字段 (单表的时候只能跟一个字段名且字段名与Select 和 from 字段名保持一致,
多表时可以看具体情况而定) 与having搭配使用
where(后面跟的是条件)
and 与
or或
内置函数
max(字段名)最大值
min(字段名)最小值
avg(字段名)平均值
sum(字段名)总和
count(字段名)统计某一字段的数量
count(1),count(*)常用
嵌套sql语句
Select 字段名 from 表名 where 字段 =(Select 字段 from 表名)
模糊查询
Select 字段 from 表名 where 字段 like " %";
或者Select 字段 from 表名 where 字段 like " _"
"_" 表示一个占位符;"%"用法需要额外注意
"% x%"表示查询的是字段中含有 x的
一些简单约束
unique 唯一
not null 非空
auto_increment 自增
primary key 主键
default 默认
如何创建外键约束和级联操作如下,仅做参考 欢迎有错误指正
Create table emp{
id int primary key auto_increment,
name varchar(32)not null,
deptId int not null,
regTime stamptime default current_stamptime,
constrain fk_emp_dept foreign key(deptId)
references dept(id)
on update cascade
on delete cascade);