scuholliday - coen 1788–1 schedule today u modifications, schemas, views. u read sections 6.5-6.7....

19
SCU Holliday - coen 178 8–1 Schedule • Today Modifications, Schemas, Views. Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) • Next Constraints. Read Sections 7.1-7.3, 7.4.1.

Upload: geraldine-lester

Post on 17-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–1

Schedule

• Today Modifications, Schemas, Views. Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6)

• Next Constraints. Read Sections 7.1-7.3, 7.4.1.

Page 2: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–2

Defining a Database SchemaCREATE TABLE name (list of elements).• Principal elements are attributes and their types, but key

declarations and constraints also appear.• Similar CREATE X commands for other schema elements X:

views, indexes, assertions, triggers.• “DROP X name” deletes the created element of kind X with

that name.

Example CREATE TABLE Sells (

bar CHAR(20),beer VARCHAR(20),price REAL

);

DROP TABLE Sells;

Page 3: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–3

Types

1.INT or INTEGER.

2.REAL or FLOAT.

3.CHAR(n) = fixed length character string, padded with “pad characters.”

4.VARCHAR(n) = variable-length strings up to n characters. Oracle uses VARCHAR2(n) as well.

PostgreSQL uses VARCHAR and does not support VARCHAR2.

Page 4: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–4

5. NUMERIC(precision, decimal) is a number with precision digits with the decimal point decimal digits from the right. NUMERIC(10,2) can store ±99,999,999.99

6. DATE. SQL form is DATE 'yyyy-mm-dd'• PostgreSQL follows the standard. Oracle uses a different

format.

7. TIME. Form is TIME 'hh:mm:ss[.ss…]' in SQL.8. DATETIME or TIMESTAMP. Form is TIMESTAMP

'yyyy-mm-dd hh:mm:ss[.ss…]' in SQL.9. INTERVAL. Form is INTERVAL 'n period' in

PostgreSQL. Period is month, days, year, etc.

Page 5: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–5

PostgreSQL DatesPostgreSQL supports extensive date calculations.• Conversions to_date(text), to_char(date/time/etc.),interval(text)

• Date ± Integer = Date;Date Date = Integer (always = number of days);Date + Date is invalid!

• Timestamp ± Interval = Timestamp;Timestamp Timestamp = Interval;Interval ± Interval = Interval;Date + Date is invalid.

• Interval: '1 month' could be 28, 29, 30, or 31 days;'31 days' is always just that.

• SQL uses DATEADD and DATEDIFF;PostgreSQL uses the simpler + and .

• Also CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP.

Page 6: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–6

Declaring Keys

Use PRIMARY KEY or UNIQUE.• But only one primary key, many UNIQUEs

allowed.• SQL instructs implementations to create an index

(data structure to speed access given a key value) in response to PRIMARY KEY only.

• SQL does not allow nulls in primary key, but allows them in “unique” columns (which may have two or more nulls, but not repeated non-null values).

Page 7: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–7

Declaring Keys

Two places to declare:

1. After an attribute’s type, if the attribute is a key by itself.

2. As a separate element. Essential if key is >1 attribute.

Page 8: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–8

Example

CREATE TABLE Sells (bar CHAR(20),beer VARCHAR(20),price REAL,PRIMARY KEY(bar,beer)

);orCREATE TABLE Sells (

bar CHAR(20) PRIMARY KEY,beer VARCHAR(20),price REAL, );

Page 9: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–9

Example CREATE TABLE Sells (

bar CHAR(20),beer VARCHAR(20),price REAL,UNIQUE(bar,beer)

);is different than:

CREATE TABLE Sells (bar CHAR(20) UNIQUE,beer VARCHAR(20) UNIQUE,price REAL

);

Page 10: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–10

Other Properties You Can Give to Attributes

1. NOT NULL = every tuple must have a real value for this attribute.

2. DEFAULT value = a value to use whenever no other value of this attribute is known.

ExampleCREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50)

DEFAULT '123 Sesame St', phone CHAR(16));

Page 11: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–11

INSERT INTO Drinkers(name) VALUES('Sally')

results in the following tuple:

name addr phone

Sally 123 Sesame St. NULL

• Primary key is by default not NULL.• This insert is legal.

OK to list a subset of the attributes and values for only this subset.

• But if we had declaredphone CHAR(16) NOT NULL

then the insertion could not be made.

Page 12: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–12

Changing Columns

Add an attribute to relation R with ALTER TABLE R ADD <column declaration>;

Example ALTER TABLE Bars ADD phone CHAR(16)

DEFAULT 'unlisted';

• Columns may also be dropped. ALTER TABLE Bars DROP license;

Page 13: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–13

Views

An expression that describes a table without creating that table in the database.

Provides a mechanism for hiding data from

certain users.

• View definition form is:CREATE VIEW <name> AS <query>;

Page 14: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–14

View to Hide Info

• Create a view of customer names and their branch names without balances or acc#

Depositor = (c-name,acc#) Account = (b-name,acc#,balance)

create view cust as (select c-name, b-name from Depositor D, Account A where D.acc#=A.acc#);

Page 15: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–15

Example

The view CanDrink is the set of drinker-beer pairs such that the drinker frequents at least one bar that serves the beer.

CREATE VIEW CanDrink AS

SELECT drinker, beer

FROM Frequents, Sells

WHERE Frequents.bar = Sells.bar;

Page 16: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–16

Querying Views

Treat the view as if it were a materialized relation.

ExampleSELECT beer

FROM CanDrink

WHERE drinker = ‘Sally’;

Page 17: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–17

View Semantics

• The view definition becomes a part of the database schema and it is re-calculated every time it is used. So your view automatically reflects changes

(updates, etc) in the base relations.

• The view instance (the actual data in the view) is not stored in the database as the view but as part of the base tables. Although special materialized views are.

Page 18: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–18

Updates on Views

• Updates can be done on views if the system can translate it to an update of the base tables. 

• You can update two or more tables in this way, however, updates on complex views that are difficult to translate to base tables (or would cause unacceptable nulls) are not allowed.

Page 19: SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read

SCU Holliday - coen 178 8–19

Updates on Views

Create a view of all loan data in the Loan table leaving out the amount attribute. create view Branch-loan as select branch-name, loan# from Loan

Now add a new row to Branch-loaninsert into Branch-loanvalues ("Oakland", 11445)

  What will happen? There will be an insert of the row

("Oakland", 11445, null) into the Loan table.