Oracle数据库操作

Oracle数据库操作工具---PL/SQL Developer

Oracle安装时候需要手动为SYS和SYSTEM输入口令密码

--注释

--创建表

CREATE TABLE userinfo(
       ID NUMBER PRIMARY KEY, //主键
       username VARCHAR2(12) NOT NULL, //非空
       password VARCHAR2(20),
       age NUMBER
);


--增加一条数据
insert into userinfo(ID,username,password,age)
values(2,'lily','123',23)

insert into userinfo(ID,username,password,age)
values(5,'狗蛋','2333',28)

insert into userinfo(ID,username,password,age)
values(6,'铁蛋','789',18)


--修改数据
update userinfo set password='456' where id=2;

--删除一条数据
delete from userinfo where username='狗蛋' and id=5;

--查询数据

select * from userinfo;

--提交(操作语句)

commit;

--左连接(两个表:userinfo和score)

格式:表1 a letf join 表2 b on...

select * from 表1 a letf join 表2 b on a.user_id=b.id;

select b.name,a.subject,a.score from 表1 a letf join 表2 b on a.user_id=b.id;//条件查询


--创建用户
create user h //用户名 h

identified by 123456 //密码 123456

default tablespace users //默认表空间

temporary tablespace temp; //临时表空间
--授权
grant connect,resource,dba to h;

--创建表空间
create tablespace Bounce //表空间名 Bounce

datafile 'F:\APP\ADMIN\ORADATA\ORCL\Bounce.dbf' //保存路径

size 50M  //初始化大小

autoextend on;  //表空间大小不够时自动创建新的表空间
--查询表空间存放路径
select name from v$datafile;

--修改用户密码
alter user h identified by 1234;
--删除用户
drop user h cascade;


--分页方法
select a.*,rownum rn from(select * from userinfo2) a where rownum between 1 and 4;
--推荐写法,性能好
select *
 from (select a.*,rownum rn
 from (select * from userinfo2) a
   where rownum <= 4)
   where rn >= 1;

select * from (select a.*,rownum rn from (select * from userinfo2) a) where rn between 1 and 10;


1.手动建表进入编辑数据模式的两种方法:1. select t.*, t.rowid from userinfo2 t;

                    2. select * from userinfo2 for update;

2.每次进行数据库操作后需要点击提交(F10)才会把数据存入硬盘里。

3.数据库操作语句不区分大小写,小写会在编译时自动转换成大写。

4.Primary:主键,一般设置ID为主键,主键具有唯一和非空的特性(用语句创建表时写成PRIMARY KEY)。

5.删除用户时后面加cascade会删除用户的所有表和数据,不加则只删除用户。

6.伪例:1.Oracle中伪例就像一个列表,但是它并没有存储在表中。

     2.伪例可以从表中查询,但不能插入、更新和删除他们的值。

     3.常用的伪例有ROWID和ROWNUM(可理解ROWNUM为数据下标)。

7.给数据分页要用ROWNUM,不能用数据ID。


--同义词        
create synonym u1 for userinfo2;

select * from u1;

--序列
create sequence user_seq2
       start with 1
       increment by 1
       nomaxvalue
       minvalue 1
       nocycle
       cache 10;
       
select user_seq2.nextval from dual;
select user_seq.currval from userinfo;

insert into userinfo values(user_seq.nextval,'ma','2233','22');

select * from userinfo;

drop sequence user_seq;

--视图
create view userinfo_view as
select s.sex,max(s.age) maxage from userinfo2 s group by sex;

create or replace view userinfo_view1 as
select s.sex,max(s.age) maxage from userinfo2 s group by sex;

select * from userinfo_view1;

--索引
create index index_userinfo on userinfo2(age);
alter index index_userinfo rebuild;

select index_name,table_name,column_name from user_ind_columns;


--复制表

--Oracle复制表结构及其数据

create table table_new as select * from table_old;

--只复制表结构

1.create table table_new as select * from table_old where 1=2;

2.create table table_new like table_old;

--只复制表数据

1.如果两个表的结构一样:insert into table_new select * from table_old;

1.如果两个表的结构不一样:insert into table_new(column1,column2,...) select column1,column2,... from table_old

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值