376a. Database Design Dept. of Computer Science Vassar College

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Database technology Lecture 2: Relational databases and SQL
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Database Design -- Basic SQL
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views.
Review: Application of Database Systems
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
Relational Algebra - Chapter (7th ed )
Onsdag The concepts in a relation data model SQL DDL DML.
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Structured Query Language
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
FEN Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 16 A First Course in Database Systems.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
The SQL Database Grammar
COP Introduction to Database Structures
CHAPTER 6 Basic SQL. CHAPTER 6 Basic SQL Chapter 6 Outline SQL Data Definition and Data Types Specifying Constraints in SQL Basic Retrieval Queries.
SQL: SchemaDefinition, Constraints, and Queries and Views
Chapter 4 Basic SQL.
Database Systems Basic SQL
Database Design The Relational Model Text Ch5
Retrieval Queries in SQL(DML)
376a. Database Design Dept. of Computer Science Vassar College
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
376a. Database Design Dept. of Computer Science Vassar College
Mapping ER Diagrams to Tables
11/9/2018.
Company Requirements.
CS4222 Principles of Database System
Session - 6 Sequence - 2 SQL: The Structured Query Language:
376a. Database Design Dept. of Computer Science Vassar College
Chapter 4 & 5 Basic SQL More SQL
Chapter 4 Basic SQL. Chapter 4 Basic SQL Chapter 4 Outline Overview of SQL SQL Data Definition (DDL) for Specifying a Relational Database Schema CREATE.
376a. Database Design Dept. of Computer Science Vassar College
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Review: Application of Database Systems
SQL-99: Schema Definition, Constraints, and Queries and Views
Chapter 2: Intro to Relational Model
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

376a. Database Design Dept. of Computer Science Vassar College http://www.cs.vassar.edu/~cs376 Class 7: Domain Relational Calculus and beginning SQL Prof. Billibon Yoshimi 11/7/2018

Housekeeping Reminder: Homework for Oct 2nd. 4.19, 7.20, 7.26, 9.11, 9.15, 9.20 (except h) Midterm: October 9th (Wednesday) Prof. Billibon Yoshimi 11/7/2018

So far… Should be able to draw ER and EER diagrams. Need to work on constraints. Relational data model. How to convert ER and EER to Relational Model. Understand tuples and tuple calculus {e.name, e.address | EMPLOYEE(e) and (d) (DEPARTMENT(d) and d.DNAME=“research” and d.DNUMBER=e.DNO) Prof. Billibon Yoshimi 11/7/2018

Safe expressions Expressions should return finite number of results. {t | not (EMPLOYEE(t)) } is not safe. Only considered safe if the results are from the domain of the range relation (right side). Not (EMPLOYEE(t)) has tuples from outside the EMPLOYEE(t) relation. Prof. Billibon Yoshimi 11/7/2018

One more calculus: Domain Relational Calculus Domain relational calculus used in query by example. Variables range of domains of attributes (instead of tuples.) E.g. {x1, x2..xn| COND(x1, x2..xn, ..xn+m} xi range of domain of attribute Ai Prof. Billibon Yoshimi 11/7/2018

Atoms are different Atom may be… 1. R(x1, x2, … xn) where r is a relation with degree n and each xi is a domain variable. In short hand R(x1 x2 … xn) no commas 2. xi. op xj. where op  { =, > , , <, , }. x’s are domain variables. 3. xi. op c where op  { =, > , , <, , } and xi is a domain variable. *Normally use lowercase l-z for domain vars Prof. Billibon Yoshimi 11/7/2018

Example Get birthdate and address of person named “John B. Smith” { uv | ( q) ( r) ( s) ( t) ( w) ( x) ( y) ( z) (EMPLOYEE (qrstwxyz) and q = ‘John’ and r=‘B.’ and s=‘Smith’)} Every attribute of EMPLOYEE is assigned a domain var. only U and V are free. Prof. Billibon Yoshimi 11/7/2018

Another way {q | EMPLOYEE( ‘John’,’B.’,’Smith’,t, u, v, w, x, y, z) } All variables are free. Prof. Billibon Yoshimi 11/7/2018

Example Name and address of everyone in research department {qsv | ( z) ( l) ( m) (EMPLOYEE(qrstuvwxyz) and DEPT(lmno) and l=‘research’ and m=z) EMPLOYEE(FNAME, MINIT, LNAME, SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN, DNO) DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE) Prof. Billibon Yoshimi 11/7/2018

Try a few For every project in Stafford, list the controlling manager’s name and birthdate. EMPLOYEE(FNAME, MINIT, LNAME, SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN, DNO) DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE) PROJECT(PNAME,PNUMBER, PLOCATION,DNUM) rstv|EI Eo Ep Eu , PROJECT(jklm) and DEPARTMENT(nopq) and EMPLOYEE (rstuvwxyza) and l = ‘Stafford’ and m=o and p=u Prof. Billibon Yoshimi 11/7/2018

Find employees with no dependents. EMPLOYEE(FNAME, MINIT, LNAME, SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN, DNO) DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE) PROJECT(PNAME,PNUMBER, PLOCATION,DNUM) WORKS_ON(ESSN,PNO,HOURS) DEPT_LOCATIONS(DNUMBER,DLOCATION) DEPENDENT(ESSN, DEPENDENT_NAME,SEX,BDATE,RELATIONSHIP) Prof. Billibon Yoshimi 11/7/2018

