Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Embedded SQL nd ADVANCED SQL

Uploaded by

Reena V 12 A
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Embedded SQL nd ADVANCED SQL

Uploaded by

Reena V 12 A
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Embedded SQL

Definition: Embedded SQL is a method of combining SQL with a general-purpose programming


language such as C, C++, Java, or COBOL. This allows the execution of SQL queries directly within the
code of the host programming language.

Key Concepts:

1. Host Variables:

o Host variables are used to pass values between the SQL statements and the host
program.

o They are declared in the host language but used in the embedded SQL statements.

2. EXEC SQL Statement:

o SQL statements are embedded in the host language code using the EXEC SQL and
END-EXEC keywords.

3. Error Handling:

o SQLCA (SQL Communication Area) or SQLCODE are used for error handling to
capture the execution status of SQL statements.

Example:

EXEC SQL BEGIN DECLARE SECTION;

int emp_id;

char emp_name[100];

EXEC SQL END DECLARE SECTION;

EXEC SQL SELECT name INTO :emp_name FROM employees WHERE id = :emp_id;

Advantages:

• Seamless Integration: Integrates SQL seamlessly with a programming language.

• Efficiency: Reduces the overhead of context switching between application and database.

• Type Safety: Ensures that data types are consistent between the application and database.

Disadvantages:

• Complexity: Increases the complexity of the application code.

• Portability: May not be portable across different database systems.


Advanced SQL
Definition: Advanced SQL refers to the use of sophisticated SQL techniques and constructs to
perform complex database operations. It includes features like subqueries, joins, indexes, stored
procedures, triggers, views, and complex data types.

Key Concepts:

1. Subqueries:

o Queries within queries to perform more complex operations.

o Example:

SELECT emp_name FROM employees WHERE emp_id IN (SELECT manager_id FROM departments);

2. Joins:

o Combining rows from two or more tables based on related columns.

o Types include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.

o Example:

SELECT e.emp_name, d.dept_name FROM employees e

INNER JOIN departments d ON e.dept_id = d.dept_id;

3. Indexes:

o Special database objects that improve the speed of data retrieval.

o Example:

CREATE INDEX idx_emp_name ON employees(emp_name);

4. Stored Procedures:

o Precompiled collections of SQL statements stored in the database.


o Example:

CREATE PROCEDURE GetEmployeeDetails (IN emp_id INT)

BEGIN

SELECT emp_name, emp_salary FROM employees WHERE emp_id = emp_id;

END;

5. Triggers:

o Automated actions performed in response to certain events on a table.

o Example:

CREATE TRIGGER after_insert_employee

AFTER INSERT ON employees

FOR EACH ROW

BEGIN

INSERT INTO audit_log (event, emp_id, timestamp)

VALUES ('INSERT', NEW.emp_id, NOW());

END;

6. Views:

o Virtual tables created by a query to simplify complex queries.

o Example:

CREATE VIEW employee_details AS

SELECT emp_name, dept_name FROM employees e

JOIN departments d ON e.dept_id = d.dept_id;

Advantages:

• Efficiency: Enhances performance through optimization techniques like indexing and


precompiled stored procedures.

• Reusability: Promotes code reuse with stored procedures and views.

• Maintainability: Simplifies maintenance by encapsulating complex logic within the database.

Disadvantages:

• Complexity: Advanced features can make the SQL code more complex and harder to debug.

• Resource Intensive: Some advanced features, like triggers, can consume more database
resources.

You might also like