16
5 Chapter 5 Structured Query Language (SQL1) Revision

Chapter 5

  • Upload
    nuru

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 5. Structured Query Language (SQL1) Revision. In this lecture, you will learn:. Data definition commands Data manipulation commands. ( Defining database, table structures, table relationship). Data Definition Commands. Data Definition Commands. Create database structure - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 5

5

Chapter 5

Structured Query Language (SQL1)

Revision

Page 2: Chapter 5

2

5

In this lecture, you will learn:

• Data definition commands• Data manipulation commands

Page 3: Chapter 5

3

5

( Defining database, table structures, table relationship)

Page 4: Chapter 5

4

5

Data Definition Commands

• Create database structure– Holds all tables and is a collection of physical files

stored on disk– DBMS automatically creates tables to store metadata

CREATE DATABASE <databasename>

Example:CREATE DATABASE TDB2111

Page 5: Chapter 5

5

5

Creating Table Structure

• Tables store end-user data• May be based on data dictionary entries

CREATE TABLE <table name>(<attribute1 name and attribute1 type,attribute2 name and attribute2 type,attribute3 name and attribute3 type,primary key designation,foreign key designation and foreign key requirement>);

Page 6: Chapter 5

6

5

Data Type

• Data Types include:CHAR, INTEGER, VARCHAR, DECIMAL, DATE (see page 110)

• Some Examples: CHAR(25), INTEGER, SMALLINT, DECIMAL(9,2), DATE

Page 7: Chapter 5

7

5

SQL Integrity Constraints

• Adherence to entity integrity and referential integrity rules is crucial– Entity integrity enforced automatically if primary

key specified in CREATE TABLE command sequence

– Referential integrity can be enforced in specification of FOREIGN KEY

– Other specifications to ensure conditions met:• ON DELETE RESTRICT• ON UPDATE CASCADE

Page 8: Chapter 5

8

5

Entity and Referential Integrity(one of the twelve reasons why Database is superior to Files)

0180 Shyness BB PSY 7.65 BB Bantam Fred 2156

0189 Kane & Able PB FIC 5.55 RH Random Jim 8988

0200 The Stranger BB FIC 8.75 SI Signet Jane 1011

0378 Dunwich Horror SI HOR 19.75

Entity Integrity:All entries are unique and no part of a primary key may be nullReferential Integrity:A foreign key may either have a null entry (providing it doesn't form part of its table's primary key) or an entry which matches the primary key value in the table to which it is related.

BOOK Table PUBLISHER Table

Page 9: Chapter 5

9

5

(retrieve, add, delete, update data)

Page 10: Chapter 5

10

5

Data Manipulation Commands

Common SQL Commands

Table 5.3

Page 11: Chapter 5

11

5

Listing Table Contents and Other Commands

• INSERT command makes data entry• UPDATE command makes data entry corrections• ROLLBACK command restores database back to

previous condition if COMMIT hasn’t been used• DELETE command removes table row• Allows table contents to be listed

SELECT <attribute names> FROM <table names>;

Page 12: Chapter 5

12

5

Data Entry (INSERT)• Enters data into a table

INSERT INTO <table name> VALUES (attribute 1 value, attribute 2 value, … etc.);

CREATE TABLE BOOK(Book_Code CHAR(4), Book_Title CHAR(30), Book_Type CHAR(3), Book_Price DECIMAL(5,2), Book_Paperback CHAR(1), Pub_Code CHAR(2), CHECK (Book_Paperback IN ('Y','N') ), PRIMARY KEY (Book_Code), FOREIGN KEY (Pub_Code) REFERENCES PUBLISHER ) ;

INSERT INTO BOOK VALUES (‘0180’,’Shyness’,’PSY’,7.65 );

•values of attributes must be listed in the correct order/sequence

• values of character attributes are enclosed in ‘ ‘

Page 13: Chapter 5

13

5

Commit & Rollback

• Saves changes to disk

COMMIT <table names> ;

COMMIT Book;

if you know something has gone wrongROLLBACK; takes you back to the last COMMIT

Page 14: Chapter 5

14

5

Update (modify data)

• UPDATE BOOK SET Book_Type=‘MYS’, Book_Price=9.95 WHERE Book_Code=‘0180’;

can set the value of more that one attribute in a single UPDATE statement by separating the list of attributes and their values with commas

Page 15: Chapter 5

15

5

Delete (Remove data)

• DELETE FROM BOOKWHERE Book_Code=‘0180’;

the WHERE clause does not have to be written in terms of the primary keyDELETE FROM BOOKWHERE Book_Type=‘PSY’; would delete all the psy books

•Note:DELETE FROM BOOKWHERE Pub_Code EXISTS; deletes all tuples where there is a non-null Pub_Code

Page 16: Chapter 5

16

5

SELECT (retrieve certain/all data)

• SELECT * FROM BOOK;• SELECT Book_Code, Book_Title FROM BOOK;• SELECT * FROM BOOK

WHERE Book_Code=‘0181’;• SELECT Book_Code, Pub_Name FROM BOOK, PUBLISHER

WHERE BOOK.Pub_Code = PUBLISHER.PubCode