Database Systems {week 01}
description
Transcript of Database Systems {week 01}
![Page 1: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/1.jpg)
Database Systems{week 01}
Rensselaer Polytechnic InstituteCSCI-4380 – Database SystemsDavid Goldschmidt, Ph.D.
![Page 2: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/2.jpg)
What is a database?
What is a Database? A collection of organized information
that persists over a long period of time Such information (or data) is managed
by a DataBase Management System
DBMS: Software used to create and manage large
amounts of data efficiently and securely
![Page 3: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/3.jpg)
DBMS characteristics
A typical DBMS is expected to: Provide a means by which users can create
databases and specify their schemas Give users the ability to query the database
(and efficiently return results) Store huge amounts of data Support durability and reliability, recovering
from failures and errors Control user access to data (i.e. security)
![Page 4: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/4.jpg)
Data modeling
Database schemas abstract elements of the real world; to do so, we use a data model
A data model describes: Structure of the data Operations on the data (reads and
writes) Constraints on the data
![Page 5: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/5.jpg)
Take the ACID test (if you dare) Transactional (and other) databases
must meet the ACID test: Atomicity: all-or-nothing execution Consistency: relationships between data
elements must adhere to defined constraints
Isolation: each transaction appears as if it occurs without other database activity
Durability: the data itself is durable in the sense that the data must never be lost
![Page 6: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/6.jpg)
Relational data modeling A relation is a two-dimensional data
structure that consists of a set of attributes and (zero or more) tuples or rows of data
Each attribute takes only simple values i.e. strings, numbers, boolean values, dates, etc.
attribute1 attribute2 attribute3 attribute4Mark Goldberg AE 108 6Mukkai Krishnamoort
hyLally 305 30
Sibel Adali Lally 313 6
firstname lastname office nuttiness
![Page 7: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/7.jpg)
Relational data modeling The relation schema consists of:
The name of the relation The set of attributes The name (and type) of each attribute Other constraints
An example:Profs( firstname, lastname, office, nuttiness )
![Page 8: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/8.jpg)
Relational data modeling A relation contains a set of tuples
Each tuple contains values for all the attributes in the relation schema that are drawn from the domain of that attribute
Example tuples:
possibly empty
( 'Mark', 'Goldberg', 'AE 108', 6 )( 'Mukkai', 'Krishnamoorthy', 'Lally 305', 30 )( 'Sibel', 'Adali', 'Lally 313', 6 )
As a set, tuple order is not significant.
![Page 9: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/9.jpg)
Keys
A key for a relation is a set of attributessuch that no pair of tuples has the same value for the key
Examples: Social Security Number RIN (Rensselaer ID Number) First and last name (would this one
work???)
Given the key, we can query the relation
and expect exactly one result (or zero!).
Profs( firstname, lastname, office, nuttiness )
![Page 10: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/10.jpg)
Primary keys
In practice, keys are used to improve efficiency of queries using such keys And note that not all keys provide
“uniqueness”
Since relations may have multiple keys,a primary key is selected The primary key might be a separate
(unused?) numeric fieldWhat would be the use of this?
![Page 11: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/11.jpg)
Using SQL to store relations To store a relation, we can use SQL
to create a table in a relational database system
Example attribute (data) types include: CHAR, VARCHAR, TEXT BIT, INT, INTEGER, FLOAT, DOUBLE, REAL DECIMAL, NUMERIC DATE, DATETIME, TIMESTAMP BLOB, MEDIUMBLOB, LONGBLOB
![Page 12: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/12.jpg)
Table creation
create table tablename ( attribute1_name attribute1_type, attribute2_name attribute2_type, ... attributeN_name attributeN_type, constraints);
might also have table options here
other attribute constraintsmight be included here
![Page 13: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/13.jpg)
Example table creation
create table student( id int, name varchar(255), major char(4), gender char(1), dob date, constraint student_pk primary key (id));
student_pk is an arbitrary name
why did we specifythese attribute types?
![Page 14: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/14.jpg)
Modifying table structure Removing a table from the schema:
Adding a new attribute to a table:
Removing an attribute from a table:
drop table tablename;truncate table tablename;
what’s the difference?
alter table tablename add attributename attributetype;
alter table tablename drop attributename;
![Page 15: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/15.jpg)
Relational algebra and sets Relational algebra consists of a set
of simple operators that can be used to query the database
Each operator takes as input two relations and produces as output a relation Think of a relation as a set of tuples The input and output relations all must
have the same schema
![Page 16: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/16.jpg)
Set operators
Given two relations R and S that havethe same schema, set operators include: Union:▪ R S { tuples that are in R or S (or both) }
Intersection:▪ R S { tuples that are in both R and S }
Set difference:▪ R – S { tuples that are in R but not in S }remember that a set
does not contain duplicates
![Page 17: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/17.jpg)
Projection (attribute filtering) The projection of a relation R on
attributes A1, A2, ..., An is given by: A1,...,An (R) = { t | t is a tuple in R
and t only contains values for attributes A1, A2, ..., An iff the schema of R contains attributes A1, A2, ..., An }
We use projection to remove existing attributes from R (by selecting a subset of them)
duplicate tuples are omitted!
![Page 18: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/18.jpg)
Selection
Find and select all tuples from relation Rthat satisfy some set of conditions Forms the basis of querying a database The selection C (R) is based on Boolean
condition C over attributes of relation R Example conditions include:▪ A = e, A > e, A >= e, A < e, A <= e, A <> e▪ A1 = A2, A1 <> A2 ▪ Any combination of conditions using AND, OR,
NOT
A, A1, and A2 are attributese is a constant or expression
![Page 19: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/19.jpg)
Selection
Selection selects a subset of tuples in relation R (with the schema unchanged) C (R) = { t | t is a tuple in R and t
satisfies the condition C on relation R }
Selection conditions can only refer toattributes in the given relation R
For conditions spanning multiple relations, we first must combine those relations (i.e. join)
![Page 20: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/20.jpg)
Homework – Part One
Download and install both the Oracle and MySQL database packages noted on the course Web site
![Page 21: Database Systems {week 01}](https://reader035.fdocuments.in/reader035/viewer/2022062501/568166ff550346895ddb668f/html5/thumbnails/21.jpg)
Homework – Part Two
Design a full schemato store informationabout celebrities,including: Basic information Relationships (e.g. marriages, flings,
etc.) Issues (e.g. drugs, affairs, addictions,
etc.)