Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation...

16
www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I

Transcript of Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation...

Page 1: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

www.hope.ac.uk Deanery of Business & Computer Sciences

SQL Structured Query Language

Implementation Lecture – 8

Database Technology Level I

Page 2: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

2 www.hope.ac.uk Deanery of Business & Computer Sciences

Aims

To introduce the implementation of a Physical design using SQL.

To introduce SQL Data Definition Language (DDL).

To introduce SQL Data Manipulation Language (DML).

Page 3: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

3 www.hope.ac.uk Deanery of Business & Computer Sciences

Implementation of a Physical Design

Database Design Language Course (courseCode, title, cost)Entity Course is converted to the following physical design. courseCode and title are set to text and indexed appropriately, cost

is set to CurrencytblCourse

Field Name Data Type Size Format Key Type Index Required

courseCode Text 8 Primary Key Yes(no duplicates) Yes

title Text 25 Yes

cost Currency Currency No

Page 4: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

4 www.hope.ac.uk Deanery of Business & Computer Sciences

SQL (Data Definition Language - DDL

Database Definition Commands CREATE DATABASE DROP DATABASE SHOW DATABASES CREATE TABLE ALTER TABLE

Page 5: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

5 www.hope.ac.uk Deanery of Business & Computer Sciences

SQL Create Table

CREATE TABLE <table name>

(

<Column Name> <Data Type> <Conditional Clauses> ,

<Column Name> <Data Type> <Conditional Clauses> ,

…..

);

Page 6: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

6 www.hope.ac.uk Deanery of Business & Computer Sciences

SQL Create Table

Each column definition has 3 components.

<COLUMN NAME> this the name of the attribute identified in the design.

<DATA TYPE> specified for the attribute in the Physical design. The data types available for use will depend on those appropriate for the software with which the database is to be implemented.

<CONDITIONAL CLAUSES> such as specification of Primary Keys, Indexes and Nulls.

Page 7: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

7 www.hope.ac.uk Deanery of Business & Computer Sciences

SQL Create Table

CREATE TABLE tblCourse (courseCode VARCHAR(8) PRIMARY KEY, title VARCHAR(25) NOT NULL, cost CURRENCY );

Page 8: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

8 www.hope.ac.uk Deanery of Business & Computer Sciences

Implementation of a Physical Design

StudentCourse(studentID, courseCode) FK studentID→tblStudent Update Cascade, Delete Restrict FK courseCode→tblcourse Update Cascade, Delete Restrict

Entity StudentCourse is converted to the following physical design.

This entity has a Compound Key, all elements of the compound key are identified. Both attributes are foreign keys. This means that their data type and size must exactly

match the data type and the field to which they will be linking to enforce the relationship between the tables.

Compound keys are always indexed yes (duplicates OK)

Field Name Data Type Size Format Key Type Index Required

studentID Text 8 Compound Key Yes(duplicates OK) Yes

courseCode Text 8 Compound Key Yes(duplicates OK) Yes

Page 9: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

9 www.hope.ac.uk Deanery of Business & Computer Sciences

SQL Create Table

CREATE TABLE tblStudentCourse (studentID VARCHAR(8), courseCode VARCHAR(8),

PRIMARY KEY (studentID, courseCode) );

CREATE TABLE tblStudentCourse (studentID TEXT(8), courseCode TEXT(8), PRIMARY KEY (studentID, courseCode) );

ALTERNATIVE

Page 10: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

10 www.hope.ac.uk Deanery of Business & Computer Sciences

Enforcing Foreign Keys

ALTER TABLE tblStudentCourse

ADD CONSTRAINT courseStudentCourseFK FOREIGN KEY (courseCode)

REFERENCES tblcourse (courseCode);

Page 11: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

11 www.hope.ac.uk Deanery of Business & Computer Sciences

Adding data using SQL

INSERT INTO targetTable (Field1, Field2, …) VALUES (Value1, Value2, …)

Page 12: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

12 www.hope.ac.uk Deanery of Business & Computer Sciences

Adding data using SQL

Method 1

INSERT INTO tblProduct (prodID, prodDesc, cost)

VALUES (‘ssaay1’, ‘Nut’, 34.50)

Page 13: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

13 www.hope.ac.uk Deanery of Business & Computer Sciences

Adding data using SQL

Method 2

INSERT INTO tblProduct

VALUES (‘ssaay2’, ‘screw’, 4.50);

Page 14: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

14 www.hope.ac.uk Deanery of Business & Computer Sciences

INSERT INTO tblSupplier

(suppName, suppAdd1, suppAdd2, suppPostCode)

VALUES

(‘Johnsons’, ‘34 Meols Parade’, ‘Meols’, ‘CH47 6AY’)

Adding data using SQL

Page 15: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

15 www.hope.ac.uk Deanery of Business & Computer Sciences

Summary

Implementation of a Physical Design Use of SQL Create Table Use of SQL Alter Table to enforce foreign

keys. Student individual work Rest of chapter 6

Page 16: Www.hope.ac.uk Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.

16 www.hope.ac.uk Deanery of Business & Computer Sciences

REMEMBER

Implementation Test (3), W/B

18th February 2008 (20%)

Group Work Implementation Due 7th March 2008