oracle数据库对象之表空间、约束、序列、同义词、索引、权限、事务 操作练习

数据库对象之表空间、约束、序列、同义词、索引、权限、事务 操作练习

一、创建以自己姓名命名的表空间,创建数据表customer,结构如下

cust_id number(4),
cname varchar2(10)
sex char(2)
age number(2)
birthday date
account number(5,2)
将customer表放到自己名字的表空间中。
建立以下约束:
(一)客户编码为主键
(二)姓名不能为空
(三)性别默认为“男”
(四)创建检查约束,要求男性年龄在18到60岁之间,女性年龄在18到55岁之间(要求为表级约束,必须有约束名称)

create tablespace 张三 datafile 'd:\text\zhangsan.dbf' size 10M;

create table customer(
		cust_id number(4),
		cname varchar2(20),
		sex char(2),
		age number(2),
		birthday date,
		account number(5,2)
) tablespace 张三;


alter table customer add constraint pk_id primary key(cust_id);

alter table customer modify cname not null;

alter table customer modify sex default '男';

alter table customer add constraint che_age check(((sex='男') and (age between 18 and 60)) or ((sex='女') and (age between 18 and 55)));

二、在一题的表中增加一列 dno number,并在dno字段上创建唯一性约束

alter table customer add dno number constraint uniq_dno unique;

或者

alter table customer add dno number;
alter table customer add constraint uniq_dno unique(dno); 

三、创建序列stu_seq,增长量是2,最小值是1,最大值是100,不循环,设置缓存为30。然后在customer表中插入3条记录,使用该序列生成主键值。

create sequence stu_seq increment by 2 minvalue 1 maxvalue 100 nocycle cache 30;

insert into customer values(stu_seq.nextval,'李四','男',18,to_date('2020-09-22','yyyy-mm-dd'),200);
insert into customer values(stu_seq.nextval,'张三','男',18,to_date('2020-08-12','yyyy-mm-dd'),200);
insert into customer values(stu_seq.nextval,'王五','男',18,to_date('2012-03-09','yyyy-mm-dd'),200);

四、针对scott用户的emp表和dept表做以下练习

(一)创建一个视图v_emp_salesman,内容是所有职位是SALESMAN的员工。

(二)需要经常在emp表的ename列上执行小写名字搜索,在此列上建立一个基于函数的索引。

(三)在emp表的job和sal列上创建一个组合索引

(四)查看scott用户下的表信息

(五)禁用dept表中pk_dno约束

(六)为dept表定义同义词d

(七)修改(三)中的索引名,为edjs。

--1
create view v_emp_salesman as select * from emp where job='SALESMAN';
--2
create index idx_ename on c##scott.emp(lower(ename));
--3
create index idx_job_sal on c##scott.emp(job,sal);
--4
select * from all_tables where owner='SCOTT';
--5
alter table c##scott.dept disable constraint pk_dno;
--6
create synonym d for c##scott.dept;
--7
alter index idx_job_sal rename to edjs;

五、已知学生表student,该表两个字段学号(id)、姓名(name),其中id列为主键列。有课程表course ,该表有两个字段编号(id)、课程名称(name),其中id列为主键列。有选课表sc,该表有三个字段学号(sid)、课程编号(cid)、成绩(score)。要求建立下列外键关联关系:

1).sc表的学号列(sid),外键关联学生表(student)的学号列(id)。

2).sc表的课程编号列(cid),外键关联课程表(course)的编号列(id)。

请写出建立上述关联关系的SQL语句。

create table student(
	id number constraint pk_id_stu primary key,
	name varchar2(10)
);

create table course(
	id number constraint pk_id_c primary key,
	name varchar2(10)
);

create table sc(
	sid number,
	cid number,
	score number(8,2)
);

alter table sc add constraint fk_sc_stu_sid foreign key(sid) references student(id);

alter table sc add constraint fk_sc_c_cid foreign key(cid) references course(id);

六、对scott方案下的表dept,完成以下内容:

(1) 使用system身份连接数据库。创建用户test_user,其口令为oracle

(2) 向用户test_user授予连接数据库的系统权限

(3) 向用户test_user授予对象scott.dept的select权限

(4) 向用户test_user授予对象scott.dept的insert、delete权限,对loc字段具有更新权力

(5) 用户test_user具有对dept表的所有权力,并具有给其他用户授权的权力

(6) 撤消用户test_user对dept表的所有权限

(7) 用户test_user只有查看10号部门的权力,不能查看其他部门信息

(8) 建立角色role1,具有连接数据库、创建表的权力

(9) 将role1角色的权力授予给用户test_user

(10) 删除角色role1

--在system/root用户下进行操作:
--1
create user c##text_user identified by oracle;
--2
grant create session to c##text_user;
--3
grant select on c##scott.dept to c##text_user;
--4
grant insert,delete,update(loc) on c##scott.dept to c##text_user; 
--5
grant all on c##scott to c##text_user with grant option;
--6
rovoke all on c##scott.dept from c##text_user;
--7
create view c##myview as select * from c##scott.dept where deptno=10;
grant select on c##myview to c##text_user;
--8
create role c##role1;  
grant create session,create table to c##role1;
--9
grant c##role1 to c##text_user;
--10
dorp role c##role1;

七、完成以下操作:

(1) 复制表dept,生成新表dept1

(2) 删除dept1中部门编号为10的记录,并提交事务

(3) 将dept1表中部门编号为20的记录地址更改为BEIJING。然后回滚该事务以取消对dept1表的修改。

(4)在事务中建立一个保存点,并使用回滚语句回滚到保存点。事务操作可自定。最后要有验证语句。

create table dept1 as select * from dept;

delete from dept1 where deptno=10;

update dept1 set loc='BEIJING' where deptno=20;
rollback;

savepoint a;
delete from dept where deptno=10;
select * from dept;
rollback to a;
select * from dept;

猜你喜欢

转载自blog.csdn.net/qq_45696288/article/details/122202125