42
MANAGING DATA(BASES) USING SQL (NON - PROCEDURAL SQL , X401.9) Professional Program: Data Administration and Management Instructor: Michael Kremer, Ph.D. Technology & Information Management Class 9

MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

Embed Size (px)

Citation preview

Page 1: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

MANAGING DATA(BASES) USING SQL

(NON-PROCEDURAL SQL, X401.9)

Professional Program: Data Administration and Management

Instructor: Michael Kremer, Ph.D.Technology & Information Management

Class 9

Page 2: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

AGENDA14. Transactions

14.1 Database Transactions

14.2 Database Locking

15. Data Definition Language (DDL)15.1 Overview of DDL

15.2 Managing Table Objects

15.3 Other Table DDL Statements

15.4 Table Constraints

15.5 Managing Views

16. Data Control Language (DCL)16.1 Overview of DCL

16.2 Managing Users

16.3 Managing System Privileges

16.4 Managing Object Privileges

Page 3: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

Transactions

14.

Page 4: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

14.1 DATABASE TRANSACTIONS AND LOCKING

Transaction: group of DML statements that belong logically

together

Save to the database only if all DML statements completed

successfully

This is part of

ACID test:

273

Page 5: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

14.1 DATABASE TRANSACTIONS AND LOCKING

First DML statement starts a transaction

Transactions ends under

the following circumstances:

274

Page 6: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

14.1 DATABASE TRANSACTIONS AND LOCKING

275

Page 7: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

14.1 DATABASE TRANSACTIONS AND LOCKING

Transaction consisting of multiple DML statements may be

partially rolled back automatically if one of the statements fails

Either issue an explicit rollback or even a commit if desired

276

Page 8: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

14.1 DATABASE TRANSACTIONS AND LOCKING

Record locking is in general an automatic mechanism that

prevents data from being changed concurrently:

Automatic lock Implicit lock

User generated lock Explicit lock

Modes for implicit locking:

Following locks are implicitly issued:

For data updates, an exclusive row lock is needed to ensure that

no one else changes the row during the transaction:

If an exclusive lock cannot be obtained, the session hangs!

Shared table lock prevents others from making DDL changes during the

transaction

277

Page 9: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

14.1 DATABASE TRANSACTIONS AND LOCKING

278

Page 10: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

Data Definition Language (DDL)

15.

Page 11: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.1 OVERVIEW OF DDL

Main Database

objects:

Most common

DDL commands:

Naming convention

for database

objects:

279

Page 12: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.2 MANAGING TABLE OBJECTS

Create table implicitly

using a subquery:

Only column name and

default value may be specified in the column definition

If not specified, then source table definition (column names,

data types) is used

No other constraints are

copied from the subquery

datasource

280

Page 13: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.2 MANAGING TABLE OBJECTS

Drop column as part of the ALTER TABLE statement

Example: To change existing column create new column, copy

over the data, then drop the old column

Dropping a column containing data is permitted!

For large tables, dropping a column

may take a long time

Set column(s) to unused, then

drop column(s) later (at night)

281

Page 14: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.2 MANAGING TABLE OBJECTS

282

Page 15: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.3 OTHER TABLE DDL STATEMENTS

Change name of object:

Foreign key references are automatically updated when

changing parent

table name

Use data dictionary

views to verify:

283

Page 16: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.3 OTHER TABLE DDL STATEMENTS

TRUNCATE command to delete all data from a table and reclaim

the table space from the database

This is a DDL statement as the TRUNCATE command irreversibly

deletes data (no rollback possible!)

Useful in data warehouse operations

284

Page 17: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.3 OTHER TABLE DDL STATEMENTS

285

Page 18: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.3 OTHER TABLE DDL STATEMENTS

Documenting your database!

Provide comments on table

and columns:

286

Page 19: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.4 TABLE CONSTRAINTS

Constraints settings at table level to ensure data integrity

Constraints are set using the CREATE or ALTER TABLE

commands

5 table constraints:

Constraints can be

defined at the column level for an individual column or at the

table level for multi-column or table constraints

Column constraints are

listed in the same line

of the column definition

Table level constraints

are listed at the end after all column definitions

287

Page 20: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.4 TABLE CONSTRAINTS

288

Page 21: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.4 TABLE CONSTRAINTS

All constraints can be defined at the column or table level

Exception: NOT NULL can only be defined for a column

System defined constraint name will be automatically assigned if

no constraint name is provided

Unique constraint:

Other columns

besides primary

key columns to

ensure uniqueness

Rules for

adding/changing

constraints:

289

Page 22: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.4 TABLE CONSTRAINTS

