* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Additional Topics
Tandem Computers wikipedia , lookup
Relational algebra wikipedia , lookup
Concurrency control wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Oracle Database wikipedia , lookup
Microsoft Access wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Ingres (database) wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Clusterpoint wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Database model wikipedia , lookup
Relational model wikipedia , lookup
CS 430
Database Theory
Winter 2005
Lecture 14: Additional SQL Topics
1
Additional Topics
Views
Programmatic Interface
2
Views
Views are Virtual tables
They can be referenced in SELECT statements just
like another table
They can be referenced in INSERT, UPDATE, and
DELETE when that makes sense
Why Views?
Information hiding, etc. etc.
Integration with Privileges
E.g. have SELECT privilege on view, not underlying table
MySQL: Views 5.0 and later
3
Defining a View
CREATE VIEW view_name(col1, …)
AS SELECT … ;
Column names are optional
Column names are taken from SELECT if not
specified
4
View Update
Problematic
Not all views are updateable
Aggregate columns
Joins
Simple Example:
CREATE VIEW Emp_Dept
AS SELECT SSN, Dno, DName
FROM Employee JOIN Department ON Dno=Dnumber;
UPDATE Emp_Dept
SET DName = ‘New Research’
WHERE SSN = ‘333445555’;
5
More View Update
Safe (all DBMSs support)
A View which is a Row and Column subset of a
single table is updateable
INSERTS require default values for invisible column
Check Option
CREATE VIEW … WITH CHECK OPTION;
For updateable views
All updates will appear in the view
Good practice: Specify for views you intend to be
updateable
DBMS will tell you if view is not updateable
6
How are Views Implemented?
Query Modification:
View Materialization
Modify a query on the view to be a query on the underlying
tables
Simple query on a complex view becomes a complex query
Maintain a table with the contents of the view
Mapping database updates to changes in the view is a
problem
Some DBMSs allow the DBA to choose a strategy
7
Application Interface
Interactive SQL:
Type SQL and have it directly executed
Good for: Database Administration, one time
and/or ad hoc queries, shell scripts
Not good for applications
Application Interface Techniques
Application Programming Interface
Embedded SQL
Database Programming Language
8
Impedance Mismatch
Problem with host languages and SQL:
Mapping needed between host language types and SQL
data types
Databases have Rows, Tables or Row Sets
Often would like to map between host language objects
and rows in tables in the DBMS
Host languages have their own types of composite data types
Dealing with update can get tricky
Bottom line
None of these problems is close to unsolvable
Code is required to deal with all of these problems
9
Application Interface Concepts
Connection
A connection to the database
Multiple SQL statements can be processed using
a connection
Host, username, password, etc. specified when
connection is established
First step in interacting with the database is to
establish a connection
10
Application Interface Concepts
Cursor
SELECT can return multiple rows. How do I
handle this in my host language?
Solution: Create a Cursor
Reflects the current status of the query
Allows iteration over the results of the query
FETCH fetches the next row from the Cursor
Some systems allow backward iteration and
seeking on a cursor
11
Application Interface Concepts
Single Statement SELECT
When a SELECT will return a single row, directly
FETCH the results of the select in a single
statement
Shared variables
Variables that are shared between the database
interface (SQL) and the programming language
Alternatively, bindings between variables in SQL
statements and variables in the programming
language
12
Application Interface Concepts
“Dynamic” SQL
SQL statements that are created on the fly by the program
PREPARE
IMMEDIATE
A statement is prepared prior to being executed
Can be used to save overhead of repeated parsing
Execute the statement without prior preparation
SQLCODE, SQLSTATE
Variables used to communicate errors, exceptions, state
between DBMS interface and program
13
Embedded SQL
SQL statements are embedded into the host
programming language
Precompiler or preprocessor used to convert
the program into standard language
Statements replaced by appropriate functions
calls
See examples:
C: Figures 9.2, 9.3, 9.4, 9.5 in text
Java: SQL/J: Figures 9.6, 9.7, 9.8, 9.9, 9.10
14
Embedded SQL
Why?
Integrated syntax for including SQL in application
programs
Why not?
Integrated? Syntax for including SQL in
application programs
Support is spotty
Preprocessor is a mini-compiler
15
Application Programming Interface
API for accessing the database
Standard: SQL/CLI (Call Level Interface)
Standardized version of ODBC
DBMS Specific
DBMS specific API
16
Application Programming Interface
SQL statements represented as character
strings
Some form of variable substitution needed
Warning: May have to escape strings
Make function calls as appropriate
Follow same model as Embedded SQL:
Establish a connection
Open a cursor
Execute statement
Fetch results
17
Example MySQL/Python
dno = raw_input('Enter a Department Number ... ').strip();
connection = MySQLdb.connect(host = ‘localhost'
db='example', user='example', passwd='example');
try:
cursor = connection.cursor()
cursor.execute(
'''
select ssn, fname, minit, lname, salary
from employee
where DNO = %(dno)s''',
{'dno' : dno})
rows = cursor.fetchall()
for row in rows:
print 'SSN: %s, Name: %s %s %s, Salary: %s' % row
cursor.close()
finally:
connection.close()
18
Open Database Connectivity (ODBC)
Originated by Microsoft
Connects to Data
Sources rather than a
specific database
Standardized as the
SQL/CLI
Initialization parameters
(.INI) describe data
source
Can manage multiple
data sources
Picture courtesy MySQL Documentation
19
Java Database Connectivity (JDBC)
Same idea as ODBC, specialized for Java
Some changes to interface to adapt to Java style
OO programming and strong typing
Provides same capabilities as ODBC
20
ODBC Examples
C/ODBC
Figures 9.11, 9.12 from book
Java/JDBC
Figures 9.13, 9.14 from book
21
Database Programming Language
Standard form: SQL/PSM (Persistent Stored
Modules)
Full scale programming language
Conditions, loops, exceptions, procedures and functions
SQL statements
SQL Data Types (e.g. Cursors)
Used to support triggers inside DBMS
May or may not have API support for calling PSM
programs
Short example: Figure 9.15 in text
22