DBMS functions.pdf

23
1 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 1 X A 2 y b 3 z c

Transcript of DBMS functions.pdf

Page 1: DBMS functions.pdf

1

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

1 X A

2 y b

3 z c

Page 2: DBMS functions.pdf

2

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

1 X A

2 y b

3 z c

After delete the city of X

OID CITY PRODUCT

2 y b

3 z c

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

2 w b

3 z c

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

1 X A

2 y b

3 z c

Page 3: DBMS functions.pdf

3

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

1 X

2 y

3 z

Page 4: DBMS functions.pdf

4

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 2308 b-25 paktia

Page 5: DBMS functions.pdf

5

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 4 parwan

23324 mohammad 2309 5 parwan

Page 6: DBMS functions.pdf

6

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)

Page 7: DBMS functions.pdf

7

After the entering data

REG_NUMBER NAME SECTION_ID ROLL_NUMBER CITY

1131 ahmad 2309 5 parwan

1141 waheed 2309 5 paktia

11561 wahedfded 2309 5 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)

);

Page 8: DBMS functions.pdf

8

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_ID EMPLOYEE_NAME DEPARTEMENT 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

Page 9: DBMS functions.pdf

9

In Build function of SQL

This is a table

NAME CITY SALARY

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);

TOTAL_SALARY

230000

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

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

MINMUM_SALARY

10000

3…the function is showing the maximum salary of my employee

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

MAXIMUM_SALARY

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_SALARY

6

Page 10: DBMS functions.pdf

10

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_SALARY

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 SALARY

Zekria 75000

Amanullah 40000

Attullah 60000

Page 11: DBMS functions.pdf

11

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 1 sharma

abdol 2 rehmah

mohit 3 sigh

ras verama

1…this function is showing the name of the person in upper word (capital word)

select upper(name)

From c123;

UPPER(NAME)

ARHU

ABDOL

MOHIT

RAS

2…this function showing the name in lower alphabet

Select lower(name)

From c123;

LOWER(NAME)

arhu

abdol

mohit

ras

Page 12: DBMS functions.pdf

12

3…this 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

5…this 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 2

raf 2

zek 6

aha 1

Page 13: DBMS functions.pdf

13

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

select name, nvl(roll_no,'')

From c123

ID NAME SALARY CITY

1213 ahamd 230000 parwan

1213 ahamd parwan

1141 zekria 50000 logar

1213 ahamd parwan

112131 rafi 60000 parwan

After this function

in place of empty the value of 10000 in entered.

NAME NVL(SALARY,'10000')

ahamd 230000

ahamd 10000

zekria 50000

ahamd 10000

rafi 60000

Page 14: DBMS functions.pdf

14

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

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

Select trunc(cgpa)

From c1234;

TRUNC(CGPA)

5

9

6

7

Page 15: DBMS functions.pdf

15

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

4…This 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

Page 16: DBMS functions.pdf

16

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%).

Page 17: DBMS functions.pdf

17

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

1 X A

2 y b

3 z c

This table b12

OID EMPLOYEE_NAME

1 ahmad

2 waheed

3 rafi

Page 18: DBMS functions.pdf

18

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

PRODUCT CITY EMPLOYEE_NAME

A X ahmad

b y waheed

c z rafi

Page 19: DBMS functions.pdf

19

Considered the following table

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

E_ID ENAME DEPNO DEPTNAME JOB SALARY

1 attuallah 14 businss clerk 50000

2 zekria 14 businss clerk 30000

4 rafi 14 businss cse 20000

7 ishaq 23 engineer clerk 6000

5 shakir 45 ENG CSE 45000

8 waheed 45 PSY CSE 5000

9 razwan 45 PSY CSE 1000

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

Select ename, deptname

From employee;

ENAME DEPTNAME

attuallah businss

zekria businss

rafi businss

ishaq engineer

shakir ENG

waheed PSY

razwan PSY

2…Find the name of those employees who are earning more than 20,000

Select ename

From employee

Where salary>20000

ENAME

attuallah

zekria

shakir

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

Select ename,

From employee

where job=’CSE’ AND deptname =’PSY’

ENAME DEPTNAME JPB

attuallah businss clerk

zekria businss clerk

Page 20: DBMS functions.pdf

20

4…Write a query to display the name and salary of employees earning more than 25000.

Select name, salary

From employee

Where salary > 25000

ENAME SALARY

attuallah 50000

zekria 30000

shakir 45000

5…Write 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 ENAME DEPNO

5 shakir 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 SALARY

attuallah 50000

ishaq 6000

waheed 5000

razwan 1000

7…Write 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

Page 21: DBMS functions.pdf

21

8…Write 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;

9…Write 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 SALARY

attuallah clerk 50000

zekria clerk 30000

ishaq clerk 6000

shakir CSE 45000

waheed CSE 5000

razwan CSE 1000

E_ID ENAME JOB

2 zekria clerk

4 rafi cse

5 shakir CSE

7 ishaq clerk

8 waheed CSE

Page 22: DBMS functions.pdf

22

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;

Page 23: DBMS functions.pdf

23

It will give the additional of salary and it is bonus

Select salary+NVL(louns,100);

Distinct command

This a table

NAME ID CITY

A 1 AM

B 2 CHD

C 3 CHD

D 4 AM

E 5 BM

F 6 AM

G 7 BM

This distinct command is showing jus the name of the city

Select distinct city from aa

CITY

BM

AM

CHD

Finish