Check constraints to enforce business rules

Individual column check constraint are defined at the column

level, whereas multi-column constraint must be set up at the

table level

Example:

290

Page 23: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.4 TABLE CONSTRAINTS

First insert statement in the above example failed (error

message shows check constraint name as the error) Good

idea to name constraints in a meaningful way

Second insert statement was successful as the bonus was

$599, which is less than $600

291

Page 24: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.4 TABLE CONSTRAINTS

Drop a constraint:

Drop primary key

or unique constraints (one or more columns) without using name

since there can only be one!

CASCADE applies to Primary Key and will drop foreign

constraints as well

Disable temporarily constraints:

CASCADE will disable foreign key constraints

Enable constraints, no CASCADE

option, must enable foreign key

explicitly!

Drop column, CASCADE drops all

constraints referenced by the dropped column

292

Page 25: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.5 MANAGING VIEWS

View saved query (not data)

Reasons for creating views:

293

Page 26: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.5 MANAGING VIEWS

294

Page 27: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.5 MANAGING VIEWS

Replace existing

views by using

REPLACE option

Dropping a view:

Make view potentially

updatable by

omitting WITH READ ONLY

Data update rules in views:

295

Page 28: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.5 MANAGING VIEWS

296

Page 29: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

15.5 MANAGING VIEWS

Previous example: Updates to the region_name resulted in an

error message key region_id cannot be a key in this view

region_name is not derived from a key-preserved table

Regions table is parent table in this join and becomes

denormalized once joined with the child records

Columns based on child table can be updated country_id

could be the key in this view

One child record in this view maps to one record in the

underlying table

297

Page 30: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

Data Control Language (DCL)

16.

Page 31: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.1 OVERVIEW OF DCL

DCL deals with users, their access to the database, and their privileges to database objects, such as tables and views

Users and schema: Student database also called a schema

User student owns the objects (that is tables, etc.) in this database

When creating other users in the database give those users access to the student schema objects so that they can query the data and possibly even modified it

But you do not want those users to create their own database objects

Important concept in an Oracle database:

A user who has the privilege to create database objects and has done so is a schema owner

All other users are simply users, they have permission to access the data in a different schema

298

Page 32: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.1 OVERVIEW OF DCL

Remember, when you lived with your parents in their house: Your

parents were the schema owner (they owned the house), but you

had permission to live in it

Well over 100 system

privileges, i.e. to connect

or being able to create tables

Object privileges data access (tables, views, stored procs)

Simplify user management by creating database roles:

Assign users to roles

Assign privileges to roles

Profile for a user: Password management and resource limits

299

Page 33: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.2 MANAGING USERS

Two system accounts: SYS and SYSTEM

DBA creates other accounts, such as one schema account and

other user accounts

In order to being able to create

accounts, you must have the

corresponding privilege!

By default account is unlocked

Specify profile if needed

Password Expire option will force users to change their initial

password at first login

300

Page 34: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.2 MANAGING USERS

Simply

creating a user

account does

not give you

access to the

database!

301

Page 35: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.2 MANAGING USERS

Manage user accounts:

Reset passwords

Lock/Unlock accounts

Drop User:

302

Page 36: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.3 MANAGING SYSTEM PRIVILEGES

Once user account is created certain system privileges need to

be assigned: Create Session, for example, to even connect to the

database

Basic System Privileges:

303

Page 37: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.3 MANAGING SYSTEM PRIVILEGES

Grant Privileges:

WITH ADMIN OPTION allows you to grant your grantee (user

receiving the privilege) the ability to grant system privileges to

other users

Assign CREATE

SESSION

privilege

User can now

log in, but

cannot do

anything else

304

Page 38: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.3 MANAGING SYSTEM PRIVILEGES

Error message:

Does not specifically

state that it is an

access problem!

Grant SELECT ANY

TABLE

Now user is able

to select data!

305

Page 39: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.3 MANAGING SYSTEM PRIVILEGES

Remove privileges:

306

Page 40: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.4 MANAGING OBJECT PRIVILEGES

Specifying object privileges fine-tune privileges for your user

accounts

System privileges are granted by the DBA role, whereas object

privileges can only be granted by the owner of the objects, in our

case the Student user

Grant object privileges

using ON clause to specify

particular object

For tables, views on a

column basis (update, insert)

307

Page 41: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.4 MANAGING OBJECT PRIVILEGES

308

Page 42: MANAGING DATA(BASES) USING SQL (NON ... DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Professional Program: Data Administration and Management Technology & Information Management

16.4 MANAGING OBJECT PRIVILEGES

Revoke object privileges

309