SQL基础

1.select 查询
2.distinct 去重查询 如 select distinct
3.where 查询条件
4.and or 且 或
5.order by 查询结果排序 (asc desc)
6.insert into 插入数据 如: insert into table_name values ('');
7.update 更新数据 如: update table_name set col = '' [where ];
8.delete 删除数据 如: delete from table_name;
9.top 规定返回查询数目 (sql server用法) 在mysql中使用limit 在oracle中使用where rownum
10.like where中的查询模式 如 like '%' like '_'
11.in 在条件中查询匹配多个值
12.between...and... 选取两个值之间的数据范围
13.join 从两个表或多个表获取查询结果
其中有
inner join(内连接) 当表中存在至少一个匹配时 返回行 (即两个表中满足ON条件的行)

select column_names
from table_name1 inner join table_name2
on table_name1.column = table_name2.column;

left join (左连接) 返回左表中所有的行 即使在右表中没有匹配的行 右表中不存在的为NULL
right join(右连接) 返回右表中所有的行 即使在左表中没有匹配的行 左表中不存在的为NULL
full join(全连接) 只要其中某个表存在匹配 就返回行 为NULL

14.union union all 合并select查询结果 union all不去重 返回所有结果
15.select into

select * into new_table_name 
from old_table_name; 

16.常见的数据类型
integer int smallint tinyint decimal(size, d) char() varchar() date(yyyymmdd) timestamp

17.SQL约束
not null 不能为空
unique 唯一
primary key 主键约束
foreign key 外键约束
check 限制列中值的范围
default 设置默认值

18.索引 index

create index index_name 
on table_name (col_name); 
drop index index_name on table_name;

19.truncate 清空表

20.alter table

alter table table_name 
add column_name datatype;
alter table table_name
drop column_name;

21.视图

create view view_name as 
select column_names
from table_name 
where condition;
drop view view_name;

22.date函数
now() 返回当前的日期和时间
curdate() 返回当前日期
curtime() 返回当前时间
date() 提取日期
date_add() 给日期添加指定的时间间隔
date_sub() 给日期减去指定的时间间隔
datediff() 返回两个日期之间的天数
date_format() 用不同的格式显示日期
23.NULL
is null is not null
IFNULL() 若为空 返回第二个参数
COALESCE() 遇到非null值 返回该值 并停止
例:

select COALESCE(null, null, 1, 2) --返回1
select COALESCE(null, null, null, null) --返回null
select if(1=1, 1, 0)

24.mysql中主要的数据类型:文本,数字,日期
text类型:
char() varchar() tinytext() 最大都为255
text, blob,mediumtext, mediumblob, longtext, longblob
enum() 枚举 set 集合
number类型:
tinyint smallint mediumint int bigint float double decimal
date类型:
date() datetime() timestamp() time() year()

25.函数
group by , having
avg() count() max() min() sum() ucase() lcase() mid() len() round()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文为 SQL必知必会 的读后笔记 SELECT 语句 多条SQL语句必须以分号(;)分隔,建议加上 ...
    Skypew阅读 4,032评论 0 0
  • 夜已沉睡,世界很静,而我却醒着。若棉纷飞的“心絮”,伴着窗外凝练如水的月华,带着我幽长的情思于深夜里远行。...
    漠洋阅读 2,688评论 0 1
  • 大家早上好,在这个春雷阵阵的日子,感谢各校校长、各位老师,来参加2018春季开学暨表彰大会,也感谢郑校长和徐校长给...
    小茹老师阅读 1,592评论 0 0
  • 你的姓, 是墨客的姓。 你的名, 是骚人的名。 谢兰燕桂,泽被四邻,文章经济。 你去到星星升起的地方, 是选择, ...
    夏实1999阅读 3,244评论 0 2
  • 你急欲离去 如雁的心思在南方 趕在最後一片叶子 轻轻落下而秋雨未至 那一山的枯萎 是我看见的世界 风吹来海洋的味道...
    红尘久客阅读 2,144评论 1 10