Вы находитесь на странице: 1из 24

ASSIGNMENT-1

2Q) Assume that a sequence S1 is created with the MAXVALUE m1, the
MINVALUE m2, INCREMENT BY i1, and with both CYCLE and CACHE
options. Then, what will be the maximum value that can be specified For
CACHE?

ANS:-

The value for cache is determined by using the formula


CACHE=CEIL (MAXVALUE - MINVALUE)/ABS(INCREMENT)

In this problem MAXVALUE =M1=58;


MINVALUE=M2=16;
INCREMENT=I1=2;
CACHE =(m1-m2)/i1
= ( 58-16)/2
=21
EXAMPLE:-

create sequence s1 maxvalue 58 minvalue 16 increment by 2 cycle cache


21;

Sequence created.

select s1.nextval from dual;


NEXTVAL
16
select s1.nextval from dual;
NEXTVAL
18
select s1.nextval from dual;
NEXTVAL
20
select s1.nextval from dual;
NEXTVAL
22
select s1.nextval from dual;
NEXTVAL
24
select s1.nextval from dual;
NEXTVAL
26
select s1.nextval from dual;
NEXTVAL
28
select s1.nextval from dual;
NEXTVAL
30
select s1.nextval from dual;
NEXTVAL
32
select s1.nextval from dual;
NEXTVAL
34
select s1.nextval from dual;
NEXTVAL
36
select s1.nextval from dual;
NEXTVAL
38
select s1.nextval from dual;
NEXTVAL
40
select s1.nextval from dual;
NEXTVAL
42

select s1.nextval from dual;


NEXTVAL
44
select s1.nextval from dual;
NEXTVAL
46
select s1.nextval from dual;
NEXTVAL
48
select s1.nextval from dual;
NEXTVAL
50
select s1.nextval from dual;
NEXTVAL
52
select s1.nextval from dual;
NEXTVAL
54
select s1.nextval from dual;
NEXTVAL
56
select s1.nextval from dual;
NEXTVAL
58
select s1.nextval from dual;
NEXTVAL
16

----------------------------------0----------------------------------------

3Q)Assume that there are three tables T1, T2, and T3.each table is with
two columns TNAME and TID. T2 and T3 are referencing tables to T1 via
TID column with neither ON DELETE CASCADE nor ON DELETE SET NULL.
What are the scenarios in which the rows of T1 can be deleted and also
write the statements for creating the tables with the referential integrity
relationships between T1, T2 and T3?

ANS:-

create table t1(tid number primary key,tname varchar2(10));


Table created.
create table t2(cid number,cname varchar2(10),tid number,foreign
key(tid)references t1(tid));
Table created.
create table t3(cid number,cname varchar2(10),tid number,foreign
key(tid)references t1(tid));
Table created.
insert into t1 values(1,'A');
1 row created.
insert into t1 values(2,'B');
1 row created.
insert into t1 values(3,'C');
1 row created.
insert into t2 values(1,'A',3);
1 row created.
insert into t2 values(2,'B',1);
1 row created.
insert into t2 values(3,'C',2);
1 row created.
insert into t3 values(3,'C',2);
1 row created.
insert into t3 values(2,'B',1);
1 row created.
insert into t3 values(1,'A',3);
1 row created.
select * from t1;

TID TNAME
1 A
2 B
3 C
select * from t2;

CID CNAME TID


1 A 3
2 B 1
3 C 2

SQL> select * from t3;

CID CNAME TID


3 C 2
2 B 1
1 A 3
SQL> delete from t1 where tid=2;
delete from t1 where tid=2
ERROR at line 1:
ORA-02292: integrity constraint (SCOTT.SYS_C0010456) violated - child
record found

Scenarios:-
 T1=parent table T2=T3=child table;
 On deleting the child rows in referencing table.
 By dropping the constraint in the referenced table.
-------------------------------0--------------------------------

4Q) Assume that there is a sequence S1 creataed with no options, and


the current value of the sequence is 10. Specify the statements in order
to reset the sequence to 8, so that the next value will be generated
after 8 is 11?

ANS:-
create sequence s1;

Sequence created.

select s1.nextval from dual;

NEXTVAL

select s1.nextval from dual;

NEXTVAL
2

select s1.nextval from dual;

NEXTVAL

select s1.nextval from dual;

NEXTVAL

select s1.nextval from dual;

NEXTVAL

select s1.nextval from dual;

NEXTVAL

select s1.nextval from dual;

NEXTVAL

