oracle基础查询语句总结(一)

--查看系统表空间
select file_name,tablespace_name from dba_data_files order by file_name;
--查询用户所使用的表空间
select user_id,username.default_tablespace from dba_users;
--修改默认表空间
alter datebase default tablespace user_date;
--重命名表空间
alter tablespace user_data rename to user_data
--删除表空间
drop tablespace user1_data;
drop tablespace user1_data including contents and datafiles;--彻底删除文件
--创建表空间
create tablespace user_data1
logging
datafile'C:\oracle\lianxi\user_data1.dbf'
size 50m
autoextend on
next 50m
maxsize 20480m
extent management local;
--为表空间增加数据文件
alter tablespace mytablespace 
add
datafile 'e:\datafile02.dbf' 
size 50m
autoextend on
next 50m
maxsize 20480m;
--创建用户
create user zhangsan identified by mima;
--修改用户密码
alter user zhangsan identified by mima1;
--删除用户
drop user zhangsan;
drop user zhangsan cascade;--彻底删除用户

--授予用户权限
grant create session to zhangsan;
grant create user to zhangsan;
grant drop user to zhangsan;
grant select on scott.emp to zhangsan1;--授权张三1可以访问其他用户中的表

--移出权限
revoke drop user from zhangsan;
revoke create session from zhangsan;
revoke create user from zhangsan;
--授予角色
grant resource to zhangsan;

------------实训补充------
DDL(数据定义语言)
----创建表
create table t_student(
sid char(5) not null primary key,
sname varchar2(10) not null,
ssex char(1) not null,
sbrithday date null,
stel varchar2(13) null,
sclass int null
)
---修改表
alter table t_student add t_address varchar2(100);
alter table t_student modify t_address varchar2(50);
alter table t_student rname column t_address to t_add;
alter table t_student drop column t_add;
---重命名表
rename t_student to tt;
---删除表
drop table tt;
---只删除表的内容
truncate table 表名
------------------------------------------------------第二章--------------------------

select * from dual;
select sysdate from dual;--查询当前系统时间
select systimestamp from dual;--精确查询当前系统时间
--伪列rowid,rownum
select * from scott.EMP;

select  rownum ename,sal from scott.EMP order by sal desc ;

select rownum,a.* from(
select  ename,sal from scott.EMP order by sal desc) a

--在scott中执行--建表--
create table t_student(
sid char(5) not null primary key,
sname varchar2(10) not null,
ssex char(1) not null,
sbrithday date null,
stel varchar2(13) null,
sclass int null
)
select *from t_student;

drop table t_student;
--在scott中执行--修改表
alter table t_student add f_address varchar2(50);--增加表信息
alter table t_student modify  f_address varchar2(250);--修改表信息
alter table t_student rename column f_address to f_add;--重命名列
alter table t_student drop column  f_add;--删除列

--第三节课的内容
--
insert into t_student values('10008','李四','m',to_date('2000-01-10','yyyy-mm-dd'),'1235646',1);
truncate table t_student;
delete from s_student where sid='10008';
----删除表
drop table t_student;
-----重命名表
rename t_student to tt;
select * from tt;
---给表和列加注释
comment on table tt is '学生表';
comment on column tt.sid is '学生id';
select comments from user_tab_comments where table_name='TT';--表名
select comments from user_col_comments where table_name='TT';--查列名

完整性
五大约束
主键只能有一个
唯一可以有多个(不允许重复)
外键:引用关系,被引用的表为从表,,从表中不能插入主表中没有的值

create table t_student(
sid char(5) not null primary key,
sname varchar2(10) not null,
ssex char(1) not null,
sbrithday date null,
stel varchar2(13) null,
sclass int null
)
select *from t_student;
--删除表
drop table t_student;
--在scott中执行--修改表
alter table t_student add f_address varchar2(50);--增加表信息
alter table t_student modify  f_address varchar2(250);--修改表信息
alter table t_student rename column f_address to f_add;--重命名列
alter table t_student drop column  f_add;--删除列
--第三节课的内容
--在zhangsan1中的代码
--删除表中记录,不做日志记录,无法回滚
insert into t_student values('10008','李四','m',to_date('2000-01-10','yyyy-mm-dd'),'1235233646','1');
truncate table t_student;
delete from s_student where sid='10008';
----删除表
drop table t_student;
-----重命名表
rename t_student to tt;
select * from tt;
---给表和列加注释
comment on table tt is '学生表';
comment on column tt.sid is '学生id';
select comments from user_tab_comments where table_name='TT';--表名
select comments from user_col_comments where table_name='TT';--查列名

----------完整性
---创建列的同时添加约束
create table class_table(
cid number(10) primary key,
cname varchar2(30) not null,
cdae date);

create table student_table(
sid number(10) primary key,
classid number(10) references class_table(cid),
sno varchar2(30) unique,
sname varchar2(30) not null,
sage number(3) check(sage>0 and sage<150),
saddeass varchar2(30) default('地址不详'));

select * from class_table;
select * from student_table;
----------------------- --dml数据操纵------增删改查----------
-------------------插入数据的四种方法
---插入数据的第一种方法
insert into class_table values(1,'一年级一班 ',to_date('2018-04-03','yyyy-mm-dd'));
insert into class_table values(2,'一年级二班 ',to_date('2018-05-03','yyyy-mm-dd'));
insert into student_table values(1,1,'10001','张三',18,null);
insert into student_table values(2,1,'10002','李四',18,default);
insert into student_table values(3,3,'10003','王武',19,default);--在另一个表中没有3
--插入数据的第二种方法
insert into student_table(sid,classid,sno,sname)values(5,1,'10004','马六');--插入数据的第二种方法
---增加数据的第三种写法,从旧表中生成新表
create table stu as select sid ,sno,sname,sage from student_table;
----从旧表生成新表(只复制框架而不复制数据)
create table stu as select sid ,sno,sname,sage from student_table where 1=2;
select * from  stu;
delete  from stu;
--增加数据的第四种写法
insert into stu(sid,sno,sname,sage) select sid ,sno,sname,sage from student_table;
------------------更新
update stu set sage=21 where sid=1; 
------------------删除
delete from stu where sid=2;
-----------删除列的方法
alter table emp4 drop column test;
------------------查询
select * from student_table;
select * from stu where sid=4;
------------事务控制
insert into stu values(2,'10002','abc',20);
update stu set sage=18 where sid=4;
commit;---提交数据
delete from stu where sid=4;
rollback;
------保存点使用
insert into stu values(3,'10003','fsgfsh',20);
savepoint stull_27;
update stu set sage=38 where sid=4;
savepoint stull_28;
delete from stu where sid=1;
rollback to stull_28;
select * from stu;
--创建约束第二种方法
create table account(
  name varchar2(32),
  acc_type number(1) not null,
  acc_code varchar2(32),
  balance number(5,2) default(100),
  sid number(10),
  constraint pk_name primary key (name),
  constraint uk_code unique (acc_code),
  constraint ck_balance check(balance>=100 and balance<=1000),
  constraint fk_sid foreign key (sid) references student_table(sid));
  
  --第三种方法,通过修改表,添加约束
  alter table account add constraint
  fk_sid foreign key (sid) references student_table(sid);
  
 alter table account drop constraint fk_sid; 
 -----数据控制语言----授权等
 -----事务控制语言(不可能局部成功)
事务四个属性。
---------------事务控制


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值