CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

RELATIONAL DATABASES. Relational data Structure RELATION: Table with columns and rows ATTRIBUTE: Column of a relation DOMAIN: Set of allowable values.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Assignment Design Methodology A structured approach that uses procedures, techniques, tools, and documentation aids to support and facilitate the.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
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.
Assignment6-1 Assignment6-2 Due Wednesday, March 13 1.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Relational Model Stores data as tables –Each column contains values about the same attribute –Each column has a distinct name –Each row contains values.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
Database Architecture The Relational Database Model.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
CSC 240 (Blum)1 Data Definition Language Based on Chapter 6 of Database Systems (Connolly and Begg)
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Exam 2 Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
Chapter 6 SQL Data Definition Language Chapter 7 in Textbook.
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.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
CS 3630 Database Design and Implementation. Assignment 3 Style! Agreement between database designer and the client. UserName1_EasyDrive UserName2_EasyDrive.
1 General Form of the SELECT Statement SELECT [DISTINCT | ALL {* | col_expr [AS new_name] [,…]} FROMtable_name [,…] [,…] [WHERE condition] [GROUP BY col_list]
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Recap of SQL Lab no 8 Advance Database Management System.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
1 E-R Model (II) Keys To identify records in a table Candidate Key Primary Key Alternate Key Composite Key.
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
CS 3630 Database Design and Implementation. 2 Functions y = f(x) x1 = x2  f(x1) = f(x2) Same x value, then same function value. Yes, it’s a function!
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
Chapter Name SQL: Data Definition
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Sql DDL queries CS 260 Database Systems.
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.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Chapter 4 Practice Problem Solutions. A list (Bno, Street, Area,Tel_No, Fax_No) of branches located in Bay Area Branch (Bno, Street, Area, City, Pcode,
Data Definition Language
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
CS 3630 Database Design and Implementation. Null Value The value of an attribute could be NULL NOT known at the moment or NOT Applicable Example Cell.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
Teacher Workshop Database Design Pearson Education © 2014.
CS 3630 Database Design and Implementation
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
SQL: Schema Definition and Constraints Chapter 6 week 6
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
Minggu 5, Pertemuan 9 SQL: Data Definition
ORACLE SQL Developer & SQLPLUS Statements
The Relational Model Relational Data Model
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
CS4222 Principles of Database System
SQL data definition using Oracle
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Database Design: Relational Model
Presentation transcript:

CS 3630 Database Design and Implementation

Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…) 2

In what order to create the tables? Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…) Will it work? YES! Branch (Bno…) Staff (Sno…Bno) PropertyForRent (Pno…Ono) Owner (Ono…) Viewing (Rno, Pno, ViewDate…) Renter (Rno…) Will it work? NO! 3 Referential Integrity!

In what order to drop the tables? Viewing (Rno, Pno, ViewDate…) Staff (Sno…Bno) PropertyForRent (Pno…Ono) Owner (Ono…) Renter (Rno…) Branch (Bno…) Will it work? YES! Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…) Will it work? NO! 4 Referential Integrity!

Insert Rows The PK must be unique The foreign key value must exist in the parent table if FK is provided FK can be null, meaning not known at the time 5

Insert Rows Insert into Branch: no problem Insert into Staff: Cannot insert a staff with Bno being ‘B123’ if Branch table has no ‘B123’ in Bno column FK can be null, meaning not known at the time Branch BnoPhone… B101 B205 Staff SnoPhoneBno SG100B101 SG363Null SA200 6

Delete Rows No records in other tables referencing the record to be deleted. 7

Delete Rows Delete from Staff: no problem Delete from Branch: Delete branch 'B101' What about staff in 'B101‘? Branch BnoPhone… B205 Staff SnoPhoneBno SG100B101 SG363Null SA200B101 8

ANSI SQL Solutions In ANSI SQL, there are five choices No Action Cannot delete Set to Null Set to Default No Check No good Cascade Delete all staff in 'B101' from Staff table when deleting branch ‘B101’ Dangerous! 9

Oracle Solutions 10

Update Record Update table Staff ‘B101’ of SG100 to ‘B205’ New value must exist in Branch Update table Branch ‘B101’ to ‘B303’ Five choices in ANSI SQL, only No Action is implemented in Oracle Branch BnoPhone… B101 B205 Staff SnoPhoneBno SG100B101 SG363Null SA200B205 11

