You are on page 1of 23

DBMS functions

How to create table


create table c12
(id varchar2(40)not null,
city varchar2(30)not null,
product varchar2(30)not null
);
How to see the table element or picture of the table
Desc table name
Name

Null?

Type

OID

VARCHAR2(40)

CITY

VARCHAR2(30)

PRODUCT

VARCHAR2(30)

Insert the values into table


Insert into table name
values(1,x,A)
This function is showing the data which is enter into table
Select*from table name
OID

CITY

PRODUCT

This function is deleting the name or anything you want in table


Delete from table name
Where name=a;
This is a table
OID

CITY

PRODUCT

After delete the city of X


OID

CITY

PRODUCT

Update function:- this function is updating the table


Update table name
set name=pp
Where name=b;
We are updating the Y city into w city
OID

CITY

PRODUCT

Alter table name: - this function is adding some column into the current table name
Alter table a12
add mobile_no varchar2(10)
The mobile_no column is added in my table see in this table.
OID

CITY

PRODUCT

MOBILE_NO

Drop:- this function is deleting the column from the table


Alter table a12
drop column product
The product column is deleted from the table see in bottom table.
OID

CITY

MOBILE_NO

Primary key
This is a key which protect the table and it will not allowed any duplicate value to enter into the
table
Primary key
create table m1
(Reg_number varchar2(20)not null primary key,
name varchar2(10)not null,
section_id varchar2(10)not null,
roll_number varchar2(14)not null,
city varchar2(45)not null
);
This is a table which data is not enter into table

Name

Null?

Type

REG_NUMBER

NOT NULL

VARCHAR2(20)

NAME

NOT NULL

VARCHAR2(10)

SECTION_ID

NOT NULL

VARCHAR2(10)

ROLL_NUMBER

NOT NULL

VARCHAR2(14)

CITY

NOT NULL

VARCHAR2(45)

After entering the data

REG_NUMBER

NAME

SECTION_ID

ROLL_NUMBER

CITY

11312200

mohammad 2307

b-25

pulamalm

113230

waheed

b-25

paktia

2308

Primary key and check constants


Check constants is a key which not allowed the value according to the condition of the table.
Like here that allow those people roll number which between 2 and 23 less than that and more than that
will not enter because of check constants.
create table m1
(Reg_number varchar2(20)not null primary key,
name varchar2(10)not null,
section_id varchar2(10)not null,
roll_number varchar2(14)not null,
city varchar2(45)not null,
check(roll_number between 2 and 23)
);
REG_NUMBER

NAME

SECTION_ID

ROLL_NUMBER

CITY

234

ahmad

2309

parwan

23324

mohammad

2309

parwan

Foreign key
Is the key which showing the relation of the two table from example there is tow table M23 and M24 the
relation here reg>number
create table m23
(Reg_number varchar2(20)not null primary key,
name varchar2(10)not null,
section_id varchar2(10)not null,
roll_number varchar2(14)not null,
city varchar2(45)not null,
check(roll_number between 2 and 23)
);
create table m24
(teacher_id varchar2(90)not null,
faculty_name varchar2(49)not null,
subject_code varchar2(10)not null,
Reg_number varchar2(20)not null,
foreign key(Reg_number)references m23(Reg_number)
);
Name

Null?

Type

REG_NUMBER

NOT NULL

VARCHAR2(20)

NAME

NOT NULL

VARCHAR2(10)

SECTION_ID

NOT NULL

VARCHAR2(10)

ROLL_NUMBER

NOT NULL

VARCHAR2(14)

CITY

NOT NULL

VARCHAR2(45)

Name

Null?

Type

TEACHER_ID

NOT NULL

VARCHAR2(90)

FACULTY_NAME

NOT NULL

VARCHAR2(49)

SUBJECT_CODE

NOT NULL

VARCHAR2(10)

REG_NUMBER

NOT NULL

VARCHAR2(20)

After the entering data

REG_NUMBER
1131

NAME
ahmad

SECTION_ID
ROLL_NUMBER
2309
5

CITY
parwan

1141

waheed

2309

paktia

11561

wahedfded

2309

paktia

TEACHER_ID

FACULTY_NAME

SUBJECT_CO

REG_NUMBER

2309

ankit

qtt201

1131

2309

preetpal

qtt201

1141

2309

preafetpal

qtt201

11561

CREATE TABLE Persons


