database oracle basic

27
Nibble Matrix By Kamlesh Kumar Singh Database - Collection of data files. DataFile - collection of records(rows). Record(rows) - collection of fields. Field = set of informations. DBMS - Database Management System. A System which manage the database. Database Handler - each & every database has one database handler - that handle uthe database. DBMS - Database Management System. - Access / FoxPro / Excel RDBMS - Relational Database Management System. - SQL Server / Oracle upto 7.3 / sybase / MySQL. ORDBMS - Object Relational Database Management System. - Oracle8 oracle8 - Object, user define composite data types. OODBMS - Object Oriented Database Management System. - Oracle 8i & 9i & 10G i - internet programming. - java is a kind of internet programming. need for java - JVM , Oracle8i - JVM include - SQLJ - SQL Java programming. SQL - Structured Query Language - common language for all Databases. SQL - DDL - create / alter / drop, truncate DML - delete / insert / update

Upload: kamlesh-kumar-singh

Post on 29-Nov-2014

768 views

Category:

Technology


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Database Oracle Basic

Nibble Matrix

By Kamlesh Kumar Singh

Database - Collection of data files.

DataFile - collection of records(rows).

Record(rows) - collection of fields.

Field = set of informations.

DBMS - Database Management System.

A System which manage the database.

Database Handler - each & every database has one database handler - that handle uthe database.

DBMS - Database Management System. - Access / FoxPro / Excel

RDBMS - Relational Database Management System. - SQL Server / Oracle upto 7.3 / sybase / MySQL.

ORDBMS - Object Relational Database Management System. - Oracle8

oracle8 - Object, user define composite data types.

OODBMS - Object Oriented Database Management System. - Oracle 8i & 9i & 10G

i - internet programming. - java is a kind of internet programming.

need for java - JVM , Oracle8i - JVM include - SQLJ - SQL Java programming.

SQL - Structured Query Language - common language for all Databases.

SQL -

DDL - create / alter / drop, truncate

DML - delete / insert / update

Page 2: Database Oracle Basic

DCL - grant / revoke

DRL - select statement

TCL - commit / rollback / savepoint.

Codd's Rules - 12 rules + 1 zero rule = 13 rules .

Codd's -

< 6 rules support - DBMS

>= 6 rules support - RDBMS.

Oracle support 12.5 rules.

.5 not supported by oracle that is view updation.

views -

simple view & complex view

auto updation - simple views updatable but complex view - not auto updatable.

work with Oracle

env -

Page 3: Database Oracle Basic

SQLPLUS - Oracle env

ISQLPLUS - in 9i , for use oracle with web browser. ie - http://localhost/isqlplus

--to create own database table.

1- without constraints 2 - with constraints

constraints -

not null - user can't insert into null values.

unique - not null + not duplicate

primary key - not null + not duplicate = can ref with other table

foreign or reference key - child records.

check - condition check at the DML operator(insert & update) time.

default - if user not spacify value for a column then

default value to be inserted.

constraints name -

database server assign (auto) user (explicit)

Page 4: Database Oracle Basic

data type

number(4) - int only

number(7,2) - floating value 99999.99

char - fixed length

varchar & varchar2 - dynamic , but has max limit

lob - upto 4 gb

long - upto 2 gb

date - for date & time.

user define composite data type - Object.

- to add constraints in table .

1 - at the table creation time

2 - after table creation.

-- to create a table without constraints.

create table semp

(

empno number(4),

ename varchar(20),

sal number(7,2),

deptno number(2)

)

-- to create a table with constraints.

Page 5: Database Oracle Basic

create table femp

(

empno number(4) constraints myno primary key,

ename varchar(20) not null,

sal number(7,2) constraints mysal check(sal> 5000),

deptno number(2) default(10) check(deptno in(10,20,30,40))

)

- to insert default value in table column

insert into cemp(empno,ename,sal) values(1003,'Amar',9299);

- to add extra column in table

