Chapter 4
Chapter 4
Relational Database
Query Languages
Presented by : Er. Lali Manandhar
Assistant Senior Lecturer
Everest Engineering College
SQL (Standard Query Language)
SQL is the standard query language to
communicate with a relational database
SQL works with database programs like MS-
and DROP.
1. CREATE DATABASE command
It is used to create a new database.
Syntax:
varchar,roll int);
3. DROP DATABASE command
It is used to destroy the complete database
structure.
Syntax:
a. INSERT INTO<table_name>
(field1,field2-----fieldN) VALUES (value1,
value2---------- valueN);
This query can be used to insert values for
from a table.
Syntax:
a. UPDATE <table_name>SET
field1=newvalue1, field2=newvalue2………
fieldN=newvalueN;
eg:
UPDATE tbl_student SET roll=5 ;
b. UPDATE <table_name> SET
field1=newvalue2, field2=newvalue2………
fielnN=newvaluN WHERE <Expression>;
from a table.
Syntax:
b. DELETE FROM<table_name>
WHERE<Expression>;
Eg: DELETE from tbl_student WHERE id>10 //
delete all records whose id >10 ;
DQL (Data Query Language)
1. SELECT command
It is used to display all or selected records
from a table.
Syntax:
a. SELECT <field1>,<field2>…..<fieldN> FROM
<table_name>;
Eg: SELECT id,name FROM tbl_student // display
field id and name with all records;
COMMIT TRANSACTION<transaction_name>;
ROLLBACKTRANSACTION <transaction_name>;
Eg:
BEGIN TRANSACTION Tr1
BEGIN TRY ;
INSERT INTO tbl_student VALUES (1,’Ram’,10);
DELETE FROM tbl_student WHERE name=’Ram’;
UPDATE tbl-student SET name=‘Bipin’ WHERE
COMMIT TRANSACTION Tr1
END TR1;
BEGIN CATCH ;
◦ GRANT<Privilage_name> ON <object_Name>To
<User_Name>;
• REMOVE <Privilage_name>ON<object_Name>
FROM <User_Name>;
Privilage_Name means access right. Some of the
access right are ALL, SELECT, INSERT, UPDATE,
DELETE, EXECUTE.
Object_Name is the name of database object
c.
ALTER TABLE<table_name> ADD PRIMARY
KEY(field1,field2);
d.
ALTER TABLE<table_name> ADD
CONSTRAINT<constraint_name> PRIMARY
KEY(field1,field2)
iii) Removing primary key Constraint
Syntax:
ALTER TABLE<table_name>DROP<constraint_name>;
Eg: ALTER TABLE tbl_student DROP idcode
2. Foreign Key constraint:
A foreign key is imposed on a column of one
table which refers the primary key column of
another table.
A foreign key constraint removes the
tables.
Syntax:
CREATE TABLE<foreign_table_name>
(
Field1 datatype1 CONSTRAINT<constraint_name>
REFERENCES<primary_table_name> (<
Primary_Key_column_name/primary key constraint name
>),
Field2 datatype2. .
FieldN datatypeN
);
on a table.
Syntax:
identity(seed,increment)
Seed: start number
Increment: incremental value for seed
Operators
To conditionally select, update and delete
data from a table, a WHERE clause followed by
an expression involving several operators is
used.
1. Arithmetic Operators:
<=.
Example:
BETWEEN.
Syntax:
SELECT <field1>,<field2>…..<fieldN> , …
from <table_name> WHERE <field_name> IN
<item1,item2…..itemN>
Example:
◦ a. Display all the records of students from
tbl_Marks who are from Pokhara, Kathmandu or
Butwal.
Select <field1>AS<NewField>,<field2>,…..
<fieldN>FROM<table_name>
1. UNION:
This operation is similar to UNION. But it also
1. AVG():
Returns the average values in a numeric
expression.
E.g. Display the average math mark for the
whole school.
SELECT AVG(maths) (AS Averagemathsmarks)
FROM tbl_Marks
2. COUNT():
Return the numbers of values in an
expression.
E.g. Display the total number of records in
table tbl_Marks.
SELECT COUNT(*) AS total FROM tbl_Marks
or
SELECT COUNT(ID) AS total FROM tbl_Marks
expression.
E.g. Display the lowest mark obtained in
maths.
SELECT MIN(maths) AS lowestmathmark FROM
tbl_Marks
4. MAX():
Returns the highest value in the numeric
expression.
E.g. Display the highest mark obtained in
maths.
SELECT MAX(maths) AS highestmathmark FROM
tbl_Marks
5. SUM():
Returns the sum of values in a numeric
expression.
E.g. Display the sum of marks in maths for all
students.
SELECT SUM(maths) AS
totalmathsmarksofallstudent FROM tbl_Marks
Grouping Data
The GROUP BY clause summarizes the result set
into groups as defined in the SELECT statement
by using `aggregate function.
Pkr 12000
Subquery / Inner query / Nested query
explained below.
1. Inner join (Natural Join):
When an inner join is applied, only rows with values
satisfying the join condition in the common column
are displayed.
Records in both the tables that do not satisfy the
join condition are not displayed.
Syntax:
SELECT <column_names>FROM<table1> INNER
JOIN <table2> ON<table1>.<common_field><join
operator><table 2>.<common field>
where tf.filmid=ta.filmid
2. Outer Join:
The outer join displays the result set
containing all the rows from one table and
matching rows from another table.
An outer join displays NULL for the columns
Disadvantages:
1. Less flexible because we need DBA to
make changes
Creating Stored Procedure
We can create stored procedure using CREATE
PROCEDURE statement.
Syntax
Sql_statementN
END
E.g.
CREATE PROCEDURE sp_mobile AS
BEGIN
SELECT * FROM tbl_mobile
END
applications to execute.
Executing a Stored Procedure
A procedure can be executed using EXECUTE
or EXEC statement.
Syntax:
EXEC sp_mobile
Altering Stored Procedure
A stored Procedure can be modified by using
ALTER PROCEDURE statement.
Syntax:
ALTER PROCEDURE<procedure_name> AS
BEGIN
Sql_statement1
Sql_statement2
Sql_statementN
END
Dropping a stored Procedure
We can destroy a stored procedure form
database by using DROP PROCEDURE
statement.
Syntax:
Sql_statementN
END
E.g.
CREATE PROC sp_mob
@mobile_type varchar(50)
AS
BEGIN
SELECT * FROM tbl_mobile WHERE
mobile_type=@mobile_type
END
Query By Example (QBE)
QBE is a graphical query language which is
based on the domain relational calculus.
A user needs minimal information to get