1增

1.1【创建一张表】

create table 表名(列名 类型);

例:

create table stu(姓名 varchar2(25),性别 varchar2(25),出生日期 date);
create table newstu(name varchar2(25),sex varchar2(25),出生日期 date);

1.2【插入单行】
insert [into] <表名> (列名) values (列值)
例:

insert into stu (姓名,性别,出生日期) values ('张三','男',to_date('1993-1-1','yyyy-mm-dd'));

1.3【将现有表数据添加到一个已有表】
insert into <已有的新表> (列名) select <原表列名> from <原表名>

列名可以不同,但是对应列的类型必须相同。
例:

insert into newstu(name,sex,出生日期) select 姓名,性别,出生日期 from stu;
--insert into newstu(name,sex,出生日期) select *from stu; --也是可以的

1.4【直接拿现有表数据创建一个新表并填充】
create table <新表> as select *from <现有表>
例:

create table nnstu as select *from newstu;

1.5【使用union关键字合并数据进行插入多行】
insert <表名> (列名) select <列值> union select <列值>
例:

在newstu表中多插入几条数据:

insert into newstu(name,sex,出生日期) values('李四','女',to_date('1995-12-25','yyyy-mm-dd'));
insert into newstu(name,sex,出生日期) values('王五','男',to_date('1996-3-8','yyyy-mm-dd'));

向stu表中插入多行数据:

insert into stu(姓名,性别,出生日期)
select '李四','女',to_date('1995-12-25','yyyy-mm-dd') from newstu union
select '王五','男',to_date('1996-3-8','yyyy-mm-dd') from newstu;

1.6【添加约束】

例:主键(id):alter table <表名> add  constraint con_<表名>_id_pk primary key(id) ;

--添加主键
alter table tbl_account add constraint con_tbl_account_id_pk primary key(id);
--添加非空
alter table tbl_account add constraint con_tbl_account_uname_ck check(username is not null);
--添加范围>
alter table tbl_account add constraint con_tbl_account_bal_ck check(balance>=0);
--添加范围in
alter table tbl_account add constraint con_tbl_account_gender_ck check(gender in ('男','女'));
--添加范围and
alter table tbl_account add constraint con_tbl_account_age_ck check(age >= 0 and age < 170);
--添加唯一
alter table tbl_account add constraint con_tbl_account_age_uk unique key(age);
--添加外键
alter table tbl_account add constraint con_tbl_account_age_fk foreign key(age) references 关联的表(关联的字段)
--添加索引
alter table cp_record add index index_cp_record_mem_id ( `mem_id` );

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2删

2.1【删除<满足条件的>行】
delete from <表名> [where <删除条件>]
例:

delete from stu where 姓名='张三';

2.2【删除所有行】
truncate table <表名>
例:

truncate table stu;

注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表

2.3【删除整张表】

drop table <表名>

例:

drop table stu;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3改

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

update newstu set name='胡八',sex='女' where name='张三'; 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

4查

4.1条件查询

4.1.1【按条件查询,按顺序展示】
select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]

例:

原字段为:name,sex,出生日期,更改列名as可加可不加

--性别为女且按出生日期降序排序
select name 姓名,sex 性别,出生日期 from newstu where sex='女' order by 出生日期 desc

说明:升序为asc

4.1.2【查询空行】
例:

select name 姓名,sex 性别,出生日期 from newstu where 出生日期 is null;

说明:查询表newstu中出生日期为空的所有行,并显示所有列;SQL语句中用is null或者is not null来判断是否为空行

4.1.3【在查询中使用常量】
例:

select name 姓名,sex 性别,出生日期,'浙江' as 地址 from newstu;

说明:查询表newstu,显示name列,并添加地址列,其列值都为'浙江'

4.1.4【查询返回限制行数(关键字:top percent)】
例:

--oracle中,前两条数据
select name 姓名,sex 性别,出生日期 from newstu where rownum<=2;
--oracle中,前60%数据
select name 姓名,sex 性别,出生日期 from newstu where rownum<=(select count(*)*0.6 from newstu);
--mysql中,前两条数据
select top 2 name 姓名,sex 性别,出生日期 from newstu;
select *from newstu limit 2
--mysql中,前60%数据
select top 60 percent name from newstu;

