Top Banner
Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell
25

Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Dec 18, 2015

Download

Documents

Claud Preston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Database Basics

I101 Summer 2006Copyright 2004, Matt Hottell

Page 2: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Relational Model

The relational model is the dominant model for database design today.

The relational model was conceived by E.F. Codd of IBM in 1970.

The relational model is based on set theory and is composed of relations (also known as tables).

Page 3: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Relations

Relations are composed of two main elements: Attributes

Metadata Tuples

The data itself

Page 4: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Attributes

Attributes are the column headings in a relational table.

They describe the data that is contained in each column

Order of attributes is unimportant All of the attributes together make

up the schema of the relation.

Page 5: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Tuples

Tuples are the complete rows of data in the table.

They are also known as records. No two tuples can be exactly the

same The order of tuples is not

important

Page 6: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Relation Example

Name Address Phone

Matt 420 Baker Street

555-4242

Jenny 12 Tutone Ave 867-5309

Sean 1600 N Penn Dr

555-1550

Page 7: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Relation Example

Name Address Phone

Matt 420 Baker Street

555-4242

Jenny 12 Tutone Ave 867-5309

Sean 1600 N Penn Dr

555-1550

Attributes

Page 8: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Relation Example

Name Address Phone

Matt 420 Baker Street

555-4242

Jenny 12 Tutone Ave 867-5309

Sean 1600 N Penn Dr

555-1550

Attributes

Tuples

Page 9: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Relation Example

Name Address Phone

Matt 420 Baker Street

555-4242

Jenny 12 Tutone Ave 867-5309

Sean 1600 N Penn Dr

555-1550

Attributes

Tuples

Schema

Page 10: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Keys

Relational tables often have an attribute or set of attributes that uniquely identifies each tuple.

These attributes or sets of attributes are called primary keys.

Page 11: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Example

Name Address Phone

Matt 420 Baker Street 555-4242

Jenny 12 Tutone Ave 867-5309

Sean 1600 N Penn Dr 555-1550

Page 12: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Primary Key Example

ID# Name Address Phone

500 Matt 420 Baker Street

555-4242

501 Jenny 12 Tutone Ave 867-5309

502 Sean 1600 N Penn Dr 555-1550

Page 13: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Foreign Keys

Foreign keys are attributes in a relational table that refer to a primary key in another relational table.

Foreign keys allow us to link relational tables together and gather information based on the contents of multiple tables.

Page 14: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Foreign Key ExampleID# Name Address Phone

500 Matt 420 Baker Street 555-4242

501 Jenny 12 Tutone Ave 867-5309

502 Sean 1600 N Penn Dr 555-1550

BookID Title Price

1001 The Code Book 14.00

1002 Core Web Programming

49.95

1003 The Hacker Ethic 19.95

ID# BookID Date

500 1001 9/13/03

501 1002 9/17/03

501 1002 9/26/03

502 1003 10/01/03

Book

Cust

Trans

Page 15: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

SQL

SQL stands for Structured Query Language.

All Relational Database Management Systems use SQL to interact with data.

SQL is often implementation-specific MySQL uses different syntax than Oracle

We will be using MySQL syntax

Page 16: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Creating a table in SQL

The syntax for creating a table is: CREATE TABLE tablename

(column1name datatype(size) [constraints], column2name datatype(size) [constraints], column3name datatype(size) [constraints]);

Page 17: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Data Types - Character

VARCHAR(maxsize) variable length character data with a

maximum size of maxsize. CHAR(fixedsize)

fixed-length character data of size fixedsize.

Page 18: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Data Types - Numbers

INT or INTEGER Allocates 4 bytes to store numbers

NUMERIC(precision, scale) allocates precision number of digits

with scale decimal places.

Page 19: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Data Types - Dates

DATE Stores year, month, day.

TIME Stores hour, minute, second

TIMESTAMP(precision) stores year, month, day, hour,

minute, and second.

Page 20: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Constraints PRIMARY KEY

Indicates that this attribute is the primary key for the table

NOT NULL Indicates that this attribute cannot

have a null value REFERENCES table(attribute)

Declares that this attribute is a foreign key.

Page 21: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Viewing Relational Tables

SHOW TABLES; Displays a list of all the available

tables in the database.

DESCRIBE table_name; Displays the schema of the table.

Page 22: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Adding data

You can add data to a table using the following syntax:

INSERT INTO table_name (attributes)VALUES (values);

Character data must be inside single quotes!

Page 23: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Updating Data

Update the value of a particular attribute for a tuple:

UPDATE table_name SET column_name = new_value WHERE condition to select row to

update

Page 24: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Modifying a table

Change an attribute’s datatypeALTER TABLE table_name MODIFY column_name datatype [NULL| NOT NULL];

Add an attribute to a tableALTER TABLE table_name ADD column_name datatype [NULL | NOT NULL];

Delete an attribute from a tableALTER TABLE table_nameDROP COLUMN column_name;

Page 25: Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Deleting a table

DROP TABLE tablename;