List names of all managers with one dependent. EMPLOYEE(FNAME, MINIT, LNAME, SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN, DNO) DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE) PROJECT(PNAME,PNUMBER, PLOCATION,DNUM) WORKS_ON(ESSN,PNO,HOURS) DEPT_LOCATIONS(DNUMBER,DLOCATION) DEPENDENT(ESSN, DEPENDENT_NAME,SEX,BDATE,RELATIONSHIP) Prof. Billibon Yoshimi 11/7/2018

SQL2 or SQL92 Related to relational algebra (types of operations) Used by most commercial RDBMS. Based on tuple calculus (declarative vs. procedural) Used to define data and manipulate data. Prof. Billibon Yoshimi 11/7/2018

Basic data types Table - relation Row - tuple Column - attribute Schema - name, tables, constraints, views, domains and authorization. Schemas allow multiple databases to exist on the same server. Prof. Billibon Yoshimi 11/7/2018

CREATE SCHEMA Used to create new schema E.g. CREATE SCHEMA MYCORPDB AUTHORIZATION YOSHIMI Creates a new table to hold relations (tables) for the schema MYCORPDB. The owner of the database is YOSHIMI. In MySQL use CREATE DATABASE no auth. Prof. Billibon Yoshimi 11/7/2018

DROP SCHEMA When you’re done with the database. In MySQL use DROP DATABASE <databasename> Prof. Billibon Yoshimi 11/7/2018

CREATE TABLE Used to create a new relation. CREATE TABLE EMPLOYEE ( FNAME VARCHAR(15) NOT NULL, MINIT CHAR, LNAME VARCHAR(15) NOT NULL, SSN CHAR(9) NOT NULL, BDATE DATE, ADDRESS VARCHAR(30), SEX CHAR, SALARY DECIMAL (10,2), SUPERSSN CHAR(9) DNO INT NOT NULL, PRIMARY KEY (SSN), FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE(SSN), FOREIGN KEY (DNO) REFERENCES DEPARTMEnT (DNUMBER)); Prof. Billibon Yoshimi 11/7/2018

CREATE TABLE Better way CREATE TABLE MYCORPDB.EMPLOYEE (associate the table with the schema) Prof. Billibon Yoshimi 11/7/2018

Basic data types INTEGER, INT or SMALLINT FLOAT, REAL, DOUBLE PRECISION DECIMAL( i, j) - total and fractional CHAR, CHARACTER VARCHAR DATE - YEAR, MONTH, DAY TIME - HOUR, MINUTE, SECOND TIMESTAMP - DATE+TIME+6digit fract sec (These are from p.387 of MySQL manual) Prof. Billibon Yoshimi 11/7/2018

Constraints on variables NOT NULL - must have a value DEFAULT <value> - if no value is specified, use the following value. Table constraints include PRIMARY KEY (attribute list) FOREIGN KEY (attribute) REFERENCES <foreign key> UNIQUE - specifies alternate key Label constraints using CONSTRAINT ESSPK PRIMARY KEY (SSN) Prof. Billibon Yoshimi 11/7/2018

Referential Integrity Through FOREIGN KEY. Referential triggered action (ON DELETE) (ON UPDATE): - SET NULL - CASCADE - SET DEFAULT Prof. Billibon Yoshimi 11/7/2018

