0% found this document useful (0 votes)
23 views

Database System Architecture Work

Uploaded by

asha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Database System Architecture Work

Uploaded by

asha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

UNIIT 1- RELATIONAL DATABASES

1. Database System Architecture work


AU: May-12,13,14,16,17, Dec.- 08,15,17,19, Marks 16
• The typical structure of typical DBMS is based on relational data model as shown in Fig 1.5.1.

• Consider the top part of Fig. 1.5.1. It shows application interfaces used by naïve users, application
programs created by application programmers, query tools used by sophisticated users and administration
tools used by database administrator.
• The lowest part of the architecture is for disk storage.
• The two important components of database architecture are - Query processor and storage manager.
Query processor:
• The interactive query processor helps the database system to simplify and facilitate access to data. It
consists of DDL interpreter, DML compiler and query evaluation engine.
• With the following components of query processor, various functionalities are performed -
i) DDL interpreter: This is basically a translator which interprets the DDL statements in data
dictionaries.
ii) DML compiler: It translates DML statements query language into an evaluation plan. This plan
consists of the instructions which query evaluation engine understands.
iii) Query evaluation engine: It executes the low-level instructions generated by the DML compiler.
• When a user issues a query, the parsed query is presented to a query optimizer, which uses information
about how the data is stored to produce an efficient execution plan for evaluating the query. An execution
plan is a blueprint for evaluating a query. It is evaluated by query evaluation engine.
Storage manager:
• Storage manager is the component of database system that provides interface between the low level data
stored in the database and the application programs and queries submitted to the system.
• The storage manager is responsible for storing, retrieving, and updating datain the database. The storage
manager components include -
i) Authorization and integrity manager: Validates the users who want to access the data and tests for
integrity constraints.
ii) Transaction manager: Ensures that the database remains in consistent despite of system failures and
concurrent transaction execution proceeds without conflicting.
iii) File manager: Manages allocation of space on disk storage and representation of the information on
disk.
iv) Buffer manager: Manages the fetching of data from disk storage into main memory. The buffer
manager also decides what data to cache in main memory. Buffer manager is a crucial part of database
system.
• Storage manager implements several data structures such as -
i) Data files: Used for storing database itself.
ii) Data dictionary: Used for storing metadata, particularly schema of database.
iii) Indices: Indices are used to provide fast access to data items present in the database
2.EXPLAIN Views of Data (AU: May-16, Marks 16)

• Database is a collection of interrelated data and set of programs that allow users to access or modify the
data.
• Abstract view of the system is a view in which the system hides certain details of how the data are
stored and maintained.
• The main purpose of database systems is to provide users with abstract view of the data.
• The view of the system helps the user to retrieve data efficiently.
• For simplifying the user interaction with the system there are several levels of abstraction - these levels
are - Physical level, logical level and view level.

Data Abstraction
Data abstraction: Data abstraction means retrieving only required amount of information /of the system
and hiding background details.
There are several levels of abstraction that simplify the user interactions with the system. These are
1) Physical level:
• This is the lowest level.
• This level describes how actually the data are stored.
• This level describes complex low level data structures.
2) Logical level:
• This is the next higher level, which describes the what data are stored in database.
• This level also describes the relationship among the data.
• The logical level thus describes then entire database in terms of small number of relatively simple
structures.
• The database administrators use logical level of abstraction for deciding what information to keep in
database.
3) View level:
This is highest level of abstraction that describes only part of the entire database.
• The view level can provide the access to only part of the database.
• This level helps in simplifying the interaction with the system.
• The system can provide multiple views of the same system.
• Clerk at the reservation system, can see only part of the database and can access the required
information of the passenger.
Fig. 1.3.1 shows the relationship among the three levels of abstraction.

For example: Consider following record


