Week 2 Notes


Physical Models

Date 09/12/2022
Topic Physical Models
Professor John DeMastri
Week 2

Relational Model - Topics

Relational Model

Most widely used database model: Edgar Codd (1969)

Recent alternatives and variations:

Relational Model Definitions

Relational Database

A set of relations Relation Made up of 2 parts

Rows = cardinality, #fields=degree/arity

E.g. Students (sid: str, name: str, login: str, age: int, gpa: real)

A collection of relations with distinct relation names.

A collection of schemas for the relations in the database.

A collection of relation instances, one per relation schema in the database schema

Cardinality = Number of rows

Degree = Number of columns

The SQL Query Language

Need for standard since it is used by many vendor's standards:

SQL-86
SQL-89
SQL-92
SQL-99
SQL-2011

Consists of:

Creating Relations in SQL

Creating a Relation (Table in SQL):

Create the admitsource relation:

CREATE TABLE admitsource(
ID int,
Code char(1),
Name varchar(50));

Modifying Relations in SQL

Tuples are inserted using the INSERT statement

Insert one tuple (row) into the relation (table) admitsource:

INSERT INTO admitsource(ID, Code, Name) values (9, 'B', 'Transfer from student rehab');

Delete one or more rows from table admitsource with restriction:

DELETE FROM admitsource WHERE Code='B';

Delete all rows from table admitsource (with no restriction):

DELETE FROM admisource;

Loading relations in MySQL

Bulk insert from file:

load Data local Infile 'D:/path' into table
INFORMS.admitsource fields terminated by '\t' lines terminated by '\r\n'
IGNORE 1 LINES;

Bulk insert from another table (fields need to match):

insert into poly_point_map
select poplyid, p1 p
from indices;

MySQL Commands | Lab 2

Date 09/13/2022
Topic MySQL Commands/ Lab 2
Professor John DeMastri
Week 2
update admitsource set Code='9' where ID=9;

Integrity Constraints Over Relations

Key Constraints

Creating a relation with primary key constraint:

CREATE TABLE admitsource(
	ID int,
	Code char(1),
	Name varchar(50),
	PRIMARY KEY(ID)); // add primary key constraint

Constraints

Date 09/14/2022
Topic Constraints
Professor John DeMastri
Week 2

Foreign Key Constraints

...
FOREIGN KEY (AdmitSource) REFERENCES admitsource(ID));
P_Id int NOT NULL,
...
UNIQUE(Code),
NOT NULL(Name),
CHECK(P_Id>0) // check/domain constraints

Adding a null check along with a custom check

...
P_Id int NOT NULL CHECK (P_Id>0),

Adding a named constraint

CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')

Altering the table to add an anonymous check

ALTER TABLE Persons ADD CHECK (P_Id>0)

Altering the table to add a named check

ALTER TABLE Persons
ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')

Inserting values into the table "admitsource"

insert into admitsource(ID, Code, Name) values (1, 'X', 'xx');

Relational Query Languages

Major Strength of the relational model:

Example single relation (entity, table) query:

Find all admit sources with a code less than or equal to 5:

select * from admitsource where Code <=5 order by ID

To find just codes and names

select Code, Name from admitsource where Code <= 5 order by ID

WHERE defines the constraint for the search

Find all patients transferred from a skilled nursing facility

select chf.PatientID, a.ID, a.Code, a.Name from chf_train chf, admitsource a where chf.AdmitSource = a.ID and a.ID = 5;

Add a primary key with multiple column values

...
PRIMARY KEY(ID, Code)

Find all patients transferred from any: ("inner join")

select chf.PatientID, a.ID, a.Code, a.Name from chf_train chf, admitsource a where chf.AdmitSource = a.ID and a.Name like '%Transfer%';

Query Multiple Relations

Find all patients transferred from any facility ("inner join") with standard syntax

select chf.PatientID, a.ID, a.Code, a.Name from chf_train chf inner join admitsource a on (chf.AdmitSource = a.ID) 
where a.Name like '%Transfer%' limit 5;

DML

Date 09/16/2022
Topic DML
Professor John DeMastri
Week 2

Basic SQL Query

SELECT [DISTINCT] select-list
FROM from-list
WHERE qualification

List official language by country:

select CountryCode, Language from CountryLanguage where isOfficial='T';

Find all languages spoken by all countries

select * from Country where Contine``` select * from city c;

### Single string compare
```nt like '%Asia%';

More complex compare with multiple tables

select c.Name Country, cl.Language from CountryLanguage cl, Country c
where cl.CountryCode=c.Code and isOfficial='T';
select c.Name Country, cl.Language from CountryLanguage cl, Country c
where cl.CountryCode=c.Code
and isOfficial='T'
and Region='Caribbean';

Expressions and Strings in SELECT statement

SELECT Name Country, 
IF(INSTR(Region, 'Central Americal'), Region, Continent) AS Area,
TRUNCATE( SurfaceArea*(0.62137*0.62137), 2) 
AS 'Area in Miles Squared'
FROM Country 
WHERE Continent LIKE '%America%';