You are on page 1of 7

how to work with database -

Database - database is the collection of interrelated data files, logically


called tables.
Datafile - data file is the collection of interrelated records - logically
called rows.
Record - collection of interrelated fiels - roll, name,fanme,dob,mobile,
address doa
Field - set of information.

EMPNO ENAME SAL DEPTNO MGR


----- ---------- ---------- ---------- ----------
7369 SMITH 800 20 7902
7499 ALLEN 1600 30 7698
7521 WARD 1250 30 7698
7566 JONES 2975 20 7839
7654 MARTIN 1250 30 7698
7698 BLAKE 2850 30 7839
7782 CLARK 2450 10 7839
7788 SCOTT 3000 20 7566
7839 KING 5000 10
7844 TURNER 1500 30 7698
7876 ADAMS 1100 20 7788
7900 JAMES 950 30 7698
7902 FORD 3000 20 7566
7934 MILLER 1300 40 7782

whole - data file - logically called table(EMP) - collection of records - rows


single row - one record - collection of fields
field - set of informations

DBMS - Database Management System - reverse - A system which Mnage the Database
each&every DBMS has one Database Handler - for handle/manage the all
database operations.
like - delete / update / insert / alter / drop / create.
RDBMS - Relational Database Management System - table link with each others - with
keys \
Primary key & foreign key.

Datatabase Archi -

1. Hierarchical Database Management System - out dated - not success - tree like
structure.
2. Relational Database Management System - More Success - Database table linked
with each other - link with keys - primary key (parent) & foreign key (childs)
3. Network Database Management System - flop - more complex - concept of
pointers.

E.F. Codd's - DBMS & RDBMS - Rules - 12 Rules + 1 rule extra(Zero Rule) - 13 Rules

according to E.F. Codd - <6 rules support - DBMS


>=6 Rules support - RDBMS

Zero Rule - apply on Remote Database - client & server Archi.

DBMS - Excel, Access , dbase, foxpro


RDBMS - SQL Server, MYSQL, Oracle upto 7.3, SQLlite .
ORDBMS - Object Relational DBMS - Oracle 8
in oracle 8 we have concept of objects - it is a kind of user define data type,
like structure in C & C++ or like classes in C++ & java .
Objects - column object + row object + ref object
OODBMS - Object Oriented DBMS - Oracle 8i, 9i , 10g, 11g (g - Grid System - Grid
Processing - fast)
in oracle 8i & 9i - we have SQLJ(SQL Java) Programming.

i - for internet - internet programming - Java,

Data.sqlj - compile - Data.java ->compile -> Data.class - execute

how to work with Oracle -

SQL - Structured Query Language - common language for all databases,


its made up with 5 Languages

1. DDL - Data Dafinition Language - create / drop / alter / truncate


2. DML - Data Manipulation Language - insert / update / delete / merge(9i)
3. DRL - Data Retreival language - select statement
4. TCL - Transaction Controll Language - commit / rollback
5. DCL - Data Control Language - grant / revoke - permission assign / remove

State statement - proper syn -

SELECT */colName/s FROM tabName


WHERE condition
GROUP BY colname/colindex
HAVING condition
ORDER BY colName/colindex DESC

DELETE TRUNCATE
* DELETE ROW BY ROW * DELETE ALL RECORD IN ONE TIME.
* RECORD DELETED, BUT MEMORY EXIST. * DELETE RECORD & MEMORY BOTH.
* WE CAN ROLLBACK AFTER DELETE * WE CAN'T ROLLBACK AFTER TRUNCATE
* SLOW * FAST
* PART OF DML * PART OF DDL

