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.

Slides:



Advertisements
Similar presentations
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
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.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Chapter 5 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
Database Systems Relational Model Concepts Toqir Ahmad Rana Database Management Systems 1 Lecture 17.
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
The Relational Model These slides are based on the slides of your text book.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
1 The Relational Model Instructor: Mohamed Eltabakh
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model.
The Relational Data Model and Relational Database Constraints
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 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Data Model and Relational Database Constraints by Pinar Senkul resources:
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
How to build ER diagrams
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
CMPT 258 Database Systems The Relationship Model (Chapter 3)
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.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Chapter 3 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
Chapter 10 SQL DDL.
COP Introduction to Database Structures
Relational Integrity Constraints
Instructor: Mohamed Eltabakh
The Relational Model Relational Data Model
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Database Design: Relational Model
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Instructor: Mohamed Eltabakh
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CS 405G: Introduction to Database Systems
Presentation transcript:

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 Tables30 mins8:30 1

Key Review Key –all possible combinations of columns Super Key –All possible key combinations that uniquely identify records Primary Key –The super key that we chose to uniquely identify each record Foreign Key –Links the table to another table’s Primary Key 2

QUIZ 3

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 Tables30 mins8:30 4

Term Review DBMS Relation / Entity / Table Row / Tuple / Record Column / Attribute Primary Key Foreign Key First Normal Form Domain NULL Values Keys Super Key Composite Key 5

66 Elmasri Company Database The company is organized into DEPARTMENTS. Each department has a name, number and an employee who manages the department –We keep track of the start date of the department manager –Departments can have multiple locations

77 Elmasri Company Database Each department controls a number of PROJECTs. Each project has a name, number and is located at a single location.

88 Elmasri Company Database For each EMPLOYEE, we store the social security number, address, salary, sex, and birthdate. Employees may have a supervisor –DIFFERENT FROM (and no connection to) manager Each employee works for one department but may work on several projects.

99 Elmasri Company Database We keep track of the number of hours per week that an employee currently works on each project. Each employee may have a number of DEPENDENTs. –For each dependent, we keep track of their name, sex, birthdate, and relationship to employee.

10 Elmasri Company Database What will be the different tables ? What will be the primary keys ? What will be the foreign keys ? An example instance

11 Elmasri COMPANY database schema

12 Elmasri database with FK: Figure 5.7

13 Elmasri Figure 5.6: Relational Instance

