1 11/3/05CS360 Windows Programming Databases and Data Representation.

Post on 12-Jan-2016

213 views 1 download

Transcript of 1 11/3/05CS360 Windows Programming Databases and Data Representation.

1

Databases and Data Representation

2

Databases and Data Representation

Database Management System (DBMS) Provides efficient, convenient, and safe multi-

user storage of and access to massive amounts of persistent data

Provides a programming interface that allows a user or program too - Create new databases and specify their structureo - Query and modify the data

Early DBMSs were ad hoc, with no two the same Now one approach predominates: relational

databases and SQL (Structured Query Language)

3

Databases

A "relation" is a table of data

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

4

Databases

The columns are known as "attributes"

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

LectID Name Course Students

5

Databases

The rows are called "tuples"

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

LectID Name Course Students

6

Databases

It is allowable for some values to be missing

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01

4 Josh CS120-02 23

LectID Name Course Students

7

Databases

We can add, remove, or update tuples

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

LectID Name Course Students

8

Databases

Each attribute has an underlying domain, or data type

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

LectID Name Course Students

int string string int

9

Databases

The structure of the table is referred to as its schemao Lecturers(LectID, Name, Course, Students)

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

LectID Name Course Students

10

Primary Key

Every table must have a primary keyo No two tuples can have the same LectID

o LectID cannot be null

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

LectID Name Course Students

11

Tables

Suppose that we want to add data about lecturers offices

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

Name Course StudentsLectID

12

Tables

But suppose that lecturers can have more than one office.

We can’t add additional rows for the same lecturers without violating the primary key constraint, so we use another table.

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

Name Course StudentsLectID Building Room

13

Multiple Tables

Solution: Add a new table

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

Name Course StudentsLectID

Shereen Strain 203C

Josh Marsh 324

Josh Strain 202

Doug Strain 201

Building Room

14

Relationships

Better to store LectID

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

Name Course StudentsLectID

1 Strain 203C

4 Marsh 324

4 Strain 202

2 Strain 201

Building RoomLectID

15

Relationships

Better to store LectID

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

Name Course StudentsLectID

1 1 Strain 203C

2 4 Marsh 324

3 4 Strain 202

4 2 Strain 201

Building RoomLectID

Foreign key

Primary key

16

SQL Queries

SELECT returns tuples that satisfy some condition

SELECT Name, Course FROM Lecturers WHERE Students > 15

1 Shereen CS360-01 12

2 Doug CS300-01 11

3 Chris CS445-01 15

4 Josh CS120-02 23

5 Mike CS120-02 19

Name Course StudentsLectID

Josh CS120-02

Mike CS120-01

Name Course

17

SQL Queries

SELECT *

FROM Lecturers

WHERE Students > 15

Attributes

Relations

Tuples

18

SQL Queries

SELECT *

FROM Lecturers

WHERE Students > 15

OR Name = ‘Shereen’

19

SQL Queries

Wildcards:o % matches any number of characters

o _ matches any single character

o [] matches any single character in set or range

o [^] matches any single character not in set or range

20

Database Management Systems

Many examples of DBMSso Oracle

o SQL Server

o MySQL http://dev.mysql.com/