select s1.nextval from dual;


NEXTVAL

select s1.nextval from dual;

NEXTVAL

select s1.nextval from dual;

NEXTVAL

10

alter sequence s1 increment by -2;


Sequence altered.
select s1.nextval from dual;

NEXTVAL

alter sequence s1 increment by 3;

Sequence altered.

select s1.nextval from dual;

NEXTVAL

11
----------------------------------------0-------------------------------------------

5Q) Give an example for a table having a composite primary key. Create
the table and provide the explanation for the scenario where this
requirement is needed?

ANS:-

create table student(no number,name varchar(7),marks


number,primary key(no,name));

Table created.

insert into student values(1,'siva',76);

1 row created.

insert into student values(1,'ravi',56);

1 row created

insert into student values(1,'somu',89);

1 row created.

insert into student values(3,'somu',89);

1 row created.

select * from student;


NO NAME MARKS

1 siva 76

1 ravi 56

1 somu 89

3 somu 89

Scenarios:-
 If two or more column together form primary key then it is called
composite primary key.
 The combination of columns values should be unique.
 We can define composite keys on referential integrity constraints.
 Composite key can be defined alter levels only.

-----------------------------------0---------------------------------
QUESTION:1

create table college(cname varchar2(5) primary key,

coffice number,

cphone number,

dename varchar2(10) unique);

Table created.

create table inst(id number unique,iname varchar2(10),

ioffice varchar2(10),

iphone number,

rank number,

cname varchar2(5),foreign key(cname) references college(cname));

Table created.

create table dept(dname varchar2(5) primary key,

dcode number unique,

doffice varchar2(5),

dphone number,

hod varchar2(10) unique,


cstart date,

id number,foreign key(id) references inst(id));

Table created.

create table course(coname varchar2(10),

ccode number,

clevel number check(clevel in(1,2,3,4,5,6)),

cleveldesc varchar2(10),

primary key(coname,ccode,clevel),

credits number,

cdesc varchar2(50),

dname varchar2(5),foreign key(dname) references dept(dname));

Table created.

create table student(fname varchar2(10),

mname varchar2(10),

lname varchar2(10),

sid number unique,

saddr varchar2(20),
sph number,

mcode number,

dob date,

grade varchar2(4),

dname varchar2(5),foreign key(dname) references


dept(dname));

Table created.

create table section(secid varchar2(5) unique,

secno varchar2(3),

year number,

sem number,

bldg varchar2(4),

croom varchar2(6),

cdays varchar2(25),

ctimings varchar2(25),

sid number,foreign key(sid) references student(sid));

Table created.
desc college;

Name Null? Type

CNAME NOT NULL VARCHAR2(5)

COFFICE NUMBER

CPHONE NUMBER

DENAME VARCHAR2(10)

insert into college values('NIT',100,9123456780,'SUNIL');

1 row created.

insert into college values('PNC',101,8123456790,'PURNA');

1 row created.

select * from college;


CNAME COFFICE CPHONE DENAME

NIT 100 9123456780 SUNIL

PNC 101 8123456790 PURNA

desc inst;

Name Null? Type

ID NUMBER

INAME VARCHAR2(10)

IOFFICE VARCHAR2(10)

IPHONE NUMBER

RANK NUMBER

CNAME VARCHAR2(5)

insert into inst values(1,'VASU','EC-1',7654321890,1,'NIT');

1 row created.

insert into inst values(2,'JAGAN','EE-1',7436551890,2,'NIT');


1 row created.

select * from inst;

ID INAME IOFFICE IPHONE RANK CNAME

1 VASU EC-1 7654321890 1 NIT

2 JAGAN EE-1 7436551890 2 NIT

desc dept;

Name Null? Type

DNAME NOT NULL VARCHAR2(5)

DCODE NUMBER

DOFFICE VARCHAR2(5)

DPHONE NUMBER

HOD VARCHAR2(10)

CSTART DATE