alter table cemp add jdate date

- to change the column size

alter table cemp modify sal number(8,2)

*** but for decreasing column size table should be empty

Page 6: Database Oracle Basic

- to add constraint after table creation.

alter table bemp add constraints mys check(sal<50000)

- to enable or desable constaints

- for disable

alter table cemp disable constraints mysal;

- for enable

alter table femp enable constraints mysal;

*** for enabling constraints condition must be fullfil

- to drop a constraints

alter table femp drop constraints mysal

- to set unused column

alter table cemp set unused column jdate

- to drop all unused columns

Page 7: Database Oracle Basic

alter table cemp drop unused columns

- to drop an column

alter table cemp drop column deptno

- to rename table

rename cemp to cc;

- to create synonym

create synonym c for femp

- to create table with foreign key

create table fincr

(

empno number(4) references femp(empno),

amount number(5)

)

-

create table fbonus

Page 8: Database Oracle Basic

(

empno number(4) references femp(empno) on delete cascade,

amount number(5) )

on delete cascade option - if parent record deleted then child record auto deleted.

else delete child record first then parent record deleted.

Joins - to select data from more then one tables.

type -

1- Equi Join - when common column exist in both tables.

select empno,ename,sal,e.deptno,dname from emp e,dept d

where e.deptno=d.deptno

2- Non Equi Join - when common column not exist in both table.

select empno,ename,sal,grade from emp,salgrade

where sal between losal and hisal

3- Cross Join - table join without any condition.

result - Ist table each row * each row of IInd table

select empno,ename,sal,e.deptno,dname from emp e,dept d

Page 9: Database Oracle Basic

4- Outer Join - like equi join.

Left Outer join - display those record also which r

exist in IInd not exist in Ist.

select empno,ename,sal,e.deptno,dname from emp e,dept d

where e.deptno=d.deptno(+)

10

20

30

40 - not 50

Right outer join - display those record also which r

exist in Ist not exist in IInd.

select empno,ename,sal,e.deptno,dname from emp e,dept d

where e.deptno(+)=d.deptno

10

20

30

50 - not 40

5- Self Join - when one table use a multiple table.

Page 10: Database Oracle Basic

- list of all emp with his/her manager name.

select e.ename,m.ename as manager from emp e,emp m

where e.mgr = m.empno

- List of all emp whose sal > their manager.

select e.ename,m.ename as manager from emp e,emp m

where e.mgr = m.empno and e.sal > m.sal

- List of all emp who join company before their manager.

VIEWS :->

-----

* A view is a logical table based on a table or another view.

* A view contains no data of its own. but it is like a window

through witch data from table can be viewed or change.

* The tables on which a view is based are called base tables.

* the view is stored as a select statement in the data dictionary.

* the changes in the table are automatically reflected in the views.

Advantages of a view : ->

* to make a complex queries easy.

* to allow data independence .

* securities.

Page 11: Database Oracle Basic

views types ->

1> Simple 2> complex

Deff. between Simple & complex views : -

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

Feature Simple Complex

1> no. of tables one one or more

2> contain function no yes

3> contain groups no yes

of data

4> DML through views yes not allowed

view type

simple view - with single table

complex view - with more then one table or with functions

read only view - read only.

force view - view created without having base table

Page 12: Database Oracle Basic

in line view - when select statement pass as a object in from clause

Q1. -> create a view, amit, that contains details of employees in deptno 10 .

Ans.

create or replace view amit as select empno,ename,sal,deptno from emp

where deptno=10;

with read only option. - > to used for create a read only view

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

Q2. -> create read only view for employees of dept 10 ?

Ans. ->

create view nnn as select empno,ename,sal from emp

where deptno=10 with read only;

with check option --> when the WITH CHECK OPTION CLAUSE is used , any DML statement that

----------------- manipulate rows that can be not be selected by a VIEW ,are rejected

Page 13: Database Oracle Basic

** to create a view with check option -

