160 likes | 366 Views
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
E N D
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 • 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
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>);
Data Type • Data Types include:CHAR, INTEGER, VARCHAR, DECIMAL, DATE(see page 110) • Some Examples:CHAR(25), INTEGER, SMALLINT, DECIMAL(9,2), DATE
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
Data Manipulation Commands (retrieve, add, delete, update data)
Data Manipulation Commands Common SQL Commands Table 5.3
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>;
Data Entry (INSERT) INSERT INTO <table name> VALUES (attribute 1 value, attribute 2 value, … etc.); • Enters data into a table • 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 ‘ ‘
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
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
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
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