Create tablespace
create tablespace user_data
logging
datafile 'D:\oracle\oradata\Oracle9i\user_data.dbf'
size 50m
autoextend on
next 50m
maxsize 20480m
extent management local;
Create user
create user username identified by password
create user jwh identified by lyq ;
Permission and power
create procedure ,create role ,create sequence .create session ,create session ,
create table .create tablespace ,create user ,create view ,drop table ,drop user…
grant create table to jwh;
grant create session to jwh;
for table’s power
insert ,update,delete ,select ,index ,alter ,reference
grant select on emp to jwh;
grant all on emp to jwh;
注意:只有表的拥有者或者有授权的人,才可以把访问该表的权限给别人
Role
oracle 提供了三种标准的角色:connect ,resource ,dba,也可以自定义角色
grant resource to jwh;
自定义角色
create role maker;
drop role maker;
grant all on emp to jwh with grant option;
允许jwh用户插入、删除、更新和查询scott 用户emp表中的记录,并允许其将权限赋予其他用户
收回权限:
revoke select on emp from jwh;
revoke all on emp from jwh;
Alter user
修改用户密码:
alter user jwh identified by pass;
alter user scott account unlock;
//给scott 用户解锁,oracle11g默认为锁定。
Drop user
drop user jwh cascade ;
Get time
select susdate from dual;
select systimestamp from dual;
Create table
crate table student
(
sid char(5) not null primary key,
sname ...
);
Alter table
alter table student add sclass char(50);
alter table student modify sclass varchar2(50);
alter table student rename column sclass to stel;
alter table student drop column stel;
alter table student add constranint uk_Sname unique(Sname);
Truncate table
truncate table 表名;
删除所有记录,但保存表结构,并且不写日志。
Drop table
drop table 表名;
Rename table
rename student to student1;
Comment
comment on table student is '用于存储学生信息';
comment on column student.sname is '学生姓名 ';
Select comment
select comments from user_tab_comments where table_name='student';
//查看表注释
select comments from user_col_comments where table_name='student';
//查看字段注释
Create table and Create constraint
create table class_table
(
cid number (10) primary key ,
cname varchar2(30) not null,
cdate 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),
saddress varchar2(100) default('地址不详');
);
update,delete,select,insert
update stu set sage=21 where sid =1;
delete from stu where sid =2;
select * from student_table;
select * from stu where sid =4;
insert into stu values(5,'10002','abc',20);
Savepoint and rollback
savepoint x1;
rollback to x1;
Distinct
select distinct sclass from t_student;
选择无重复的。
Order by
select * from t_score where sid ='10002' order by score desc;
Like
select * from t_student where sname like '姜%';
Create table use as
create table t_stubak as seclect * from t_student;
create table t_stuclass1 as select * from t_student where Sclass=1;
Connection select
part1:
select sname ,cname ,score from t_student
join t_score
on t_student.sid=t_score.sid
join t_sourse
on t_course.cid = t_score.cid;
part2:
select sname ,cname ,score from t_student ,t_scourse,t_score
where t_student.sid=t_score.sid and t_course.cid=t_score.cid;
rename some table
select sname ,cname from t_student t1,t_scourse t2
where t1.sid=t2.sid;
左外联接:
左外联接就是将左表作为主表,结果集中除了满足联接条件的记录外,还有主表中不满足条件的记录
select * from t_teacher left join t_teachcourse
on t_teacher.tid = t_teachcourse.tid;
右外联接:
右外联接就是将右表作为主表,结果集中除了满足联接条件的记录外,还有主表中不满足条件的记录(和左外联接相对)
Select * from t_teachcourse right join t_teacher
On t_teachcourse.tid = t_teacher.tid;
全联接:
全联接就是结果集中除了满足联接条件的记录外,还有左、右表中不满足条件的记录
Select * from t_teacher full join _teachcourse
on t_teacher.tid = t_teachcourse.tid;
交叉链接:
select * from t_student cross join t_course;
分组聚合:
select max(chours) as max_chours from t_course;
[NOT]IN 子查询:
select * from t_course where cid in (select cid from t_teachcourse);
Exists子查询:
select * from t_course where exists (select * from t_teachcourse where t_teachcourse.cid = t_course.cid);
存在查询,子查询不返回任何结果,只产生true或者false。
算术操作符:
select sid ,cid ,score+10 as lastscore from t_score where cid =2;
比较操作符:
select * from t_student where sbirthday <'01-1月-1980';
select * from t_student where sbirthday between '01-1月-1980' and ’31-12月-1980’
检索1980年前出生的,和检索1980年出生的
select * from t_student where sclass in (1,2);
检索班级是1班或者2班的信息。
逻辑操作符:
select * from t_student where sbirthday >='01-1月-80'
and sclass=1;
连接操作符:
select ('学号为'||sid||'的同学姓名是'||sname)as 学生信息 from t_student ;
集合操作符:
或,且,前符合后不符合
select sid from t_score where score >=70 and cid =1
union(intersect ,minus)
select sid from t_score where score >=70 and cid =2;
字符函数:
select initcap('hello') from dual; Hello
select lower('FUN') from dual; fun
select upper('sun') from dual; SUN
select ltrim('xyzadams','xyz')from dual; adams
select rtrim('xyzadans','ams')from dual; xyzad