Advanced Data Definition Commands

46
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa Binsaleh 1

description

Al- Maarefa College for Science and Technology INFO 232: Database systems Chapter 7 ( P art II) Introduction to Structured Query Language (SQL) Instructor Ms. Arwa Binsaleh. Advanced Data Definition Commands. All changes in table structure are made by using ALTER command Three options: - PowerPoint PPT Presentation

Transcript of Advanced Data Definition Commands

Page 1: Advanced Data Definition Commands

AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGYINFO 232: DATABASE SYSTEMS

CHAPTER 7(Part II)

INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

InstructorMs. Arwa Binsaleh

1

Page 2: Advanced Data Definition Commands

Database Systems, 9th Edition 2

Advanced Data Definition Commands

• All changes in table structure are made by using ALTER command

• Three options:– ADD adds a column– MODIFY changes column characteristics– DROP deletes a column

• Can also be used to: – Add table constraints– Remove table constraints

Page 3: Advanced Data Definition Commands

Database Systems, 9th Edition 3

Changing a Column’s Data Type

• ALTER can be used to change data type• Some RDBMSs do not permit changes to data

types unless column is empty

Page 4: Advanced Data Definition Commands

Database Systems, 9th Edition 4

Changing a Column’s Data Characteristics

• Use ALTER to change data characteristics• Changes in column’s characteristics are

permitted if changes do not alter the existing data type

Page 5: Advanced Data Definition Commands

Database Systems, 9th Edition 5

Adding a Column Dropping a Column

• Use ALTER to add column– Do not include the NOT NULL clause for new

column

• Use ALTER to drop column– Some RDBMSs impose restrictions on the

deletion of an attribute

Page 6: Advanced Data Definition Commands

6

Adding a Column

Database Systems, 9th Edition

Page 7: Advanced Data Definition Commands

7

Dropping a Column

Database Systems, 9th Edition

Page 8: Advanced Data Definition Commands

Database Systems, 9th Edition 8

Advanced Data Updates

• UPDATE command updates only data in existing rows

• If relationship between entries and existing columns, can assign values to slots

• Arithmetic operators are useful in data updates• In Oracle, ROLLBACK command undoes

changes made by last two UPDATE statements

Page 9: Advanced Data Definition Commands

9

Example

Database Systems, 9th Edition

Page 10: Advanced Data Definition Commands

Database Systems, 9th Edition 10

Page 11: Advanced Data Definition Commands

Database Systems, 9th Edition 11

Copying Parts of Tables

• SQL permits copying contents of selected table columns– Data need not be reentered manually into newly

created table(s)• First create the table structure• Next add rows to new table using table rows

from another table

Page 12: Advanced Data Definition Commands

12

Example

Database Systems, 9th Edition

Page 13: Advanced Data Definition Commands

Database Systems, 9th Edition 13

Page 14: Advanced Data Definition Commands

Database Systems, 9th Edition 14

Adding Primary and Foreign Key Designations

• When table is copied, integrity rules do not copy– Primary and foreign keys are manually defined

on new table• User ALTER TABLE command

– Syntax:ALTER TABLE tablename ADD PRIMARY KEY(fieldname);

– For foreign key, use FOREIGN KEY in place of PRIMARY KEY

Page 15: Advanced Data Definition Commands

15

Example

Database Systems, 9th Edition

Page 16: Advanced Data Definition Commands

Database Systems, 9th Edition 16

Deleting a Table from the Database

• DROP– Deletes table from database– Syntax:

DROP TABLE tablename;• Can drop a table only if it is not the “one” side

of any relationship– Otherwise, RDBMS generates an error message– Foreign key integrity violation

Page 17: Advanced Data Definition Commands

Database Systems, 9th Edition 17

Additional SELECT Query Keywords

• Logical operators work well in the query environment

• SQL provides useful functions that:– Count– Find minimum and maximum values– Calculate averages, etc.

• SQL allows user to limit queries to:– Entries having no duplicates– Entries whose duplicates may be grouped

Page 18: Advanced Data Definition Commands

Database Systems, 9th Edition 18

Ordering a Listing

• ORDER BY clause is useful when listing order is important

• Syntax: SELECT columnlist FROM tablelist [WHERE conditionlist] [ORDER BY columnlist [ASC | DESC]];

• Ascending order by default

Page 19: Advanced Data Definition Commands

19

Example

Database Systems, 9th Edition

Page 20: Advanced Data Definition Commands

Database Systems, 9th Edition 20

Listing Unique Values

• DISTINCT clause produces list of only values that are different from one another

• Example:SELECT DISTINCT V_CODEFROM PRODUCT;

• Access places nulls at the top of the list– Oracle places it at the bottom– Placement of nulls does not affect list contents

Page 21: Advanced Data Definition Commands

21

Example

Database Systems, 9th Edition

Page 22: Advanced Data Definition Commands

Database Systems, 9th Edition 22

