Docsity

Defining a Database Schema - Lecture Slides | CMPS 180, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
Winter 2002 Arthur Keller – CS 180 8–1
Schedule
•Today: Jan. 29 (T)
◆Modifications, Schemas, Views.
◆Read Sections 6.5-6.7. Assignment 3 due.
•Jan. 31 (TH)
◆Constraints.
◆Read Sections 7.1-7.3, 7.4.1. Project Part 3 due.
•Feb. 5 (T)
◆Triggers, PL/SQL.
◆Read Sections 7.4, 8.2. Assignment 4 due.
•Feb. 7 (TH)
◆PL/SQL, Embedded SQL, CLI, JDBC.
◆Read Sections 8.1, 8.3-8.5.
•Reminder: Midterm is Feb. 14 (TH)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Defining a Database Schema - Lecture Slides | CMPS 180 and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

Winter 2002

Arthur Keller – CS 180

Schedule

•^

Today: Jan. 29 (T)^ ◆

Modifications, Schemas, Views. ◆

Read Sections 6.5-6.7. Assignment 3 due.

-^

Jan. 31 (TH)^ ◆

Constraints. ◆

Read Sections 7.1-7.3, 7.4.1. Project Part 3 due.

-^

Feb. 5 (T)^ ◆

Triggers, PL/SQL. ◆

Read Sections 7.4, 8.2. Assignment 4 due.

-^

Feb. 7 (TH)^ ◆

PL/SQL, Embedded SQL, CLI, JDBC. ◆

Read Sections 8.1, 8.3-8.5.

-^

Reminder: Midterm is Feb. 14 (TH)

Winter 2002

Arthur Keller – CS 180

Defining a Database Schema

CREATE TABLE

name (list of elements).

-^

Principal elements are attributes and their types, but keydeclarations 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;

Winter 2002

Arthur Keller – CS 180

5. NUMERIC(

precision, decimal

is a number with

precision

digits with the decimal point

decimal

digits

from the right.

NUMERIC(10,2)

can store

6. DATE

. SQL form is

DATE

'yyyy-mm-dd'

-^

PostgreSQL follows the standard. Oracle uses a differentformat.

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.

Winter 2002

Arthur Keller – CS 180

8–

PostgreSQL Dates

PostgreSQL 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

.

Winter 2002

Arthur Keller – CS 180

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.

Winter 2002

Arthur Keller – CS 180

Example

CREATE TABLE Sells (

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

Winter 2002

Arthur Keller – CS 180

Other Properties You Can Give to Attributes

1. NOT NULL

= every tuple must have a real value

for this attribute.

  1. DEFAULT

value = a value to use whenever no

other value of this attribute is known. Example

CREATE TABLE Drinkers (

name CHAR(30) PRIMARY KEY,addr

CHAR(50)

DEFAULT '123 Sesame St',

phone CHAR(16) );

Winter 2002

Arthur Keller – CS 180

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 onlythis subset.

-^

But if we had declared

phone CHAR(16) NOT NULL

then the insertion could not be made.

Winter 2002

Arthur Keller – CS 180

Changing Columns

Add an attribute of relation

R

with

ALTER TABLE

R

ADD

Example^ ALTER TABLE Bars ADD phone CHAR(16)

DEFAULT 'unlisted';

•^

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

Winter 2002

Arthur Keller – CS 180

Views

An expression that describesa table without creating it.•^

View definition form is:

CREATE VIEW

AS

;

Winter 2002

Arthur Keller – CS 180

Semantics of View Use

Example

Winter 2002

Arthur Keller – CS 180

Compose