Database Design Ack: Amanda Gerdes
Overview Learning the Language What’s A Database? How Do We Talk About Databases? Database Models “Flat file” Database Relational Database Relational Database Design Step One: Determine The Information You Need Step Two: Assign Your Attributes To Entities Step Three: Assign Primary Keys To Your Entities Step Four: Determine The Relationships Between Your Entities Step Five: Link Your Entities Together
Learning the Language What’s A Database? Database: a collection of related records that is typically:... structured according to a schema … managed through a Database Management System (DBMS) Oooookay… that’s, uh… that’s just great. What the hell’s a schema? Or a DBMS? Schema: the overall structure of the database which defines data represented in the database and the relationships between them Database Management System (DBMS): a collection of software programs which aids in the storage, manipulation, reporting, management, and control of data that: … is a “front end” for the database, hiding potentially complex data relationships from the users … includes things like sales and order tracking systems and airline reservation systems … is often just called a “database” Hey, great. That’s not confusing at all.
Learning the Language How Do We Talk About Databases? idnameplanet 1LukeTatooine 2LeiaAlderaan 3HanCorellia 4ChewbaccaKashyyyk 5YodaCoruscant Table: A set of data elements organized into horizontal rows (records) and vertical columns (fields). Each database can have multiple tables! A table has a specific number of columns but can have any number of rows. Record: A row in the database representing a single set of related data. All records have the same structure (in this case id, name, and planet) and each element of a record is related. Field: A column in the database representing a set of a type of data. In this case, the fields are id, name, and planet and each column provides a list of id numbers, names, and planets respectively. We will often see data types associated with fields. For example, our id field expects numbers.
Database Models The Flat Model Flat Model: A two-dimensional array of data elements, consisting of only data and delimiters Delimiter: A sequence of one or more characters that specifies the boundary between fields and between records
Database Models The Flat Model (continued) idnameplanet 1LukeTatooine 2LeiaAlderaan 3HanCorellia 4ChewbaccaKashyyyk 5YodaCoruscant “1”,”Luke”,”Tatooine” “2”,”Leia”,”Alderaan” “3”,”Han”,”Corellia” “4”,”Chewbacca”,”Kashyyyk” “5”,”Yoda”,”Coruscant” 1-Luke-Tatooine/2-Leia- Alderaan/3-Han-Corellia/4- Chewbacca-Kashyyyk/5-Yoda- Coruscant/ Tab-Separated Record delimiter: Newline Field delimiter: Tab Comma-Separated Record delimiter: Newline Field delimiter: Comma Other Record delimiter: / Field delimiter: -
Database Models The Flat Model (continued) Limitations The “flat file” database works when you’re collecting non- redundant data. Luke Skywalker only has one home planet; we’re not going to see him in our database twice. But what if we want to know what planets he’s visited? Suddenly, things are less simple… idnameplanet_fromplanet_to 1LukeTatooineYavin 4 2LeiaAlderaanHoth 3LukeTatooineHoth 4HanCorelliaHoth 5LukeTatooineDagobah 6LeiaAlderaanBespin 7HanCorelliaBespin 8LukeTatooineBespin 9LukeTatooine 10LukeTatooineEndor
Database Models The Relational Model Relational Model: A database consisting of multiple tables, each similar to a “flat file” table, which are linked together through common fields Pieces of the Relational Database Entity: A table in a relational database. Attribute: A field in a relational database. Key: An attribute used to identify, sort, or link records in a database. Primary Key: A key that is a unique identifier for a single, specific record contained in a specified field. Foreign Key: A key that acts as a “link” between different tables/entities.
Database Models The Relational Model (continued) Relational Model: … continued … Database Normalization: The process of restructuring a database to eliminate redundancy and organize data efficiently. Third Normal Form (3NF): The most common method of structuring a database; method where any field that is not directly related to the key is moved to a separate table. Product Number (primary key) Product NameManufacturerManufacturer Address 1337Nimbus 2000Nimbus Racing Broom CompanyDiagon Alley 4242Clone ArmyThe Galactic EmpireKamino, Outer Rim 6666The One RingDark Lord SauronMordor, Middle Earth 6789Nimbus 2001Nimbus Racing Broom CompanyDiagon Alley Product # (primary key) Product NameManufacturer 1337Nimbus 2000Nimbus Racing Broom Company 4242Clone ArmyThe Galactic Empire 6666The One RingDark Lord Sauron 6789Nimbus 2001Nimbus Racing Broom Company ManufacturerManufacturer Address Nimbus Racing Broom CompanyDiagon Alley The Galactic EmpireKamino, Outer Rim Dark Lord SauronMordor, Middle Earth Product InformationManufacturer Information
Relational Database Design Our Example We’re part of a college advising team. Our job is to meet with students prior to the start of the semester, review their academic performance with them, go over pending degree requirements, and then counsel them about their best options for the coming semester.
Relational Database Design Step One: Determine The Information You Need Make a list of the information we need. ▪ Student’s name ▪ Student’s address ▪ Names of classes ▪ Professors who are teaching the classes ▪ Contact info for professors ▪ Class start and end times ▪ Class locations ▪ Class descriptions ▪ Student’s major/emphasis ▪ Course number ▪ Student ID number
Relational Database Design Step Two: Assign Your Attributes To Your Entities Now that we know what information we need, our next job is to organize the data pieces (fields/attributes) into categories (tables/entities). ▪ Student’s name ▪ Student’s address ▪ Names of classes ▪ Professors who are teaching the classes ▪ Contact info for professors ▪ Class start and end times ▪ Class locations ▪ Class descriptions ▪ Student’s major/emphasis ▪ Course number ▪ Student’s grades Info About StudentsInfo About ClassesInfo About Professors Tables/Entities Fields/Attributes
Relational Database Design Step Three: Assign Primary Keys To Your Entities We now have three separate tables (entities) for our database. We now need a way to uniquely identify each row in each of the tables. To determine a primary key, let’s do the following: Determine if we already have a field that uniquely identifies each row. Each value in this field must be completely unique within the entire table. If we do: This is called a natural key and we can use it as our primary key. If we don’t: We will have to add a field to our database to hold a unique identifier we can use as a primary key. We can either add a data field (e.g., a SSN acting as an ID number) or create what we call a surrogate key; this is just a “fake” value that has no real-world meaning to our row but will uniquely identify it (e.g., having the DBMS generate a random number or auto- increment number for every record)
Relational Database Design Step Three (Continued) STUDENT TABLE Student IDStudent Last Name Student First Name Address PotterHarryCupboard Under the Stairs 4 Privet Drive Little Whinging, Surrey GreyJeanXavier Institute for Higher Learning 1407 Greymalkin Lane Salem Center, New York BagginsFrodoBag End Hobbiton, Westfarthing of the Shire CLASSES TABLE Course #Course NameStart TimeEnd TimeDescriptionLocation ABC-109-1Lightsaber Combat10:00am3:00pmPrereq: Light Side100 Building A ABC-109-2Lightsaber Combat7:00pm10:00pmPrereq: Light Side104 Building A ABC-111-1Divination4:00pm7:00pmTea leaves, palmistry102 Building A ABC-112-1Tengwar 1017:00pm10:00pmIntro to Tengwar language100 Building A PROFESSORS TABLE Professor Last Name Professor First Name Phone Number Address Professor ID
Relational Database Design Step Four: Determine The Relationships Between Your Entities Now that we have our entities more or less in order, our next job is to figure out how they are related to each other. One-to-One (1:1). One instance of Entity A relates to one instance of Entity B. Example: Every employee gets his/her own office; each employee gets only one office, each office houses only one employee One-to-Many (1:N). One instance of Entity A relates to many instances of Entity B. Example: Employees are assigned to different regional centers; each employee works at only one regional center, each regional center houses multiple employees Many-to-Many (M:N). Many instances of Entity A relate to many instances of Entity B. Example: Employees are assigned to projects; one employee has many projects and each project has many employees assigned to it
Relational Database Design Step Four: (continued) Determine the relationship for our entities: Professors to Classes One professor can have ________ class(es). One class can have ________ professor(s). Students to Classes One student can have ________ class(es). One class can have ________ student(s). MANY ONE Many-To-Many One-To-Many
Relational Database Design Step Five: Link Your Entities Together Now that we have our tables and we know how they are related to each other… we must link them together. Linking Tables Related tables are linked via a common field The primary key in Entity A will appear as a foreign key in Entity B; we can then match Entity A’s primary key to Entity B’s foreign key for a match. Product # (primary key) Product NameManufacturer 1337Nimbus 2000Nimbus Racing Broom Company 4242Clone ArmyThe Galactic Empire 6666The One RingDark Lord Sauron 6789Nimbus 2001Nimbus Racing Broom Company ManufacturerManufacturer Address Nimbus Racing Broom CompanyDiagon Alley The Galactic EmpireKamino, Outer Rim Dark Lord SauronMordor, Middle Earth Foreign KeyPrimary Key=
Relational Database Design Step Five: (continued) Guidelines for Linking Tables One-to-One: Either table can hold the foreign key. One-to-Many: The “Many” table should hold the foreign key. Many-to-Many: We will create a joint table that contains the primary keys for both tables.
Relational Database Design Step Five: (continued) Linking Professors and Classes Relationship: ONE Professor to MANY Classes Keep in Mind: Foreign key belongs in the MANY table Foreign key in one table = Primary Key of the other table CLASSES TABLE Course # (primary key) Course NameStart TimeEnd TimeDescriptionLocation ABC-109-1Lightsaber Combat10:00am3:00pm{..}100 Building A ABC-109-2Lightsaber Combat7:00pm10:00pm{..}104 Building A ABC-111-1Divination4:00pm7:00pm{..}102 Building A ABC-112-1Tengwar 1017:00pm10:00pm{..}100 Building A PROFESSORS TABLE Professor ID (primary key) Professor Last Name Professor First Name Phone Number Address Professor ID (foreign key)
Relational Database Design Step Five: (continued) Linking Students and Classes Relationship: MANY Students to MANY Classes With a Many-to-Many relationship, we have to create a joint table! A Joint Table should include: A primary key Foreign keys to each of the tables we’re linking Any data fields that are specific to the joint relationship (e.g., a student’s grade depends on both the student and the class)
Relational Database Design Step Five: (continued) CLASSES TABLE Course # (primary key) Course NameStart TimeEnd TimeDescriptionLocationProfessor ID (foreign key) ABC-109-1Lightsaber Combat10:00am3:00pm{..}100 Building A ABC-109-2Lightsaber Combat7:00pm10:00pm{..}104 Building A ABC-111-1Divination4:00pm7:00pm{..}102 Building A ABC-112-1Tengwar 1017:00pm10:00pm{..}100 Building A STUDENT TABLE Student ID (primary key) Student Last Name Student First Name Address PotterHarryCupboard Under the Stairs 4 Privet Drive Little Whinging, Surrey GreyJeanXavier Institute for Higher Learning 1407 Greymalkin Lane Salem Center, New York BagginsFrodoBag End Hobbiton, Westfarthing of the Shire STUDENTS-CLASSES TABLE ID Number (primary key) Student ID (foreign key) Course # (foreign key) Grade ABC-109-1B ABC-111-1B ABC-109-2C ABC-112-1A ABC-109-1A ABC-111-1B-
Relational Database Design Summary View CLASSES TABLE Course # (primary key) Course NameStart TimeEnd TimeDescriptionLocationProfessor ID (foreign key) ABC-109-1Lightsaber Combat10:00am3:00pm{..}100 Building A ABC-109-2Lightsaber Combat7:00pm10:00pm{..}104 Building A ABC-111-1Divination4:00pm7:00pm{..}102 Building A ABC-112-1Tengwar 1017:00pm10:00pm{..}100 Building A STUDENT TABLE Student ID (primary key) Student Last Name Student First Name Address PotterHarry{…} GreyJean{…} BagginsFrodo{…} STUDENTS-CLASSES TABLE ID Number (primary key) Student ID (foreign key) Course # (foreign key) Grade ABC-109-1B ABC-111-1B ABC-109-2C ABC-112-1A ABC-109-1A ABC-111-1B- PROFESSORS TABLE Professor ID (primary key) Professor Last Name Professor First Name Phone Number Address JonesIndiana {…} MiyagiKesuke {…} YodaMaster {…} KrabappleEdna {…}