create view a2 as select empno,ename,sal,deptno from emp

where deptno=10 with check option;

* in this view we can insert only in dept 10 not in any other depts.

* to select data from a view

select * from viewname;

* to create a complex view .

CREATE VIEW CCC3 AS SELECT EMPNO,ENAME,SAL,E.DEPTNO,D.DNAME FROM EMP E,DEPT D

* create table with keys(primary key , Foreign key)

-->with Primary key

CREATE TABLE M_DEPT

(

DEPTNO NUMBER(2) CONSTRAINTS DE PRIMARY KEY,

DNAME VARCHAR2(10) NOT NULL

);

--> with foreign key

Page 14: Database Oracle Basic

CREATE TABLE M_EMP

(

EMPNO NUMBER(4) PRIMARY KEY,

ENAME VARCHAR2(10),

SAL NUMBER(7,2),

DEPTNO NUMBER(2),

FOREIGN KEY(DEPTNO) REFERENCES M_DEPT(DEPTNO)

);

--> create view

CREATE VIEW CCC4 AS SELECT EMPNO,ENAME,SAL,E.DEPTNO,D.DNAME FROM M_EMP E,M_DEPT D

WHERE E.DEPTNO=D.DEPTNO

-- force view - view create without a base table.

create force view amit6 as select * from baba

--------->

The view must not contain any of the following constructs:

A set operator

A DISTINCT operator

An aggregate or analytic function

A GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause

Page 15: Database Oracle Basic

A collection expression in a SELECT list

A subquery in a SELECT list

A subquery designated WITH READ ONLY

An inline view is a SELECT statement in the FROM-clause of another SELECT statement. In-line views are

commonly used

simplify complex queries by removing join operations and condensing several separate queries into a

single query.

This feature was introduced in Oracle 7.2.

This feature is commonly referred to in the MSSQL community as a derived table, and in the Postgres

community

simply refers to it as a subselect (subselects are inline views + subqueries in Oracle nomenclature).

[edit]Examples

Example inline view:

SELECT *

FROM ( SELECT deptno, count(*) emp_count

FROM emp

GROUP BY deptno ) emp,

dept

WHERE dept.deptno = emp.deptno;

Another good example of an inline view is:

SELECT a.last_name, a.salary, a.department_id, b.maxsal

FROM employees a,

( SELECT department_id, max(salary) maxsal

FROM employees

Page 16: Database Oracle Basic

GROUP BY department_id ) b

WHERE a.department_id = b.department_id

AND a.salary = b.maxsal;

Materialized view or snapshot-> it is one kind of physical table not a logical table.

automatic updation not allowed.

*to create a Materialized view ->

create materialized view cc2 as select * from m_emp;

or

create snapshot cc2 as select * from m_emp

-to drop a materialized view

drop materialized view viewname

* Materialized view not created if table does not contain a primary key constraint.

* for creating materialized view user should have dba privileges.

the DBA can create a user by using the create user command, once a user

is created the DBA can grant the specific system privileges to a user.

Page 17: Database Oracle Basic

Schema - collection of Objects such as tables / views & sequences.

user schema - collection of users Object.

Data Securities -

System Securities - System privileges - gain access to the database.

Data Securities - Object privileges - manipulates the content of

the database Objects.

Object privileges vary from Object to Objects.

Role - group of privileges , role is a kind of intermediate user.

oracle server provide the 3 standard role.

connect - to connect with database

resource - to use the data base resources

dba - for DBA task (database Administration )

- to create user define role -

create role baba;

Page 18: Database Oracle Basic

grant create session,create table , create procedure to baba;

grant baba to singh;

-- TO DROP A ROLE

DROP ROLE BABA

Grant - to assign the privileges

revoke - to remove the privileges.

System privileges -

create session - for connect

create table - for create table

create view - for view creation

create sequence - for sequence creation

create procedure. - for function , procedure & package creation

unlimited tablespace

