Introduction to Database Management Systems

23
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

description

Introduction to Database Management Systems. Dr. Adam Anthony Fall 2012. Lecture 4 Overview. Chapter 3, sections 1-4 SQL language introduction Data Definition Language Data Query Language Basic Query Practice. History of SQL. - PowerPoint PPT Presentation

Transcript of Introduction to Database Management Systems

Page 1: Introduction to Database Management Systems

INTRODUCTION TO DATABASE MANAGEMENT SYSTEMSDr. Adam Anthony

Fall 2012

Page 2: Introduction to Database Management Systems

Lecture 4 Overview

Chapter 3, sections 1-4 SQL language introduction Data Definition Language Data Query Language Basic Query Practice

Page 3: Introduction to Database Management Systems

History of SQL

IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory

Renamed Structured Query Language (SQL) ANSI and ISO standard SQL:

SQL-86, SQL-89, SQL-92 SQL:1999, SQL:2003, SQL:2008

Commercial systems offer most, if not all, SQL-92 features, plus varying feature sets from later standards and special

proprietary features. Not all examples here may work on your particular

system.

Page 4: Introduction to Database Management Systems

Data Definition Language

Converts a schema into a real thing! New information: Domain Types Managing data integrity Other Security, efficiency features (take

Database 2 class for more info) Indexing (pre-sorting, basically) Security and access privilege How to store on disk

Page 5: Introduction to Database Management Systems

Common Data Types

Most systems have these types exactly, or something very close in name and usage: char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified

maximum length n. int. Integer (a finite subset of the integers that is machine-

dependent). smallint. Small integer (a machine-dependent subset of the integer

domain type). numeric(p,d). Fixed point number, with user-specified precision of p

digits, with n digits to the right of decimal point. real, double precision. Floating point and double-precision floating

point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at

least n digits. More are covered in Chapter 4.

Page 6: Introduction to Database Management Systems

Example Schema

PerformerPerformer_ID name

Song

Song_ID TitleGenreAlbum

Performer-SongPerformer_ID Song_ID

Page 7: Introduction to Database Management Systems

Create Table Statement

create table NAME (A1 D1,

A2 D2,

..., An Dn,(integrity-

constraint1),...,(integrity-

constraintk)

);

create table PERFORMER (

performer_id int,

name varchar(30),

primary key (performer_id)

);

Page 8: Introduction to Database Management Systems

Integrity Constraints

Not Null Primary Key Foreign Key create table PERFORMER-SONG ( Performer_ID int, Song_ID int, primary key (Performer_ID,Song_ID) foreign key (Performer_ID) references PERFORMER foreign key (Song_ID) references SONG);**A primary key specification requires values to be not null!

Page 9: Introduction to Database Management Systems

Not Null Example

Create table SONG(Song_ID int,Title varchar(30) not null, Genre varchar (10), Album varchar(50) not null, primary key (Song_ID)

);

Page 10: Introduction to Database Management Systems

Multi-Valued Keys

create table takes ( ID varchar(5), course_id varchar(8), sec_id varchar(8), semester varchar(6), year numeric(4,0), grade varchar(2), primary key (ID, course_id, sec_id, semester, year), foreign key (ID) references student, foreign key (course_id, sec_id, semester, year)

references section );

Note: sec_id can be dropped from primary key above, to ensure a student cannot be registered for two sections of the same course in the same semester

Page 11: Introduction to Database Management Systems

Drop and Alter Table

drop table student Deletes the table and its contents

delete from student Deletes all contents of table, but retains table

alter table alter table r add A D

where A is the name of the attribute to be added to relation r and D is the domain of A.

All tuples in the relation are assigned null as the value for the new attribute.

alter table r drop A where A is the name of an attribute of relation r Dropping of attributes not supported by many databases

Page 12: Introduction to Database Management Systems

Data Manipulation Language SQL features that allow us to query data in novel

ways, and to update and delete specific values Basic query construct: the Select clause:

SELECT Name FROM PERFORMERWHERE Performer_ID < 5000; All SQL clauses are case insensitive—performer_id =

PERFORMER_ID = Performer_ID, etc. Use capitalization, line breaks to enhance readability

Connect each portion above to the relational algebra equivalent

Page 13: Introduction to Database Management Systems

Projection, Plus!

If you want everything: SELECT * FROM Student;

When we get to multi-table queries: SELECT Student.* FROM Student, TakesWHERE …

Expressions for columns: SELECT ID, name, salary*1.10

FROM instructor

Page 14: Introduction to Database Management Systems

INTERACTIVE LECTURE TIME! Queries are best learned through doing! Download and extract:

http://www.bw.edu/~apanthon/courses/CSC280/DataFiles/InClassLec4-5.zip

Page 15: Introduction to Database Management Systems

DISTINCT and ALL

Output of an SQL statement is a relation, but it MAY have duplicates! Differs from theoretical underpinnings

Find all the courses that have ever been taken by a student:

Try: Then Try: SELECT course_id FROM takes;

SELECT DISTINCT course_id

FROM takes;

Page 16: Introduction to Database Management Systems

Using WHERE

The WHERE clause does most of the interesting work

True/False tests based on attribute values

Use AND, OR, NOT to combine tests just like you would in an IF statement

SQL provides many useful operators for WHERE clauses

Page 17: Introduction to Database Management Systems

Comparing Strings

Strings represented with singe quote: ‘Comp. Sci.’

Comparisons are case-sensitive ‘Comp. Sci.’ != ‘comp. sci.’

Upper(s) and Lower(s) to get all-upper or all-lower

Trim(s) to get rid of trailing white space LIKE Wildcards:

% represents any number or characters _ represents exactly one character Ex: WHERE name LIKE ‘%Thompson’

Page 18: Introduction to Database Management Systems

Practice

Find all students with A name more than 5 characters long A name that starts with P or ends in S (case

insensitive) A name that is exactly five letters and

starts with B

Page 19: Introduction to Database Management Systems

Range Parameters

Cool trick to save time Find all students with total credits more

than 3 and less than 10 Inclusive range:

SELECT * FROM StudentWHERE tot_cred BETWEEN 4 and 9

This construct is completely optional How to do this with >=, <= ?

Page 20: Introduction to Database Management Systems

Using more than one table

The FROM clause automatically produces a cartesian (cross) product:

Command .headers ON Try:

SELECT * FROM Student, Takes

Is this useful, ever? What should we add?

Page 21: Introduction to Database Management Systems

Manual Joins

Just filter out the junk!SELECT * FROM Student, TakesWHERE Student.ID = Takes.ID

Called a ‘Join’ because it results in a combined table that is true to the intent of relational database design

Depending on the design, a Join can still be HUGE and COSTLY to compute!

Page 22: Introduction to Database Management Systems

Natural Joins

IF the names of the columns you wish to join on match PERFECTLY you can let SQL join automatically:

SELECT * FROM STUDENT NATURAL JOIN

TAKES If the names don’t match, or you need to

do something clever, stick with a manual join

Just a time-saver

Page 23: Introduction to Database Management Systems

Natural Join Problems

Danger in natural join: beware of unrelated attributes with same name which get equated incorrectly

List the names of instructors along with the the titles of courses that they teach

Incorrect version (makes course.dept_name = instructor.dept_name) select name, title

from instructor natural join teaches natural join course; Correct version

select name, titlefrom instructor natural join teaches, coursewhere teaches.course_id = course.course_id;