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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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
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
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
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
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)
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)
);
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
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
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
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
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
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
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
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
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%).
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
18
After the process two table is joining and the table is this one