12
1 CS 338: Computer Applications in Business: Databases (Fall 2014) ©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.) CS 338: Computer Applications in Business: Databases Data and Schema Modifications ©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.) Rice University Data Center Fall 2014 Chapters 4,5 SQL Data Manipulation Statements INSERT statement DELETE statement UPDATE statement 2

10 Data and Schema Modifications

Embed Size (px)

Citation preview

Page 1: 10 Data and Schema Modifications

1

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

CS 338: Computer Applications in Business: Databases

Data and Schema Modifications

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.) Rice University Data Center

Fall 2014

Chapters 4,5

SQL Data Manipulation Statements

INSERT statement

DELETE statement

UPDATE statement

2

Page 2: 10 Data and Schema Modifications

2

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

SQL Data Manipulation Statements INSERT Command

• There are different forms of the INSERT statement

1. INSERT Without Column list

• Provide values for all attributes of the relation then the list of attributes can be omitted

• Values are listed in the same order in which the corresponding attributes are specified in the CREATE TABLE command

3

INSERT is used to add tuples to a relation • Need to specify the relation name and a list of values for the tuple(s).

SQL Data Manipulation Statements INSERT Command

• There are different forms of the INSERT statement

2. INSERT With Column list (explicit list)

• Specify explicit attribute names that correspond to the values provided in the INSERT command

• Useful if a relation has many attributes but only a few of those attributes are assigned values in the new tuple

• Attributes with NULL allowed or DEFAULT values are the ones that can be left out

4

Attributes not specified in U1A are set to their DEFAULT or to NULL, and the values are listed in the same order as the attributes are listed in the INSERT command itself

Page 3: 10 Data and Schema Modifications

3

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

SQL Data Manipulation Statements INSERT Command

• There are different forms of the INSERT statement

3. INSERT with SELECT statement

• Insert one or more tuples as a result of a SELECT statement

5

INSERT and Integrity Constraints

• A DBMS that fully implements SQL should support and enforce all the integrity constraints that can be specified in the DDL • Example: If we issue the command in U2, the DBMS should reject the

operation because no DEPARTMENT tuple exists in the database with Dnumber = 2

• Example: U2A should be rejected because no Ssn value is provided and it is the primary key (cannot be NULL)

Page 4: 10 Data and Schema Modifications

4

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

SQL Data Manipulation Statements DELETE Command

WHERE clause can be arbitrarily complex (like for SELECT), including the use of nested SELECT statements

7

DELETE is used to remove tuple(s) from a relation • Needs relation name and (optionally) a WHERE clause to select

tuple(s) to be deleted

SQL Data Manipulation Statements DELETE Command

Syntax: DELETE FROM table_name [WHERE search_condition]

• Rows are explicitly deleted from only one table at a time. • Deletion may propagate to rows in other tables if referential

triggered actions are specified in the referential integrity constraints of the DDL

• WHERE clause is optional • if omitted, all rows are deleted from table • if specified, only those rows that satisfy the search_condition are

deleted

Page 5: 10 Data and Schema Modifications

5

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

SQL Data Manipulation Statements UPDATE Command

• Required SET clause in the UPDATE command • Specifies attributes to be modified and new values

• May use old value(s) and relations to determine new value(s) UPDATE Employee SET Salary = Salary*1.03 WHERE Dno IN ( SELECT Dnumber FROM Department WHERE Dname LIKE '%Research%');

9

UPDATE is used to modify column value(s) in one or more selected tuples • Needs relation name, column(s) to be modified and new values, and

(optionally) WHERE clause to select tuple(s) to be modified

SQL Data Manipulation Statements UPDATE Command

• Example 1: • Update salaries for all employees by a 10% increase UPDATE Employee

SET Salary = Salary * 1.10;

• Example 2: • Update salaries for all employees by a 10% increase in a specific

department UPDATE Employee

SET Salary = Salary * 1.10 WHERE Dno = 5;

WHERE clause finds all the rows containing Dno = 5. UPDATE is applied ONLY to these particular

rows

UPDATE is applied ALL rows in the EMPLOYEE

table

Page 6: 10 Data and Schema Modifications

6

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

SQL Data Manipulation Statements UPDATE Command Might Fail

• Recall: constraints specified in schema declaration

1. Inserted tuples might violate … • domain constraints, • uniqueness constraints, • referential constraints, or • check constraints

• Recall: CHECK(Dept_create_date <= Mgr_start_date)

2. Deleted tuples might violate referential constraints • Instead of failing, might cause cascaded deletes (recall: ON DELETE

CASCADE)

3. Modifications might fail (or cascade) like deletions or insertions

11

SQL Data Manipulation Statements Example 1

12

Write SQL update statements to do the following on the database schema shown in Figure 1.2

Insert a new student <‘Johnson’, 25, 1, ‘Math’>, in the database

INSERT INTO STUDENT VALUES ('Johnson', 25, 1, 'MATH')

Page 7: 10 Data and Schema Modifications

7

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