(
P_Id varchar2(50) NOT NULL PRIMARY KEY ,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

CREATE TABLE Orders


(
O_Id varchar2(50) NOT NULL PRIMARY KEY ,
OrderNo varchar2(50) NOT NULL,
P_Id varchar2(50),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
);

Default constants
create table aaa
(employee_id varchar2(10),
employee_name varchar2(30),
departement varchar2(10)default'business',
city varchar2(10)default'CHD'
);
When inserting the values

Insert into aaa(EMPLOYEE_ID,EMPLOYEE_NAME)


values ('attuallah','11312431');

EMPLOYEE_I D

EMPLOYEE_NAME

DEP ARTEMENT

CITY

attuallah

11312431

business

CHD

rafi

11312142

business

CHD

zekria

11312145

business

CHD

shakir

11312200

business

CHD

Ishaq

11413432

business

CHD

sayed-aman

11311941

business

CHD

In Build function of SQL


This is a table
NAME

CITY

SAL ARY

ahmad

kabul

25000

zekria

logar

75000

Rafi

parwan

20000

Amanullah

badgheis

40000

shakir

logar

10000

attullah

wardak

60000

1... we will find the sum of employee salary

Select sum(salary) as total_salary from table name(like afghan12);


TOT AL_S AL ARY
230000

2...we will find the minimum salary of our employee

Select min(salary) as minmum_salary from table name( like afghan12);


MINMUM_SAL ARY
10000

3the function is showing the maximum salary of my employee

Select max(salary) as maximum_salary from table name( like afghan12);


MAXIMUM_S AL ARY
75000

4.this function shows the total number of people from that table

Select count(salary) as count_salary from table name( like afghan12);


COUNT_S AL ARY
6

5.this function is showing the average salary of the employee from that table

Select avg(salary) as average_salary from table name( like afghan12);


AVERAGE_S AL ARY
38333.3333333333333333333333333333333333

6.this function is show the employee name and salary which (more than, less than and equal by
putting this points =, < and >) the average salary

Select name, salary from table name( like afghan12)


where salary>(select avg(salary) as average from table name {like afghan12} );
NAME

SAL ARY

Zekria

75000

Amanullah

40000

Attullah

60000

10

Character function
create table c123
(name varchar2(30),
roll_no varchar2(20),
last_name varchar2(40)
)
This is the table which created

NAME

ROLL_NO

LAST_NAME

arhu

sharma

abdol

rehmah

mohit

sigh

ras

verama

1this function is showing the name of the person in upper word (capital word)
select upper(name)
From c123;

UPPER(NAME)
ARHU
ABDOL
MOHIT
RAS
2this function showing the name in lower alphabet
Select lower(name)
From c123;

LOWER(NAME)
arhu
abdol
mohit
ras
11

3this function is showing the first word in capital letter


Select initcap(name)
From c123;

INITCAP(NAME)
Arhu
Abdol
Mohit
Ras

4..this function is showing the name and last name of the person to together

select concat(name,last_name)
From c123;

CONCAT(NAME,LAST_NAME)
arhusharma
abdolrehmah
mohitsigh
rasverama
5this function shows that the three alphabet of the name and the position of the a in the table.

select substr(name,1,3),instr(name,'a')
From asad:

SUB

INSTR(NAME,'A')

wah

raf

zek

aha

12

6When the any column is not having the salary or ect.. it will add some numbers

select name, nvl(roll_no,'')


From c123
ID
1213

NAME
ahamd

1213

ahamd

1141

zekria

1213

ahamd

112131

rafi

SALARY
230000

CITY
parwan
parwan

50000

logar
parwan

60000

parwan

After this function


in place of empty the value of 10000 in entered.

NAME
ahamd

NVL(SALARY,'10000')
230000

ahamd

10000

zekria

50000

ahamd

10000

rafi

60000

13

Next chapter numeric function


first we will create table
create c1234
(name varchar2(30)
Cgpa float
);
This is the table c1234

NAME

CGPA

rahul

5.2

raja

9.1

sohn

6.4

smith

7.1

1..This function is using for the round off method related to math
Select round (cgpa)
From c1234;
ROUND(CGPA)
5
9
6
7

2This function is showing just cgpa and all other is cut


Select trunc(cgpa)
From c1234;
TRUNC(CGPA)
5
9
6
7

14

3...This function showing just the positive and negative view of the CGPA if it is positive>0 it will +1
and if CGPA is less than<0 it will show -1
Select sign (cgpa)
From c1234;
SIGN(CGPA)
1
1
1
1

4This function is rised the power of cgpa with the number which we wants for example
Select power (cgpa,5)
From c1234;
POWER(CGPA,5)
3802.04032
62403.2145
10737.4182
18042.2935

15

Like
This function is showing the name which start with r
Select*from c1234
Where name like('s%');

NAME

CGPA

sohn

6.4

smith

7.1

The second function is showing the name which end with n


Select*from c1234
Where name like('%n');

NAME

CGPA

sohn

6.4

This function is showing the name and details where and is coming in between of the city
select*from q123
where city like('%and%')

STUDENT_NAME

CITY

ahmad

jalandhar

shakir

jalandhar

Select name
From table name
Where name like(%a%e%);
Where name like(__c%).

16

Joins
Who we are joining two table
This is the function
select a12.product, b12.employee_name
from a12
inner join b12
on a12.oid=b12.oid;
now create two table
create table a12
(oid varchar2(40),
city varchar2(30),
product varchar2(30)
);
create table b12
(oid varchar2(10),
employee_name varchar2(30)
);
This is table a12

OID

CITY

PRODUCT

This table b12

OID

EMPLOYEE_NAME

ahmad

waheed

rafi

17

After the process two table is joining and the table is this one

PRODUCT

CITY

EMPLOYEE_NAME

ahmad

waheed

rafi

18

Considered the following table


Employee(emp_id, ename, deptno, deptname, job, salary)
E_ID

ENAME

DEPNO

DEPTN AME

JOB

attuallah

14

businss

clerk

50000

SAL ARY

zekria

14

businss

clerk

30000

rafi

14

businss

cse

20000

ishaq

23

engineer

clerk

6000

shakir

45

ENG

CSE

45000

waheed

45

PSY

CSE

5000

razwan

45

PSY

CSE

1000

1.Find the employee names and their respective department names.

Select ename, deptname


From employee;
ENAME

DEPTN AME

attuallah

businss

zekria

businss

rafi

businss

ishaq

engineer

shakir

ENG

waheed

PSY

razwan

PSY

2Find the name of those employees who are earning more than 20,000
Select ename
From employee
Where salary>20000
ENAME
attuallah
zekria
shakir

3Find employee names who are working as clerk in businss department.

Select ename,
From employee
where job=CSE AND deptname =PSY
ENAME

DEPTN AME

JPB

attuallah

businss

clerk

zekria

businss

clerk

19

4Write a query to display the name and salary of employees earning more than 25000.
Select name, salary
From employee
Where salary > 25000
ENAME

SAL ARY

attuallah

50000

zekria

30000

shakir

45000

5Write a query to display the employee name and department number for employee number 5.
Select E_id,name, dept_id
From employee
Where e_id =5;
E_ID
5

ENAME
shakir

DEPNO
45

6...Write a query to display the name and salary for all the employees whose salary is not in the range of Rs
20000 and 46000.
Select ename, salary
From employee
Where salary NOT BETWEEN 20000 AND 46000
ENAME

SAL ARY

attuallah

50000

ishaq

6000

waheed

5000

razwan

1000

7Write a query to display the name and department number of all the employees in department number 8
and 12 in an alphabetical order by name.
Select ename,depno
From employee
Where depno='14' or depno='45'
Order by ename;
ENAME

DEPNO

attuallah

14

rafi

14

razwan

45

shakir

45

waheed

45

zekria

14

20

8Write a query to display the employee name, job and joining date of employees joined between June 11,
2010 and July 15, 2010. Also order the result in ascending order of joining date.

select name, job, join_date


From employee
Where join_date BETWEEN 11-Jun-2010 AND 15-Jun-2010
Order by Join_Date

In place of the date of joined I have made according to employee id


Select e_id,ename, job
From employee
Where E_id BETWEEN '2' AND '8'
Order by E_id;
E_ID

ENAME

JOB

zekria

clerk

rafi

cse

shakir

CSE

ishaq

clerk

waheed

CSE

9Write a query to display the name, job and salary of all the employees whose job is clerk or CSE and
whose salary is not equal to 50000.
select ename, job, salary
From employee
Where job='clerk' or job='CSE' AND Salary! =50000;
ENAME

JOB

SAL ARY

attuallah

clerk

50000

zekria

clerk

30000

ishaq

clerk

6000

shakir

CSE

45000

waheed

CSE

5000

razwan

CSE

1000

21

TRIGGER
Is a statement which automatically execute

CREATE TRIGGER noori


AFTER INSERT ON ClientsToKeywords
FOR EACH ROW BEGIN
UPDATE Clients SET NumKeywords = NumKeywords+1 WHERE ClientID = NEW.ClientID;
UPDATE Keywords SET NumClients = NumClients+1 WHERE keywordID =
NEW.KeywordID;

End

CREATE TRIGGER Decrement_After_Deletion AFTER DELETE ON ClientsToKeywords


FOR EACH ROW BEGIN
UPDATE Clients SET NumKeywords = NumKeywords-1 WHERE ClientID = OLD.ClientID;
UPDATE Keywords SET NumClients = NumClients-1 WHERE KeywordID = OLD.KeywordID;
END;

CREATE TRIGGER Modify_After_Update AFTER UPDATE ON ClientsToKeywords


FOR EACH ROW BEGIN
UPDATE Clients SET NumKeywords = NumKeywords+1 WHERE ClientID = NEW.ClientID;
UPDATE Keywords SET NumClients = NumClients+1 WHERE keywordID = NEW.KeywordID;
UPDATE Clients SET NumKeywords = NumKeywords-1 WHERE ClientID = OLD.ClientID;
UPDATE Keywords SET NumClients = NumClients-1 WHERE KeywordID = OLD.KeywordID;
END;

22

It will give the additional of salary and it is bonus


Select salary+NVL(louns,100);

Distinct command
This a table
NAME

ID

CITY

AM

CHD

CHD

AM

BM

AM

BM

This distinct command is showing jus the name of the city

Select distinct city from aa


CITY
BM
AM
CHD

Finish

23