常用SQL语句

[b]DDL操作[/b]

--创建数据库: ­

create database 数据库名 ­

--连接的数据库: ­

use 数据库名 ­

--创建表: ­

create table 表名 ­

--创建索引: ­

create unique index 索引名 on 表名(字段) ­

--删除索引: ­

drop index 表名.索引名 ­

--复制表结构: ­

select (字段) into 新表名 from 旧表名 where 1=0; ­

--复制表数据 ­

select * from into 新表名 from 旧表名; ­

--添加数据 ­

insert into 表名(字段名) values (数据); ­

--修改数据 ­

update 表名 set 字段 = '数据' where条件 ­

--删除指定数据 ­

delete 表名 where条件 ­

--删除数据库 ­

drop database 数据库名称 ­

--删除整个表 ­

drop table 表名 ­

--删除表数据 ­

truncate table 表名 ­

--排除重复数据 ­

select distinct 列名 from 表名 ­

--计算列 ­

select s_id,score,score+10 as 新的别名 from 表名 where条件 ­

--限定行 ­

select top 行数 * from 表名 ­

--条件查询: ­

select g_id,g_city from t_games where g_id = 2000 or g_city like ('成都'); ­

--范围测试 ­

select * from 表名 where 字段名 between '2003-09-09' and '2009-09-09'; ­

--值的集合(in) ­

select 列名 from 表名 where 列名 in ('value1','value2','value3'); ­

--模糊查询(like) ­

select * from 表名 where 列 like 模式 如:select * from u_user where s_name like '张__'; 也可以: like '张&%' ­

--处理空数据(is null,is not null) ­

select 字段名 from 表名 where 列 is null --为空 ­

select 字段名 from 表名 where 列 is not null --不为空 ­

--排序操作 ­

select 字段名 from 表名 order by 列 (ASC/DESC);

默认是升序 ­,ASC升序,DESC降序

--求折扣公式: ­

单价*折扣/10 = 现在已经打折扣的价格 ­

--事务公式: ­

begin trunsaction --开启事务 ­

[b]DML操作 ­[/b]

commit --提交事务 ­

roollback --回滚事务 ­

--添加多行数据: ­

insert into 已存在表名 (字段) select 字段 from 旧表名 ­

--分页显示 ­

select top 3 * from d where p_id in (select top 3 p_id from d); ­

--执行列计数 ­

select count(计数规范) as 列名 from 表名 ­

--返回列合计值(注意:不能使用*) ­

select sum(计数规范) from 表名 ­

--返回平均值 ­

select avg(all age) from 表名 ­

--返回最大值 ­

select max(最大值规范) from 表名 ­

--返回最小值 ­

select min(最小值规范) from 表名 ­

--数据分组 ­

select 列,聚合函数() from 表名 where过滤条件 group by 列 ­

select 列,聚合函数() from 表名1 别名1 left join 表名2 别名2 on 别名1.id=别名2.id group by 列;­

--having子句: ­

select 列,聚合函数() from 表名 where过滤条件 group by 列 having 条件 ­

--子查询语句 ­

select 列,(子查询) from 表名 --相关子查询 ­

select 列,列 from 表名 where 列 表达式 (子查询) --非相关子查询 ­

--添加约束: ­

primary key (主键约束) 、 unique (唯一约束) 、check (约束范围) 、Foreign key (外键约束) ­

default (默认约束) ­

--添加PK ­

alter table 表名 add constrint 主键名 primary key(列名) ­

--添加外键FK ­

alter table 表名 add constraint 外键名 foreign key(列名) references 引用表名(引用列); ­

--添加唯一UQ ­

alter table 表名 add constraint UQ名 unique (字段) ­

--添加默认DF ­

alter table 表名 add constraint DF名 default ('默认值') for 列名 ­

--添加CK ­

alter table 表名 add constraint CK名 check (数据范围) 如:年龄大于18 ­

--添加列 ­

alter table 表名 add 列名 列的类型 ­

--覆盖已存在的列(如有的时候需要给已有列指定非空-not null约束) ­

alter table 表名 alter column 列名 列的类型 约束­

--删除列 ­

alter table 表名 drop column 列名 ­

--其他

子查询在字段处(相关子查询):select a.*,(?) 别名 from 表1 a ­

子查询在From处(内存“表”):select a.* from (?) a ­

子查询在条件处(非相关子查询):select * from 表 where 字段>(?) ­

Exists子查询:select ? from ? where [not] exists ­

(select * from ? where 联接条件) ­

左连接通用写法:select * from a left join b on a.id=b.id ­

右连接通用 写法:select * from a right join b on a.id=b.id ­

全连接通用 写法:select * from a full join b on a.id=b.id ­

左连接Oracle 写法:select * from a,b where a.id=b.id(+) ­

右连接Oracle 写法:select * from a,b where a.id (+) =b.id
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值