第一篇基本操作--解锁用户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 ycspacedatafile 'E:\oracle\app\product\11.2.0\dbhome_1\oradata\ycspace.dbf'size 2mautoextend on next 2m maxsize 5moffline ;--创建临时表空间create temporary yctempspacetempfile 'E:\oracle\app\product\11.2.0\dbhome_1\oradata\ycspace.dbf'size 2mautoextend on next 2m maxsize 5moffline ;--查询数据文件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:\oracle\app\product\11.2.0\dbhome_1\oradata\ycspace.dbf' resize 10m;--增加数据文件alter tablespace ycspace add datafile 'E:\oracle\app\product\11.2.0\dbhome_1\oradata\add.dbf' 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 scott.emp;--赋予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 scott.emp(empno,ename) value(111,'acv');update scott.emp 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 scott.emp;select *from scott.dept;insert into scott.dept 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, --班级idcname 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, --班级idcname 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 必须插入的。