14 In class problem: relational schemas Database keeps track of student enrollment in courses and the books adopted for each course: STUDENT(SSN, Name, Major, Bdate) COURSE(Course#, Cname, Dept) ENROLL(SSN, Course#, Quarter, Grade) BOOK_ADOPTION(Course#, Quarter, Book_ISBN) TEXT(Book_ISBN, Book_Title, Publisher, Author)

15 In class problem: relational schemas Why is Quarter part of P.Key in Enroll ? Suppose Book_ISBN was part of P.K. in the BOOK_ADOPTION table –How would we interpret that ? Draw relational schema specifying foreign keys.

10 Min Break 16

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 Tables30 mins8:30 17

18 QBE/ SQL QBE : Query By Example –Visual approach to writing queries –Used in MS-Access & SQL Server SQL: Structured Query Language –Developed by IBM in the 1970s –Need for a standard since it is used by many vendors –Various revisions over the years

19 SQL DDL and DML DDL: data definition language –Creating tables –Altering tables –Deleting (dropping) tables DML: data manipulation language – Retrieving (selecting) data – Inserting, deleting, updating data

20 SQL DDL and DML Our plan of action: – SQL DDL – SQL DML – SQL DDL again

21 Creating Tables in SQL CREATE TABLE: Creates a new relation by giving it a name, and specifying each of its attributes and their data types (domain) (INTEGER, FLOAT, DECIMAL(i,j), CHAR(n), VARCHAR(n), DATE).

22 Creating Tables in SQL DBMS enforces domain: like type checking CREATE TABLE Students( name VARCHAR(20), st_num CHAR(2), class INTEGER, major VARCHAR(10))

23 CREATE TABLE NOT NULL can be specified on an attribute Can specify DEFAULT Can specify primary key, secondary keys, and foreign keys. (Secondary key is a key used for sorting data – sometimes called an INDEX)

24 CREATE TABLE CREATE TABLE DEPT ( Dname varchar(10) Not Null, DnumberintegerNot Null Default 1 Primary Key MgrSSNchar(9) Not Null References EMP(SSN), MgrStartDtdate);

25 Elmasri Figure 8.1: CREATE TABLE

26 Creating tables in MSSQL CREATE TABLE MyTable( SSN char(9) Primary Key, LName varchar(10) Not Null); Duplicate values not allowed in SSN –Why? Null value not allowed if Not Null SQL Server Online Books for CREATE TABLE

27 Composite P. Keys in MSSQL CREATE TABLE MyTable2 ( SSN char(9), LNAME varchar(10), CONSTRAINT [PK_MyFKTable] Primary Key ([SSN],[Lname])); Is this valid? –{ “001”, “Johnson” } –{ “001”, “Klemenz” } –{ “002”, “Johnson” }

28 Foreign Keys in MS SQL CREATE TABLE MyFKTable ( DName varchar(10) Not Null, DNumber integer Not NullPrimary Key, MgrSSN char(9) REFERENCES JUNK45(SSN)); Non-valid values : string for DNUMBER Foreign key violation

29 Integrity Constraints Integrity Constraint: condition that must be true for any instance of the database: – are specified when schema is defined. – are checked when relations are modified.

30 Integrity Constraints Legal Instance: of a relation is one that satisfies all specified Integrity Constraints – DBMS should not allow illegal instances. Since the DBMS checks Integrity Constraints, stored data better captures real-world meaning. – Also avoids data entry errors

31 Integrity constraints Domain Constraint: value has to be from domain Key Constraint: key has to be unique Entity Integrity Constraint: no part of a key can be null. Referential Integrity Constraint: foreign key value must be either null or match the primary key value.

Design Considerations Why are integrity checks good? Why are integrity checks bad? 32

33 Referential Integrity Violations If change made to referencing table which leads to referential integrity violation, what to do? –Eg: added non-existent employee’s dependent Have to reject If change made to referenced table which leads to referential integrity violation, what to do? –Eg: employee 123 leaving the company, what to do about 123’s dependent information ? In this case, SQL provides different options – i.e. can specify what to do.

34 REF. INTEGRITY OPTIONS ON DELETE/UPDATE If there is a referential integrity violation on a delete or update in referenced table, SQL supports NO ACTION/REJECT: this is default SET DEFAULT: default value spec. earlier SET NULL : not possible if NOT NULL CASCADE: will delete or update in the referencing table We look at our own example –Similar to Elmasri Fig. 7.1b example

35 REF. INTEGRITY OPTIONS ON DELETE/UPDATE CREATE TABLE DEPT (DNamevarchar(10) Not Null Unique, DNumber integer Not Null Primary Key, MgrSSNchar(9) Not Null References EMP(SSN) ON DELETE NO ACTION ON UPDATE CASCADE, MgrStartDtchar(9));

36 REF. INTEGRITY OPTIONS ON DELETE/UPDATE CREATE TABLE EMP ( EName varchar(30) Not Null, ESSNchar(9) Primary Key, BirthDtdate, DNOint default 1 References Dept(Dnumber) On Delete Set Default On Update Cascade, SuperSSNchar(9) References EMP(SSN) On Delete SET Null On Update Cascade);

37 Date/Time Data Types SQL has DATE, TIME, and TIMESTAMP data types DATE: Made up of year-month-day in the format yyyy-mm-dd TIME: Made up of hour:minute:second in the format hh:mm:ss TIMESTAMP: Has both DATE and TIME components

38 Modifying and Deleting Tables Alter TableAlter LinkAlter Link Modifies tables Drop TableDrop LinkDrop Link Deletes tables Please read the SQL Server books online for more info

10 Min Break 39

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 Tables30 mins8:30 40

41 Lab Install MS SQL Create a database Create a table Show how to manually add data Add primary key to table Show how to add multiple primary keys to table Add an “auto-number” column (Right click in design view) Relationships and link the tables Show Script database to show how to create / view the SQL