Type employee = record
empID:numeric(10)
empname:char(20)
dept_no:numeric (10)
salary:numeric(8,2)
end
This code defines a new record employee with four fields. Each field is associated with field name and its
type. There are several other records such as
department with fields dept_no, dept_name, building
customer with fields cust_id,cust_name
• At the physical level, the record - customer, employee, department can be Vibe described as block of
consecutive storage locations. Many database systems hide lowest level storage details from database
programmer.
• The type definition of the records is decided at the logical level. The programmer work of the record at
this level, similarly database administrators also work at this level of abstraction.
• There is specific view of the record is allowed at the view level. For instance - - customer can view the
name of the employee, or id of the employee but cannot access employee's salary.

Instances and Schemas

Schema: The overall design of the database is called schema

For example - In a program we do variable declaration and assignment of values to the variable. The
variable declaration is called schema and the value assigned to the variable is called instance. The schema
for the student record can be

Instances: When information is inserted or deleted from the database then the database gets changed. The
collection of information at particular moment is called instances. For example - following is an instance
of student database

Types of Schema: The database has several schema based on the levels of abstraction.

(1) Physical Schema: The physical schema is a database design described at the physical level of
abstraction.

(2) Logical Schema: The logical schema is a database design at the logical level of abstraction.

(3) Subschema:A database may have several views at the view level which are called subschemas.

Database Languages

There are two types of languages supported by database systems. These are

(1) DDL
• Data Definition Language (DDL) is a specialized language used to specify a database schema by a set of
definitions.
• It is a language which is used for creating and modifying the structures of tables, views, indexes and so
on.
• DDL is also used to specify additional properties of data.
• Some of the common commands used in DDL are - CREATE, ALTER, DROP.) The main use of
CREATE command is to build a new table. Using ALTER command, the users can add up some
additional column and drop existing columns. Using DROP command, the user can delete table or view.
(2) DML

• DML stands for Data Manipulation Language.


• This language enables users to access or manipulate data as organized by appropriate data model.
• The types of access are-
• Retrieval of information stored in the database.
• Insertion of new information into the database.
• Deletion of information from the database.
• Modification of information stored in database.
• There are two types of DML -
• Procedural DML - Require a user to specify what data are needed and how to get those data.

• Declarative DML - Require a user to specify what data are needed without of au aw specifying how to
get those data.

• Query is a statement used for requesting the retrieval of information. This retrieval of information using
some specific language is called query language.

3.explain three different groups of datamodels with suitable example (AU MAY-19,MARKS 13)

Definition: It is a collection of conceptual tools for describing data, relationships among data, semantics
(meaning) of data and constraints.

• Data model is a structure below the database.

• Data model provides a way to describe the design of database at physical, logical and view level.

• There are various data models used in database systems and these are as follows -

(1) Relational model:

• Relation model consists of collection of tables which stores data and also guilatxo represents the
relationship among the data.
• Table is also known as relation.

• The table contains one or more columns and each column has unique name.

• Each table contains record of particular type, and each record type defines a fixed number of fields or
attributes.

• For example - Following figure shows the relational model by showing the relationship between Student
and Result database. For example - Student Ram lives in city Chennai and his marks are 78. Thus the
relationship between these two databases is maintained by the SeatNo. Column

Advantages:

(i) Structural Independence: Structural independence is an ability that allows us to make changes in one
database structure without affecting other. The relational levsiz model have structural independence.
Hence making required changes in thedatabase is convenient in relational database model.

(ii)Conceptual Simplicity: The relational model allows the designer to simply focus on logical design
and not on physical design. Hence relational models are conceptually simple to understand.

(iii) Query Capability: Using simple query language (such as SQL) user can get egile information from
the database or designer can manipulate the database structure.

(iv) Easy design,maintenance and usage: The relational models can be designed logically hence they
are easy to maintain and use.

Disadvantages:

(i) Relational model requires powerful hardware and large data storage devices.

(ii) May lead to slower processing time.

(iii) Poorly designed systems lead to poor implementation of database systems.

1) Entity relationship model:

• As the name suggests the entity relationship model uses collection of basic objects called entities and
relationships.

• The entity is a thing or object in the real world.

• The entity relationship model is widely used in database design.


• For example - Following is a representation of Entity Relationship modelin which the relationship
works_for is between entities Employee and Department.

Advantages:

i) Simple: It is simple to draw ER diagram when we know entities and relationships.

ii) Easy to understand: The design of ER diagram is very logical and hence they are easy to design and
understand.

iii) Effective: It is effective communication tool.

iv) Integrated: The ER model can be easily integrated with Relational model.

v) Easy conversion: ER model can be converted easily into other type of models.

Disadvantages:

i) Loss of information: While drawing ER model some information can be hidden or lost.

ii) Limited relationships: The ER model can represent limited relationships as compared to other
models.

iii) No Representation for data manipulation: It is not possible to represent data manipulation in ER
model.

iv) No industry standard: There is no industry standard for notations of ER diagram.

(3) Object Based Data Model:

• The object oriented languages like C++, Java, C# are becoming the

dominant in software development.

• This led to object based data model.

• To The object based data model combines object oriented features with relationaldata model.

Advantages:

i) Enriched modelling: The object based data model has capability of modelling the real world objects.

ii) Reusability: There are certain features of object oriented design such as inheritance, polymorphism
which help in reusability.
iii) Support for schema evolution: There is a tight coupling between data and b applications, hence there
is strong support for schema evolution.

iv)Improved performance: Using object based data model there can be significant improvement in
performance using object based data model.

Disadvantages:

i) Lack of universal data model: There is no universally agreed data model for an object based data
model, and most models lack a theoretical foundation.

ii) Lack of experience: In comparison with relational database management the use of object based data
model is limited. This model is more dependent on the skilled egi programmer.

iii) Complex: More functionalities present in object based data model make the design complex.

(4) Semi-structured data model:

• The semi-structured data model permits the specification of data where individual data items of same
type may have different sets of attributes.

• The Extensible Markup Language (XML) is widely used to represent semi- structured data model.

Advantages

i) Data is not constrained by fixed schema.

ii) It is flexible.

iii) It is portable.

Disadvantages

i) Queries are less efficient than other types of data model.

4. Explain various operations in relational algebra with examples


AU: May-03, Marks 10, Dec-07, Marks 8, Dec.- 08, Marks 10, May-14, Marks 16

There are two formal query languages associated with relational model and those are relational algebra
and relational calculus.

• Definition: Relational algebra is a procedural query language which is used to access database tables to
read data in different ways.

• The queries present in the relational algebra are denoted using operators.

• Every operator in relational algebra accepts relational instances (tables) as input and returns relational
instance as output. For example-
• Each relational algebra is procedural. That means each relational query describes a step-by-step
procedure for computing the desired answer, based on the order in which operators are applied in the
query.

• A sequence of relational algebra operations forms a relational algebra expression, whose result will also
be a relation that represents the result of a database query. The By composing the operators in relational
expressions the complex relation can be defined.

Relational Operations

Various types of relational operations are as follows-

(1) Selection:

• This operation is used to fetch the rows or tuples from the table(relation).

• Syntax: The syntax is

σpredicate(relation)

Where σrepresents the select operation. The predicate denotes some logic using which the data from the
relation (table) is selected.

• For example - Consider the relation student as follows-


Query: Fetch students with age more than 18
We can write it in relational algebra as
Σage>>18 (student)
The output will be-

We can also specify conditions using and, or operators.

(2)Projection :
• Project operation is used to project only a certain set of attributes of a relation. That means if you want
to see only the names all of the students in the Student table, then you can use Project operation.
• Thus to display particular column from the relation, the projection operator is used.
• It will only project or show the columns or attributes asked for, and will also vait remove duplicate data
from the columns.
• Syntax:
ПС1, С2... (r)
where C1, C2 etc. are attribute names(column names).
• For example - Consider the Student table given in Fig. 1.13.2.
Query: Display the name and age all the students
This can be written in relational algebra as
Пsname, age (Student)
Above statement will show us only the Name and Age columns for all the rows of data in Student table.
(3) Cartesian product:
• This is used to combine data from two different relations(tables) into one and fetch data from the
combined relation.
• Syntax: A x B
• For example: Suppose there are two tables named Student and Reserve as follows