Aggregate Functions

• COUNT function tallies number of non-null values of an attribute– Takes one parameter: usually a column name

• MAX and MIN find highest (lowest) value in a table– Compute MAX value in inner query– Compare to each value returned by the query

• SUM computes total sum for any specified attribute

• AVG function format is similar to MIN and MAX

Page 23: Advanced Data Definition Commands

23

COUNT Example

Database Systems, 9th Edition

Page 24: Advanced Data Definition Commands

24

MAX and MIN output Example

Database Systems, 9th Edition

Page 25: Advanced Data Definition Commands

25

SUM Example

Database Systems, 9th Edition

Page 26: Advanced Data Definition Commands

26

AVG Example

Database Systems, 9th Edition

Page 27: Advanced Data Definition Commands

Database Systems, 9th Edition 27

Grouping Data

• Frequency distributions created by GROUP BY clause within SELECT statement

• Syntax:SELECT columnlistFROM tablelist[WHERE conditionlist][GROUP BY columnlist][HAVING conditionlist][ORDER BY columnlist [ASC | DESC] ] ;

Page 28: Advanced Data Definition Commands

28

GROUP BY Example

Database Systems, 9th Edition

Page 29: Advanced Data Definition Commands

Database Systems, 9th Edition 29

Page 30: Advanced Data Definition Commands

30

HAVING Clause Example

Database Systems, 9th Edition

Page 31: Advanced Data Definition Commands

Database Systems, 9th Edition 31

Virtual Tables: Creating a View

• View is virtual table based on SELECT query• Create view by using CREATE VIEW command• Special characteristics of relational view:

– Name of view can be used anywhere a table name is expected

– View dynamically updated– Restricts users to only specified columns and

rows– Views may be used as basis for reports

Page 32: Advanced Data Definition Commands

32

View Example

Database Systems, 9th Edition

Page 33: Advanced Data Definition Commands

Database Systems, 9th Edition 33

Joining Database Tables

• Joining tables is the most important distinction between relational database and other DBs

• Join is performed when data are retrieved from more than one table at a time– Equality comparison between foreign key and

primary key of related tables• Join tables by listing tables in FROM clause of

SELECT statement– DBMS creates Cartesian product of every table

Page 34: Advanced Data Definition Commands

34

JOIN Example

Database Systems, 9th Edition

Page 35: Advanced Data Definition Commands

35Database Systems, 9th Edition

Page 36: Advanced Data Definition Commands

Database Systems, 9th Edition 36

Joining Tables with an Alias

• Alias identifies the source table from which data are taken

• Alias can be used to identify source table• Any legal table name can be used as alias• Add alias after table name in FROM clause

– FROM tablename alias

Page 37: Advanced Data Definition Commands

37

Alias Example

Database Systems, 9th Edition

Page 38: Advanced Data Definition Commands

Database Systems, 9th Edition 38

Recursive JoinsOuter Joins

• Alias is especially useful when a table must be joined to itself– Recursive query– Use aliases to differentiate the table from itself

• Two types of outer join– Left outer join– Right outer join

Page 39: Advanced Data Definition Commands

39

Recursive Join Example

Database Systems, 9th Edition

Page 40: Advanced Data Definition Commands

40

Cont’d

Database Systems, 9th Edition

Page 41: Advanced Data Definition Commands

41

LEFT and RIGHT JOIN

Database Systems, 9th Edition

Page 42: Advanced Data Definition Commands

42Database Systems, 9th Edition

Page 43: Advanced Data Definition Commands

Database Systems, 9th Edition 43

Summary

• SQL commands can be divided into two overall categories: – Data definition language commands – Data manipulation language commands

• The ANSI standard data types are supported by all RDBMS vendors in different ways

• Basic data definition commands allow you to create tables, indexes, and views

Page 44: Advanced Data Definition Commands

Database Systems, 9th Edition 44

Summary (cont’d.)

• DML commands allow you to add, modify, and delete rows from tables

• The basic DML commands:– SELECT, INSERT, UPDATE, DELETE,

COMMIT, and ROLLBACK• SELECT statement is main data retrieval

command in SQL

Page 45: Advanced Data Definition Commands

Database Systems, 9th Edition 45

Summary (cont’d.)

• WHERE clause can be used with SELECT, UPDATE, and DELETE statements

• Aggregate functions– Special functions that perform arithmetic

computations over a set of rows• ORDER BY clause

– Used to sort output of SELECT statement– Can sort by one or more columns– Ascending or descending order

Page 46: Advanced Data Definition Commands

Database Systems, 9th Edition 46

Summary (cont’d.)

• Join output of multiple tables with SELECT statement– Join performed every time you specify two or

more tables in FROM clause– If no join condition is specified, DBMX performs

Cartesian product• Natural join uses join condition to match only

rows with equal values in specified columns• Right outer join and left outer join select rows

with no matching values in other related table