Chapter 10 – Database Creation1 Data Modeling and Database Design Chapter 10: Database Creation.
Post on 12-Jan-2016
234 Views
Preview:
Transcript
Chapter 10 – Database Creation 1
Data Modeling and Database Design
Chapter 10:Database Creation
Chapter 10 – Database Creation 2
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
Chapter 10 – Database Creation 3
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
Chapter 10 – Database Creation 4
Data Definition in SQL
Three major constructs:• Create• Alter• Drop
Applied to:• Table• Domain • Schema• Views
Chapter 10 – Database Creation 5
Example
Chapter 10 – Database Creation 6
Example (continued)
Chapter 10 – Database Creation 7
CREATE TABLE Commands: Box 1
Chapter 10 – Database Creation 8
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
Chapter 10 – Database Creation 9
SQL Attribute Domains
SQL-92 Data Types
Chapter 10 – Database Creation 10
SQL Attribute Domains
SQL-92 Data Types (continued)
Chapter 10 – Database Creation 11
SQL-92 Data Types (continued)
Chapter 10 – Database Creation 12
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
Chapter 10 – Database Creation 13
CREATE TABLE Commands: Box 2
Chapter 10 – Database Creation 14
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.
Chapter 10 – Database Creation 15
CREATE TABLE Commands: Box 3
Chapter 10 – Database Creation 16
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)
Chapter 10 – Database Creation 17
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;
Chapter 10 – Database Creation 18
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;
Chapter 10 – Database Creation 19
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))
Chapter 10 – Database Creation 20
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; orALTER TABLE patient DROP CONSTRAINT nn_patage RESTRICT;
Chapter 10 – Database Creation 21
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;
Chapter 10 – Database Creation 22
CREATE, ALTER, DROP DOMAIN
• The SQL-92 standard provides for the formal specification of a Domain. A domain specification can be used to define a constraint over one or more columns of a table with a formal name so that the domain name can be used wherever that constraint is applicable.
Chapter 10 – Database Creation 23
CREATE DOMAIN Syntax
• where – domain_name is a user supplied name for the domain – the optional default-definition specifies a default value for the
DOMAIN. In the absence of an explicit default definition, the domain has no default value
– the optional domain-constraint-definition is of the form: [ CONSTRAINT constraint_name ] CHECK (VALUE (conditional-expression))
Chapter 10 – Database Creation 24
Example 1
• Specify a domain to capture integer values 1, 2 and 3 with a default value of 2:CREATE DOMAIN measure smallint DEFAULT 2 CONSTRAINT chk_measure CHECK (VALUE IN (1,2,3));
• Since [ CONSTRAINT constraint_name ] is optional the above statement can also be stated as:CREATE DOMAIN measure smallint DEFAULT 2 CHECK (VALUE IN (1,2,3));
• Now, for instance, the column definition in the orders table Ord_dosage smallint DEFAULT 2 CONSTRAINT chk_dosage CHECK (Ord_dosage BETWEEN 1 AND 3) can be coded asOrd_dosage measure
• Likewise, Ord_freq smallint DEFAULT 1 CONSTRAINT chk_freq CHECK (Ord_freq IN (1, 2, 3)) can also be coded asOrd_freq measure DEFAULT 1
Chapter 10 – Database Creation 25
Example 1 (continued)
Chapter 10 – Database Creation 26
Example 2
• Specify a domain with mandatory values for the U.S. postal abbreviation for the list of states OH, PA, IL, IN, KY, WV, MI. Also, designate OH as the default state.
CREATE DOMAIN valid_states CHAR (2) DEFAULT ‘OH’ CONSTRAINT nn_states CHECK (VALUE IS NOT NULL) CONSTRAINT chk_states CHECK ( VALUE IN (‘OH’, ‘PA’, ‘IL’, ‘IN’, ‘KY’, ‘WV’, ‘MI’));
Chapter 10 – Database Creation 27
Example 3
• Remove the ‘not null’ constraint from the domain specification for valid_states.
ALTER DOMAIN valid_states DROP CONSTRAINT nn_states;
• Note: Observe that had we not named the constraint, the only recourse we have is to get rid of the complete domain definition and create it over again.
Chapter 10 – Database Creation 28
ALTER DOMAIN Syntax• ALTER DOMAIN domain_name action;
where domain_name is the name of the DOMAIN being altered and the actions possible are:
– Alteration of domain default value via the syntax:• SET default-definition
– (Adds the default-definition or replaces an existing default-definition), or
• DROP DEFAULT – (Copies the default definition to the columns defined on the
domain which do not have explicitly specified default values of their own and then removes default-definition from the domain definition.
Or– Alteration of a previously specified domain constraint in force via the syntax
• ADD domain_constraint_definition– (Permits addition to existing set of constraints, if any)
• DROP CONSTRAINT constraint_name– (Removes the named constraint)
Chapter 10 – Database Creation 29
Example 4
• Suppose we want to add Maryland (MD) and Virginia (VA) to the domain valid_states. This is done by adding a new constraint to the domain valid_states as shown below:
ALTER DOMAIN valid_states CONSTRAINT chk_2more CHECK (VALUE IN (‘MD’, ‘VA’));
Chapter 10 – Database Creation 30
Example 5
• Assuming that the scripts in Example 1 and Box 4 have been executed, remove the default value for the DOMAIN measure. As of now, the default for measure is 2, the default for Ord_dosage is 2, and the default for ord_freq is 1. The DDL/SQL syntax follows:
ALTER DOMAIN measure DROP DEFAULT;
Chapter 10 – Database Creation 31
Example 6
• Change the default value of State in the valid_states domain to PA.ALTER DOMAIN valid_states SET DEFAULT ‘PA’;
• A domain definition can be eliminated using the DDL/SQL syntax:DROP DOMAIN domain_name CASCADE| RESTRICT
• Note: With the restrict option any attempt to drop a domain definition fails if any column in the database tables, views and/or integrity constraints references the domain name. With the cascade option, however, dropping a domain entails dropping of any referencing views and integrity constraints only. The columns referencing the domain are not dropped. Instead the domain constraints are effectively converted into base table constraints and are attached to every base table that has a column(s) defined on the domain.
Chapter 10 – Database Creation 32
Example 7
• The domain named measure is no longer needed
DROP DOMAIN measure RESTRICT;
• Since the columns Ord_dosage and Ord_freq are defined on the DOMAIN measure, the DROP DOMAIN operation will fail with the drop behavior specification of RESTRICT. Therefore:
DROP DOMAIN measure cascade;
Chapter 10 – Database Creation 33
Data Population Using SQL
Chapter 10 – Database Creation 34
Sample Tables
Chapter 10 – Database Creation 35
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.
Chapter 10 – Database Creation 36
INSERT Example
Chapter 10 – Database Creation 37
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 <table-name> 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.
Chapter 10 – Database Creation 38
DELETE Example
Chapter 10 – Database Creation 39
DELETE Example (continued)
Chapter 10 – Database Creation 40
UPDATE
• The UPDATE statement modifies the values of one or more columns in selected rows of a single table.
• Syntax:
• The SET clause specifies which columns are to be updated and calculates the new values for the columns.
• It is important that an UPDATE statement not violate any existing constraints.
Chapter 10 – Database Creation 41
Access Control
Chapter 10 – Database Creation 42
Access Control
• SELECT – permission to retrieve data from a table or view;• INSERT – permission to insert rows into a table or view; • UPDATE – permission to modify column values of a row in a
table or view;• DELETE – permission to delete rows of data in a table or view;• REFERENCES – permission to reference columns of a named
in integrity constraints;• USAGE – permission to use domains, collation sequences,
character sets, and translations
Chapter 10 – Database Creation 43
GRANT
• The GRANT statement is used to grant privileges on database objects to specific users. The format of the GRANT statement is:
• Privilege-list can consists of one or more of the following privileges, separated by commas:
SELECTDELETEINSERT [(Column-name [, … ])]UPDATE [(Column-name [, … ])]REFERENCES [(Column-name [, … ])]USAGE
Chapter 10 – Database Creation 44
REVOKE
• The REVOKE statement is used to take away all or some of the privileges previously granted to another user or users. The format of the REVOKE statement is:
Chapter 10 – Database Creation 45
Example 1
• USER_A grants an assortment of privileges on the ORDERS, PATIENT, and MEDICATION tables to USER_B, USER_C, and USER_D.
Chapter 10 – Database Creation 46
Example 2
• USER_A grants all privileges on the MEDICATION table to the PUBLIC.
Twice for User D
Chapter 10 – Database Creation 47
Example 3
• USER_A has not granted USER_B any privileges on the PATIENT table. The following GRANT statement allows USER_B to retrieve (i.e., select) rows from USER_A’s PATIENT table and also grant the SELECT privilege to other users.
Chapter 10 – Database Creation 48
Example 4
• At this point assume USER_B is connected to the database and attempts to grant the SELECT privilege received from USER_A to USER_D.
Chapter 10 – Database Creation 49
Example 4 (continued)
Chapter 10 – Database Creation 50
Example 5
• Privileges can be granted on specific columns of a table as well as on all columns. Here, USER_A grants USER_E the UPDATE privilege on the three columns of the PATIENT table.
Chapter 10 – Database Creation 51
Example 5 (continued)
Chapter 10 – Database Creation 52
Example 5 (continued)
Chapter 10 – Database Creation 53
Example 6
• USER_A is granting the UPDATE privilege on all columns of the patient table to USER_F.
Chapter 10 – Database Creation 54
Example 6Example 6 (continued)
Chapter 10 – Database Creation 55
Privileges Granted by User_A and USER_B
top related