database basics i101 summer 2006 copyright 2004, matt hottell
TRANSCRIPT
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).
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.
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
Relation Example
Name Address Phone
Matt 420 Baker Street
555-4242
Jenny 12 Tutone Ave 867-5309
Sean 1600 N Penn Dr
555-1550
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
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
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
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.
Example
Name Address Phone
Matt 420 Baker Street 555-4242
Jenny 12 Tutone Ave 867-5309
Sean 1600 N Penn Dr 555-1550
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
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.
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
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
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]);
Data Types - Character
VARCHAR(maxsize) variable length character data with a
maximum size of maxsize. CHAR(fixedsize)
fixed-length character data of size fixedsize.
Data Types - Numbers
INT or INTEGER Allocates 4 bytes to store numbers
NUMERIC(precision, scale) allocates precision number of digits
with scale decimal places.
Data Types - Dates
DATE Stores year, month, day.
TIME Stores hour, minute, second
TIMESTAMP(precision) stores year, month, day, hour,
minute, and second.
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.
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.
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!
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
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;