SQL Data Manipulation Statements Example 2

13

Write SQL update statements to do the following on the database schema shown in Figure 1.2

Change the class of student ‘Smith’ to 2

UPDATE STUDENT SET CLASS = 2 WHERE Name='Smith'

SQL Data Manipulation Statements Example 3

14

Write SQL update statements to do the following on the database schema shown in Figure 1.2

Insert a new course <‘Knowledge Engineering’, ‘CS4390’, 3, ‘CS’>

INSERT INTO COURSE VALUES ('Knowledge Engineering','CS4390', 3,'CS')

Page 8: 10 Data and Schema Modifications

8

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

SQL Data Manipulation Statements Example 4

15

Write SQL update statements to do the following on the database schema shown in Figure 1.2

Delete the record for the student whose name is ‘Smith’ and whose student number is 17

DELETE FROM STUDENT WHERE Name='Smith' AND StudentNumber=17

Assertions & Triggers

• Assertions are easier for the programmer to use since they merely require the programmer to state what must be true

• Triggers, on the other hand, tell exactly when the DBMS needs to deal with them

16

Recall: an assertion is a Boolean-valued SQL expression that must be true at all times

Recall: a trigger is a series of actions that are associated with certain events

Page 9: 10 Data and Schema Modifications

9

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Assertions & Triggers CREATE ASSERTION

• Specify additional types of constraints outside scope of built-in relational model constraints

• An assertion is declared with a CREATE statement

• Syntax: CREATE ASSERTION <assertion-name> CHECK (<condition>)

• Each assertion is given a constraint name

• The condition in an assertion must be true when the assertion is created and must remain true • Any database modification that causes it to become false will be

rejected

• Use only in cases where it is not possible to use CHECK on attributes and domains

17

Assertions & Triggers CREATE ASSERTION

Example

• Whenever some tuples in the database cause the condition of an ASSERTION statement to evaluate to FALSE, the constraint is violated

• Only to be used for cases not otherwise covered

• To delete an assertion: DROP ASSERTION <assertion-name>

18

What does the above assertion mean? Specifies a constraint that the salary of an employee must not be greater than the salary of the manager of the department that the employee works for

Page 10: 10 Data and Schema Modifications

10

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

Assertions & Triggers CREATE TRIGGER

• Specify automatic actions that database system will perform when certain events and conditions occur

• Used to monitor the database and enforce business rules • Might update derived data in (possibly some other) table • Might enforce constraint (e.g., by first updating related data) • Might raise an alarm

• Typical trigger has three components: • Event(s): Which updates are being monitored? Before/after/instead? • Condition: What specific data values are of concern? • Action: What should the system do when the conditions are met?

• Example: Nobody’s salary should be increased by more than 10%. CREATE TRIGGER Limit_sal AFTER UPDATE OF Salary ON EMPLOYEE (event) REFERENCING OLD ROW AS O, NEW ROW AS N FOR EACH ROW WHEN (N.Salary > 1.1*O.Salary) (condition) UPDATE EMPLOYEE (action) SET Salary = 1.1*O.Salary; 19

Schema Evolution Commands

• Schema evolution commands • Can be done while the database is operational • Does not require recompilation of the database schema

• Revise schema declaration as business needs evolve • Change set of tables • Change attributes within tables • Change set of constraints

• Part of DDL rather than DML • Contrast to database update commands

• Can be done while the database is operational 20

Nathan Wilson
Nathan Wilson
Nathan Wilson
Page 11: 10 Data and Schema Modifications

11

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

The DROP Command

• Drops named schema elements (e.g., tables, domains, constraints)

• Drop behavior options: • CASCADE and RESTRICT • Latter means no ripple-on effects allowed

• Example:

DROP SCHEMA Company CASCADE; • CASCADE Causes tables, domains, and constraints in schema to be

dropped as well • With RESTRICT, command would succeed only if schema is empty

21

The ALTER Command

• Can add a column to a table ALTER TABLE Company.Employee

ADD COLUMN Job VARCHAR(12);

• Can drop a column • Choose either CASCADE or RESTRICT • CASCADE permits constraints on columns to be dropped automatically

• Can alter a column definition • Change type, NULL-ability, or default value

• Can add or drop a named table constraint ALTER TABLE Company.Employee

DROP CONSTRAINT EmpSuperFK;

22

Page 12: 10 Data and Schema Modifications

12

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book (Garcia-Molina et al.)

The ALTER Command

ALTER TABLE COMPANY.EMPLOYEE DROP COLUMN Address CASCADE;

23

How to drop column Address from table Employee?

ALTER TABLE COMPANY.Department ALTER COLUMN Mgr_ssn DROP DEFAULT;

How to remove the default value set in the CREATE TABLE command for column Mgr_ssn in Department table?

ALTER TABLE COMPANY.Department ALTER COLUMN Mgr_ssn SET DEFAULT ‘123456789’;;

How to modify the default value set in the CREATE TABLE command for a column Mgr_ssn in Department table?