CMPE 180-38 Database Systems Workshop Review: May 2 – 16, 2017 Department of Computer Engineering San Jose State University Summer 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak
Database System Architecture Database system: A computer-based system that enables efficient interaction between users and information stored in a database. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Steps to Develop a Database It’s an iterative process! Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Database Requirements First and most critical step: Collect, define, and visualize the requirements. What data will the database hold and how? What will be the capabilities and functionalities of the database? Reliability, performance, and security issues? Use the requirements to model and implement the database and to create the front-end applications.
Conceptual Database Model Visualize the requirements. Use a conceptual data modeling technique. Example: Entity-relationship (ER) modeling Implementation independent: No dependencies on the logic of a particular database management system (DBMS). Example DBMS: Oracle, MySQL, etc. Blueprint for the logical model.
Logical Database Model Create the relational database model. Later: Non-relational NoSQL models. It’s usually straightforward to map an ER model to a relational model.
Physical Database Model The physical model is the actual database implementation. Use relational DBMS (RDBMS) software. Later: NoSQL systems Structured Query Language (SQL) commands to create, delete, modify, and query database structures.
Front-End Application Development End users generally do not access the database directly. Front-end applications Access the database directly. Provide end users with safe application-oriented interfaces to query and manipulate the data. Fulfill the end user’s requirements.
Operational vs. Analytical Databases Operational database Supports day-to-day operational business needs. Contains operational (transactional) information. Example: sales transactions Analytical database Supports analytical business tasks. Contains analytical information. Examples: usage patterns, sales trends, etc. Derived from operational information. Often associated with data warehousing.
Entities and Attributes Entity Represents a real-world concept. Examples: customer, product, store, event, etc. Data that the database stores. Attribute Characteristic of an entity that the database stores. Examples (for a customer): name, address, id, etc. A unique attribute of an entity has a value that is different for each entity instance.
Entities and Attributes, cont’d In an ER diagram (ERD), show an entity with a rectangle and its attributes with ovals. Underline the unique attribute. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Entities and Attributes, cont’d An entity can have multiple unique attributes. Each one is called a candidate key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Composite Attributes A composite attribute is composed of several attributes. Parenthesize the name of the composite attribute. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Composite Attributes, cont’d An entity’s unique attribute can be composite. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Multivalued Attributes An entity instance can have multiple values for an attribute. If the number of values is fixed, we can use separate attributes instead. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Derived Attributes The value of a derived attribute is not stored. It’s calculated from the values of the other attributes and additional data such as the current date. Show with a dashed oval. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Optional Attributes An optional attribute does not always have to have a value. Indicate with (O). Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Relationships Each entity in an ER diagram must be related to at least one other entity. Show a relationship with a diamond and connect the diamond to the entities that are part of the relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Relationship Cardinality Show cardinality (how many instances of an entity) with symbols at the end of the relationship lines. Maximum symbol closest to the entity. Minimum symbol further away. Zero, one, many Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Relationship Cardinality, cont’d Read each relationship in both directions in this order: rectangle diamond cardinality Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Types of Relationships One-to-one (1:1) One-to-many (1:M) Each employee is allotted at most one vehicle. Each vehicle is allotted to exactly one employee. Each region has located in it at least one (i.e., many) stores. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Types of Relationships, cont’d Many-to-many (M:N) Each employee is assigned to no or several (i.e., many) projects. Each project has assigned to it at least one (i.e., many) employee. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Exact Cardinalities Indicate exact cardinalities with parenthesized minimum and maximum values. Example: (2, 6) Use M for a non-specific minimum or maximum. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Relationship Attributes An relationship can also have attributes. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Unary Relationships In a unary relationship, an entity is involved in a relationship with itself. One instance has a relationship with another instance of the same entity. You can indicate the relationship role. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Multiple Relationships Two entities can have multiple relationships with each other. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Weak Entities A weak entity does not have its own unique attribute. It only has a partial key. Underline the partial key with dashes. Therefore, it must be associated with an owner entity via an identifying relationship. Indicate the weak entity and the identifying relationship with double borders. The partial key and the owner attribute’s unique attribute uniquely identifies the weak entity.
Weak Entities, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Associative Entities An associative entity is an alternate way to depict a many-to-many (M:N) relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Associative Entities, cont’d Associative entity for a unary M:N relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
ER Diagram Example Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Logical Database Model Map the ER diagram to a logical model represented as a relational schema. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Conditions for a Table to be a Relation Each column must have a name. Within a table, each column name must be unique. All values in each column must be from the same (predefined) domain. Within a table, each row must be unique. Within each row, each value in each column must be single-valued. Multiple values of the content represented by the column are not allowed in any rows of the table.
Relational vs. Non-Relational Tables Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Additional Properties for a Relational Table The order of columns is irrelevant. The order of rows is irrelevant.
Primary Key Each relation must have a primary key. A column or set of columns whose value uniquely identifies each row. Underline the primary key of the relational table. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Entities Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Entities, cont’d As mapped. Entity with a composite attribute. As seen by a front-end application. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Entities, cont’d Attribute with a composite primary key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Entities, cont’d Entity with an optional attribute. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Entity Integrity Constraint No primary key column of a relational table can have null (empty) values. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Entity Integrity Constraint, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Foreign Keys A foreign key is a column in a table that refers to a primary key column in another table. In a relational schema, draw an arrow from the foreign key to the corresponding primary key.
Mapping 1:M Relationships Mandatory participation on both sides. The foreign key on the M side of the 1:M relationship corresponds to the primary key on the 1 side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping 1:M Relationships, cont’d Optional participation on the 1 side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping 1:M Relationships, cont’d Optional participation on the M side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping 1:M Relationships, cont’d Rename a foreign key to better reflect the role of a relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping M:N Relationships Use the bridge relation BELONGSTO with two foreign keys. AKA: linking table or join table Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping M:N Relationships, cont’d Optional participation on both sides. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping M:N Relationships, cont’d with an attribute. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping 1:1 Relationships Map 1:1 relationships similarly to 1:M relationships. One table will have a foreign key pointing to the primary key of the other table. It can be an arbitrary choice of which table has the foreign key. Make the choice that is most intuitive or efficient.
Mapping 1:1 Relationships, cont’d Table VEHICLE has the foreign key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Referential Integrity Constraint The value of a foreign key must either: Match one of the values of the primary key in the referred table. Be null. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #1: ER Diagram Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #1: Relational Schema Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #1: Sample Data Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Candidate Keys Choose one of the candidate keys to be the primary key. Map the other as non-primary. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Candidate Keys, cont’d Generally choose a non-composite candidate key as the primary key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Multivalued Attributes Map the multivalued attribute as a new table. This becomes a 1:M relationship with the new table on the M side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Derived Attributes Do not map derived attributes. Sample data records. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 As seen by the front-end application.
Mapping Example #2: Attributes Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Unary 1:M Relationships The table contains a foreign key that corresponds to its own primary key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Unary M:N Relationships Use a bridge relation where both foreign keys refer to the primary key of the same table. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Unary 1:1 Relationships Map similarly to a unary 1:M relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Multiple Relationships Map each relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Weak Entities Map weak entities the same way you map regular entities. The resulting table has a composite primary key that is composed of: The partial identifier of the table. The foreign key corresponding to the primary key of the owner table.
Mapping Weak Entities, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 The APARTMENT table’s composite primary key consists of AptNo (the partial key) and BuildingID (the foreign key corresponding the the primary key of the owner BUILDING table).
Mapping Example #3: ER Diagram Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #3: Relational Schema Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #3: Sample Data Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #3: Sample Data, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Relational Database Constraints A constraint is a rule that a relational database must satisfy in order to be valid. Two types of constraints: implicit constraints user-defined constraints
Implicit RDB Constraints Each relation (table) in a relational schema must have a unique name. For each relation: Each column name must be unique. Each row must be unique. Each column of each row must be single-valued. Domain constraint: All values in a column must be from the same predefined domain. The column order is irrelevant. The row order is irrelevant.
Implicit RDB Constraints, cont’d Primary key constraint: Each relation must have a primary key (a column or set of columns) whose value is unique for each row. Entity integrity constraint: No primary key column can have a null value. Referential integrity constraint: In each row containing a foreign key, either the value of the foreign key must match one of the values of the primary key of the referred table, or the foreign key is null.
User-Defined RDB Constraints Optional attributes Mandatory foreign key Example: Each employee must report to a department. Exact cardinalities Example: A student can take at most 5 classes. Business rules Usually enforced by front-end applications. Example: Each organization must have both male and female students.
User-Defined RDB Constraints, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
ER Modeling and Relational Modeling Why not skip ER modeling and go directly to logical modeling (creating relational schemas)? ER modeling is better for visualizing requirements. Certain concepts can be visualized graphically only in an ER diagram. Every attribute appears only once in an ER diagram. A conceptual model (ER diagram) is better for communication and documentation.
Database Update Operations Insert operation Enter new data into a table. Delete operation Remove data from a table. Modify operation Change existing data in a table.
Update Anomalies Insertion anomaly Deletion anomaly Modification anomaly
A Table Containing Redundant Data Can you spot the data redundancies? Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
A Table Containing Redundant Data, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Update Anomalies, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Normalization Normalize tables to eliminate update anomalies. Normalization is based on the concept of functional dependencies.
Functional Dependencies Functional dependency: The value of one or more columns in each row of a table determines the value of another column in the same row. Write A B Column(s) A functionally determines column(s) B. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 ClientId ClientName
Functional Dependencies, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Functional Dependencies, cont’d (Set 1) CampaignMgrID CampaignMgrName But not the reverse! Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Functional Dependencies, cont’d (Set 2) ModeID Media, Range But neither Media nor Range determine ModeID. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Functional Dependencies, cont’d (Set 3) AdCampaignID AdCampaignName, StartDate, Duration, CampaignMgrId, CampaignMgrName Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Functional Dependencies, cont’d (Set 4) AdCampaignName AdCampaignID, StartDate, Duration, CampaignMgrID, CampaignMgrName Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Functional Dependencies, cont’d (Set 5) AdCampaignID, ModeID AdCampaignName, StartDate, Duration, CampaignMgrID, CampaignMgrName, Media, Range, BudgetPctg Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Functional Dependencies, cont’d (Set 6) AdCampaignName, ModeID AdCampaignID, StartDate, Duration, CampaignMgrID, CampaignMgrName, Media, Range, BudgetPctg Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Trivial Functional Dependencies Remove trivial function dependencies (FDs): A A A, B A, B A, B A
Augmented Functional Dependencies If A B, then remove augmented FDs such as A, C B Because replace with (Set 2) ModeID Media, Range (Set 3) AdCampaignID AdCampaignName, StartDate, Duration, CampaignMgrId, CampaignMgrName (Set 5) AdCampaignID, ModeID AdCampaignName, StartDate, Duration, CampaignMgrID, CampaignMgrName, Media, Range, BudgetPctg (Set 5) AdCampaignID, ModeID BudgetPctg
Equivalent Functional Dependencies Equivalent FDs: A B B A A B, X B A, X Y, A B, X Y, B A, X Remove all but one from each equivalence set.
Equivalent Functional Dependencies Because are equivalent: FD Sets 3 and 4 are equivalent: Remove Set 4 FD Sets 5 and 6 are equivalent: Remove Set 6 AdCampaignID AdCampaignName AdCampaignName AdCampaignID Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Streamlined Functional Dependencies Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Partial Functional Dependency A table column is functionally dependent on a component of a composite primary key. (Set 2) ModeID Media, Range (Set 3) AdCampaignID AdCampaignName, StartDate, Duration, CampaignMgrId, CampaignMgrName Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Full Key Functional Dependency A table column is functionally dependent on the primary key and not partially dependent on any separate component of the primary key. (Set 5) AdCampaignID, ModeID BudgetPctg Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Transitive Functional Dependency A nonkey column is functionally dependent on another nonkey column. CampaignMgrID CampaignMgrName Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Another Functional Dependencies Example Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Normalization Improve the design of database tables. Eliminate update anomalies. Three normal forms. First normal form (1NF) Second normal form (2NF) Third normal form (3NF) From lower to higher, each normal form has increasingly stricter conditions. Even higher normal forms mostly of theoretical value. Boyce-Codd (BCNF), 4NF, 5NF, domain key (DKNF).
First Normal Form (1NF) A table is in 1NF if: Each row is unique. All values in a column must be from the same predefined domain. No column in any row contains multiple values. In other words, any valid relational table is, by definition, in 1NF.
First Normal Form (1NF), cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Second Normal Form (2NF) A table is in 2NF if: It is in 1NF. It does not contain partial functional dependencies.
Second Normal Form (2NF), cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Third Normal Form (3NF) A table is in 3NF if: It is in 2NF. It does not contain transitive functional dependencies.
Third Normal Form (3NF), cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Third Normal Form (3NF), cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Third Normal Form (3NF), cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Third Normal Form (3NF), cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Another Normalization Example Original table Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Another Normalization Example, cont’d 2NF Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Another Normalization Example, cont’d 3NF Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Another Normalization Example, cont’d Normalized tables Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Normalization vs. Denormalization Normalization spreads data out over more tables. The result is slower performance. Sometimes it make sense to denormalize in order to improve performance.
Normalization vs. Denormalization, cont’d Original table Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Normalization vs. Denormalization, cont’d Suppose the query is to retrieve for each campaign: AdCampaignID, AdCampaignName, CampaignMgrID, CampaignMgrName, ModeID, Media, Range, and BudgetPctg. It is more efficient to retrieve this data from the original table than from the several normalized tables.
Normalization vs. Denormalization, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Structured Query Language (SQL) Data Definition Language (DDL) Create and modify the structure of a database. tables, indexes, constraints, etc. CREATE, ALTER, DROP Data Manipulation Language (DML) Insert, modify, delete, and retrieve data. INSERT INTO, UPDATE, DELETE, SELECT
Structured Query Language (SQL), cont’d Data Control Language (DCL) Facilitate data access control. Transaction Control Language (TCL) Manage database transactions.
CREATE TABLE Table name Column expressions Table constraints column name data type column constraints Table constraints referential integrity
Common SQL Data Types
SQL Syntax A semicolon at the end of an SQL command. SQL keywords, as well as the table and column names used in the SQL commands, are not case sensitive. For clarity and consistency, we will use all caps for SQL commands. An SQL statement can be written as one long line of text. For legibility reasons, SQL statements are usually written as multiple lines of text.
CREATE TABLE Examples Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
CREATE TABLE Examples, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
CREATE TABLE Examples, cont’d CREATE TABLE vendor ( vendorid CHAR(2) NOT NULL, vendorname VARCHAR(25) NOT NULL, PRIMARY KEY (vendorid) ); CREATE TABLE category ( categoryid CHAR(2) NOT NULL, categoryname VARCHAR(25) NOT NULL, PRIMARY KEY (categoryid) CREATE TABLE product ( productid CHAR(3) NOT NULL, productname VARCHAR(25) NOT NULL, productprice NUMERIC(7,2) NOT NULL, vendorid CHAR(2) NOT NULL, categoryid CHAR(2) NOT NULL, PRIMARY KEY (productid), FOREIGN KEY (vendorid) REFERENCES vendor(vendorid), FOREIGN KEY (categoryid) REFERENCES category(categoryid) data types column constraints table constraints
CREATE TABLE Examples, cont’d CREATE TABLE region ( regionid CHAR(1) NOT NULL, regionname VARCHAR(25) NOT NULL, PRIMARY KEY (regionid) ); CREATE TABLE store ( storeid VARCHAR(3) NOT NULL, storezip CHAR(5) NOT NULL, regionid CHAR(1) NOT NULL, PRIMARY KEY (storeid), FOREIGN KEY (regionid) REFERENCES region(regionid) CREATE TABLE customer ( customerid CHAR(7) NOT NULL, customername VARCHAR(15) NOT NULL, customerzip CHAR(5) NOT NULL, PRIMARY KEY (customerid)
CREATE TABLE Examples, cont’d CREATE TABLE salestransaction ( tid VARCHAR(8) NOT NULL, customerid CHAR(7) NOT NULL, storeid VARCHAR(3) NOT NULL, tdate DATE NOT NULL, PRIMARY KEY (tid), FOREIGN KEY (customerid) REFERENCES customer(customerid), FOREIGN KEY (storeid) REFERENCES store(storeid) ); CREATE TABLE soldvia ( productid CHAR(3) NOT NULL, tid VARCHAR(8) NOT NULL, noofitems INT NOT NULL, PRIMARY KEY (productid, tid), FOREIGN KEY (productid) REFERENCES product(productid), FOREIGN KEY (tid) REFERENCES salestransaction(tid) composite key
DROP TABLE Referential integrity prevents deletion of a primary key that is referenced by foreign keys. Invalid sequence: DROP TABLE region; DROP TABLE store; DROP TABLE salestransaction; DROP TABLE product; DROP TABLE vendor; DROP TABLE category; DROP TABLE customer; DROP TABLE soldvia; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
DROP TABLE, cont’d Valid sequence: DROP TABLE soldvia; DROP TABLE salestransaction; DROP TABLE store; DROP TABLE product; DROP TABLE vendor; DROP TABLE region; DROP TABLE category; DROP TABLE customer; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
INSERT INTO INSERT INTO vendor VALUES ('PG','Pacifica Gear'); INSERT INTO vendor VALUES ('MK','Mountain King'); INSERT INTO category VALUES ('CP','Camping'); INSERT INTO category VALUES ('FW','Footwear'); INSERT INTO product VALUES ('1X1','Zzz Bag',100,'PG','CP'); INSERT INTO product VALUES ('2X2','Easy Boot',70,'MK','FW'); INSERT INTO product VALUES ('3X3','Cosy Sock',15,'MK','FW'); INSERT INTO product VALUES ('4X4','Dura Boot',90,'PG','FW'); INSERT INTO product VALUES ('5X5','Tiny Tent',150,'MK','CP'); INSERT INTO product VALUES ('6X6','Biggy Tent',250,'MK','CP'); INSERT INTO region VALUES ('C','Chicagoland'); INSERT INTO region VALUES ('T','Tristate');
INSERT INTO, cont’d INSERT INTO store VALUES ('S1','60600','C'); INSERT INTO store VALUES ('S3','35400','T'); INSERT INTO customer VALUES ('1-2-333','Tina','60137'); INSERT INTO customer VALUES ('2-3-444','Tony','60611'); INSERT INTO customer VALUES ('3-4-555','Pam','35401'); INSERT INTO salestransaction VALUES ('T111','1-2-333','S1','01/Jan/2013'); INSERT INTO salestransaction VALUES ('T222','2-3-444','S2','01/Jan/2013'); INSERT INTO salestransaction VALUES ('T333','1-2-333','S3','02/Jan/2013'); INSERT INTO salestransaction VALUES ('T444','3-4-555','S3','02/Jan/2013'); INSERT INTO salestransaction VALUES ('T555','2-3-444','S3','02/Jan/2013');
INSERT INTO, cont’d INSERT INTO soldvia VALUES ('1X1','T111',1);
INSERT INTO, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
INSERT INTO, cont’d Explicitly specify column names: Insert more than one row per command: INSERT INTO soldvia(noofitems, tid, productid) VALUES (1, 'T555', '6x6'); INSERT INTO customer VALUES ('1-2-333','Tina','60137'), ('2-3-444','Tony','60611'), ('3-4-555','Pam','35401');
SELECT Retrieve data from database tables. The most common SQL command.
SELECT, cont’d Retrieve the entire contents of the Product table. SELECT * FROM product; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT, cont’d Retrieve only certain columns of the Product table. Projection operation SELECT productid, productprice FROM product; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT, cont’d Calculate a column (derived attribute) from the Product table. SELECT productid, productprice, 1.1 * productprice FROM product; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT DISTINCT Which vendor IDs are in the Product table? SELECT DISTINCT vendorid FROM product; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT WHERE Which products in the FW category have price less than or equal to $110? SELECT productid, productname, vendorid, productprice FROM product WHERE productprice <= 110 AND categoryid = 'FW'; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
MySQL Conditional Operators PHP and MySQL for Dynamic Web Sites, 4th ed. by Larry Ullman Peachpit Press, 2012 ISBN 978-0-321-78407-0
SELECT ORDER BY Sort products by product price. SELECT productid, productname, categoryid, productprice FROM product WHERE categoryid = 'FW' ORDER BY productprice; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT ORDER BY, cont’d Sort products by product price in descending order. SELECT productid, productname, categoryid, productprice FROM product WHERE categoryid = 'FW' ORDER BY productprice DESC; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT ORDER BY, cont’d Sort products first by category ID and then by product price. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 SELECT productid, productname, categoryid, productprice FROM product ORDER BY categoryid, productprice;
Limiting Query Results SELECT first_name, last_name FROM users ORDER BY registration_date DESC LIMIT 5; Also: Return n records starting with the ith record. Does not improve the query execution speed, since MySQL still has to match all the records. Reduces the number of returned records. Useful for “paging” the results. LIMIT i, n PHP and MySQL for Dynamic Web Sites, 4th ed. by Larry Ullman Peachpit Press, 2012 ISBN 978-0-321-78407-0
SELECT LIKE Which products have “Boot” in their name? SELECT * FROM product WHERE productname LIKE '%Boot%'; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT LIKE, cont’d String comparisons using wildcard characters: _ matches any single character % matches any zero or more characters mysql> select * from people; +-----+---------+---------+--------+--------+ | id | first | last | gender | salary | | 101 | Charles | Jones | M | 100000 | | 103 | Mary | Adams | F | 150000 | | 105 | Susan | Miller | F | 50000 | | 110 | Roger | Brown | M | 75000 | | 112 | Leslie | Adamson | F | 105000 | 5 rows in set (0.00 sec) mysql> select * from people -> where last like 'Adam%'; +-----+--------+---------+--------+--------+ | id | first | last | gender | salary | | 103 | Mary | Adams | F | 150000 | | 112 | Leslie | Adamson | F | 105000 | 2 rows in set (0.02 sec)
SELECT Aggregate Functions What is the average price of all products? SELECT AVG(productprice) FROM product; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT Aggregate Functions, cont’d How many products are there? SELECT COUNT(*) FROM product; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT Aggregate Functions, cont’d How many vendors supply the products? SELECT COUNT(DISTINCT vendorid) FROM product; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT Aggregate Functions, cont’d Aggregate functions COUNT, SUM, AVG, MIN, MAX SELECT COUNT(*), AVG(productprice), MIN(productprice), MAX(productprice) FROM product WHERE categoryid = 'CP'; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT GROUP BY What is the average price for each vendor? SELECT vendorid, COUNT(*), AVG(productprice) FROM product GROUP BY vendorid; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT GROUP BY, cont’d When a SELECT has aggregate functions, it cannot have individual columns unless the columns are in a GROUP BY clause. You can group by multiple columns. SELECT vendorid, COUNT(*), AVG(productprice) FROM product GROUP BY vendorid;
SELECT GROUP BY, cont’d Group by vendor, and then by category. SELECT vendorid, categoryid, COUNT(*), AVG(productprice) FROM product GROUP BY vendorid, categoryid; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT GROUP BY HAVING Group products $50 or more by vendor and category. Count and average price only if more than one per group. SELECT vendorid, categoryid, COUNT(*), AVG(productprice) FROM product WHERE productprice >= 50 GROUP BY vendorid, categoryid HAVING COUNT(*) > 1; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT GROUP BY HAVING, cont’d For products that sold more than 3 items in all sales transactions, what were the total number of items sold? SELECT productid, SUM(noofitems) FROM soldvia GROUP BY productid HAVING SUM(noofitems) > 3; The WHERE clause applies to records. The HAVING clause applies to groups. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT GROUP BY HAVING, cont’d For each product that was sold in more than one sales transaction, what is the number of transactions in which the product was sold? SELECT productid, COUNT(TID) FROM soldvia GROUP BY productid HAVING COUNT(TID) > 1; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Nested Queries An outer query makes use of the results from a nested (inner) query.
Nested Queries, cont’d Which products sell below the average price? SELECT productid, productname, productprice FROM product WHERE productprice < (SELECT AVG(productprice) FROM product); First, we need to calculate the average. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Nested Queries, cont’d Wrong: Aggregate functions can only appear within a SELECT clause or a HAVING clause. SELECT productid, productname, productprice FROM product WHERE productprice < AVG(productprice); Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT IN Which products have more than 3 items sold in all sales transactions? SELECT productid, productname, productprice FROM product WHERE productid IN (SELECT productid FROM soldvia GROUP BY productid HAVING SUM(noofitems) > 3); Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
SELECT IN, cont’d Which products were each sold in more than one sales transaction? SELECT productid, productname, productprice FROM product WHERE productid IN (SELECT productid FROM soldvia GROUP BY productid HAVING COUNT(*) > 1); Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Recall: Relational Algebra The mathematical theory behind database operations: project which columns? listed after the SELECT keyword select which rows? WHERE and HAVING clauses join Query multiple tables at the same time.
Joins What is the id, product name, vendor name, and price of each product? SELECT productid, productname, vendorname, productprice FROM product, vendor WHERE product.vendorid = vendor.vendorid; Qualified names Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Joins, cont’d Without a join condition, you get a Cartesian product. Each record of one table matched with every record from the other table.
Joins, cont’d SELECT * FROM product, vendor; Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Joins, cont’d Include the join condition WHERE product.vendorid = vendor.vendorid
Join Alias Use aliases anywhere within a query instead of the full table name. No effect on the query itself. Improve legibility. SELECT productid, productname, vendorname, productprice FROM product, vendor WHERE product.vendorid = vendor.vendorid; SELECT p.productid, p.productname, v.vendorname, p.productprice FROM product p, vendor v WHERE p.vendorid = v.vendorid;
Join Alias, cont’d Use aliases to rename columns in query results. SELECT p.productid pid, p.productname pname, v.vendorname vname, p.productprice pprice FROM product p, vendor v WHERE p.vendorid = v.vendorid; You cannot use aliased column names in the WHERE clause. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Joining Multiple Tables SELECT t.tid, t.tdate, p.productname, sv.noofitems AS quantity, (sv.noofitems * p.productprice) AS amount FROM product AS p, salestransaction AS t, soldvia AS sv WHERE sv.productid = p.productid AND sv.tid = t.tid ORDER BY t.tid; You can use AS to introduce an alias. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Oracle does not allow AS for table aliases.