WHERE HAVING
* use with select statement(just after select) * use with Group by Statement.
* single row condition * group condition
* group funcrtions not allowed. * only group function allowed
(we can't use without group by.)
-- list of all employees whose sal > 10000
select * from emp
where sal > 10000;

-- list of all employees who have more then 5 increments.


select count(*) as NOI,sum(amount) as sum,avg(amount) as avg from incr
group by empno
having count(*) >= 5
/

PL/SQL - Procedural languages / Structured Query Language - programming, but SQL


- non programming
in PL/SQL - Anonymous block / named blocks - procedures + functions +
triggers +
Cursors + ref cursor + (nested table & Varays - in Oracle only)
SQL*Plus - SQL*Plus - oracle working environment
iSQL*PLUS - Browser Based - for connect with Remote/local database also.
Open browser - URL - http://127.0.0.1:8080/apex

oracle provides default 3 Users -

1. sys/pass at install time - whole control of database , like CEO - Modi Ji(PM)
Database Management - create/alter/drop/startup / shutdown
User Management - user create / drop /alter/ lock / unlock / privilege
assign / remove
Table Management

shortcut for connect with sys user - only for windows Administrator user.

SQL>connect / as sysdba
SQL>show user

2. system/pass at install time - less control of sys user, like Manager - Yogi
Ji(CM)
User Management - user create / drop /alter/ lock / unlock / privilege
assign / remove
Table Management

3. scott/tiger - normal user, like Employee - aam aadmi


Table Management

-- for create own user -

connect with system or sys user -

connect system/agile

create user chhavi identified by tyagi;

-- for connect with chhavi user we need to assign privileges.

default role provided by the oracle corp - role - set of privileges

connect - user can connect with database


resource - user can use the database resources - table management
dba - user can perform the dba task also, work same as system user

for privileges assign -

SQL>grant connect,resource to chhavi;

for privileges remove -

SQL>revoke connect,resource from chhavi;

-- to change the user password, normal user can change only own password, but dba
user can change the any user password.

SQL>alter user chhavi identified by gzb123;

-- for password expire


SQL>alter user chhavi password expire;

-- for lock the existing user

SQL>alter user chhavi account lock;


SQL>alter user chhavi account unlock;

-- to create new user with account lock & password expire

SQL>create user monisha identified by gzb account lock password expire;

-- to list of all users

SQL> select * from all_users;

-- to drop existing user

SQL> drop user aniket;


SQL> drop user aniket cascade; - drop user with all links.

-- to create own table in oracle(own user)

- table design - with constraint & without constraint

constraint - check conditions at the record insert & update time

NOT NULL - user can't insert null values


UNIQUE - user can't insert duplicate values, but can insert null values.
PRIMARY KEY - UNIQUE + NOT NULL + WE CAN REFERENCIATE IN ANOTHER TABLE AS FOREIGN
KEY.
FOREIGN KEY - child key of primary key - reference key
CHECK - check condition at the record insert & update - sal must be > 20000
DEFAULT - if user does not sepecify value for a column then default value to be
inserted

** one table can contain only one primary key, but one primary key can contain
multiple columns that is called composite primary key

create table student


(
roll number(4),
name varchar(20),
mobile varchar(14),
dob date,
primary key(roll,mobile)
);

** we can insert null values in unique & reference key but can't insert null
values
in primary key.
** we can't insert record in foreign key without primary key values, means child
record me insert karne se pahle unka primary key table me record hoona chahiye
Emp - empno = 1001 , we can insert 1001 record in increment table,
but 1002 ka incr table me record insert nahi kar sakte bcos 1002 ka
record
pahle emp table me insert karna hooga
** we can't disable primary key & unique constraints.
constraint name - Oracle server define & user define -

best - user define is the best bcos we can manage easily.

-- to create table without constraint


create table emp
(
empno number(4),
ename varchar(20),
sal number(7,2),
deptno number(2)
);

sal number(7,2) - 99999.99

-- to create table with oracle server define constraint name


create table emp1
(
empno number(4) primary key,
ename varchar(20) not null,
sal number(7,2) check (sal>20000),
deptno number(2) check(deptno in(10,20,30,40))
);

-- to create table with user define constraint name


create table emp2
(
empno number(4) constraint cn_eno primary key,
ename varchar(20) constraint cn_nm not null,
sal number(7,2) constraint cn_sal check (sal>20000),
deptno number(2) default 10 constraint cn_dno check(deptno in(10,20,30,40))
);

-- to create table with foreign key


create table incr
(
empno number(4) references emp(empno),
amount varchar(5)
);

-- to create table with foreign key


create table bonus
(
empno number(4) references emp(empno) on delete cascade,
amount varchar(5)
);

with on delete casecade - if parent record deleted then child record auto deleted,
else
1st delete the child record then parent record deleted...
On delete cascade example - Employee Management System - if emp record deleted
from
emp table then all child record from related table auto
deleted.
without On delete cascade example - Student Management System.

insert into emp1 values(1002,'Ms. Divi Gupta',21000,22)


-- commit & rollback - Types

Auto commit -
* before any DDL statement execute. - Data Definition- create / drop / alter
* before user switch / logout.
* exit with exit command.
Explicit commit - with commit command.

SQL>commit;

roolback -
Auto Rollback -
* exit with exit/close button
* after any type of Abnormal condition. - Network problem / PC Hangup / any
abnormal
Explicit rollback - with rollback command
SQL>rollback;

Q- we want to disable/enable constraint

alter table emp2 disable constraint cn_sal;


alter table emp2 enable constraint cn_sal;

** for enable or add new constraint, constraint condition must be fulfill

Q- we want to change the constraint condition, sal constraint - sal > 20000 but
now we
want to set sal > 10000

SQL>alter table emp2 drop constraint cn_sal;


SQL>alter table emp2 add constraint cn_sal check(sal>10000);

alter table emp2 add mob varchar(14) default '+91-9990046906';

alter table emp2 drop column mob;

alter table emp2 set unused column mob;

4shered.com - for ebooks

select constraint_name,table_name,search_condition from user_constraints;

-- to create new table with existing table

create table aa as select empno,ename,sal from emp2; - table create with data
create table aa1 as select empno,ename,sal from emp2 where 5 = 2; - table create
without data
create table aa2(roll,name,marks) as select empno,ename,sal from emp2; - table
create with diff col name

Q- constraint copy honge ya nahi?


Ans- only not null copy hooga.

Q- we want to insert record from another table.

insert into cc select * from emp2;


Q- how to delete duplicate records?

select empno,ename,sal,rownum,rowid from emp;

SQL> delete from emp where rowid not in(select min(rowid) from emp group by empno);

You might also like