Dbms Record Prog
Dbms Record Prog
4 Constraints
5 Operators
6 Clauses
7 Functions
9 Joins
10 View
11 Implicit cursors
12 Explicit cursors
13 Triggers
14 Function(PL/SQL)
15 Report
DDL COMMANDS
CREATE
The CREATE command is used to create the table with the columns and its corresponding
data types.
Syntax:
CREATE TABLE < table name> (col1 datatype(size), col2 datatype(size), col3
datatype(size), ….., coln datatype(size));
SQL> create table emp5 (empno number(3), ename varchar2(10), job varchar2(10), sal
number(6), comm number(4));
Table created.
ALTER
The ALTER command is used to modify the existing column data type or add a new column
to existing table and also add the constraints to the existing table.
Table altered.
ALTER TABLE < table name> MODIFY(col1 datatype(size), col2 datatype(size), col3
datatype(size), ….., coln datatype(size));
Table altered.
DESC
SYNTAX:
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
SAL NUMBER(6)
COMM NUMBER(4)
FNAME VARCHAR2(10)
DROP
SYNTAX
Table dropped.
DML COMMANDS
INSERT
Insert command is used to insert the rows of data into the table.
Syntax:
1 row created.
DELETE
This command is used to delete the rows of data from the table.
Syntax:
1 row deleted.
UPDATE
Syntax:
1 row updated.
SELECT
This command is used to select the rows and columns from the table.
Syntax1:
Syntax2:
12 rows selected.
SQL> select empno,ename,job from emp3;
1 aaa manager
2 bbb supervisor
3 ccc clerk
4 ddd clerk
5 eee salesman
6 ddd salesman
7 fff manager
8 ggg clerk
9 hhh clerk
10 iii salesman
11 jjj manager
12 rows selected.
Constraints
Constraints are the rules that are used to control the invalid entry in a column. There are 6
constraints:
1. Primary key
2. Null
3. Not null
4. Unique
5. Check
6. Foreign key
Primary key
It defines the column as a mandatory or we can say that the column cannot be left blank or
not null.
ERROR at line 1:
SQL> /
1 row created.
SQL> /
ERROR at line 1:
NULL
If in a record any field that is created as null means it is not having any value.
Table created.
1 row created.
Not null
This constraint ensures that the user always types the value for that column.
Table created.
ERROR at line 1:
Unique
The unique key ensures that the information in columns must not be repeated.
Table created.
1 row created.
SQL> /
ERROR at line 1:
Check
This ensures that when data is entered, the data in the column is limited to specific values.
Table created.
1 row created.
SQL> /
ERROR at line 1:
This key represents relationship between tables. A foreign key is a column whose
values are derived from the primary key of the other table.
Table created.
SQL> create table employee2(eno number(4) primary key, ename varchar2(15), dno
number(4) references department1);
Table created.
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
Arithmetic Operators
SAL+COMM
----------
16500
13000
9000
9300
12000
10500
12500
8250
8800
10700
17600
11 rows selected.
SAL-500
----------
14500
11500
7500
7900
9500
9000
10500
7000
7500
9300
15500
SAL*.2
----------
3000
2400
1600
1680
2000
1900
2200
1500
1600
1960
3200
11 rows selected.
SQL> select sal/4 from emp3;
SAL/4
----------
3750
3000
2000
2100
2500
2375
2750
1875
2000
2450
4000
11 rows selected.
Relational Operators
4 rows selected.
2 rows selected.
4 rows selected.
2 rows selected.
7 rows selected.
Logical operators
2 bbb 12000
7 fff 11000
7 rows selected.
1 aaa manager
3 ccc clerk
4 ddd clerk
7 fff manager
8 ggg clerk
9 hhh clerk
11 jjj manager
7 rows selected.
1 aaa manager
2 bbb supervisor
7 fff manager
11 jjj manager
3 ccc clerk
4 ddd clerk
5 eee salesman
6 ddd salesman
8 ggg clerk
9 hhh clerk
10 iii salesman
7 rows selected.
SQL> select empno,ename,sal from emp3 where sal between 10000 and 15000;
1 aaa 15000
2 bbb 12000
5 eee 10000
7 fff 11000
6 rows selected.
SQL> select * from emp3 where sal between 9000 and 13000;
2 bbb supervisor
5 eee salesman
6 ddd salesman
10 iii salesman
6 rows selected.
SQL CLAUSES
The GROUP BY clause is used in conjunction with the aggregate functions to group the
result-set by one or more columns.
Avg(),max(),min(),sum()
JOB AVG(SAL)
---------- ----------
salesman 9766.66667
clerk 7975
manager 14000
supervisor 12000
JOB MAX(SAL)
---------- ----------
salesman 10000
clerk 8400
manager 16000
supervisor 12000
Min() : it returns the minimum value of the given data.
JOB MIN(SAL)
---------- ----------
salesman 9500
clerk 7500
manager 11000
supervisor 12000
JOB SUM(SAL)
---------- ----------
salesman 29300
clerk 31900
manager 42000
supervisor 12000
Having Clause
The HAVING CLAUSE is used to specify certain conditions, rows retrieved by using
GROUP BY CLAUSE. This clause should be preceded by GROUP BY CLAUSE.
---------- ----------
3 salesman
4 clerk
3 manager
Distinct Clause
This clause is used to retrieve the unique values from a specified table.
JOB
----------
salesman
clerk
manager
supervisor
WHERE CLAUSE
This clause is used to retrieve the values where a particular condition is given.
Order By Clause
This clause is used to retrieve the rows either in ascending or descending order from a given
table.
ENAME
----------
jjj
iii
hhh
ggg
fff
eee
ddd
ddd
ccc
bbb
aaa
11 rows selected.
8 ggg 7500
9 hhh 8000
3 ccc 8000
4 ddd 8400
6 ddd 9500
10 iii 9800
5 eee 10000
7 fff 11000
2 bbb 12000
1 aaa 15000
11 jjj 16000
11 rows selected.
SQL FUNCTIONS
Group Functions
AVG(SAL)
---------
9920
MIN(SAL)
----------
7500
MAX(SAL)
----------
15000
------------
10
5. Count(*) : it displays the number of rows in table including duplicates with null
values.
COUNT(*)
----------
10
Sum(): it returns the sum of values of the given expression.
SUM(SAL)
----------
99200
SUM(COMM)
----------
11350
Character Functions:
----------
aaa
bbb
ccc
ddd
eee
ddd
fff
ggg
hhh
iii
10 rows selected.
UPPER(ENAM
----------
AAA
BBB
CCC
DDD
EEE
DDD
FFF
GGG
HHH
III
10 rows selected.
INITCAP(EN
----------
Aaa
Bbb
Ccc
Ddd
Eee
Ddd
Fff
Ggg
Hhh
Iii
10 rows selected.
LENGTH(JOB)
-----------
10
10 rows selected.
SUBS
----
Perv
NUMERIC FUNCTIONS
ABS(-3)
----------
Power(): power(m,n)
POWER(2,3)
----------
Round() : The ROUND() function is used to round a numeric field to the number of
decimals specified.
ROUND(14.4587,3)
----------------
14.459
SQRT(9)
----------
Ceil(): this function returns the smallest integer value that is greater than or equal to a
number.
-----------
19
CEIL(13.42)
-----------
14
Floor(): this function returns the greatest integer value that is lesser than or equal to a
number.
FLOOR(13.65)
------------
13
FLOOR(12.11)
------------
12
Mod() : The SQL MOD() function returns the remainder from a division.
MOD(5,3)
----------
2
Date Functions
SYSDATE
---------
20-SEP-16
ADD_MONTH
---------
12-JUL-16
Last_day() : it returns the last day of the month for the given date.
LAST_DAY(
---------
29-FEB-16
Months_between() : this calculates the number of months between two dates and returns the
difference as a number.
MONTHS_BETWEEN('19-JAN-16','20-SEP-14')
---------------------------------------
15.9677419
RELATIONSHIP BETWEEN TABLES
Table created.
1 row created.
SQL> /
1 row created.
1 aaa 10000 10
2 bbb 15000 10
3 ccc 14000 11
4 ddd 20000 11
Table created.
1 row created.
SQL> /
1 row created.
DNO DNAME
---------- ---------------
10 payments
11 purchase
1 aaa payments
2 bbb payments
3 ccc purchase
1 aaa payments
2 bbb payments
2 bbb payments
JOINS
Table created.
1 row created.
SQL> /
1 row created.
SQL> /
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
1 aaa 101
2 bbb 102
3 ccc 103
4 ddd 101
5 eee 102
6 fff 103
7 101
7 rows selected.SQL> create table dept(deptno number(3) primary key, dname varchar2(10));
Table created.
1 row created.
SQL> /
SQL> /
1 row created.
1 row created.
DEPTNO DNAME
---------- ----------
101 maths
102 stat
103 comp
104
7 maths
4 ddd maths
1 aaa maths
5 eee stat
2 bbb stat
6 fff comp
3 ccc comp
7 rows selected.
1 aaa maths
2 bbb stat
3 ccc comp
4 ddd maths
5 eee stat
6 fff comp
7 maths
8 rows selected.
3 ccc comp
6 fff comp
1 aaa maths
4 ddd maths
7 maths
2 bbb stat
5 eee stat
8 rows selected.
3 ccc comp
6 fff comp
1 aaa maths
4 ddd maths
7 maths
2 bbb stat
5 eee stat
8 rows selected.
6 fff comp
3 ccc comp
7 maths
1 aaa maths
4 ddd maths
2 bbb stat
5 eee stat
7 rows selected.
2 bbb stat
VIEW
SQL> create view e1 as select * from emp;
View created.
1 aaa 101
2 bbb 102
3 ccc 103
4 ddd 101
5 eee 102
6 fff 103
7 101
7 rows selected.
View created.
DEPTNO DNAME
---------- ----------
101 maths
102 stat
103 comp
104
IMPLICIT CURSORS
SQL> Declare
2 veno emp17.eno%type;
3 begin
4 veno:=&eno;
6 if sql%found then
7 DBMS_OUTPUT.PUT_LINE('record deleted');
8 Else
10 end if;
11 commit;
12 end;
13 /
old 4: veno:=&eno;
new 4: veno:=10;
Deleting
no such employee
SQL> /
old 4: veno:=&eno;
new 4: veno:=202;
Deleting
record deleted
1 aaa 12000 20
2 bbb 16000 20
3 ccc 16000 21
4 ddd 20000 21
6 rows selected.
1 aaa 12000 20
2 bbb 16000 20
3 ccc 16000 21
4 ddd 20000 21
SQL> Declare
3 vno emp17.eno%type;
4 vname emp17.ename%type;
5 vsal emp17.sal%type;
6 Begin
7 Open c1;
8 Loop
11 Dbms_output.put_line(vno);
12 Dbms_output.put_line(vname);
13 Dbms_output.put_line(vsal);
14 End loop;
15 Close c1;
16 End;
17 /
1
aaa
12000
bbb
16000
ccc
16000
ddd
20000
111
abb
23000
202
www
11000
2 BEFORE
3 INSERT OR
5 DELETE
6 ON emp17
7 BEGIN
8 CASE
10 DBMS_OUTPUT.PUT_LINE('Inserting');
12 DBMS_OUTPUT.PUT_LINE('Updating salary');
16 DBMS_OUTPUT.PUT_LINE('Deleting');
17 END CASE;
18 END;
19 /
Trigger created.
SQL> select * from emp17;
1 aaa 11000 10
2 bbb 16000 10
3 ccc 15000 11
4 ddd 20000 11
Updating salary
2 rows updated.
Deleting
2 rows deleted.
Inserting
1 row created.
1 aaa 12000 10
2 bbb 16000 10
3 ccc 16000 11
Rollback complete.
Updating department ID
6 rows updated.
1 aaa 12000 20
2 bbb 16000 20
3 ccc 16000 21
4 ddd 20000 21
6 rows selected.
FUNCTION
CREATE OR REPLACE FUNCTION totalemp
RETURN number IS
total number(2) := 0;
BEGIN
FROM emp;
RETURN total;
END;
Function created.
TOTALEMP
----------