Anda di halaman 1dari 44

Database Management System

Oracle 8.x
Data Types
Char( size) : max( 255 character)
:Padded with spaces
Varchar(size): max(2000 char)
:not padded
Number(p,s): p=38 digits
s=125 zeros
Date: DD-MON-YY
Long: variable length char string up to 2GB
(Substring cannot be applied)
RAW:picture or images upto 255 bytes
LONGRAW: 2GB
Creating a Table

Create table <table name>(


Column name1 datatye(size),
Column name1 datatye(size));
Example of creating table

SQL> create table emp(


Emp_no varchar(10),
Emp_name varchar(10));

Table created
Retrieving data from table
(DML statements)
SQL> select *From < table Name>;

SQL> select < attribute list>


From < table list>;

SQL> select < attribute list>


From < table list>
Where <condition>;
Example
SQL> select *from emp;

SQL> select emp_no, ename


From emp;

SQL> select emp_no, ename


From emp
Where deptno=5;
Order By function
Display the record in either ascending
or descending order.

DEFAULT: Ascending
Continue ……..
Select < attribute list>
From <table name>
Where < condition> (optional)
Order By attribute_name;
Default in ascending order.
Select < attribute list>
From <table name>
Where < condition> (optional)
Order By attribute_name desc;
Example
SQL> select empno,ename
From emp
Where deptno=5
Order by empno;
Default Ascending

SQL> select empno,ename


From emp
Where deptno=5
Order by empno desc;
Delete Operation
SQL> delete from table name;

All record will be deleted.

SQL> delete from table name


Where condition;
Some records will be deleted.
Example
SQL> delete from emp;
All record will be deleted.

SQL> delete from emp


where deptno=5;
All the employee records who belongs to
deptno 5 will be deleted.
Updating the contents of table
SQL> update <table name>
set < column name=exp>, < column name=exp>;
All the record will be updated

SQL> update <table name>


set < column name=exp>, < column name=exp>
where <condition>;

Some of the records will be updated.


Alter table Statements
Used to change the definition of the table
(DDL) Data Defi. Language
SQL> alter table <table name>
add( column name datatype(size));
Add new column
SQL> alter table <table name>
Modify( column name Newdatatype(size));
Increase the size of an column
Example
SQL> alter table emp
Add( bal_due number(10,2));
Add new column bal_due in EMP table.

SQL> alter table emp


Modify( bal_due number(14,2));
Increase the size of bal_due.
Can not do the followings by
Alter Table Statements

1. Change the table name


2. Rename the column
3. Drop column ( Only in Oracle 8.0)
4. Decrease the size of column
( if data is present)
Drop Command
Remove the table and its information
from the database
SQL> drop table <table name>;

SQL> drop table emp;

Emp table will be removed from the database.


Describe Command
To see the description of the table
SQL> describe <table name>;

SQL> desc <table name>;

SQL> desc emp;


Save and Run the file
SQL> Save <file name>;
Recently executed SQL statements will be saved
with .sql file extension.

SQL> run < file name>


Output with SQL statement.

SQL> @< file name>


Only output will generate.
Logical Operator

AND
OR
NOT
AND Operator
SQL> select <attribute list>
from <table name>
where condition1 and condition2;

SQL> select empno,ename


from emp
where deptno=5 and salary>5000;
OR Operator
SQL> select <attribute list>
from <table name>
where condition1 or condition2;

SQL> select empno,ename


from emp
where deptno=5 or deptno=2 ;
NOT Operator
SQL> select <attribute list>
from <table name>
where NOT( column name=‘value’);

SQL> select empno,ename


from emp
where NOT( deptno=5 or deptno=2 );
Range ( Between)
SQL> select < attribute list>
from <table name>
where column name between ( range);
SQL> select empno,ename
from emp
where salary between 1000 and 5000;
SQL> select empno,ename
from emp
where salary not between 1000 and 5000;
Pattern Matching
(Like)
SQL> select <attribute list>
from <table name>
where column name like ‘s%’;
where column name like ‘sa%’;
where column name like ‘_s%’;
where column name like ‘_s_’;
Example
SQL> select empno,ename
from emp
where ename like ‘a%’;
where ename like ‘as%’;
where ename like ‘_a%’;
where ename like ‘_a_’;
IN AND NOT IN
SQL> select <attribute list>
from <table name>
where column name IN(‘value’,’’);
where column name NOT IN(‘value’,’’);
SQL> select empno,ename
from emp
where ename IN (‘arun’,’tarun’);
where ename NOT IN (‘arun’,’tarun’);
Aggregate function
1. SUM
2. AVG
3. MIN
4. MAX
5. COUNT
Syntax
SQL> select aggregate_fun( column name)
from <table name>;
SQL> select sum(sal) from emp;
SQL> select avg(sal) from emp;
SQL> select min(sal) from emp;
SQL> select max(sal) from emp;
SQL> select count(empno) from emp;
Count the no. of values.
Group By and Having

Group By ( for making groups)

Having ( condition)
Group By and Having
SQL> select aggregate_function( column name)
from <table name>
group by <column name>
having <condition>;
SQL> select sum(salary),deptno
from emp
group by deptno
having salary>1000;
Constraints
1. Column Level
( if apply on single column)

2. Table Level
( if apply on more than one column)
Table Level

SQL> create table emp(


empno varchar(10),
deptno number(2),
primary key( empno,deptno));
Column Level
SQL> create table emp(
empno varchar(10) primary key,
ename varchar(10)
salary number(10,2)
deptno number(2));
Additional Constraints
1. NOT NULL
attribute value will not be NULL

2. UNIQUE
attribute value will be unique but at
least one time it will accept null value.
NOT NULL
SQL> create table emp(
empno varchar(10) NOT NULL,
ename varchar(10)
salary number(10,2)
deptno number(2));
UNIQUE
SQL> create table emp(
empno varchar(10) UNIQUE,
ename varchar(10)
salary number(10,2)
deptno number(2));
Foreign Key
SQL> create table emp(
empno varchar(10) primary key,
ename varchar(10)
salary number(10,2)
deptno number(2)
foreign key (deptno) references dept(deptno));
Check Constraints
SQL> create table emp(
empno varchar(10) primary key,
ename varchar(10)check (ename like ‘a%’),
salary number(10,2),
deptno number(2));
Only ename starting with letter ‘a’ are allowed.
Default Constraints
SQL> create table emp(
empno varchar(10) primary key,
ename varchar(10),
salary number(10,2),
sex varchar(1) default ‘M’,
deptno number(2));
Adding constraints by
Alter Table Command

SQL> alter table emp


add primary key( empno);

SQL> alter table emp


Add foreign key (deptno) references dept(deptno);
Drop the Primary Key

SQL> Alter table emp


drop primary key;
Join Operation

1. Equi-join

2. Cartesian Product/ Cartesian Join


Equi-Join

SQL> select *from emp,dept


where emp.deptno=dept.deptno;
Cartesian Product/ Cartesian Join

SQL> select *from emp,dept;


Emp(m) dept(n)
m,n are number of column in emp and dept table.
p,q are number of records in emp and dept table.

In result m+n number of columns and p*q number


of records .

Anda mungkin juga menyukai