oracle中的简单sql语句——基础知识

Structured Query Langugae (结构化查询语言)

一、sql是所有关系型数据库的通用语言,具体类别有

数据定义语言 DDL Data Define Language
       create alter drop truncate
数据操纵语言 DML Data manipulation Language
       insert update delete select
事务控制语言 TCL Transaction Control Language
       commit savepoint rollback
数据控制语言 DCL Data Control Language
        grant revoke

二、详解sql

数据定义语言DDL

创建create create table tablename (列名1 属性1 ,列名2 属性2);

 create table stu (sno number(6) , sname varchar2(10)); 
 --  创建stu表中包含两列
create table stu2 as select * from table stu; 
 --  相当于copy表stu命名为stu2
create  table stu3  as select * from table stu where 1>2; 
 -- 创建表stu3与stu表结构一致,但表中无内容

更改 alter alter table tablename 具体操作 列名 属性;

> alter  table tablename add  列名  属性; `       
>  --  在表中增加一列
> alter  table tablename modify 列名  属性;  
>  --  修改表中某一列的属性值  
> --  一般将属性值由小改大,由大改小若表中数据超出最小限制则不被允许
>  alter table tablename drop column 列名  属性;
>  --  删除表中某一列

删除drop

> drop table tablename; 
>  --将表删除,但会保留在回收站中。
> drop table tablename purge; 
> --永久删除,不保存在回收站。
> flashback table tablename to before drop; --回收站恢复
> purge recyclebin; --清空回收站 
> drop user  user_name; --删除指定用户
> drop tablespace tablespace_name including content and datafiles;  
> --删除指定存储空间

截断 快速删除表中所有数据 并且无法恢复

> truncate table tablename ; --删除表中的记录,保留表结构。

数据操纵语言 DML

增insert

> insert into tablename values(列1值,列2值,列3值);  
>   --向表中增加记录
> insert into tablename (列1,列3)values(列1值,列3值);
>   --向表中增加记录,可选列
> insert into tablename select * from  tablesname2 where 条件; 
>   --给表中增加表2中满足条件的所有记录

删除delete

> delete  from tablename;
> commit;  --删除动作进入日志可找回
> delete  tablename  where  条件; --删除表中满足条件的记录

修改update

> update  tablename set 更新内容 where 更新条件;
> update stu set sname='小虫子'  where   sno=1; 
>  update stu set sname='bluce''li'  where   sno=1;
>  --如果sname中包含' 则输入''来实现。

查询 select select from where;

> select * from stu where sno=1 ;
> select * from stu where upper(sname)='a'; 
> --upper表示查询结果不区分大小写。
> select * from stu where length(sname)=2;  
> --length查询名字长度为2的学生记录。
> select count(*) from stu where sname is not nul;
> --查询满足条件记录总数
> select * from stu where sname like 'a_';    
> --表示单个匹配,一个任意字符
> select * from stu where sname like 'a%';  
> --%表示0个或多个任意字符

group by ……having …… order by……

> select * from stu order by desc; --降序desc 升序asc
> select sname , avg(score) from grade group by sname
 having sname is not null;

实例:

> create table grade (sno  number(6),classnum number(2),score number(3));
> select  classnum , avg(score)  from grade  
  group by classnum having ((avg(score)>60 )
  order by classnum desc;
> --查询平均分大于60的班级编号和平均分,并按照班级号降序排列

事务控制语言TCL
commit;
用于提交用户发出的DML操作.,表示确定提交,commit以后其余用户即可看到你执行的dml操作结果。

rollback;
回滚你之前执行的操作,即撤销执行,总而言之, 你的操作在没有COMMIT和ROLLBACK前,都还在本机的SESSION中,可以理解中还没有更新至数据库。
注意如果对操作进行了commit 则无法用rollback回滚。

savepoint;
建立保存点,主要用于回滚到指定的点。

实例:

SQL> Insert into T_GHDWML Values('G31003','哈尔滨自然公司',
'经理','郑毅','13936656561');

  SQL> Savepoint A;

  SQL> Update T_GHDWML set lxr='陈宇' where dwbm=’G31003’;

  SQL> Savepoint B;

  SQL> Delete From T_GHDWML where dwbm=’G31003’;

  SQL> Rollback;

  SQL> Rollback;

  SQL> Rollback; 

这里写图片描述

数据控制语言DCL
grant 权限1,权限 2,…权限 n to user_name;
revoke 权限1,权限 2,…权限 n from user_name;

实例:在conn system/密码@orcl 下
grant create session to test1; ——把连接数据库的权限赋给test1用户
grant create table to test1; ——把创建表的权限赋给test1用户
revoke select on scott.emp from test1; ——收回test1用户查询scott中emp表的权限
revoke allon scott.emp from test1; ——收回test1用户对scott中emp表的所有操作权限
revoke unlimit tablespace to test1; ——收回test1 无限表空间的权限

三、oracle中的数据类型

这里写图片描述

oracle中还包括伪列,常用的伪列有ROWID ROWNUM 只能用于查询

ROWID 表中行的存储地址,唯一标识表中的一行,实现快速定位
ROWNUM 用来限制查询返回的结果集的行号,用于限制查询返回行数。

date类型

select sysdate from dual;    
--结果:  05-12月-18
select to_char(sysdate, 'yyyymmdd hh24:mi:ss') from dual; 
--结果:   20180512 21:50:30
select to_char(systimestamp, 'yyyymmdd hh24:mi:ssxff6') from dual; 
--结果:   20180512 21:50:30.703000   精确到小数点后六位

四、oracle中支持的sql操作符

操作符的优先级:算数 >连接> 比较> not> and >or
算数操作符     +     -     *     /

select name, age+2*age from person;

比较操作符     =   !=   >   <   >=   <=  
                          in   like   is null     is not null    between and     not between and    

select * from student where sno>5;

逻辑操作符       and       or       not
集合操作符     union     union all     intersect     minus

select * from A union  select * from B;
 --取两个表中所有行无重复
select * from A union all  select * from B; 
--取两个表中所有行有重复
select * from A intersect select * from B; 
--取两个表的交集
select * from A minus  select * from B; 
--取在前一个表中有后一个表中无的记录

连接操作符 || (用于将两个或多个字符串连接起来)

create table stu (sno number(6) ,sname varchar2(10));
insert into stu values150504 , 'alex');
insert into stu values150505 , 'blex');
commit;
select '学号是:'|| sno ||'姓名是:'|| sname  from stu;

结果是:

'学号是:'||SNO||'姓名是:'||SNAME
--------------------------------------------------------------------------------
学号是:150504姓名是:alex
学号是:150505姓名是:blex

作者:lili小虫子(bibibrave):-:-:-:-:-:-:-:-这里写图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值