oracle数据库基础知识

20世纪约70年代 一间名为Ampex的软件公司,正为中央情报局设计一套名叫Oracle的数据库,埃里森是程序员之一。下面是小编整理的关于oracle数据库基础知识,欢迎大家参考!

oracle数据库基础知识

【1】oracle数据库基础知识

第一篇 基本操作

--解锁用户 alter user 用户 account unlock;

--锁定用户 alter user 用户 account lock;

alter user scott account unlock;

--创建一个用户yc 密码为a create user 用户名 identified by 密码;

create user yc identified by a;

--登录不成功,会缺少create session 权限,赋予权限的语法 grant 权限名 to 用户;

grant create session to yc;

--修改密码 alter user 用户名 identified by 新密码;

alter user yc identified by b;

--删除用户

drop user yc ;

--查询表空间

select *from dba_tablespaces;

--查询用户信息

select *from dba_users;

--创建表空间

create tablespace ycspace

datafile 'E:oracleappproduct11.2.0dbhome_1oradata'

size 2m

autoextend on next 2m maxsize 5m

offline ;

--创建临时表空间

create temporary yctempspace

tempfile 'E:oracleappproduct11.2.0dbhome_1oradata'

size 2m

autoextend on next 2m maxsize 5m

offline ;

--查询数据文件

select *from dba_data_files;

--修改表空间

--1、修改表空间的状态

--默认情况下是online,只有在非离线情况下才可以进行修改

alter tablespace ycspace offline ; --离线状态,不允许任何对象对该表空间的使用,使用情况:应用需要更新或维护的时候;数据库备份的时候

alter tablespace ycspace read write;--读写状态

alter tablespace ycspace online;

alter tablespace ycspace read only; --只读,可以查询信息,可以删除表空间的对象,但是不能创建对象和修改对象 。使用情况:数据存档的时候

--2、修改表空间的大小

--增加文件的大小

alter database datafile 'E:oracleappproduct11.2.0dbhome_1oradata' resize 10m;

--增加数据文件

alter tablespace ycspace add datafile 'E:oracleappproduct11.2.0dbhome_1oradata' size 2m;

--删除表空间的数据文件

alter tablespace 表空间的名字 drop datafile 数据文件名;

--删除表空间

drop tablespace ycspace;

--删除表空间且表空间中的内容和数据文件

drop tablespace ycspace including contents and datafiles;

--指定表空间 的 创建用户的语法

create user yc1 identified by a default tablespace ycspace temporary tablespace temp;

--删除用户

drop user yc1;

--权限

--赋予创建会话的权限

grant create session to yc1;

--创建一个表

create table studentInfo(

sid int,

sname varchar2(10)

);

--赋予yc1用户创建表的权限

grant create table to yc1;

--赋予yc1使用表空间的权限

grant unlimited tablespace to yc1;

--系统权限

--对象权限

--插入

insert into studentInfo values (2,'abcd');

--查询

select *from studentInfo;

--修改

update studentInfo set sid=1;

--删除

delete studentInfo ;

drop table studentInfo; --系统权限删除表

--赋权的语法

--系统权限

grant 权限名(系统权限或对象权限,角色,all) to 用户(角色,public) with admin option;

--对象权限

grant 权限名(系统权限或对象权限,角色,all) on 用户(角色,public) with grant option;

--收权语法

--系统权限

revoke 权限名(系统权限或对象权限,角色,all) from 用户(角色,public) with admin option;

--对象权限

revoke 权限名(系统权限或对象权限,角色,all) from 用户(角色,public) with grant option;

--赋予创建用户的权限并且把这个权限传递下去,即yc1可以给别人赋权

grant create user to yc1 with admin option;

--收回权限,只能收回scottd ,不能收回由scott赋权的yc1的权限

revoke create user from scott;

--查看用户所具有的权限

select *from user_sys_privs;

--对象权限详解

select * from emp;

--使用yc1来查询scott里面的emp表

select * from ;

--赋予yc1查询emp表和插入的权限

grant select on emp to yc1;

grant insert on emp to yc1;

grant update(empno,ename) on emp to yc1;

grant delete on emp to yc1;

--对scott的emp表添加数据

insert into (empno,ename) value(111,'acv');

update set ename='yc'where empno=111;

--赋予查询、赋予删除、添加、修改

grant select on 表名 to 用户

--grant select,delete,update,insert on 表名 to 用户

grant select,delete,update,insert on emp to yc1;

grant all on dept to yc1; --all代表所有的对象权限

select *from ;

select *from ;

insert into values(50,'企事业文化部','bumen');

--查看角色

--dba:数据库管理员,系统最高权限,可以创建数据结构(表空间等)

--resource:可以创建实体(表、视图),不可以创建数据库的结构

--connect:连接的权限,可以登录数据库,但是不可以创建实体和不可以创建数据库结构

select *from role_sys_privs;

grant connect to yc1;

--将可以连接的角色赋予给yc1,则yc1就是应该可以连接数据库的.人,类似于 create session 。

