










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Winter 2002
Arthur Keller – CS 180
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
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.
CREATE
TABLE
Sells
(
bar
CHAR(20), beer
VARCHAR(20),
price
REAL
); DROP
TABLE
Sells;
Winter 2002
Arthur Keller – CS 180
precision, decimal
is a number with
precision
digits with the decimal point
decimal
digits
from the right.
can store
. SQL form is
'yyyy-mm-dd'
-^
PostgreSQL follows the standard. Oracle uses a differentformat.
. Form is
TIME 'hh:mm:ss[.ss…]
' in SQL.
or
. Form is
'yyyy-mm-dd hh:mm:ss[.ss…]
' in SQL.
. Form is
'n
period
' in
PostgreSQL.
Period
is
month
days
year
, etc.
Winter 2002
Arthur Keller – CS 180
8–
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
◆
Essential if key is >1 attribute.
Winter 2002
Arthur Keller – CS 180
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
= every tuple must have a real value
for this attribute.
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
Add an attribute of relation
with
DEFAULT 'unlisted';
Columns may also be dropped. ALTER TABLE Bars DROP license;
Winter 2002
Arthur Keller – CS 180
An expression that describesa table without creating it.•^
View definition form is:
CREATE VIEW
AS
Winter 2002
Arthur Keller – CS 180
Winter 2002
Arthur Keller – CS 180