SQL常用语句总结

版权声明:转载请注明出处 https://blog.csdn.net/yrwan95/article/details/82146057

<>表示此处位置的数据 []表示可选项

DML(数据操纵语言)

增(INSERT)

insert into <表名> (列名) values (列值)
insert into <已有的新表> (列名) select <原表列名> from <原表名>

删(DELETE)

delete from <表名> [where <删除条件>]

改(UPDATE)

update <表名> set<列名=更新值> [where <更新条件>]

查(SELECT)

精确查询

select * from <表名>
select <列名> "别名" from <表名> [where <查询条件>] [order by <排序的列名>[asc或desc]]
select '常量名' "别名" from <表名>
select top 数量 <列名> from <表名>
select top 百分比 percent <列名> from <表名>

模糊查询

select * from <表名> where <列名> like 'a%'
select * from <表名> where <列名> between 1 and 20
select * from <表名> where <列名> in ('AA','BB','CC')

分组查询

select <列a, AVG(列b)> from <表名> group by <列a> [having <筛选条件>]

内联接

select a.列一, b.列二
from a, b
where a.列A=b.列A

select a.列一, b.列二
from a join b on (a.列A=b.列A)

外联接

select a.列一, b.列二
from a left|right|full outer join b on (a.列A=b.列A)

DDL(数据定义语言)

create table <表名>(列名 类型(长度), .......)
create table <表名> as select <原表列名> from <原表名> [where <条件>]
drop table <表名>
truncate table  <表名>
rename <表名> to <新表名>
alter table <表名>
add(列名 类型(长度))
nodify(列名 类型(长度) [default 默认值])
drop column <列名>
rename column <列名> to <新列名>

DCL(数据控制语言)

commit
savepoint
rollback [to savapoint A]

猜你喜欢

转载自blog.csdn.net/yrwan95/article/details/82146057