Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D."— Presentation transcript:

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

2 Unit 5 Review: assignment Chapter 10 – Database Creation2

3 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)

4 Assignment question 3 Chapter 10 – Database Creation4

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

6 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

7 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

8 Chapter 10 – Database Creation8

9 9

10 10

11 Chapter 10 – Database Creation11

12 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.

13 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.

14 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.

15 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.

16 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.

17 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.

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

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

20 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.

21 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.

22 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.

23 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.

24 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.

25 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.

26 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.

27 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.

28 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.

29 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.

30 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.

31 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.

32 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.

33 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.

34 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.

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

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

37 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.

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

39 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.

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

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

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

43 Q & A Questions?


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

Similar presentations


Ads by Google