4.2模糊查询
4.2.1【使用like进行模糊查询】
注意:like运算副只用于字符串,所以仅与char和varchar数据类型联合使用
例:

select *from newstu where name like '李%';
select *from newstu where name='李四' and age=22;
select *from newstu where age=20 or age=21 or age=22;

说明:查询显示表newstu中,name字段第一个字为赵的记录

4.2.2【使用between在某个范围内进行查询】
例:

select *from newstu where age between 20 and 22;

说明:查询显示表newstu中age在20到22之间的记录

4.2.3【使用in在列举值内进行查询】
例:

select *from newstu where name in('李四','胡八');

说明:查询表newstu中name值为李四或者胡八的记录

4.3.分组查询
4.3.1【使用group by进行分组查询】
例:

select sex,avg(age) as 平均年龄 from newstu group by sex;

说明:在newstu表中查询,展示按sex分组的sex和平均age字段,在这个条件下不能展示其他字段

4.3.2【使用having子句进行分组筛选】
例:

select sex,avg(age) as 平均年龄 from newstu group by sex having count(age)>2;

说明:接上面例子,显示分组后count(age)>2的行,由于where只能在没有分组时使用,分组后只能使用having来限制条件。

4.4.多表联接查询

4.4.1内联接

4.4.1.1【在where子句中指定联接条件】
例:

select s.name,a.are from newstu s,neware a where s.name=a.name;

4.4.1.2【在from子句中使用join…on】
例:

select s.name,a.are from newstu s inner join neware a on(s.name=a.name);

内联与外联的区别:内联是两个表在连接的条件下,所有的字段相同才显示。

4.4.2外联接

4.4.2.1【左外联接查询】
例:

select s.name,a.are from newstu s left join neware a
on s.name=a.name where a.are='江苏';

说明:查询出江苏地区的搜有人的姓名、所在地,newstu表的字段为name,sex,出生日期,age,neware表的字段为name,are。

4.4.2.2【右外联接查询】
例:

select s.name,a.are from newstu s right join neware a
on s.name=a.name where a.are='江苏';

说明:查询出江苏地区的搜有人的姓名、所在地,newstu表的字段为name,sex,出生日期,age,neware表的字段为name,are。

左关联与有关联的区别:左联显示左表所有内容,右表有就显示,没有就显示null;右联显示右表所有内容,左表有就显示,没有就显示null。

neware表中name字段的值如果在newstu表中的name字段是没有的,那么左关联时左边中没有的,但是右关联可以查出左表中没有的。

以4.2.1和4.2.2为例:

newstu表:

name sex 出生日期 age

胡八 女 1993-01-01 00:00:00 24
李四 女 1995-12-25 00:00:00 22
王五 男 1996-03-08 00:00:00 21
赵六 男             20
陈七 男               32

neware表:

name are

胡八 江苏
胡八 苏州
李四 宿迁
李四 江苏
张三 江苏

左关联:select s.name,a.are from newstu s left join neware a on s.name=a.name where a.are='江苏';

  结果:name   are

      胡八     江苏

     李四      江苏

右关联:select s.name,a.are from newstu s right join neware a on s.name=a.name where a.are='江苏';

  结果:name   are

     胡八     江苏

      李四      江苏

     null     江苏