Query: Find the names of all the students who have reserved isbn = 005. To satisfy this query we need to
extract data from two table. Hence the cartesian product operator is used as
(σStudent.sid = Reserve.sid ˄ Reserve.Isbn = 005 (Student × Reserve)
As an output we will get

Note:that although the Sid columns is same, it is repeated.


(4) Set operations: Various set operations are - union, intersection and set-difference. Let us understand
each of these operations with the help of examples.
(i) Union:
• This operation is used to fetch data from two relations(tables) or temporary relation(result of another
operation).
• For this operation to work, the relations(tables) specified should have same number of
attributes(columns) and same attribute domain. Also the duplicate tuples are automatically eliminated
from the result.
• Syntax: AUB
• Where A and B are relations.
• For example: If there are two tables student and books as follows-

• Query: We want to display both the student name and book names from both the tables then
Пsname (Student) U Пbname (Book)
(ii) Intersection:
• This operation is used to fetch data from both tables which is common in both the tables.
• Syntax: A ∩ B
where A and B are relations.
• Example - Consider two tables - Student and Worker

• Query: If we want to find out the names of the students who are working in a company then 300
Пname (Student) ∩ Пname (Worker)
(iii) Set-Difference: The result of set difference operation is tuples, which are present in one relation but
are not in the second relation.
Syntax: A - B
For Example: Consider two relations Full_Time_Employee and Part_Time_Employee, if we want to
find out all the employee working for Fulltime, then the set difference operator is used -
ПEmpName(Full Time_Employee) – ПEmpName(Part_Time_Employee)
(5) Join:The join operation is used to combine information from two or more relations. Formally join can
be defined as a cross-product followed by selections and projections, joins arise much more frequently in
practice than plain cross-products. The join operator is used as
A) Inner Join
There are three types of joins used in relational algebra
i) Conditional join: This is an operation in which information from two tables is combined using some
condition and this condition is specified along with the join operator.
A c B = σc (A x B)
Thus is defined to be a cross-product followed by a selection. Note that the condition c can refer to
attributes of both A and B. The condition C can be specified using <,<,>,< or = operators.
For example consider two table student and reserve as follows-

If we want the names of students with sid(Student) = sid (Reserve) and isbn = 005,then we can write it
using Cartesian product as -
(σ((Student.sid = Reserve.sid) ∩(Reserve.(isbn) =005)) (Student × Reserve))
Here there are two conditions as
i) (Student.sid =Reserve.sid) and ii) (Reserve.isbn = 005) which are joined by∩operator.
Now we can use c instead of above statement and write it as -
(Student ( Student.sid - Reserve.sid) ˄ (Reserve.(Isbn) - 005) Reserve))
The result will be-

ii) Equijoin: This is a kind of join in which there is equality condition between two attributes(columns) of
relations(tables). For example - If there are two table Book and Reserve table and we want to find the
book which is reserved by the student having isbn 005 and name of the book is 'DBMS', then :

(Obname='DBMS' (Book (Book.isbn = Reserve.isbn) Reserve)


Then we get

iii) Natural Join: When there are common columns and we have to equate these common columns then
we use natural join. The symbol for natural join is simply without any condition. For example,
consider two tables-

Now if we want to list the books that are reserved, then that means we want to match Books.isbn with
Reserve.isbn. Hence it will be simply Books Reserve
B) Outer Join
There are three types of outer joins - Left outer join, Right outer join and Full outer join.
(1) Left Outer join
• This is a type of join in which all the records from left table are returned and the matched records from
the right table gets returned.
• The result is NULL from the right side, if there is no match.
• The symbol used for left outer join is
• This can be graphically represented as follows

• For example - Consider two tables Student and Course as follows –

The result of Left outer join will be

(2) Right Outer join


• This is a type of join in which all the records from right table are returned and the matched records from
the left table gets returned.
• The result is NULL from the left side, if there is no match.
• The symbol used for right outer join is
• This can be graphically represented as follows

• For example - Consider two tables Student and Course as follows-


The result of Right outer join will be

(3) Full Outer join