Integrity Rules: Constraints Column Constraints Table Constraints 12

Column Constraints Primary key Sno Char(4) Primary Key, Alternate Key SSN Char(9) Unique, Foreign Key BNo char(4) References Branch, -- when attribute has the same name BNo char(4) References Branch, -- even when FK and PK have different names BNo char(4) References Branch on Delete Cascade, -- Do NOT use “Foreign Key” in column constaints! 13

References Only for Foreign Key Cannot reference other attributes Even not AKs 14

Column Constraints Domain constraint Salary Number Check (Salary > and Salary < ), PType varchar2(6) Check (PType in ('House', 'Flat', 'Appt')), -- Strings are in single quotes, NOT double quotes Bno Char(4) Default 'B363' References Branch, Rent Float Check (Rent Between 200 and 400), -- between is Inclusive Required data LName Varchar2(20) Not Null, -- Can be specified only by column constraint 15

Column Constraints Create table Staff ( SNo char(4) Primary Key, Bno Char(4) Default 'B363' References Branch on Delete Cascade, FName Varchar2(20) Not Null, LName Varchar2(20) Not Null, -- assuming functions DateDiff and Now DOB Date Not Null Check (DateDiff(Year, Now, DOB) >= 16), Salary Number Check (Salary Between and ), SSN Char(9) Unique, Tel_No Char(12)); -- Primary Key, Unique, References should be the last constraint for a column -- Do not use Foreign key for column constraints, only use References 16

Table Constraints Constraints on one or more columns –Composite PK, AK, FK Cannot use Not Null in table constraints 17

Table Constraints Create table Staff ( SNo char(4), FName Varchar2(20) Not Null, LName Varchar2(20) Not Null, DOB Date, Salary Number default, BNo Char(4), Tel_No Char(12), SSN Char(11), Constraint PK_Staff Primary Key (SNo), Constraint Range_of_Salary Check (Salary between and ), Unique (SSN), Foreign Key (BNo) References Branch (BNo)); 18

Attribute Order in Foreign Key -- Primary key (c1, c2) for TableA Foreign Key (c1, c2) References TableA, -- Same order as PK Foreign Key (c2, c1) References TableA(c2, c1), -- Different order from PK Foreign Key (c2, c1) References TableA, -- Incorrect! 19

-- Composite PK Create Table Test3 ( C1 Char(5), C2 Varchar2(50), C3 Integer, C4 Date, Constraint Test3_PK Primary Key (C1, C3)); Desc Test3 pause; Create Table Test4 ( D1 VARCHAR2(25) Primary Key, D2 Char(5), D3 Integer, Constraint Test4_FK Foreign Key (D2, D3) references Test3); Desc Test4; PauseWorks. 20

-- Composite PK Create Table Test3 ( C1 Char(5), C2 Varchar2(50), C3 Integer, C4 Date, Constraint Test3_PK Primary Key (C1, C3)); Desc Test3 pause; Create Table Test4 ( D1 VARCHAR2(25) Primary Key, D2 Char(5), D3 Integer, Constraint Test4_FK Foreign Key (D3, D2) references Test3); Desc Test4; Pause Does Not Work. 21

-- Composite PK Create Table Test3 ( C1 Char(5), C2 Varchar2(50), C3 Integer, C4 Date, Constraint Test3_PK Primary Key (C1, C3)); Desc Test3 pause; Create Table Test4 ( D1 VARCHAR2(25) Primary Key, D2 Char(5), D3 Integer, Constraint Test4_FK Foreign Key (D3, D2) references Test3(C3, C1)); Desc Test4; PauseWorks. 22

UserName_Lab7.sql Name : Qi Yang -- UserName : YangQ -- Date : Course : CS Description: Creating tables -- Inserting records Drop Table test2; Drop Table test1; Create table test1... Desc Test1 Pause Create Table Test2... Desc test2 Pause Insert into test1... Commit; Select * From Test1;... 23

Assignment7 Table names and column names must be exactly the same as specified. 24

SQL Script File Using any text editor outside SQL*Plus File extension.SQL UserName_Lab7.Sql Multiple SQL commands Each command ends with ; 25

Running Script File Using either Start SQL> Start file_name_with_full_path Use Arrow Keys to get previous commands! 26