Oracle常用命令(转)

表空间: 包含表、视图、索引
段       : 包含数据段、索引段、回退段、临时段
数据块: 是Oracle中最小的逻辑存储单元

创建表空间:

create tablespace rootspace*
datafile 'rootfile' size 1000m
autoextend on

创建用户:
create user root
identified by root
default tablespace rootspace
temporary tablespace temp

    CREATE USER username
    IDENTIFIED BY password
    [DEFAULT TABLESPACE tablespace]
    [TEMPORARY TABLESPACE tablespace];

授予用户username【用户名】权限:
·grant connect to username; CONNECT角色允许用户连接至数据库,并创建数据库对象
·grant resource to username; RESOURCE角色允许用户使用数据库中的存储空间
·grant create sequence to username; 此系统权限允许用户在当前模式中创建序列,此权限包含在CONNECT角色中
·grant select on test to username; 允许用户查询 TEST 表的记录
·grant update on test to username; 允许用户更新 TEST 表中的记录
·grant all on test to username; 允许用户插入、删除、更新和查询TEST 表中的记录
·alter user username identified by newpassword;用于修改用户口令
·drop user username cascade;删除用户

撤销用户权限:
Revoke connect from username;

设置显示行长度:
Set linesize 12;

伪列rownum:
Select rownum from table;

–创建表tb_stu

create table tb_stu(
stu_id char(12) primary key,
stu_name varchar(50) not null,
sex varchar(5),
birthday date
)

–查询表tb_stu
select * from tb_stu;

–在表中插入tb_stu记录
insert into tb_stu(stu_id,stu_name,sex,birthday) values('123456789012','李四','男',to_date('2009-9-9','yyyy-mm-dd'));

–事务提交
commit;

–事务回滚
rollback;

–根据stu_name查询所有信息
select * from tb_stu where stu_name='田七'

–根据stu_id删除一条记录
delete from tb_stu where stu_id=123456789015

–根据stu_id修改一条记录
update tb_stu set sex='女',stu_name='梁朝伟' where stu_id='123456789013';

–to_date 修改存入数据库中日期的格式
update tb_stu set birthday=to_date('2009-10-5','yyyy-mm-dd') where stu_id='123456789014'

–to_date 查询数据库中日期按指定格式输出
select * from tb_stu where birthday between (to_date('2009-10-1','yyyy-mm-dd')) and (to_date('2009-12-1','yyyy-mm-dd'))

–虚列 rownum 数据库中实际并不存在 对符合条件的查询结果的编号
select rownum,stu_id,stu_name,sex,birthday from tb_stu where sex='男';

–在查询结果中进行查询
select * from (select rownum rn,stu_id,stu_name,sex,birthday from tb_stu where sex='男') where rn<3;

–创建表tb_employee

create table tb_employee(
em_id number primary key,
em_name varchar(50) not null,
sex varchar(2),
birthday date,
sal number(20,2)
)

–向表tb_employee中插入数据
insert into tb_employee values(1,'梁朝伟','男',sysdate,11000000);

–按字段升序排列(默认的为升序)
select * from tb_employee where sal>200 order by sal asc

–按字段降序排列
select * from tb_employee where sal>200 order by sal desc

–取别名:将查询的字段按一个特定的字段名输出
select em_name,((sal-2000)*0.2) 税收 from tb_employee where sal>2000;

–联合字段,将查询出的多个字段或者是字符串连接在一起,以一个字段输出,用“||”连接
select em_name||'的应该缴税: '||((sal-2000)*0.2) as 税收 from tb_employee where sal>2000 order by 税收 desc;

–将em_name为“梁朝伟”的记录的birthday字段,按指定的日期格式进行修改
update tb_employee set birthday=to_date('1969-1-1','yyyy-dd-mm') where em_name='梁朝伟';

–查询birthday字段不为当前系统时间并且不为空的值
–不等于的三种书写方式(!=,^=,<>)
select * from tb_employee where to_char(birthday,'yyyy')^=to_char(sysdate,'yyyy');

– or 连接的多条件“或”查询
select * from tb_employee where birthday is null or em_id=1;

–between 3 and 5 查询条件为:大于等于3同时小于等于5
select * from tb_employee where em_id not between 3 and 5;

–查询条件为:大于其中任意一个(只要大于其中的某一个就为满足条件)
select * from tb_employee where em_id > any(1,3,5);

–查询条件为:小于其中所有的(只有比括号中所有的数字都小才为满足条件)
select * from tb_employee where em_id < all(3,5);

–下划线表示任意的一个字符
select * from tb_employee where em_name like '周__';

– % 表示任意多个字符
select * from tb_employee where birthday like '%';

–快速创建和tb_stu相同的表结构的表tb_stu_temp1
create table tb_stu_temp1 as select * from tb_stu where 1=2;

–将表tb_stu按条件查询的结果插入表tb_stu_temp1中
insert into tb_stu_temp1(select * from tb_stu);

–查询所有的表
select * from tab ;

–根据表名查询表
select * from tab where tname='tb_stu';

猜你喜欢

转载自blog.csdn.net/m0_38016313/article/details/82182760