Transcript Slides
Database Modifications,
Data Types,
Views
Database Modifications
•
A modification command does not
return a result as a query does, but it
changes the database in some way.
•
There are three kinds of modifications:
1. Insert a tuple or tuples.
2. Delete a tuple or tuples.
3. Update the value(s) of an existing tuple
or tuples.
Insertion
• To insert a single tuple:
INSERT INTO <relation>
VALUES ( <list of values> );
Example
• Consider MovieExec(name, address, cert#, netWorth)
INSERT INTO MovieExec
VALUES('Melanie Griffith', '34 Boston Blvd', 700, 300000);
Specifying Attributes in INSERT
•
We may add to the relation name a list of
attributes.
INSERT INTO MovieExec(name, address, cert, netWorth)
VALUES('Melanie Griffith', NULL, 700, 3000000);
•
There are two reasons to do so:
1. We forget the standard order of attributes for the
relation.
2. We don’t have values for all attributes.
Inserting Many Tuples
• We may insert the entire result of a query into a relation, using the form:
INSERT INTO <relation>
( <subquery> );
Example
CREATE TABLE DisneyMovies(
name VARCHAR2(25),
year INT
);
INSERT INTO DisneyMovies
(SELECT title, year
FROM Movie
WHERE studioName = 'Disney'
);
Deletion
• To delete tuples satisfying a condition from some relation:
DELETE FROM <relation>
WHERE <condition>;
Example
• Delete from the Movie table the Disney’s movies:
DELETE FROM Movie
WHERE studioName ='Disney';
Example: Delete all Tuples
• Make the relation Movie empty:
DELETE FROM Movie;
• No WHERE clause needed here.
Updates
• To change certain attributes in certain tuples of a relation:
UPDATE <relation>
SET <list of attribute assignments>
WHERE <condition on tuples>;
Example
• Change the length of 'Godzilla' to 200.
UPDATE Movie
SET length = 200
WHERE title = 'Godzilla';
Another Example
• Suppose that Brown’s movies have approximately 20
min of info before starting.
• So, let’s take that 20 min off.
UPDATE Movie
SET length = length - 20
WHERE (title, year) IN
(SELECT title, year
FROM Movie, Movieexec
WHERE Movie.producerc = Movieexec.cert
AND name = 'Brown');
Exercise
Product(maker, model, type)
PC(model, speed, ram, hd, rd, price)
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)
a)
b)
c)
d)
e)
f)
g)
Using two INSERT statements, store in the database the fact that PC model
1100 is made by manufacturer C, has speed 1800, RAM 256, hard disk 80,
a 20x DVD, and sells for $2499.
Insert the facts that for every PC there is a laptop with the same
manufacturer, speed, RAM and hard disk, a 15-inch screen, a model
number 1000 greater, and a price $500 more.
Delete all PC’s with less than 20 GB of hard disk.
Delete all laptops made a manufacturer that doesn’t make printers.
Manufacturer A buys manufacturer B. Change all products made by B so
they are now made by A.
For each PC, double the amount of RAM and add 20 GB to the amount of
hard disk.
For each laptop made by manufacturer B, add one inch to the screen size
and subtract $100 from the price.
Data Types
• The principal element in a table creation is a
pair consisting of an attribute and a type.
• The most common types are:
–
–
–
–
–
INT or INTEGER (synonyms).
REAL
FLOAT
CHAR(n ) = fixed-length string of n characters.
VARCHAR(n ) = variable-length string of up to n
characters.
– DATE
Example: Create Table
CREATE TABLE Movie(
title CHAR(20),
year INT,
length INT,
inColor CHAR(1),
studioName CHAR(20),
producerC INT,
PRIMARY KEY (title, year)
);
Oracle NUMBER
•
NUMBER(p,s), where:
– p is the precision, which in ORACLE is the total number of digits.
– s is the scale, which in ORACLE is the number of digits to the right of the
decimal point.
– If the scale is negative, the actual data is rounded to the specified number
of places to the left of the decimal point.
•
Examples
Actual Data
----------7456123.89
7456123.89
7456123.89
7456123.89
7456123.8
7456123.8
7456123.89
7456123.89
Specified as
-----------NUMBER
NUMBER(9)
NUMBER(9,2)
NUMBER(9,1)
NUMBER(6)
NUMBER(15,1)
NUMBER(7,-2)
NUMBER(7,2)
Stored as
--------7456123.89
7456124
7456123.89
7456123.9
exceeds precision
7456123.8
7456100
exceeds precision
CREATE TABLE A1( attrib NUMBER(3,2) );
INSERT INTO A1 VALUES(100);
What happens?
•
•
•
NUMBER(p) is equivalent to
NUMBER(p,0).
INT is a synonym for
NUMBER(38), i.e. NUMBER(38,0)
A NUMBER(5) is not any different
from a NUMBER(38) space-wise.
– The 5 is just an "edit", a
format, an integrity constraint.
It doesn’t affect the physical
storage at all.
In absence of precision and scale,
the default is the maximum range
and precision for an Oracle
number.
Dates and Times
• DATE and TIME are types in SQL.
• No TIME type in ORACLE, but DATE also keeps the time.
CREATE TABLE Movie(
title CHAR(20),
year INT,
length INT,
inColor CHAR(1),
studioName CHAR(20),
producerC INT,
my_date DATE DEFAULT SYSDATE,
PRIMARY KEY (title, year)
);
Getting a Date in/out
INSERT INTO Movie(title, year, length, inColor, studioName, producerC,
my_date)
VALUES('Godzilla', 1998, 120.45, 'C', 'Paramount', 123, '12-Feb-1998');
INSERT INTO Movie(title, year, length, inColor, studioName, producerC,
my_date)
VALUES('Pretty Woman', 1990, 120, 'C', 'Disney', 234, '13-09-90');
VALUES('Pretty Woman', 1990, 120, 'C', 'Disney', 234, '13-09-90')
*
ORA-01843: not a valid month
INSERT INTO Movie(title, year, length, inColor, studioName, producerC,
my_date)
VALUES('Pretty Woman', 1990, 120, 'C', 'Disney', 234, TO_DATE('1309-90', 'dd-mm-yy'));
Getting a Date in/out (II)
Getting the date and time out:
SELECT TO_CHAR(my_date, 'DD-MON-YYYY:HH:MI:SS')
FROM Movie;
For more info: http://www-db.stanford.edu/~ullman/fcdb/oracle/ortime.html
Adding/Deleting/Modifying Attributes
ALTER TABLE MovieExec ADD salary INT;
ALTER TABLE MovieExec ADD
phone CHAR(16) DEFAULT 'unlisted';
ALTER TABLE MovieExec DROP COLUMN phone;
ALTER TABLE MovieExec MODIFY phone CHAR(18);
Also in ORACLE:
ALTER TABLE starsIN RENAME COLUMN title TO movieTitle;
Views
• A view is a “virtual table,” a relation that is defined in terms
of the contents of other tables and views.
• Declare by:
CREATE VIEW <name> AS <query>;
• In contrast, a relation whose value is really stored in the
database is called a base table.
Example
CREATE VIEW DisneyMovie AS
SELECT title, year
FROM Movie
WHERE studioName = 'Disney';
Accessing a View
• Query a view as if it were a base table.
Examples
SELECT title
FROM DisneyMovie
WHERE year = 1973;
SELECT DISTINCT name
FROM DisneyMovie, MovieExec
WHERE producerc = cert;
View on more than one relation;
renaming the attributes
CREATE VIEW MovieProd(movieTitle, prodName) AS
SELECT title, name
FROM Movie, MovieExec
WHERE producerc = cert;
Same as:
CREATE VIEW MovieProd2 AS
SELECT title AS movieTitle, name AS prodName
FROM Movie, MovieExec
WHERE producerc = cert;
Updateable Views
Only when:
1. There is only one relation, say R, in the FROM clause
(of the query defining the view).
2.
There isn’t a subquery involving R in the WHERE
clause (of the query defining the view).
Not problem for ORACLE.
3.
The list in the SELECT clause includes enough
attributes that for every tuple inserted into the view, we
can fill the other attributes out with NULL or the default,
and have a tuple that will yield the inserted tuple in the
view.
This is only checked for views defined WITH CHECK OPTION.
CREATE VIEW ParamountMovie AS
SELECT title, year
FROM Movie
WHERE studioName = 'Paramount'
WITH CHECK OPTION;
INSERT INTO ParamountMovie
VALUES ('Star Trek', 1979);
This insertion will fail!
Why this insertion is not possible?
The rationale for this behavior is:
• The above insertion, were it allowed to get through, would insert a
tuple with NULL for studioName in the underlying Movie table.
• However, such a tuple doesn't satisfy the condition for being in the
DisneyMovie view!
• Thus, it shouldn't be allowed to get into the database through the
DisneyMovie view.
CREATE VIEW ParamountMovie2 AS
SELECT studioName, title, year
FROM Movie
WHERE studioName = 'Paramount'
WITH CHECK OPTION;
INSERT INTO ParamountMovie2
VALUES ('Paramount', 'Star Trek', 1979);
Now it succeeds. Why?