21
First lecture of this chapter is in slides (PPT file)

First lecture of this chapter is in slides (PPT file) · sqlcode = -40202: row containing empty fields encountered. o > 0 = Statement executed successfully but some exceptional condition

  • Upload
    lykien

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

First lecture of this chapter is in slides (PPT file)

Review of referential integrity

CREATE TABLE other_table (

b1 INTEGER,

c1 INTEGER,

PRIMARY KEY (b1, c1)

)

CREATE TABLE t (

a integer PRIMARY KEY,

b2 integer,

c2 integer,

FOREIGN KEY (b, c) REFERENCES other_table (b1, c1)

)

Let’s say that other_table exists, but does not yet have the primary key defined. How do we

add a primary key (or any other constraint) to a table?

(p.131)

Let’s say that a tuple (b1, c1) is deleted from other_table. What happens to the referencing

tuples from t?

Let’s say that a tuple (b1, c1) is updated in other_table …

Actions possible (pp.130-131):

RESTRICT / NO ACTION

CASCADE

SET NULL / SET DEFAULT

CREATE TABLE R (

A INTEGER,

B INTEGER

)

CREATE ASSERTION ab

CHECK (

(SELECT COUNT (*)

FROM R AS R1, R AS R2

WHERE (R1.B != R2.B) AND (R1.A = R2.A))

=0)

What exactly does the above assertion imply?

Review of authorization (4.3)

Forms of authorization to query/modify DB instance:

Read - allows reading, but not modification of data.

Insert - allows insertion of new data, but not modification of existing data.

Update - allows modification, but not deletion of data.

Delete - allows deletion of data.

Forms of authorization to modify DB schema (covered in Chapter 8):

Index - allows creation and deletion of indices.

Resources - allows creation of new relations.

Alteration - allows addition or deletion of attributes in a relation.

Drop - allows deletion of relations.

grant <privilege list>

on <relation name or view name> to <user list>

grant select on branch to U1, U2, U3

revoke <privilege list>

on <relation name or view name> from <user list>

revoke select on branch from U1, U2, U3

4.4 Embedded SQL

EXEC SQL <embedded SQL statement > END_EXEC

EXEC SQL open c END_EXEC

EXEC SQL fetch c into :cn, :cc END_EXEC Repeated calls to fetch get successive tuples in the query result

A variable called SQLSTATE in the SQL communication area (SQLCA) gets set to ‘02000’ to

indicate no more data is available

EXEC SQL close c END_EXEC

Example: Add $100 to all account at Perryridge branch.

EXEC SQL

declare c cursor for

select *

from account

where branch_name = ‘Perryridge’

for update

;

EXEC SQL open c;

while(SQLSTATE != ‘02000’)

EXEC SQL

update account

set balance = balance + 100

where current of c

;

EXEC SQL close c;

If you really want to know (not in text, not required) … This is the SQLCA in C:

#include <SQLCA.H>

typedef struct {

char sqlcaid[8];

long sqlcabc;

long sqlcode;

struct {

short sqlerrml;

char sqlerrmc [70];

} sqlerrm;

struct {

char sqlwarn0;

char sqlwarn1;

char sqlwarn2;

char sqlwarn3;

char sqlwarn4;

char sqlwarn5;

char sqlwarn6;

char sqlwarn7;

} sqlwarn

char sqlext[8];

} SQLCA;

sqlerrml is set to 1 an error has been encountered, otherwise it is set to 0.

sqlcode is an integer which is set as follows:

o 0 = Statement executed successfully.

o < 0 = An error has occurred. The value is the negative of the error number

sqlcode = -40202: row containing empty fields encountered.

o > 0 = Statement executed successfully but some exceptional condition has occurred, e.g. no data processed (empty field encountered).

sqlcode = 100: empty row encountered.

4.5 Dynamic SQL

Example of the use of dynamic SQL from within a C program.

char * sqlprog = “ update account

set balance = balance * 1.05

where account_number = ?”

EXEC SQL prepare dynprog from :sqlprog;

char account [10] = “A-101”;

EXEC SQL execute dynprog using :account;

--------------------------------------

Null-terminated

string

4.6 Functions and Procedural Constructs in SQL

Since SQL 2003, functions can return tables as well

What is the difference between functions and procedures?

Global variable!

Procedural constructs (since SQL 1999):

Can we do this with non-procedural SQL?

Can we do this with non-procedural SQL?

Exception conditions:

Explicitely defined (exit, continue, execute_error_handling_function)

Predefined (sqlexception, sqlwarning, not found)

READ: Extended example on p.149 of text.

READ: Section 4.6.3 – External Language Routines

4.7 Recursive queries

Transitive closure: find all employees managed (directly or indirectly) by Jones

READ and understand the iterative solution (Sec.4.7.1)

SKIP Sec. 4.8

Homework for Ch.4: 3, 4, 5, 10, 11, 13 – Due next Friday, March 11, before the

midterm