create table StuInfos(sid int);

select *from StuInfos;

create table stuInfo(

sid int primary key , --主键 primary key 非空且唯一 (主键约束)

sname varchar2(10) not null, --姓名不能为空,(非空约束)

sex char(2) check(sex in('男','女')), --(检查约束),check,

age number(3,1) constraint ck_stuInfo_age check(age>10 and age<100) , --也可以用varchar ;age between 10 and 100 ,在10和100之间,是一个闭区间

tel number(15) unique not null, --唯一约束,

address varchar2(200) default '什么鬼'

)

insert into stuInfo values(3,'大大','男',18,4321543,default);

insert into stuInfo values(1,'张三','男',10);

select *from stuInfo;

drop table stuInfo;

create table classInfo(

cid int primary key, --班级id

cname varchar2(20) not null unique --班级名

)

create table stuInfo(

sid int primary key,

sname varchar2(20),

cid int constraint fofk_stuInfo_cid references classInfo(cid) on delete cascade

)

insert into classInfo values(1,'1班');

insert into classInfo values(2,'2班');

insert into classInfo values(3,'3班');

insert into classInfo values(4,'4班');

select *from classInfo;

select *from stuInfo;

insert into stuInfo values(1001,'张三',2);

insert into stuInfo values(1002,'张四',4);

update classInfo set cid=1 where cid=8;

drop table stuInfo;--要先删除这个

drop table classInfo; --再删除这个

delete classInfo where cid=4 ;--同时删除这两个表中的4

--删除用户的时候

drop user yc1 [cascade] --删除用户的同时把它创建的对象都一起删除

--修改表

--1、添加表中字段

--alter table 表名 add 字段名 类型

alter table classInfo add status varchar2(10) default '未毕业'

--2、修改已有字段的数据类型

--alter table 表名 modify 字段名 类型

alter table classInfo modify status number(1)

--3、修改字段名

--alter table 表名 rename column 旧字段名 to 新的字段名

alter table classInfo rename column cname to 班级名;

--4、删除字段

--alter table 表名 drop column 字段名

alter table classInfo drop column status ;

--5、修改表名

--rename 旧表名 to 新表名

rename classInfo to 班级信息;

--删除表

--1、截断表效率高,每删除一次会产生一次日志 2、截断会释放空间,而delete不会释放空间

--删除表结构和数据

drop table 表名;

--删除表中所有数据

truncate table classInfo;

delete classInfo;

create table classInfo(

cid int primary key, --班级id

cname varchar2(20) not null unique , --班级名

stasuts varchar2(100)

);

select *from classInfo;

--数据的操作

--增加数据语法

--insert into 表名[(列名,....)] values (对应的数据的值);

insert into classInfo values(1,'一班','未毕业');--需要按照表结构的顺序插入

insert into classInfo values(4,'六班','未毕业');

insert into classInfo(cname,cid) values('二班',2); --需要按照括号中的顺序插入,但是 not null primary key 必须插入的。

insert into classInfo(cname,cid) values('三班',3);

--删除的语法

--delete 表名 [where 条件]

delete classInfo where cid>=2;

--修改记录的语法

--update 表名 set [字段='值' ] [where 条件]

update classInfo set cname='三班'; --会修改所有该字段

update classInfo set cname='四班' where cid=1;

update classInfo set cname='五班', stasuts ='未毕业' where cid=3;

--alter table classInfo drop constraint SYS_C0011213;

--添加多个时可以使用序列

--用序列来做自动增长

create sequence seq_classInfo_cid start with 1001 increment by 1;

insert into classInfo values(seq_classInfo_val,'七班','未毕业');

insert into classInfo values(seq_classInfo_val,'八班','未毕业');

insert into classInfo values(seq_classInfo_val,'九班','未毕业');

insert into classInfo values(seq_classInfo_val,'十班','未毕业');

create table classInfo2(

cid int primary key, --班级id

cname varchar2(20) not null unique , --班级名

stasuts varchar2(100)

);

select *from classInfo2;

drop table classInfo2;

insert into classInfo2 select *from classInfo;

insert into classInfo(cname,cid) select cname,cid from classInfo;

alter table classInfo2 drop constraint SYS_C0011213;

select seq_classInfo_val from dual;

select seq_classInfo_val from dual;

--直接创建一个新表,并拿到另一个表其中的数据

create table newTable as select cname,cid from classInfo;

create table newTable1 as select *from classInfo;

select *from newTable;

select *from newTable1;

insert into newTable1 values(1008,'dg','');

第二篇:高级操作

直接在使用scott登陆,进行查询操作

----------------------------------------------------------------------------------

--简单查询

select *from emp;

select empno as id,ename as name from emp;

select empno 编号,ename 姓名 from emp;

--去除重复

select job from emp;

select distinct job from emp;

select job,deptno from emp;

select distinct job,deptno from emp;

--字符串的连接

select '员工编号是' ||empno || '姓名是' ||ename ||'工作是'||job from emp;