DATABASE MANAGEMENT SYSTEM

Embedded SQL is a technique used to include SQL statements within a high-level programming language, allowing seamless interaction between a database management system (DBMS) and the application code. The integration of SQL statements directly into the programming language enables developers to perform database operations within the context of their application code. Embedded SQL is commonly used in various programming languages, including C, C++, Java, and COBOL.

Here are the key components and considerations when using Embedded SQL in a DBMS:

Basic Structure:

  • Embedded SQL Tags:
    • SQL statements are typically enclosed within special tags or annotations, depending on the programming language. These tags indicate to the compiler that the enclosed code contains SQL statements.
  • Host Variables:
    • Variables from the host programming language can be used to pass values between the application code and SQL statements. These variables act as placeholders for data in SQL queries.
  • Preprocessing:
    • Before the application code is compiled, a preprocessor analyzes the code, extracts SQL statements, and generates modified code in which SQL statements are replaced with function calls or method invocations.
  • Compilation:
    • The modified code is then compiled using a regular compiler for the chosen programming language.
  • Execution:
    • At runtime, the embedded SQL statements are executed, and database operations are performed based on the specified SQL queries.

Here's a simple example in C using Embedded SQL:

#include <stdio.h>
#include <sql.h>
#include <sqlext.h>

EXEC SQL BEGIN DECLARE SECTION;
int employee_id;
char employee_name[50];
float salary;
EXEC SQL END DECLARE SECTION;

int main() {
    // Application code

    // Embedded SQL SELECT statement
    EXEC SQL SELECT employee_name, salary
             INTO :employee_name, :salary
             FROM employees
             WHERE employee_id = :employee_id;

    // More application code

    return 0;
}

In this example:

  • EXEC SQL BEGIN DECLARE SECTION; and EXEC SQL END DECLARE SECTION; define the section where host variables are declared.
  • :employee_name, :salary, and :employee_id are host variables used to bind values between the application code and the SQL statement.
  • The SELECT statement retrieves data from the "employees" table based on the provided employee_id.