Intro to Database Systems and Relational Model George Kollios Boston University (based on the slides by Feifei Li, U of Utah)
About the course – Administrivia n Instructor: – George Kollios, gkollios@cs.bu.edu MCS 104, Tue 12:30-2:00 and Wed 1:00-2:30 n Teaching Fellow: – Xiao Zhou, zhouxiao@bu.edu – Ugrad Lab, 730 Coom Ave, Room 302 Thu 3:15-5:15 and Fri 9:00-11:00 n Home Page: – http://www.cs.bu.edu/fac/gkollios/cs660f19 Check frequently! Syllabus, schedule, assignments, announcements… – Piazza site for discussions, etc – Blackboard from Student link for grades
Textbook Raghu Ramakrishnan and Johannes Gehrke, "Database Management Systems", McGraw-Hill, Third Edition. 2002.
Grading (Tentative) (Tentative Grading) ■ Written Homeworks: 15% ■ Midterm: 20% ■ Final: 25% ■ Programming Assignments: 40% ■ See next
Workload n Set of Projects: – “SimpleDB” projects from MIT/UW – Done individually or in pairs – Java-based implementations of key DBMS functions – 5(or 4) project phases: • Files, Operators, Optimizer, Transactions, Recovery n Some Written Homeworks n Exams – 1 Midterm & 1 Final
Why Study Databases?? Big Data Revolution: “Between the dawn of civilization and 2003, we only created five exabytes of information; now we’re creating that amount every two days.” Eric Schmidt, Google
“Big Data” Buzz
“Big Data” Buzz Amount of Stored Data By Sector (in Petabytes, 2009) 966 1000 848 900 800 715 700 619 600 500 434 364 400 269 300 227 200 100 0 g t s g e l n n n i n a n n r o o e a t i o i i i r k e t t m c i a a u t n R h a c t t n a t r c c u r l o a B a e i d p n f e v u E s u o H n n m G a a m M r T o C Source: "Big Data: The Next Frontier for Innovation, Competition and Productivity." US Bureau of Labor Statistics | McKinsley Global Institute Analysis
DB System Landscape https://db-engines.com/en/ranking 9
What’s a database management system (DBMS)? 10
What’s a database management system (DBMS)? Application Interface SQL Interpreter Query Evaluator Plan Plan Plan Optimizer Executor Generator Data Access Crash Recovery Security File & Index Buffer Transaction Manager Manager Concurrency Manager Manager Control Database System & Metadata Data Index Files Files Files 11
Highlights n Why use a DBMS? OS provides RAM and disk – Concurrency – Recovery – Abstraction, Data Independence – Query Languages (declarative language: specify what you want, but not how to do it) – Efficiency (for most tasks) – Security – Data Integrity 12
Data Models n DBMS models real world n Data Model is the link between user’s view of the world and bits stored in DBMS n Many models exist n We will concentrate on the Relational Model Students(sid: string, name: string, login: string, age: integer, gpa:real) 10101 11101 13
Why Study the Relational Model? n Most widely used model for structured data. – Vendors: IBM DB2, Microsoft SQL Server, Oracle, MySQL, PostgreSQL, etc. n Have motivated the design of many other data models for structured/semi-structured data. XML, JSON – n Is the basis for understanding many models for unstructured data. 14
ANSI/SPARC Model Users n Views describe how users see the data. View 1 View 2 View 3 n Conceptual schema Conceptual Schema defines logical structure Physical Schema n Physical schema DB describes the files and indexes used.
Relational Database: Definitions n Relational database: a set of relations . n Relation: made up of 2 parts: – Instance : a table , with rows and columns. • #rows = cardinality – Schema : specifies name of relation, plus name and type of each column. • E.g. Students( sid : string, name : string, login : string, age : integer, gpa : real) • #fields = degree / arity • type: specify the domain of a field (the set of possible valid values a field may take) n Can think of a relation as a set of rows or tuples . – i.e., all rows are distinct 16
Example Instance of Students Relation sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 • Cardinality = 3, arity = 5 , all rows distinct • Do all values in each column of a relation instance have to be distinct? 17
SQL - A language for Relational DBs n SQL: standard language for DBMS, structured query language n Data Definition Language (DDL) – create, modify, delete relations – specify constraints – administer users, security, etc. n Data Manipulation Language (DML) – Specify queries to find tuples that satisfy criteria – add, modify, remove tuples 18
SQL Overview CREATE TABLE <name> ( <field> <domain>, … ) n INSERT INTO <name> (<field names>) n VALUES (<field values>) DELETE FROM <name> n WHERE <condition> UPDATE <name> n SET <field name> = <value> WHERE <condition> SELECT <fields> n FROM <name> WHERE <condition> 19
Creating Relations in SQL n Creates the Students relation. n Note: the type (domain) of each field is specified, and enforced by the DBMS – whenever tuples are added or modified. n Another example: the Enrolled table holds information about courses students take. CREATE TABLE Students CREATE TABLE Enrolled (sid CHAR(20), (sid CHAR(20), name CHAR(20), cid CHAR(20), login CHAR(10), grade CHAR(2)) age INTEGER, gpa FLOAT) 20
Adding and Deleting Tuples n Can insert a single tuple using: INSERT INTO Students (sid, name, login, age, gpa) VALUES (‘53688’, ‘Smith’, ‘smith@ee’, 18, 3.2) n Can delete a single (or a set of) tuple(s) using: DELETE FROM Students S WHERE S.name = ‘Smith’ ✦ Powerful variants of these commands are available; more later! 21
Keys n Keys are a way to associate tuples in different relations n Keys are one form of integrity constraint (IC) Enrolled Students sid cid grade sid name login age gpa 53666 Carnatic101 C 53666 Jones jones@cs 18 3.4 53666 Reggae203 B 53688 Smith smith@eecs 18 3.2 53650 Topology112 A 53650 Smith smith@math 19 3.8 53666 History105 B 22
Primary Keys n A set of fields is a superkey if: – No two distinct tuples can have same values in all key fields n A set of fields is a key for a relation if : – It is a superkey – No proper subset of the fields is a superkey n >1 key for a relation? one of the keys is chosen (by DBA) to be the primary key . – n E.g. – sid is a key for Students. – What about name ? – The set { sid, gpa } is a superkey. 23
Primary and Candidate Keys in SQL n Possibly many candidate keys (specified using UNIQUE ), one of which is chosen as the primary key . CREATE TABLE Enrolled (sid CHAR(20) “For a given student and course, there is a single grade.” cid CHAR(20), vs. grade CHAR(2), PRIMARY KEY (sid,cid)) “Students can take only one course, and receive a single grade for that course; further, no two students in a CREATE TABLE Enrolled course receive the same grade.” (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY(sid,cid), UNIQUE (cid, grade)) 24
Foreign Keys n A Foreign Key is a field whose values are keys in another relation (think about them as “pointers”). Enrolled Students sid cid grade sid name login age gpa 53666 Carnatic101 C 53666 Jones jones@cs 18 3.4 53666 Reggae203 B 53688 Smith smith@eecs 18 3.2 53650 Topology112 A 53650 Smith smith@math 19 3.8 53666 History105 B 25
Foreign Keys, Referential Integrity n Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. – Must correspond to primary key of the second relation. – Like a `logical pointer’. n E.g. sid is a foreign key referring to Students: – Enrolled( sid : string, cid : string, grade : string) – If all foreign key constraints are enforced, referential integrity is achieved (i.e., no dangling references.) 26
Foreign Keys in SQL n Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ) Enrolled Students sid cid grade sid name login age gpa 53666 Carnatic101 C 53666 Jones jones@cs 18 3.4 53666 Reggae203 B 53688 Smith smith@eecs 18 3.2 53650 Topology112 A 53650 Smith smith@math 19 3.8 53666 History105 B 27
Integrity Constraints (ICs) n IC: condition that must be true for any instance of the database; e.g., domain constraints (i.e., types). – ICs are specified when schema is defined. – ICs are checked when relations are modified. n A legal instance of a relation is one that satisfies all specified ICs. – DBMS should not allow illegal instances. n If the DBMS checks ICs, stored data is more faithful to real-world meaning. – Avoids data entry errors, too! 28
Recommend
More recommend