• In a full outer join, all tuples from both relations are included in the result, irrespective of the matching
condition.
• It is denoted by
• Graphically it can be represented as

• For example - Consider two tables Student and Course as follows –

The result of Right outer join will be

The symbols used are –

(6) Rename operation: This operation is used to rename the output relation for any query operation
which returns result like Select, Project etc. Or to simply rename a relation(table). The operator ρ(rho) is
used for renaming.
Syntax: ρ (RelationNew, RelationOld)
For example: If you want to create a relation Student_names with sid and sname from Student, it can be
done using rename operator as:
ρ(Student_names, (Πsid.sname (Student))
(7) Divide operation:The division operator is used when we have to evaluate queries which contain the
keyword ALL.
It is denoted by A/B where A and B are instances of relation.
Formal Definition of Division Operation: The operation A/B is define as the set of all x values (in the
form of unary tuples) such that for every y value in (a tuple of) B, there is a tuple <x,y> in A.
For example - Find all the customers having accounts in all the branches. For that consider two tables -
Customer and Account as

Here We check all the branches from Account table against all the names from Customer table. We can
then find that only customer A has all the accounts in all the branches.

4. SQL Fundamentals

AU: Dec.-14,15,17,19, May-15,16,17,18, Marks 15

• Structure Query Language(SQL) is a database query language used for storing and managing data in
Relational DBMS.
• Various parts of SQL are -
• Data Definition Language(DDL): It consists of a set of commands for defining relation schema,
deleting relations, and modifying relation schemas.
• Data Manipulation Language(DML): It consists of set of SQL commands for inserting tuples into
relational schema, deleting tuples from or modifying tuples in databases.
• Integrity: The SQL DDL includes commands for specifying integrity constraints. These constraints
must be satisfied by the databases.
• View definition: The SQL DDL contains the commands for defining views for database.
• Transaction control: The SQL also includes the set of commands that indicate beginning and ending of
the transactions.
• Embedded SQL and Dynamic SQL: There is a facility of including SQL commands in the
programming languages like C,C++, COBOL or Java.
• Authorization: The SQL DDL includes the commands for specifying access rights to relations and
views.

Data Abstraction
The Basic data types used in SQL are -
(1) char(n): For representing the fixed length character string this data type is used. For instance to
represent name, designation, coursename, we use this data type. Instead of char we can also use character.
The n is specified by the user.
(2) varchar(n): The varchar means character varying. That means - for denoting the variable length
character strings this data type is used. The n is user specified maximum character length.
(3) int: For representing the numeric values without precision, the int data type is used.
(4) numeric: For representing, a fixed point number with user-specified precision this data type is used.
The number consists of m digits plus sign k digits are to the right of precision. For instance the
numeric(3,2) allows 333.11 but it does not allow 3333.11
(5) smallint: It is used to store small integer value. It allows machine dependent subset of integer type.
(6) real: It allows the floating point, double precision numbers.
(7) float(n): For representing the floating point number with precision of at least n digits this data type is
used.

Basic Schema Definition


In this section, we will discuss various SQL commands for creating the schema definition.
There are three categories of SQL commands.

Data Definition Language


Data Manipulation Language

Data Control Language

1. Creation
• A database can be considered as a container for tables and a table is a grid with rows and columns to
hold data.
• Individual statements in SQL are called queries.
• We can execute SQL queries for various tasks such as creation of tables, insertion of data into the tables,
deletion of record from table, and so on.
In this section we will discuss how to create a table.
Step 1: We normally create a database using following SQL statement..
Syntax
CREATE DATABASE database_name;
Example
CREATE DATABASE Person _DB
Step 2: The table can be created inside the database as follows -
CREATE TABLE table name (
Col1_name datatype,
col2 _name datatype,
……
coln_name datatype
);
Example
CREATE TABLE person_details{
AdharNo int,
FirstName VARCHAR(20),
MiddleName VARCHAR(20),
LastName VARCHAR(20),
Address VARCHAR(30),
City VARCHAR(10)
}
The blank table will be created with following structure
Person_details

2. Insertion
We can insert data into the table using INSERT statement.
Syntax
INSERT INTO table_name (col1, col2,...,coln)
VALUES (value1,value,...., valuen)
Example
INSERT INTO person_details (AdharNo, FirstName, MiddleName, LastName, Address, City)
VALUES (111, 'AAA','BBB','CCC','M.G. Road', 'Pune')
The above query will result into –

3. Select
• The Select statement is used to fetch the data from the database table.
• The result returns the data in the form of table. These result tables are called resultsets.
• We can use the keyword DISTINCT. It is an optional keyword indicating that the answer should not
contain duplicates. Normally if we write the SQL without DISTINCT operator then it does not eliminate
the duplicates.
Syntax
SELECT col1, col2, ...,coln FROM table_name;
Example
SELECT AdharNo, FirstName, Address, City from person_details
The result of above query will be

• If we want to select all the records present in the table we make use of * character.
Syntax
SELECT FROM table_name;
Example
SELECT * FROM person_details;
The above query will result into

4. Where Clause
The WHERE command is used to specify some condition. Based on this condition the data present in the
table can be displayed or can be updated or deleted.
Syntax
SELECT col1,col2, ...,coln
FROM table_name
WHERE condition;
Example
Consider following table-

If we execute the following query


SELECT AdharNo
FROM person_details
WHERE city='Pune';
The result will be
If we want records of all those person who live in city Pune then we can write the query using WHERE
clause as
SELECT *
FROM person_details
WHERE city='Pune';
The result of above query will be

5. Update
• For modifying the existing record of a table, update query is used.
Syntax
UPDATE table name
SET col1-value1, col2-value2,...
WHERE condition;
Example
Consider following table

Person_details table
If we execute following query
UPDATE rerson_details
SET city 'Chennai'
WHERE AdharNo=333
The result will be
6. Deletion
We can delete one or more records based on some condition. The syntax is as follows -
Syntax
DELETE FROM table_name WHERE condition;
Example
DELETE FROM person_details
WHERE AdharNo=333
The result will be –

We can delete all the records from table. But in this deletion, all the records get deleted without deleting
table. For that purpose the SQL statement will be
DELETE FROM person_details;

7. Logical Operators
• Using WHERE clause we can use the operators such as AND, OR and NOT.
• AND operator displays the records if all the conditions that are separated using AND operator are true.
• OR operator displays the records if any one of the condition separated using OR operator is true.
• NOT operator displays a record if the condition is NOT TRUE.
Consider following table

Syntax of AND
SELECT col1, col2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3...;
Example of AND
If we execute following query-
SELECT AdharNo, FirstName, City
FROM person_details
WHERE AdharNo=222 AND City= 'Pune';
The result will be –

Syntax of OR
SELECT col1, col2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Example of OR
SELECT AdharNo, FirstName, City
FROM person_details
WHERE City='Pune' OR City='Mumbai';
The result will be –

Syntax of NOT
SELECT col1, col2, ...
FROM table_name
WHERE NOT condition;
Example of NOT
SELECT AdharNo, FirstName, City
FROM person_details
WHERE NOT City='Pune';
The result will be

8. Order By Clause
• Many times we need the records in the table to be in sorted order.
• If the records are arranged in increasing order of some column then it is called ascending order.
• If the records are arranged in decreasing order of some column then it is called descending order.
• For getting the sorted records in the table we use ORDER BY command.
• The ORDER BY keyword sorts the records in ascending order by default.
Syntax
SELECT col1, col2,...,coln
FROM table_name
ORDER BY col1,col2.... ASC | DESC
Here ASC is for ascending order display and DESC is for descending order display.
Example
Consider following table

SELECT *
FROM person_details
ORDER BY AdharNo DESC;
The above query will result in

9. Alteration
There are SQL command for alteration of table. That means we can add new column or delete some
column from the table using these alteration commands.z
Syntax for Adding columns
ALTER TABLE table_name
ADD column_name datatype;
Example
Consider following table
If we execute following command
ALTER TABLE Customers
ADD Email varchar(30);
Then the result will be as follows –

Syntax for Deleting columns


ALTER TABLE table_name
DROP COLUMN column name;
Example
Consider following table

If we execute following command


ALTER TABLE Customers
DROP COLUMN Address;
Then the result will be as follows –

10. Defining Constraints


• We can specify rules for data in a table.
• When the table is created at that time we can define the constraints.
• The constraint can be column level i.e. we can impose constraint on the column and table level i.e we
can impose constraint on the entire table.
There are various types of constraints that can be defined are as follows -
1) Primary key: The primary key constraint is defined to uniquely identify the records from the table.
The primary key must contain unique values. Hence database designer should choose primary key very
carefully.

