CIS275 - Chapter 8: Database Programming Flashcards
____ contain control flow statements that determine the execution order of program steps.
Imperative languages
Control flow statements include loops for repeatedly executing code and conditionals for conditionally executing code.

_____ are composed of procedures, also called functions or subroutines.
Procedural languages
Most languages developed prior to 1990 are procedural. Ex: C and COBOL.

_____ organize code into classes.
Object-oriented languages
A class combines variables and procedures into a single construct. Most languages developed since 1990 are object-oriented. Ex: Java, Python, and C++.

_____ do not contain control flow statements.
Declarative languages
Each statement declares what result is desired, using logical expressions, rather than how the result is processed.

Compilers for declarative languages
optimizers
the compiler determines an optimal way to process each declarative statement.
SQL is the leading example of a declarative language.


Building applications with both SQL and a general-purpose language is called _____.
database programming
Database programming presents two challenges:
Syntax gap. The syntax of SQL and most other languages are quite different. Ex: Groups of statements, called blocks, are fundamental to most procedural and object-oriented languages. SQL has no blocks.
Paradigm gap. Query processing is fundamentally different in SQL and procedural or object-oriented languages. Ex: A result set with many rows is processed in a single SQL statement, but usually requires loops and conditional statements in other languages.
- A declarative language specifies what result is desired. The SQL selects all airline names and flight numbers departing from JFK airport after noon.
- A procedural language specifies how the result is processed. The code fragment is written in C.
- printf() displays column headings. The string containing % characters specifies the heading format.

- The for loop checks every row of the Flight table. Variable i is the row number.
- The if statement selects flights departing from JFK airport after noon.
- printf() displays the airline name and flight number of selected flights.



_____ codes SQL statements directly in a program written in another language.
Embedded SQL
The keywords EXEC SQL precede all SQL statements so the compiler can distinguish SQL from other statements. Ex: Embedded SQL in C for Oracle Database is called Pro*C.
Embedded SQL allows the programmer to write SQL statements directly within another programming language. This approach is conceptually simple but suffers three problems:
Gaps. Most programming today uses object-oriented languages. The syntax and paradigm gap between object-oriented language and SQL is wide, so programs are difficult to write and maintain.
Compile steps. The SQL statements and host language must be compiled with different compilers, in two steps.
Network traffic. Application programs usually run on a client computer, connected to the database server via a network. With embedded SQL, each query generates a ‘round-trip’ from client to server, increasing network traffic and execution time.
For the above reasons, embedded SQL is no longer widely used.

_____ extends the SQL language with control flow statements, creating a new programming language.
Procedural SQL
Procedural SQL is limited compared to general-purpose languages, however, and is used primarily for database applications. Ex: In Oracle Database, procedural SQL is called PL/SQL.
Procedural SQL has several advantages over embedded SQL:
Gaps. Procedural SQL gracefully extends SQL. The syntax and paradigm gaps are minimal so programs are relatively easy to write and maintain.
Compile steps. Procedural SQL is compiled in one step rather than two.
Network traffic. Client applications typically call SQL procedures for execution on the database server. Each procedure executes multiple database operations, reducing network traffic and execution time.
Optimization level. The database optimizes entire procedures rather than individual queries, resulting in better optimization and faster query execution.
Procedural SQL languages vary significantly by database and do not offer the full capabilities of general-purpose languages. Ex: Most procedural SQL languages are not object-oriented. For these reasons, procedural SQL is commonly used for limited database tasks.

a library of procedures or classes.
application programming interface, or API
The library links an application programming language to a computer service, such as a database, email, or web service. The procedure or class declarations are written in the application programming language. Ex: JDBC is a library of Java classes that access relational databases.
APIs address the limitations of embedded SQL and procedural SQL:
Gaps. Since API procedures and class declarations are written in the application language, applications are compiled in one step, the syntax gap disappears, and the paradigm gap is reduced.
Applications. Database APIs are available for general-purpose, object-oriented languages. Applications are not limited to database processing tasks and procedural languages.
Database independence. Unlike procedural SQL, language and API syntax is independent of data source.
For the reasons above, APIs are the most commonly used database programming technique.

- The reservationConnection object connects the program to the reservation database.
- The listFlights object contains the SELECT statement and connects to the Reservation database.
- The flightReader object points to individual rows of the result table.

- ExecuteReader() executes the SELECT statement stored in the listFlights object.
- Read() moves flightReader to the next result table row. The while loop repeats until all rows have been read.
- GetInt32(0) and GetString(1) return the first and second columns of the current FlightReader row.





Embedded SQL statements appear in programs written in another language, called the _____.
host language
To distinguish SQL from host language statements, embedded SQL begins with the keyword EXEC SQL, followed by an SQL statement and a semicolon.
Programs containing embedded SQL are compiled in two steps:
A precompiler translates SQL statements into host language statements and function calls.
The host language compiler translates the host language program to an executable program.
a variant of embedded SQL designed for Java.
SQLJ
SQLJ is similar to standard embedded SQL, but the syntax is adapted to Java. SQLJ is not widely supported, since object-oriented languages like Java commonly use an API rather than embedded SQL.
- The C program begins with int main() and ends with return 0.
- printf() is a C statement that outputs text. \n prints a new line.

- An embedded SQL statement begins with EXEC SQL and ends with a semicolon. The UPDATE statement executes within the C program.
- The precompiler translates embedded SQL to C statements and function calls.



- The CONNECT TO statement creates a connection to the database at internet address 100.320.420.999.
- FlightConnection is active.

- Subsequent queries refer to the FlightConnection database.
- When the connection is no longer needed, release the associated resources.



a host language variable that appears in SQL statements.
shared variable
The results of SELECT statements are assigned to shared variables for further processing in the host language. Shared variables must be declared between BEGIN DECLARE SECTION and END DECLARE SECTION statements.
- Three shared variables are declared between BEGIN DECLARE SECTION and END DECLARE SECTION.
- The C code allows the user to input a flight number. The shared variable flight is assigned the number.
- The query selects rows with column FlightNumber equal to the shared variable flight.

- The shared variable airline is assigned AirlineName. The shared variable airport is assigned AirportCode.
- The C code outputs the result of the SELECT statement.































































