ID NUMBER
insert into dept values('ECE',4,'EC-1',7776565451,'VENKATRAO','15-
JUN-2003',1);

1 row created.

insert into dept values('EEE',3,'EE-1',7776565422,'LAKSHAMAN','19-


JAN-2016',2);

1 row created.

insert into dept values('IT',2,'IT-1',7546565422,'LAKSHMI','19-JUL-


2016',2);

1 row created.

select * from dept;

DNAME DCODE DOFFI DPHONE HOD CSTART ID

ECE 4 EC-1 7776565451 VENKATRAO 15-JUN-03 1

EEE 3 EE-1 7776565422 LAKSHAMAN 19-JAN-16 2

IT 2 IT-1 7546565422 LAKSHMI 19-JUL-16 2


desc course;

Name Null? Type

CONAME NOT NULL VARCHAR2(10)

CCODE NOT NULL NUMBER

CLEVEL NOT NULL NUMBER

CLEVELDESC VARCHAR2(10)

CREDITS NUMBER

CDESC VARCHAR2(50)

DNAME VARCHAR2(5)

insert into course values('NETWORK',786,1,'FRESHMAN',60,'INTRODUCTION TO


NETWORKS','EEE');

1 row created.

insert into course values('NETWORK',786,2,'SOPHOMORE',90,'NETWORKS


THEORY','EEE');

1 row created.
insert into course values('NETWORK',786,3,'JUNIOR',120,'NETWORKS
REPESENTATION','EEE');

1 row created.

insert into course values('NETWORK',786,4,'SENIOR',150,'NETWORKS


DESIGN','EEE');

1 row created.

insert into course values('NETWORK',786,5,'MS LEVEL',180,'NETWORKS


GRAPHS','EEE');

1 row created.

insert into course values('NETWORK',786,6,'PHD LEVEL',210,'NETWORKS


TABLES','EEE');

1 row created.

CONAME CCODE CLEVEL CLEVELDESC CREDITS CDESC DNAME

NETWORK 786 1 FRESHMAN 60 INTRODUCTION TO NETWORKS EEE

NETWORK 786 2 SOPHOMORE 90 NETWORKS THEORY EEE

NETWORK 786 3 JUNIOR 120 NETWORKS REPESENTATION EEE

NETWORK 786 4 SENIOR 150 NETWORKS DESIGN EEE

NETWORK 786 5 MS LEVEL 180 NETWORKS GRAPHS EEE

NETWORK 786 6 PHD LEVEL 210 NETWORKS TABLES EEE

6 rows selected.
desc student;

Name Null? Type

FNAME VARCHAR2(10)

MNAME VARCHAR2(10)

LNAME VARCHAR2(10)

SID NUMBER

SADDR VARCHAR2(20)

SPH NUMBER

MCODE NUMBER

DOB DATE

GRADE VARCHAR2(4)

DNAME VARCHAR2(5)

insert into student values('SIVA','REDDY','DODDA',1,'3-40 NEAR BUS


STAND',7095754681,4,'05-JAN-1997','A+','ECE');

1 row created.

insert into student values('VASU','DEVAREDDY','DODDA',2,'3-40 NEAR


BUS STAND',7093666803,4,'23-DEC-1999','B','ECE');

1 row created.
select * from student;

FNAME MNAME LNAME SID SADDR SPH MCODE DOB GRAD DNAME

SIVA REDDY DODDA 1 3-40 NEAR 7095756481 4 05-JAN-97 A+ ECE

BUS STAND

VASU DEVAREDDY DODDA 2 3-40 NEAR 7093666803 4 23-DEC-99 B ECE

BUS STAND

desc section;

Name Null? Type

SECID VARCHAR2(5)

SECNO VARCHAR2(3)

YEAR NUMBER

SEM NUMBER

BLDG VARCHAR2(4)

CROOM VARCHAR2(6)

CDAYS VARCHAR2(25)

CTIMINGS VARCHAR2(25)

SID NUMBER

insert into section values('E-2',1,1,1,3,'E31','MONDAY FRIDAY','9AM-9.50AM 3.30PM-5.20PM',1);

1 row created.

insert into section values('E-1',1,1,1,3,'E31','MONDAY FRIDAY','9AM-9.50AM 3.30PM-5.20PM',2);

1 row created.

insert into section values('E-3',1,1,1,3,'E31','MONDAY FRIDAY','9AM-9.50AM 3.30PM-5.20PM',2);


1 row created.

select * from section;

SECID SEC YEAR SEM BLDG CROOM CDAYS CTIMINGS SID


E-2 1 1 1 2 E31 MONDAY FRIDAY 9AM-9.50AM 1
3.30PM-5.20PM
E-1 1 1 1 3 E31 MONDAY FRIDAY 9AM-9.50AM 2
3.30PM-5.20PM

E-3 1 1 1 3 E31 MONDAY FRIDAY 9AM-9.50AM 2


3.30PM-5.20PM

------------------------------------------------0------------------------------------------

Вам также может понравиться