Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Slides:



Advertisements
Similar presentations
MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
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.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Overview Relational Databases and SQL Pertemuan 1 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Oracle Data Definition Language (DDL)
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Data Modeling and Database Design
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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,
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
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.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Chapter 3: Relational Databases
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
SQL Basics Review Reviewing what we’ve learned so far…….
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.
In this session, you will learn to: Manage databases Manage tables Objectives.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
Fundamental of Database Systems
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Insert, Update and the rest…
Module 5: Implementing Data Integrity by Using Constraints
STRUCTURED QUERY LANGUAGE
The Basics of Data Manipulation
SQL-1 Week 8-9.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Unit 5 Review: assignment Chapter 10 – Database Creation2

Develop a dependency-preserving solution that eliminates this normal form violation. Chapter 10 – Database Creation3 CAR_DESC (Model, #cylinders, origin); CAR_TAX (Origin, Tax); CAR_FEE (#cylinders, Fee)

Assignment question 3 Chapter 10 – Database Creation4

Develop a solution that eliminates the normal form violations. Chapter 10 – Database Creation5

Unit 6 Objectives Demonstrate the ability to create, alter, and drop tables using the SQL-92 data definition language Demonstrate the ability to populate tables using the SQL-92 INSERT, DELETE, and UPDATE statements Chapter 1 – Database Systems: Architecture and Components 6

Unit 6 To-Do List Complete the reading Textbook and Web Participate in the discussion board 30 points Attend the introductory seminar or complete FLA quiz 20 points Complete the unit assignment 50 points Chapter 1 – Database Systems: Architecture and Components 7

Chapter 10 – Database Creation8

9

10

Chapter 10 – Database Creation11

Key Concepts The CREATE TABLE statement is the SQL/DDL statement used to establish the structure of a table. The ALTER TABLE statement is the SQL/DDL statement used to modify the structure of a table. The DROP TABLE statement is the SQL/DDL statement used to remove a table (structure as well as content) from the database. Chapter 1 – Database Systems: Architecture and Components 12 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Key concepts Type constraint-A declarative constraint on a column of a table that specifies the type of data (e.g., number, string, date/time, and interval) and the domain associated with the column. CONSTRAINT- constraint_name phrase-Used to define the name of a constraint. Attribute(column)-level constraint- A constraint imposed on a single column in a table. Tuple(row)-level constraint- A constraint imposed on set of columns in a table. Chapter 10 – Database Creation13 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Key concepts CHECK clause-To restrict column or domain values. PRIMARY KEY clause- Used in the specification of an entity integrity constraint. UNIQUE clause-Used in the specification of an alternate key (uniqueness constraint). FOREIGN KEY clause-Used to define a tuple-level referential integrity constraint. REFERENCES clause- Used to define both a tuple- level and a column-level referential integrity constraint. Chapter 10 – Database Creation14 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation15 Overview Structured Query Language (SQL) Data Definition Language (DDL) Data Manipulation Language (DML) Data Control Language (DCL) SQL is a standard developed by ANSI and ISO SQL statements are case-insensitive The book uses SQL-92 (SQL2) and SQL-99 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation16 Database Creation The principal task includes creation and modification of the database tables and other related structures enforcement of integrity constraints population of the database tables specification of security and authorizations as a means of access control and transaction processing controls From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation17 Data Definition in SQL Three major constructs: Create Alter Drop Applied to: Table Domain Schema Views From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation18 Example From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation19 Example (continued) From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation20 CREATE TABLE Commands: Box 1 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation21 CREATE TABLE Syntax CREATE TABLE table_name (comma delimited list of table-elements); where table_name is a user supplied name for the base TABLE and each table- element in the list is either a column-definition or a constraint-definition. The basic syntax for a column-definition is of the form: column_name representation [default-definition] [column-constraint list] where column_name is a user supplied name for a COLUMN representation specifies the relevant data type or alternatively the predefined domain-name the optional (indicated by [ ] ) default-definition specifies a default value for the COLUMN which overrides any default value specified in the domain definition, if applicable. In the absence of an explicit default definition (directly or via the domain definition), the implicit assumption of a NULL value for the default prevails. The default-definition is of the form: DEFAULT ( literal | niladic-function| NULL ) the optional (indicated by [ ] ) column-constraint list specifies constraint-definition for the column. The basic syntax for a constraint- definition follows the form: [CONSTRAINT constraint_name ] constraint-definition From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation22 SQL Attribute Domains SQL-92 Data Types From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation23 SQL Attribute Domains SQL-92 Data Types (continued) From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation24 SQL-92 Data Types (continued) From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation25 CREATE TABLE: Things to Remember Data types supported by SQL-92 are grouped under Number, String, Date/time & Interval. However, DBMS vendors often build their own data types based on these four categories. Make sure that all attributes names are unique Some terms are reserved words, i.e., SQL uses these words in its language (e.g., order, grant, etc.) Attribute-level constraint vs. table-level constraint From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation26 CREATE TABLE Commands: Box 2 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation27 Still Missing… Pat_name, Pat_age, Pat_admit_dt, Med_code and Med_qty_onhand are mandatory attributes – i.e., cannot have null values in any tuple Med_code is the alternate key since Med_name has been chosen as the primary key of medication table Participation of order in the Placed_for relationship is total Participation of patient in the Placed_for relationship is partial Participation of order in the is_for relationship is partial Participation of medication in the is_for relationship is total The deletion rule for the Is_for relationship is restrict The deletion rule for the Placed_for relationship is cascade [Pat_wing, Pat_room] is a molecular attribute [Pat_wing, Pat_room, Pat_bed] is a molecular attribute Note: The cardinality ratio of the form (1, n) in a relationship type is implicitly captured in the DDL specification via the foreign key constraint. Any (1, 1) cardinality ratio can be implemented using the UNIQUE constraint definition. From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation28 CREATE TABLE Commands: Box 3 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation29 ALTER TABLE Syntax where table_name is the name of the base TABLE being altered and the actions possible are: –Actions pertaining to alteration of a column via the syntax: ADD [ COLUMN ] column_definition ALTER [ COLUMN ] column_name { SET default-definition | DROP DEFAULT } –(Adds the default-definition or replaces an existing default-definition) or –(removes an existing default-definition) DROP [ COLUMN ] column_name { RESTRICT | CASCADE } Or –Alteration of a previously specified table constraint in force via the syntax ADD table_constraint_definition –(Permits addition to existing set of constraints, if any) DROP CONSTRAINT constraint_name { RESTRICT | CASCADE } –(Removes the named constraint) From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation30 ALTER TABLE Examples Suppose we want to add a column to the base table patient to store the phone number of every patient. The DDL/SQL code to do this is: ALTER TABLE patient ADD Pat_phone# char (10); In order to delete the column from the base table, the following code applies: ALTER TABLE patient DROP Pat_phone# CASCADE; or ALTER TABLE patient DROP Pat_phone# RESTRICT; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation31 ALTER TABLE Examples (continued) Suppose we want to specify a default value of $3.00 for the unit price of all medications. This can be done as follows: ALTER TABLE medication ALTER Med_unitprice SET DEFAULT 3.00; The default clause can be removed by: ALTER TABLE medication ALTER Med_unitprice DROP DEFAULT; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation32 Best Practices Method 1: Pat_age smallint not null Method 2: Pat_age smallint constraint nn_Patage not null, CONSTRAINT chk_age CHECK (Pat_age IN (1 through 90)) Method 3: Pat_age smallint not null CHECK (Pat_age IN (1 through 90)) From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation33 Best Practices (continued) If we decide to permit null value for Pat_age, in Method 3, the whole column definition has to be re-specified. In Method 2, we simply drop the “not null” constraint as shown below: ALTER TABLE patient DROP CONSTRAINT nn_patage CASCADE;or ALTER TABLE patient DROP CONSTRAINT nn_patage RESTRICT; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation34 DROP TABLE Where table_name is the name for the base TABLE being deleted and the drop behaviors possible are: CASCADE or RESTRICT. Example: DROP TABLE medication CASCADE; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation35 Data Population Using SQL From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation36 Sample Tables From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation37 INSERT Single-row INSERT – adds a single row of data to a table Multi-row INSERT – extracts rows of data from another part of the database and adds them to a table. From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation38 INSERT Example From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation39 DELETE The DELETE statement removes selected rows of data from a single table Syntax: Since the WHERE clause in a DELETE statement is optional, a DELETE statement of the form DELETE FROM can be used to delete all rows in a table. When used in this manner, while the target table has no rows after execution of the deletion, the table still exists and new rows can still be inserted into the table with the INSERT statement. To erase the table definition from the database, the DROP TABLE statement must be used. From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation40 DELETE Example From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 10 – Database Creation41 DELETE Example (continued) From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Summary Create, alter, and drop tables using the SQL-92 data definition language Populate tables using the SQL-92 INSERT, DELETE, and UPDATE statements

Q & A Questions?