`

常用SQL语句

阅读更多
DDL操作

--创建数据库: ­

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 --开启事务 ­

DML操作 ­

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
2
2
分享到:
评论

相关推荐

    ORACLE常用SQL语句大全.pdf

    Oracle 常用 SQL 语句大全 本文档总结了 Oracle 中常用的 SQL 语句,包括数据库的创建、删除、备份、表的创建、删除、修改、索引的创建、视图的创建等基本操作,以及一些高级查询运算符的使用。 一、数据库操作 ...

    50个常用SQL语句.zip

    50个常用SQL语句.zip50个常用SQL语句.zip50个常用SQL语句.zip50个常用SQL语句.zip50个常用SQL语句.zip50个常用SQL语句.zip50个常用SQL语句.zip50个常用SQL语句.zip50个常用SQL语句.zip50个常用SQL语句.zip50个常用...

    oracle常用SQL语句(汇总版).docx

    Oracle 常用 SQL 语句汇总 Oracle 是一个功能强大且复杂的关系数据库管理系统,它提供了多种 SQL 语句来管理和操作数据库。在本文中,我们将详细介绍 Oracle 中常用的 SQL 语句,包括数据控制语句(DML)、数据定义...

    Mysql常用SQL语句

    MySQL常用SQL语句 MySQL是一种关系型数据库管理系统,使用SQL(Structured Query Language)语言来管理和操作数据库。下面是MySQL中一些常用的SQL语句: 创建、删除和基本查询 * 显示数据库:`show databases;` *...

    EXCEL 常用SQL语句解释+VBA 完全手册

    学习"EXCEL 常用SQL语句解释+VBA 完全手册"的内容,不仅可以提升你在Excel中的数据处理能力,还能够使你在面对复杂数据挑战时游刃有余。无论你是数据分析师、项目经理还是企业决策者,掌握这些技能都将大大提高你的...

    oracle常用sql语句大全 注释完整 放心使用

    本资料集包含了Oracle数据库中常用SQL语句的大全,注释完整,非常适合初学者和有经验的开发者参考使用。 1. **数据查询(SELECT语句)**: - `SELECT * FROM table_name;`:查询表table_name中的所有列。 - `...

Global site tag (gtag.js) - Google Analytics