Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.

Similar presentations


Presentation on theme: "Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell."— Presentation transcript:

1 Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell

2 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).

3 Relations Relations are composed of two main elements: Attributes Metadata Tuples The data itself

4 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.

5 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

6 Relation Example NameAddressPhone Matt420 Baker Street555-4242 Jenny12 Tutone Ave867-5309 Sean1600 N Penn Dr555-1550

7 Relation Example NameAddressPhone Matt420 Baker Street555-4242 Jenny12 Tutone Ave867-5309 Sean1600 N Penn Dr555-1550 Attributes

8 Relation Example NameAddressPhone Matt420 Baker Street555-4242 Jenny12 Tutone Ave867-5309 Sean1600 N Penn Dr555-1550 Attributes Tuples

9 Relation Example NameAddressPhone Matt420 Baker Street555-4242 Jenny12 Tutone Ave867-5309 Sean1600 N Penn Dr555-1550 Attributes Tuples Schema

10 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.

11 Example NameAddressPhone Matt420 Baker Street555-4242 Jenny12 Tutone Ave867-5309 Sean1600 N Penn Dr555-1550

12 Primary Key Example ID#NameAddressPhone 500Matt420 Baker Street555-4242 501Jenny12 Tutone Ave867-5309 502Sean1600 N Penn Dr555-1550

13 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.

14 Foreign Key Example ID#NameAddressPhone 500Matt420 Baker Street555-4242 501Jenny12 Tutone Ave867-5309 502Sean1600 N Penn Dr555-1550 BookIDTitlePrice 1001The Code Book14.00 1002Core Web Programming49.95 1003The Hacker Ethic19.95 ID#BookIDDate 50010019/13/03 50110029/17/03 50110029/26/03 502100310/01/03 Book Cust Trans

15 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

16 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]);

17 Data Types - Character VARCHAR(maxsize) variable length character data with a maximum size of maxsize. CHAR(fixedsize) fixed-length character data of size fixedsize.

18 Data Types - Numbers INT or INTEGER Allocates 4 bytes to store numbers NUMERIC(precision, scale) allocates precision number of digits with scale decimal places.

19 Data Types - Dates DATE Stores year, month, day. TIME Stores hour, minute, second TIMESTAMP(precision) stores year, month, day, hour, minute, and second.

20 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.

21 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.

22 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!

23 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

24 Modifying a table Change an attribute’s datatype ALTER TABLE table_name MODIFY column_name datatype [NULL| NOT NULL]; Add an attribute to a table ALTER TABLE table_name ADD column_name datatype [NULL | NOT NULL]; Delete an attribute from a table ALTER TABLE table_name DROP COLUMN column_name;

25 Deleting a table DROP TABLE tablename;


Download ppt "Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell."

Similar presentations


Ads by Google