Example EMPLOYEE( … SUPERSSN CHAR (9), FOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEE(SSN) ON DELETE SET NULL When supervisor is deleted, set this field to NULL. Prof. Billibon Yoshimi 11/7/2018

Example EMPLOYEE( … SUPERSSN CHAR (9), FOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEE(SSN) ON UPDATE CASCADE If SSN of manager is corrected, results propagate to manager’s workers. Prof. Billibon Yoshimi 11/7/2018

Use CASCADE For relationships like WORKS_ON, multi-valued attributes like DEPT_LOCATION and weak entities. Prof. Billibon Yoshimi 11/7/2018

Remember… Rows are not ordered. Attributes for a given relation have an implicit order. Prof. Billibon Yoshimi 11/7/2018

DROP TABLE When you’re finished with a table. VERY DANGEROUS. WILL destroy entire table. Prof. Billibon Yoshimi 11/7/2018

ALTER TABLE Used to add/remove/modify attributes (domains). Used to add/remove/modify constraints ALTER TABLE MYCORPDB.EMPLOYEE ADD PAYSCALE CHAR(3); Why can’t you have a NOT NULL constraint on a new attribute? Prof. Billibon Yoshimi 11/7/2018

Other ALTER TABLE operations ALTER TABLE EMPLOYEE DROP PAYSCALE CASCADE; ALTER TABLE EMPLOYEE DROP PAYSCALE DEFAULT; cancel the default value ALTER TABLE EMPLOYEE SET DEFAULT ‘001’ ALTER TABLE EMPLOYEE DROP CONSTRAINT EMPPK CASCADE; Prof. Billibon Yoshimi 11/7/2018

SELECT statement No relation to sigma SQL tables are not sets (they’re multi-sets). Use DISTINCT to regain set-like quality. Basically: SELECT <attribute list> FROM <list of tables> WHERE <condition list> Prof. Billibon Yoshimi 11/7/2018

Get the birthdate and address of employees with the name “John B Get the birthdate and address of employees with the name “John B. Smith” R.A. SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME=“JOHN” and MINIT=“B” and LNAME=“SMITH” Prof. Billibon Yoshimi 11/7/2018

Can use SELECT to do join operation too FNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DEPARTMENT=“RESEARCH” AND DNUMBER=DNO Print the firstname and address of all employees in the research department. Prof. Billibon Yoshimi 11/7/2018

Resolving ambiguous names Relationships may have same named attributes. Use relation.attribute to disambiguate. When using multiple instances of a relation in a SELECT, use aliases.. SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE as E, EMPLOYEE as S WHERE E.SUPERSSN = S.SSN Prof. Billibon Yoshimi 11/7/2018

Can also create attribute aliases, EMPLOYEE AS E(FN,MI,LN,SSN,BD,ADDR,S,SAL,SSSN,DNO) Prof. Billibon Yoshimi 11/7/2018

SELECT - FROM statements Unspecified where is *. If multiple relations are specified in FROM then CROSSPRODUCT Prof. Billibon Yoshimi 11/7/2018

Other modifiers to SELECT SELECT * FROM - WHERE * - Selects all attributes SELECT ALL x FROM WHERE - get all values, including duplicates SELECT DISTINCT * FROM WHERE - removes duplicates *EXPENSIVE* Prof. Billibon Yoshimi 11/7/2018

UNION, EXCEPT and INTERSECT operations (SELECT *) UNION (SELECT *) Sub sets should be union compatible, same attribute tuples, same ordering. Prof. Billibon Yoshimi 11/7/2018

Comparing strings In WHERE statements use LIKE WHERE NAME LIKE “%ITH%” % - replaces arbitrary numbers of characters _ - replaces a single character “_____5_____” In MySQL, use REGEX too. ^ - match beginning of line $- match end of line [bB] - match any one char in bracket * - zero or one instances of preceding thing Match anywhere in the input, unlike LIKE Prof. Billibon Yoshimi 11/7/2018

Operations on return values +,-,*,/ || is string append, Prof. Billibon Yoshimi 11/7/2018

SELECT FROM WHERE ORDER BY ORDER BY attribute ASC|DESC, attribute ASC|DESC By default it is in ascending order. Order on first attribute, then second,then third. Prof. Billibon Yoshimi 11/7/2018

More complex queries. Nexted queries WHERE X IN takes (SELECT as argument) Prof. Billibon Yoshimi 11/7/2018