Nibble MatrixBy Kamlesh Kumar SinghDatabase - 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 / ExcelRDBMS - Relational Database Management System. - SQL Server / Oracle upto 7.3 / sybase / MySQL.ORDBMS - Object Relational Database Management System. - Oracle8oracle8 - Object, user define composite data types.OODBMS - Object Oriented Database Management System. - Oracle 8i & 9i & 10Gi - 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, truncateDML - delete / insert / update
DCL - grant / revokeDRL - select statementTCL - commit / rollback / savepoint.Codds Rules - 12 rules + 1 zero rule = 13 rules .Codds -< 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 viewauto updation - simple views updatable but complex view - not auto updatable.work with Oracleenv -
SQLPLUS - Oracle envISQLPLUS - in 9i , for use oracle with web browser. ie - http://localhost/isqlplus--to create own database table.1- without constraints 2 - with constraintsconstraints -not null - user cant insert into null values.unique - not null + not duplicateprimary key - not null + not duplicate = can ref with other tableforeign or reference key - child records.check - condition check at the DML operator(insert & update) time.default - if user not spacify value for a column thendefault value to be inserted.constraints name -database server assign (auto) user (explicit)
data typenumber(4) - int onlynumber(7,2) - floating value 99999.99char - fixed lengthvarchar & varchar2 - dynamic , but has max limitlob - upto 4 gblong - upto 2 gbdate - for date & time.user define composite data type - Object.- to add constraints in table .1 - at the table creation time2 - 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.
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 columninsert into cemp(empno,ename,sal) values(1003,Amar,9299);- to add extra column in tablealter table cemp add jdate date- to change the column sizealter table cemp modify sal number(8,2)*** but for decreasing column size table should be empty
- to add constraint after table creation.alter table bemp add constraints mys check(sal<50000)- to enable or desable constaints- for disablealter table cemp disable constraints mysal;- for enablealter table femp enable constraints mysal;*** for enabling constraints condition must be fullfil- to drop a constraintsalter table femp drop constraints mysal- to set unused columnalter table cemp set unused column jdate- to drop all unused columns
alter table cemp drop unused columns- to drop an columnalter table cemp drop column deptno- to rename tablerename cemp to cc;- to create synonymcreate synonym c for femp- to create table with foreign keycreate table fincr(empno number(4) references femp(empno),amount number(5))-create table fbonus
(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 dwhere e.deptno=d.deptno2- Non Equi Join - when common column not exist in both table.select empno,ename,sal,grade from emp,salgradewhere sal between losal and hisal3- Cross Join - table join without any condition.result - Ist table each row * each row of IInd tableselect empno,ename,sal,e.deptno,dname from emp e,dept d
4- Outer Join - like equi join.Left Outer join - display those record also which rexist in IInd not exist in Ist.select empno,ename,sal,e.deptno,dname from emp e,dept dwhere e.deptno=d.deptno(+)10203040 - not 50Right outer join - display those record also which rexist in Ist not exist in IInd.select empno,ename,sal,e.deptno,dname from emp e,dept dwhere e.deptno(+)=d.deptno10203050 - not 405- Self Join - when one table use a multiple table.
- list of all emp with his/her manager name.select e.ename,m.ename as manager from emp e,emp mwhere e.mgr = m.empno- List of all emp whose sal > their manager.select e.ename,m.ename as manager from emp e,emp mwhere 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 windowthrough 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.
views types ->1> Simple 2> complexDeff. between Simple & complex views : -------------------------------------Feature Simple Complex1> no. of tables one one or more2> contain function no yes3> contain groups no yesof data4> DML through views yes not allowedview typesimple view - with single tablecomplex view - with more then one table or with functionsread only view - read only.force view - view created without having base table
in line view - when select statement pass as a object in from clauseQ1. -> 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 empwhere 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 empwhere 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
** to create a view with check option -create view a2 as select empno,ename,sal,deptno from empwhere 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 viewselect * 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 keyCREATE TABLE M_DEPT(DEPTNO NUMBER(2) CONSTRAINTS DE PRIMARY KEY,DNAME VARCHAR2(10) NOT NULL);--> with foreign key
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 viewCREATE VIEW CCC4 AS SELECT EMPNO,ENAME,SAL,E.DEPTNO,D.DNAME FROM M_EMP E,M_DEPT DWHERE 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 operatorA DISTINCT operatorAn aggregate or analytic functionA GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause
A collection expression in a SELECT listA subquery in a SELECT listA subquery designated WITH READ ONLYAn inline view is a SELECT statement in the FROM-clause of another SELECT statement. In-line views arecommonly usedsimplify complex queries by removing join operations and condensing several separate queries into asingle 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 Postgrescommunitysimply refers to it as a subselect (subselects are inline views + subqueries in Oracle nomenclature).ExamplesExample inline view:SELECT *FROM ( SELECT deptno, count(*) emp_countFROM empGROUP BY deptno ) emp,deptWHERE dept.deptno = emp.deptno;Another good example of an inline view is:SELECT a.last_name, a.salary, a.department_id, b.maxsalFROM employees a,( SELECT department_id, max(salary) maxsalFROM employees
GROUP BY department_id ) bWHERE a.department_id = b.department_idAND 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;orcreate snapshot cc2 as select * from m_emp-to drop a materialized viewdrop 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 useris created the DBA can grant the specific system privileges to a user.
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 ofthe 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 databaseresource - to use the data base resourcesdba - for DBA task (database Administration )- to create user define role -create role baba;
grant create session,create table , create procedure to baba;grant baba to singh;-- TO DROP A ROLEDROP ROLE BABAGrant - to assign the privilegesrevoke - to remove the privileges.System privileges -create session - for connectcreate table - for create tablecreate view - for view creationcreate sequence - for sequence creationcreate procedure. - for function , procedure & package creationunlimited tablespace
Object privileges -privileges table view sequence procedurealter y - y -delete Y Y - -execute - - - Yindex Y - - -insert Y Y - -references Y - - -select Y Y Y -update Y Y - -=allpublic - 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
create user username identified by password-- to change the passwordalter user username identified by password;-- for connecting user should have connect / create sessionprivilege.-- to assign Object privileges -grant select , insert , update on emp to singh.grant all on emp to public - for all userpublic - for all usersgrant select , insert , update on emp to singh-- TO DROP A USERDROP USER USERNAMEDROP USER USERNAME CASCADE - DROP USER WITH ALL RELATED LINKS
- for assign Object privilegesgrant select,insert , update on emp to singh;grant select,insert , update on emp to public; -- to assign select , insert & updateprivilege 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 rrevoked when the granters previleges is revoked.sequence - sequence is used for auto number generation ,for serial no ,
rollno / empno & more.syn -create sequence mystart witn 1001increment by 1minvalue 1001maxvalue 1010cyclecache 3attributes for sequence -nextval - next value from sequencecurrval - current value in sequence-- to alter seqnencealter sequence mysincrement by 2maxvalue 1020cycle
select ab.nextval from dual-- to use sequence in tableinsert into emp values(ab.nextval,&name,&sal)-- to drop a sequencedrop sequence ab;-- to display list of all sequenceselect * from user_sequences;Temporary Table :----------------used for Temporary result.data exist only for the duration of a transction or session .
only visible with in a single transaction or session. no redo generated.created in users temporary tablespace.syn:-create global temporary table ab(eno number(4) primary key,ename char(22))on commit delete rowsthis table is for transction duration.* no data segment created.Indexing - An Oracle Server Index is a Schema Object that can speedup the retrieval of row by using a pointer. Indexes can becreated 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 oftable creation, if a table definition contains a PRIMARY KEY.Explicit Index - that is created by user.
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 andoften accessed together.=> A cluster is group of rows from separate tables storing in thesame 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 cant be partition.steps for creating clusters -=> creating cluster
create cluster cname(colname datatype)=> creating cluster tablescreate 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 datablock.=> they save disk space and improve perforence for many operations.syn -create index iname on cluster cname;
=> to drop an clusterdrop cluster cname;*** cluster not deleted if cluster is not empty.drop cluster cname including tables ;drop cluster cname including tables cascade constraints ;=>