Database Insertions:
Insert Multiple Tuples from another Table
(SQL)
INSERT INTO tableA(key)
SELECT DISTINCT tableB.attr
FROM tableB
WHERE <condition></condition>
Use a subquery instead of the “VALUES” keyword
Can insert multiple tuples at once
Database Insertions:
General Form
INSERT INTO R(A1, …, An)
VALUES (V1, …, Vn)
Notes:
Example:
INSERT INTO Purchases( buyer, seller, product, store)
VALUES (‘Joe’, ‘Fred’, ‘clock’, ‘The Sharper Image’)
Database Deletions:
General Form
DELETE FROM table_name
WHERE <condition></condition>
Example:
DELETE FROM Purchase
WHERE seller = ‘Joe’ AND
product = ‘Brooklyn Bridge’
Database Update:
General Form
UPDATE table_name
SET column1 = value1, col2 = value2
WHERE some_column = some_value;
Views:
Definition
Views
Relations that are not physically stored.
Used to present different information to different users
Can be used as a table later in a session.
Database
View Creation
CREATE VIEW view_name AS <query></query>
Example:
CREATE VIEW Developers AS
SELECT name, project
FROM Employee
WHERE department = “Development”
Updating Views
When inserting into a view, entries on actual tables are updated
Example:
Setup: Developers(name, project) is a view of
Employee( ssn, name, dept, project, salary)
Update:
INSERT INTO Developers VALUES(“Joe”, “Optimizer”)
is equivalent to
INSERT INTO Employee
VALUES( NULL, “Joe”, NULL, “Optimizer”, NULL)
Constraints
vs
Triggers
Constraint
A relationship among data elements that the DBMS is required to enforce.
Ex: Key Constraints
Triggers
Are only executed when in specific condition occurs.
Easier to implement than complex constraints.
Ex: Only checked when a tuple is inserted
Kinds of Constraints
Constraints:
Defining Single Attribute Keys
and
Multiple Attribute Keys
Both done while creating tables
Defining Single Attribute Keys:
Use PRIMARY KEY or UNIQUE keywords after declaration of an attribute
example:
CREATE TABLE Beers(
name CHAR(20) PRIMARY KEY,
price FLOAT );
Defining Multiple Attribute Keys:
Precede the tuple with PRIMARY KEY within the variable list
example:
CREATE TABLE Sells(
bar CHAR(20), beer VARCHAR(20), price REAL,
PRIMARY KEY (bar, beer) );
Constraints:
Defining Foreign Keys
Use the keyword REFERENCES between attribute and other relation.
Either:
Referenced attributes must have been declared with PRIMARY KEY or UNIQUE in their own table
Enforcing Foreign Key Constraints:
Two Possible Violations
If there is a foreign-key constraint,
from relation R to relation S, two violations are possible:
Enforcing Foreign Key Constraints:
Handling an insertion/update violation of a
Foreign Key Constraint
The insertion or update must be rejected
Example:
Constraint: R=sells, S=beers,
sells(beer) references beers(name)
If an insertion is attempted to insert a beer in “sells” that is not in beers(name),
the operation is rejected.
Enforcing Foreign Key Constraints:
Handling Deletion/Update operations that
violate a Foreign Key Constraint
(Dangling Attribute)
3 Ways to Handle
Enforcing Foreign Key Constraints:
Choosing a Policy
Attribute Based Checks
Example:
CREATE TABLE sells(
bar CHAR(20),
beer CHAR(20),
price REAL CHECK( price <= 5.00 )
);
When are Attribute Checks performed?
Only when a value for that attribute is
inserted
or
updated
Triggers:
Motivation
Triggers let the user decide
when to check for any condition
Triggers:
ECA Rule
Event-Condition-Action Rule
Describes the structure of a trigger:
Triggers:
Syntax
CREATE TRIGGER trigger_name
(EVENT)
[BEFORE/AFTER] [INSERT/UPDATE/DELETE] ON table
[FOR EACH ROW / FOR EACH STATEMENT] [WHEN condition] (CONDITION)
BEGIN
statements; (ACTION)
END;
Triggers:
Possible Events
Can specify:
BEFORE or AFTER,
on the events:
INSERT
UPDATE
or
DELETE
Triggers:
Condition
Basics:
References:
Triggers:
Two Types
FOR EACH ROW trigger:
Trigger is fired for every affected tuple/row that is modified
FOR EACH STATEMENT trigger:
The trigger associated with a table is fired one time for each SQL statement,
regardless of how many tuples are modified
Note: SQLite does NOT support the FOR EACH STATEMENT type of trigger