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. 1、Nexus安装

    1.nexus 下载地址: https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.5-02-bundle.tar.g ...

  2. Winform 窗体实现圆角展示

    触发窗体的Resize事件,如下所示: #region 界面实现圆角 private void LoginForm_Resize(object sender, EventArgs e) { if (t ...

  3. mplayer用法收集【转】

    转自:https://blog.csdn.net/wylhistory/article/details/4816653 1,录音: mplayer mms://202.***.***.***/test ...

  4. Spring编程式事务管理和声明式事务管理

    本来想写一篇随笔记一下呢,结果发现一篇文章写的很好了,已经没有再重复写的必要了. https://www.ibm.com/developerworks/cn/education/opensource/ ...

  5. Selenium常用方法

    Selenium是一个自动化测试工具,利用它可以驱动浏览器执行特定的动作,如点击.下拉等操作,同时还可以获取浏览器当前呈现的页面的源代码,做到可见即可爬.对于一些JavaScript动态渲染的页面来说 ...

  6. python之路——25

    复习 1.继承 表示什么是什么的关系 1.单继承 1.先抽象,再继承 2.子类没有的名字,调用父类 3.类中使用self,看清楚self指向谁 2.多继承 1.新式类和经典类 1.新式类:广度优先 经 ...

  7. JSON 序列化与反序列化, 与XML相互转换.

    方式一: 使用于 JavaScriptSerializer类 适用于普通场景, Excel导入导出, 前台传输查询参数直接处理等. JavaScriptSerializer serializer = ...

  8. electron+antd详细教程

    第一步: 要做一个electron项目,理论上我们应该从electron-quick-start开始,就是说我们需要如下3个文件: package.json,node工程最基本的要求,类似于Java的 ...

  9. NoSuchMethodError解决方法

    下面演示下如何在啥都不知道的情况下遇到该错误的解决思路: 随便找一个错误示例: Caused by: java.lang.NoSuchMethodError: org.eclipse.jdt.inte ...

  10. tornado+jsonrpc

    rpc:远程过程调用(A服务调用B服务的一个方法或函数) tornado中jsonrpc的使用 import json import tornado.httpserver import tornado ...