sql语句(已在Oracle中测试,之后有添加内容放在评论中)的更多相关文章

  1. 【转】SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误

    SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误 最近在VS2013上连接远程数据库时,突然连接不上,在跑MSTest下跑的时候,QTAgent32 crash.换成IIS ...

  2. 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】

    问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...

  3. ABAP中SQL语句,指定索引(oracle)

    ①常用的两种方法: 1.指定使用全表扫描:%_HINTS ORACLE 'FULL(table_name)' 表示扫描整个表 2.指定索引:%_HINTS ORACLE 'INDEX("ta ...

  4. MFC中给控件添加变量,DoDataExchange中

    DoDataExchange函数其实是一项数据动态绑定技术.比如你在写动态按钮过程中须对按钮添加变量时,怎么添加?控件类已经写好了,其变量是已经固定的.你要添加新的变量就要用到DoDataExchan ...

  5. angularjs中向html页面添加内容节点元素代码段的两种方法

    第一种方式:原生JS向html页面添加内容节点元素代码段: <!DOCTYPE html> <html> <head> <meta charset=" ...

  6. SQL Server 已提交读快照 测试

    1. 打开数据库 已提交读快照 选项 2. 数据库 已提交读快照 模式下的测试 a) 测试表 Test b) 开启事务1,更新数据C2 = '200'(未提交) BEGIN TRAN ' WHERE ...

  7. SQL语句技巧:查询存在一个表而不在另一个表中的数据记录

    方法一(仅适用单个字段)使用 not in ,容易理解,效率低 select A.ID from A where A.ID not in (select ID from B) 方法二(适用多个字段匹配 ...

  8. sql语句,多个表之间,通过相同内容字段的联合查询

    1 , select j.id,         jt.Name,        j.ApproveType ,         j.ProductCode,         j.CustomerCo ...

  9. C语言中对于结构的定义一般放在.h中还是.c中

    单文件使用的结构体放.c文件中 本模块内部使用结构体,放模块内部头文件中 供外部使用的结构体,放模块对外头文件中.

随机推荐

  1. 12 个 JS 技巧

    1. 过滤唯一值 ES6 引入了 Set 对象和延展(spread)语法…,我们可以用它们来创建一个只包含唯一值的数组. 复制代码     const array = [1, 1, 2, 3, 5, ...

  2. py-day4-4 python 其他内置函数

    # ascii码转换 print(chr(98)) 结果: b print(ord('b')) 结果: 98 # 求几的几次方 print(pow(2,3)) # 2**2 =2*2*2 结果: 8 ...

  3. ActiveMq报错Channel was inactive for too (>30000)long

    生成环境的activemq 隔一到两周,就报错: 查看 activeme的日志: 2018-12-04 11:59:44,744 | WARN  | Transport Connection to: ...

  4. ios开发的frame、物理屏幕尺寸和图片分辨率

    型号 屏幕尺寸(inch) 逻辑分辨率(point) 缩放因子(scale factor) 物理分辨率(pixel) 像素密度(PPI) iPhone3GS 3.5 320 * 480 @1x 320 ...

  5. 学习笔记《Java多线程编程实战指南》三

    3.1串行.并发与并行 1.串行:一件事做完接着做下一件事. 2.并发:几件事情交替进行,统筹资源. 3.并行:几件事情同时进行,齐头并进,各自运行直到结束. 多线程编程的实质就是将任务处理方式由串行 ...

  6. zookeeper的读写流程

    zookeeper的读写流程 基本架构 节点数要求是奇数. 常用的接口是 get/set/create/getChildren. 读写流程 写流程 客户端连接到集群中某一个节点 客户端发送写请求 服务 ...

  7. sql 存储过程和触发器

    mysql----------------------------------------------------------------------------------------------- ...

  8. 【原创】Open JDK更换过程及更换后的问题总结与分析

    由于2019年1月起Oracle对通用计算以外的应用场景开始收费,综合看来还是主要针对嵌入式的Java应用进行收费,毕竟嵌入式设备的数量是庞大的,可以有数亿元进账. 因Oracle JDK收费,各大公 ...

  9. 地下产链——创建安装包捆绑软件(Bundled software)

    Bundled_Software 首先,因为个人知识不足的情况下,无法进行EXE文件捆绑机的制作说明,所以有需要请转至http://www.cnblogs.com/qintangtao/archive ...

  10. Ubuntu、CenOS、Debian等不同版本简单概念与不同

    最近在云计算中使用虚拟机,在进行Xen搭建时发现Ubuntu好像从10版本没有开始官方维护,又去了解了更多的Linux的版本 后续打算采用CenOS尝试一下 下文选自https://blog.csdn ...