Basic Structure of SQL Queries


The basic form of SQL queries is
SELECT-FROM-WHERE. The syntax is as follows:
SELECT[DISTINCT] target-list
FROMRelation-list
WHEREQualification
• SELECT: This is one of the fundamental query command of SQL. It is similar to the projection
operation of relational algebra. It selects the attributes based on the condition described by WHERE
clause.
• FROM: This clause takes a relation name as an argument from which attributes are to be
selected/projected. In case more than one relation names are given, this clause corresponds to Cartesian
product.
• WHERE: This clause defines predicate or conditions, which must match in order to qualify the
attributes to be projected.
• Relation-list: A list of relation names(tables)
• Target-list: A list of attributes of relations from relation list(tables)
• Qualification:Comparisons of attributes with values or with other attributes combined using AND, OR
and NOT.
• DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Normally if
we write the SQL without DISTINCT operator then it does not eliminate the duplicates.
Example
SELECT sname
FROM Student
WHERE age>18
• The above query will return names of all the students from student table where age of each student is
greater than 18

5.what is embedded SQL? Embedded SQL


AU: May-14,17, Dec.-14,16,17, Marks 8
• The programing module in which the SQL Statements are embedded is called Embedded SQL module.
• It is possible to embed SQL statements inside the programming language such as C, C++,
PASCAL,Java and so on.
• It allows the application languages to communicate with DB and get requested result.
• The high level languages which supports embedding SQLS within it are also known as host language.
• An embedded SQL program must be processed by a special preprocessor prior to compilation. The
preprocessor replaces embedded SQL requests with host-language declarations and procedure calls that
allow runtime execution of the database accesses. Then, the resulting program is compiled by the host-
language compiler. This is the main distinction between embedded SQL and JDBC or ODBC.
Example of Embedded SQL - Following program prompts the user for an order number,
retrieves the customer number, salesperson, and status of the order, and displays the retrieved information
on the screen.
int main() {
EXEC SOL INCLUDE SOLCA;
EXEC SOL BEGIN DECLARE SECTION;
int OrderID;/* Employee ID (from user) */
int CustID;/* Retrieved customer ID */
char SalesPerson[10] /* Retrieved salesperson name */
char Status[6]/* Retrieved order status */
EXEC SOL END DECLARE SECTION;
/* Set up error processing */
EXEC SOL WHENEVER SOLERROR GOTO query_error;
EXEC SOL WHENEVER NOT FOUND GOTO bad_number;
/* Prompt the user for order number */
printf ("Enter order number: ");
scanf_s("%d", &OrderID);
/* Execute the SQL query */
EXEC SOL SELECT CustID, SalesPerson, Status
FROM Orders
WHERE OrderID = :OrderID
INTO:CustID, :SalesPerson, :Status;
/* Display the results */
printf ("Customer number: %d\n", CustID);
printf ("Salesperson: %s\n", SalesPerson);
printf ("Status: %s\n", Status);
exit():
query_error:
printf ("SQL error: %ld\n", sqlca->sqlcode);
exit();
bad_number:
printf ("Invalid order number.\n");
exit();
}
Features of Embedded SQL
(1) It is easy to use.
(2) It is ANSI/ISO standard programming language.
(3) It requires less coding
(4) The precompiler can optimize execution time by generating stored procedures for the Embedded SQL
statements.
(5) It is identical over different host languages, hence writing applications using different programming
languages is quite easy.

You might also like