SQL基础

作者: 瓜皮小咸鱼 | 来源:发表于2019-04-02 22:02 被阅读0次

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

    sql-基础sql-基础查询-1sql-基础查询-2sql-更新 概览 数据库(Database,DB):将大量数...

  • SQL基础及元数据获取(数据类型,表的属性)

    1、SQL基础应用 ①.SQL的介绍SQL标准:SQL-92、SQL-99SQL_MODE:都是为了保证SQL语句...

  • MySql手动注入

    information_schema SQL基础 1.1 什么是sql? SQL(structured query...

  • MySQL

    数据类型 sql基础 数据库表 SQL SELECT 语句: SQL WHERE 子句: SQL AND & OR...

  • SQL语句

    SQL基础应用 SQL语句自动补全 SQL的介绍 SQL-92标准SQL-99标准 image SQL常用分类 表...

  • SQL高级运用

    -- =================================sql基础补充==============...

  • mysql的用法2

    -- =================================sql基础补充==============...

  • Oracle学习-day26:SQL语句

    一、SQL语言基础 1.什么是SQL语言? (1)SQL, Structured Query Language, ...

  • oracle 基础复习

    1. SQL 基础 https://mubu.com/doc/3ANPHhveeK 2. PL/SQL 基础 ht...

  • mysql手工注入

    SQL基础 1.1 什么是sql? SQL(structured query language),即结构化查询语言...

网友评论

      本文标题:SQL基础

      本文链接:https://www.haomeiwen.com/subject/wgrnbqtx.html