Module 4
Module 4
Point Base supports the following data types for its column and parameter
declarations.
• CHARACTER [(length)] or CHAR [(length)]
• VARCHAR (length)
• BOOLEAN
• SMALLINT
• INTEGER or INT
• DECIMAL [(p[,s])] or DEC [(p[,s])]
• NUMERIC [(p[,s])]
• REAL
• FLOAT(p)
• DOUBLE PRECISION
• DATE
• TIME
• TIMESTAMP
• CLOB [(length)] or CHARACTER LARGE OBJECT [(length)] or CHAR LARGE
OBJECT [(length)]
• BLOB [(length)] or BINARY LARGE OBJECT [(length)]
SQL Components
Syntax:
DROP TABLE <table_name>;
OR
DROP DATABASE <database_name>;
Syntax:
ALTER TABLE <table_name>
ADD <column_name datatype>;
OR
OR
OR
OR
Example:
RENAME TABLE emp TO employee;
TRUNCATE COMMAND
Example:
TRUNCATE TABLE employee;
What is constraints?
For example:
Empid INT PRIMARY KEY,
FOREIGN KEY (Empid) references Department
(Empid);
•Null is a reserved keyword in SQL that indicates a data value does not exist
in the database.
•It identifies the Null special marker.
•Null introduced by E. F. Codd, Creator of the relational database model.
•A Null value indicates that the value is unknown, not applicable and has no
value, but it does not mean that it has a zero value or a field which contains
spaces.
•It is used to represent a missing value.
For example:
<employee> Table
Output:
Output:
All aggregate function (min, max, sum, count) ignores the NULL values except
count() function.
Example:
INSERT INTO employee (`eid`, `ename`, `city`)
VALUES (`1`, `ABC`, `PUNE`);
• DELETE command is used to delete some or all records from the existing
table.
• It deletes all the records from a table.
•
Syntax:
DELETE FROM <table_name> WHERE <condition>;
Syntax:
GRANT <privilege list>
ON <relation name or view name>
TO <user/role list>;
Example:
SAVEPOINT no_update;
Example:
ROLLBACK TO SAVEPOINT no_update;
SET TRANSACTION
ROLLBACK COMMIT
ROLLBACK command is used The COMMIT command is
to undo the changes made by used to save the modifications
the DML commands. done to the database values by
the DML commands.
Syntax: Syntax:
DELETE FROM table_name COMMIT;
ROLLBACK
SELECT clause has many optional clauses are as follow;
Clause Description
FROM It is used for selecting a table name in a database.
Example
SELECT * FROM employee
WHERE salary >=10000;
OR
<Employee> Table
Output:
Ename
ABC
PQR
3. SELECT using DISTINCT
DISTINCT clause is used to eliminate the duplicate values from the
table.
Example:
SELECT DISTINCT city FROM Employee;
Output:
City
Bangalore
Mumbai
Pune
4. SELECT using IN
'IN' determines whether a specified value matches any value in a
sub-query or a list.
Example:
SELECT Eid, Ename FROM Employee
WHERE Salary IN (5000, 20000);
Output:
Eid Ename
E001 ABC
E003 LMN
5. SELECT using BETWEEN
'BETWEEN' is used to get those values who fall within a range.
Example:
SELECT Eid, Ename, Salary FROM Employee
WHERE Salary BETWEEN 5000 AND 30000;
Output:
Example:
SELECT Eid, Ename, Age FROM Employee
WHERE Age NOT BETWEEN 24 AND 25;
Output:
E001 ABC 29
E002 PQR 30
E005 STU 32
6. SELECT using LIKE
•LIKE clause is used for comparing a value with similar
values using wildcard operators (% and _ ).
•Suppose, if you want user name starts with 'S', then
use 'LIKE' clause as follows,
Example:
SELECT Eid,Ename, City, Salary FROM Employee
WHERE Ename LIKE 'S%';
Output:
Note: In the below LIKE statements, instead of 'S' and 'P' you can
use any value according to your needs while writing the query. 'S'
and 'P' are examples, mentioned for understanding.
Statement Description
s
LIKE 'S%' It finds any value which starts with 'S'.
LIKE '%S%' It finds any value which have 'S' in any position.
LIKE '_SS It finds any value which have 'SS' in the second and
%' third positions.
LIKE 'S_%_ It finds any value which starts with 'S' and have at least
%' three characters in length.
LIKE '%S' It finds any value which ends with 'S'.
LIKE '_S It finds any value which have 'S' in the second position
%P' and ends with 'P'.
LIKE It finds any value in a five digit numbers which start with
'S___P' 'S' and ends with 'P‘.
6. SELECT using GROUP BY
Example:
SELECT Eid, Ename FROM Employee
ORDER BY Ename asc;
Output:
Eid Ename
E001 ABC
E002 LMN
E003 PQR
E005 STU
E004 XYZ
MySQL HAVING Clause
Rames
100 Electrical 24 25000 Bangalore
h
Aeronautic
102 Harsha 28 35000 Mysore
s
Soumy
103 Electronics 22 20000 Bangalore
a
name salary
---------- ----------
Soumya 20000
Ramesh 25000
Priya 30000
Hrithik 35000
Harsha 35000
SELECT dept, SUM (salary) FROM employee GROUP BY
dept
HAVING SUM (salary) > 25000
dept salary
------------- -------------
Electronics 55000
Aeronautics 35000
InfoTech 30000
8. SELECT using AND, OR and NOT
i. AND
AND requires that two conditions are true.
Syntax:
SELECT <list_of_column_name>
FROM <list_of_table_name>
WHERE <condition1> AND <condition2>;
Example:
SELECT * FROM Employee
WHERE Age= 30 AND City="Pune";
Output:
Syntax:
SELECT <list_of_column_name>
FROM <list_of_table_name>
WHERE <condition1> OR <condition2>;
Example:
SELECT * FROM Employee
WHERE City="Pune" OR City="Mumbai";
Output:
Syntax:
SELETE <list_of_column_name>
FROM <list_of_table_name>
WHERE NOT <condition>;
Example:
SELECT * FROM Employee
WHERE NOT City="Pune";
Output:
Output:
Ename Salary
PQR 30000
Following are the comparison operators where
sub-queries are expressed as one SELECT
statement connected to another,
Comparison Operator
Operator Description
= Equal to
< > or != Not equal to
> Greater than
< Less than
>= Greater than Equal to
<= Less than Equal to
Multiple-row Comparison Operator
Operator Description
IN Equal to any value retrieved in an Inner query.
<Employee> Table
Ename DeptName
ABC Finance
PQR Production
LMN Sales
XYZ Marketing
STU Human Resource
• Standard Views
• Indexed Views
• Partitioned Views
Advantages of View
• View provides data security for the same base tables.
• It allows different users to view the same data in
different ways at the same time.
• It is used to represent additional information like
derived columns.
• It is used to hide complex queries.
• It presents a consistent, unchanged image of the
database structure, even if the tables are split or
renamed.
• It does not allow direct access to the tables of the
data dictionary.
Disadvantages of View
Declare
Var1 integer;
Var2 integer;
Var3 integer;
Begin
Var1:=&var1;
Var2:=&var2;
Var3:=var1+var2;
Dbms_output.put_line(var3);
End;
/
Note: PL/SQL block can be nested within other PL/SQL blocks with the help of
BEGIN and END keyword. The code starts with DECLARE and it is used to define
all variables, cursors, subprograms and other elements which can be used in the
program.
Output:
Q. Write a Pl/SQL block to obtain factorial of a number.
Answer:
Factorial number: The factorial of a non-negative integer 'n' is denoted by n!. It is the
product of all positive integers less then or equal to 'n'.
For example:
3! = 3*2*1 = 6
Declare
num number;
fact number:= 1;
temp number;
begin
temp :=& num;
while (num > 0)
loop
fact := fact * num;
num := num - 1;
end loop;
Dbms_Output.Put_line('factorial of ' || temp || ' is ' || fact);
end;
/
The above program will compute the factorial of a given number.
Output:
Q. Write a PL/SQL block to obtain Fibonacci series.
Answer:
Fibonacci series: It is a series of a numbers, in which each number is the sum of
two previous numbers.
For example:
1, 1, 2, 3, 5, 8, 13, 21.........etc.
The following code will calculate the Fibonacci series of a given
number.
declare
a number(3):=1;
b number(3):=1;
c number(3);
n number(3):=&n;
begin
Dbms_output.put_line('the fibinocci series is:');
while a<=n
loop
dbms_output.put_line(a);
c:=a+b;
a:=b;
b:=c;
end loop;
end;
Output:
/
Introduction to Trigger
• A trigger is a special kind of stored procedures
that automatically executes when an event
occurs in the database server.
• Triggers executes when a user tries to modify
data through a data manipulation language
(DML) event, such as Insert, Delete, Update.
• These triggers fire when any valid event is fired.
• A Trigger that contains statement which causes
the other triggers are known as Cascading
Triggers.
Difference between Trigger and Stored
Procedure
Trigger Stored Procedure
Trigger is an act which is Stored procedure is a set of
performed automatically before functionality which is executed
or after an event occurs. when it is explicitly invoked.
Attributes Description