Page 19: Database Oracle Basic

Object privileges -

privileges table view sequence procedure

alter y - y -

delete Y Y - -

execute - - - Y

index Y - - -

insert Y Y - -

references Y - - -

select Y Y Y -

update Y Y - -

=

all

public - for all users

** for DBA - user should have DBA privilege.

-- to display list of all existsing.

select * from all_user.

-- to create a new user

Page 20: Database Oracle Basic

create user username identified by password

-- to change the password

alter user username identified by password;

-- for connecting user should have connect / create session

privilege.

-- to assign Object privileges -

grant select , insert , update on emp to singh.

grant all on emp to public - for all user

public - for all users

grant select , insert , update on emp to singh

-- TO DROP A USER

DROP USER USERNAME

DROP USER USERNAME CASCADE - DROP USER WITH ALL RELATED LINKS

Page 21: Database Oracle Basic

- for assign Object privileges

grant select,insert , update on emp to singh;

grant select,insert , update on emp to public; -- to assign select , insert & update

privilege for all user.

grant all on emp to public; -- to assign all privileges for all user.

-- to revoke the Object privileges -

revoke select , insert , update on emp from singh;

with grant Option - Object privileges granted with grant option r

revoked when the granter's previleges is revoked.

sequence - sequence is used for auto number generation ,for serial no ,

Page 22: Database Oracle Basic

rollno / empno & more.

syn -

create sequence my

start witn 1001

increment by 1

minvalue 1001

maxvalue 1010

cycle

cache 3

attributes for sequence -

nextval - next value from sequence

currval - current value in sequence

-- to alter seqnence

alter sequence mys

increment by 2

maxvalue 1020

cycle

Page 23: Database Oracle Basic

select ab.nextval from dual

-- to use sequence in table

insert into emp values(ab.nextval,'&name',&sal)

-- to drop a sequence

drop sequence ab;

-- to display list of all sequence

select * from user_sequences;

Temporary Table :-

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

used for Temporary result.data exist only for the duration of a transction or session .

Page 24: Database Oracle Basic

only visible with in a single transaction or session. no redo generated.

created in user's temporary tablespace.

syn:-

create global temporary table ab

(

eno number(4) primary key,

ename char(22)

)

on commit delete rows

this table is for transction duration.

* no data segment created.

Indexing - An Oracle Server Index is a Schema Object that can speed

up the retrieval of row by using a pointer. Indexes can be

created explicit & implicit.

if U do not have index on the column, then a full table scan will occur.

implicit index - ORACLE itself creates indexes at the time of

table creation, if a table definition contains a PRIMARY KEY.

Explicit Index - that is created by user.

Page 25: Database Oracle Basic

create [UNIQUE] index INDEXNAME on TABLENAME[COLNAME];

*** to display list of all user indexes.

select * from user_indexes;

CLUSTERING -

=> Clusters r used for performance improvement.

=> Clustering is a method of storing tables that r related and

often accessed together.

=> A cluster is group of rows from separate tables storing in the

same disk block.

=> Clustered tables must have a common column called cluster column.

=> to Cluster tables the user must own the tables.

*** if table is the part of cluster can't be partition.

steps for creating clusters -

=> creating cluster

Page 26: Database Oracle Basic

create cluster cname(colname datatype)

=> creating cluster tables

create table tname

(

colname datatype primary key,

colname datattype

)cluster cname(colname)

create table tname

(

colname datatype references tname(colname),

colname datattype

)cluster cname(colname)

=> creating cluster indexes.

=> In a clustered index, the actual data is stored in the indexed order.

=> Each Unique cluster key value is stored only once in the each data

block.

=> they save disk space and improve perforence for many operations.

syn -

create index iname on cluster cname;

Page 27: Database Oracle Basic

=> to drop an cluster

drop cluster cname;

*** cluster not deleted if cluster is not empty.

drop cluster cname including tables ;

drop cluster cname including tables cascade constraints ;

=>