| Date | 09/12/2022 |
| Topic | Physical Models |
| Professor | John DeMastri |
| Week | 2 |
Most widely used database model: Edgar Codd (1969)
Recent alternatives and variations:
Relational Database
A set of relations Relation Made up of 2 parts
- Instance: a table, with rows and columns
Rows = cardinality, #fields=degree/arity
- Schema: specifies name of relation, and the name and type of each column.
E.g. Students (sid: str, name: str, login: str, age: int, gpa: real)
Think of a relation as a set of rows or tuples
A relational database
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
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 a Relation (Table in SQL):
Create the admitsource relation:
CREATE TABLE admitsource(
ID int,
Code char(1),
Name varchar(50));
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;
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;
| Date | 09/13/2022 |
| Topic | MySQL Commands/ Lab 2 |
| Professor | John DeMastri |
| Week | 2 |
update admitsource set Code='9' where ID=9;
CREATE TABLE admitsource(
ID int,
Code char(1),
Name varchar(50),
PRIMARY KEY(ID)); // add primary key constraint
| Date | 09/14/2022 |
| Topic | Constraints |
| Professor | John DeMastri |
| Week | 2 |
...
FOREIGN KEY (AdmitSource) REFERENCES admitsource(ID));
P_Id int NOT NULL,
...
UNIQUE(Code),
NOT NULL(Name),
CHECK(P_Id>0) // check/domain constraints
...
P_Id int NOT NULL CHECK (P_Id>0),
CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
ALTER TABLE Persons ADD CHECK (P_Id>0)
ALTER TABLE Persons
ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
insert into admitsource(ID, Code, Name) values (1, 'X', 'xx');
Major Strength of the relational model:
Example single relation (entity, table) query:
select * from admitsource where Code <=5 order by ID
select Code, Name from admitsource where Code <= 5 order by ID
WHERE defines the constraint for the search
select chf.PatientID, a.ID, a.Code, a.Name from chf_train chf, admitsource a where chf.AdmitSource = a.ID and a.ID = 5;
...
PRIMARY KEY(ID, Code)
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%';
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;
| Date | 09/16/2022 |
| Topic | DML |
| Professor | John DeMastri |
| Week | 2 |
Primary functionality:
Additional functionality
SELECT [DISTINCT] select-list
FROM from-list
WHERE qualification
select CountryCode, Language from CountryLanguage where isOfficial='T';
select * from Country where Contine``` select * from city c;
### Single string compare
```nt like '%Asia%';
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';
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%';