SQL Structured Query Language. Aims To introduce the implementation of a Physical design using SQL....

17
SQL Structured SQL Structured Query Language Query Language

Transcript of SQL Structured Query Language. Aims To introduce the implementation of a Physical design using SQL....

Page 1: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

SQL Structured Query SQL Structured Query LanguageLanguage

Page 2: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

AimsAims

To introduce the implementation of a To introduce the implementation of a Physical design using SQL.Physical design using SQL.

To introduce SQL Data Definition To introduce SQL Data Definition Language (DDL).Language (DDL).

To introduce SQL Data Manipulation To introduce SQL Data Manipulation Language (DML).Language (DML).

Page 3: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

Implementation of a Physical Implementation of a Physical DesignDesign

Database Design LanguageDatabase Design Language Course (Course (courseCodecourseCode, title, cost), title, cost)Entity Entity CourseCourse is converted to the following physical is converted to the following physical

design.design. courseCode courseCode andand title title are set to text and indexed are set to text and indexed

appropriately, appropriately, cost cost is set tois set to Currency CurrencytblCoursetblCourse

Field Field NameName

Data TypeData Type SizeSize FormatFormat Key TypeKey Type IndexIndex RequiredRequired

courseCodecourseCode TextText 88 Primary KeyPrimary Key Yes(no Yes(no duplicates)duplicates)

YesYes

titletitle TextText 2525 YesYes

costcost CurrencyCurrency CurrencyCurrency NoNo

Page 4: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

SQL (Data Definition Language SQL (Data Definition Language - DDL- DDL

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

Page 5: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

SQL Create TableSQL Create Table

CREATE TABLE <table name> CREATE TABLE <table name>

( (

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

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

… ….. ..

));;

Page 6: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

SQL Create TableSQL Create Table

Each column definition has 3 components.Each column definition has 3 components.

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

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

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

Page 7: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

SQL Create TableSQL Create Table

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

Page 8: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

Implementation of a Physical Implementation of a Physical DesignDesign

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

Entity Entity StudentCourseStudentCourse is converted to the following physical design. is converted to the following physical design.

This entity has a Compound Key, all elements of the compound key are identified.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 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 exactly match the data type and the field to which they will be linking to enforce the relationship between the tables.the relationship between the tables.

Compound keys are always indexed yes (duplicates OK)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: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

SQL Create TableSQL Create Table

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

PRIMARY KEY (studentID, PRIMARY KEY (studentID, courseCode)courseCode)

););

Page 10: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

Enforcing Foreign KeysEnforcing Foreign Keys

ALTER TABLE tblStudentCourse ALTER TABLE tblStudentCourse

ADD CONSTRAINT courseStudentCourseFK ADD CONSTRAINT courseStudentCourseFK FOREIGN KEY (courseCode)FOREIGN KEY (courseCode)

REFERENCES tblcourse (courseCode);REFERENCES tblcourse (courseCode);

Page 11: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

Adding data using SQLAdding data using SQL

INSERT INTO targetTable (Field1, INSERT INTO targetTable (Field1, Field2, …)Field2, …)

VALUES (Value1, Value2, …) VALUES (Value1, Value2, …)

Page 12: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

Adding data using SQLAdding data using SQL

INSERT INTO targetTable (Field1, INSERT INTO targetTable (Field1, Field2, …)Field2, …)

VALUES (Value1, Value2, …) VALUES (Value1, Value2, …)

Page 13: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

Adding data using SQLAdding data using SQL

Method 1Method 1

INSERT INTO tblProduct (prodID, INSERT INTO tblProduct (prodID, prodDesc, cost)prodDesc, cost)

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

Page 14: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

Adding data using SQLAdding data using SQL

Method 2Method 2

INSERT INTO tblProductINSERT INTO tblProduct

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

Page 15: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

INSERT INTO tblSupplierINSERT INTO tblSupplier

(suppName, suppAdd1, suppAdd2, (suppName, suppAdd1, suppAdd2, suppPostCode)suppPostCode)

VALUESVALUES

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

Adding data using SQLAdding data using SQL

Page 16: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

SummarySummary

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

foreign keys.foreign keys. Student individual workStudent individual work Rest of chapter 6Rest of chapter 6

Page 17: SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).

NEXT WEEKNEXT WEEK

Chapter 7Chapter 7

More Advanced SQLMore Advanced SQL

Next Week is an in class-Test-Next Week is an in class-Test-

Logical Design TestLogical Design Test