DB Creation with SQL DDL Outline SQL Concepts Data Types Schema/Table/View Creation Transactions and Access Control
Objectives of SQL Ideally, database language should allow user to: create the database and relation structures; perform insertion, modification, deletion of data from relations; perform simple and complex queries. SQL consists of two major components: A DDL for defining database structure. A DML for retrieving and updating data. Until SQL3, SQL did not contain flow of control commands. Cf. Flow of control: sequence, branch, loop, (function call) Standard English Words in SQL 1) CREATE TABLE Staff(staffNo VARCHAR(5), lname VARCHAR(15), salary DECIMAL(7,2)); 2) INSERT INTO Staff VALUES ( SG16, Brown, 8300); 3) SELECT staffno, lname, salary FROM Staff WHERE salary > 10000; Used by application developers, DBAs, management, and even end users. ISO SQL standards vs. Vendor-specific features
ISO SQL Data Types More on Data Types Exact Numeric: NUMERIC, DECIMAL, INTEGER, SMALLINT Fixed point numbers: Precision and Scale [ ][ ][ ].[ ][ ] : NUMERIC (5,2) Approximate Numeric Floating point numbers: Fraction and Exponent Characters and Bits 2 8 = 256 : A byte can represent 256 distinct symbols (signals) ASCII needs only 128 symbols => MSB for control How to handle Asian alphabets such as Korean Two byte code: [Byte-1][Byte-2]
Common Error in Floating Point Number Comparison #include <stdio.h> main() { float a, b, c; a = 1.345f; b = 1.123f; c = a + b; if (c == 2.468) printf( Equal.\n"); else printf( Not equal! The value of c is %13.10f,or %f",c,c); } Execution Not equal! The value of c is 2.4679999352 or 2.468000. IEEE 754 for Floating Point Numbers Single-Precision Sign: 1 bit, Exponent: 8 bits (with bias), Fraction: 23 bits Example: -118.625 Negative Sign bit = 1 118.625 (10) = 1110110.101 (2) 1110110.101 = 1.110110101 x 2 6 Fraction = 110110101 with following zero s up to 23 bits Exponent: 6 127(2 8 /2 1) + 6 = 133 = 10000101 (2) 1 8 23 +-+--------+-----------------------+ S Exp Fraction 1 10000101 11011010100000000000000 +-+--------+-----------------------+ 31 30 23 22 0
Data Definition Main SQL DDL statements are: CREATE SCHEMA CREATE/ALTER TABLE CREATE VIEW DROP SCHEMA DROP TABLE DROP VIEW Many DBMSs also provide: CREATE INDEX DROP INDEX CREATE SCHEMA / TABLE CREATE SCHEMA Name DROP SCHEMA Name CREATE TABLE PropertyForRent ( propertyno varchar(5) NOT NULL, rooms smallint NOT NULL, DEFAULT 4, rent decimal(6,2) NOT NULL, DEFAULT 600, ownerno varchar(5) NOT NULL, staffno varchar(5), branchno varchar(5) NOT NULL, PRIMARY KEY (propertyno), FOREIGN KEY (staffno) REFERENCES Staff )
Integrity Constraints Entity Integrity Primary key of a table must contain a unique, non-null value for each record. PRIMARY KEY (propertyno) Can only have one PRIMARY KEY clause per table. Can still ensure uniqueness for alternate keys using UNIQUE: UNIQUE(telNo) Referential Integrity if FK contains a value, it must refer to an existing row in its parent table. FOREIGN KEY(branchNo) REFERENCES Branch FOREIGN KEY (staffno) REFERENCES Staff ON DELETE SET NULL FOREIGN KEY (ownerno) REFERENCES Owner ON UPDATE CASCADE Enterprise Constraints CREATE ASSERTION StaffNotHandlingTooMuch CHECK (NOT EXISTS (SELECT staffno FROM PropertyForRent GROUP BY staffno HAVING COUNT(*) > 100)) ALTER/DROP TABLE ALTER TABLE Client ADD prefnorooms PRooms; DROP TABLE TableName [RESTRICT CASCADE] DROP TABLE PropertyForRent; Removes named table and all rows within it. With RESTRICT, if any other objects depend for their existence on continued existence of this table, SQL does not allow it. With CASCADE, SQL drops all dependent objects (and objects dependent on these objects).
CREATE VIEW Create view so that manager at branch B003 can only see details for staff who work in his or her office. CREATE VIEW Manager3Staff AS SELECT * FROM Staff WHERE branchno = B003 ; Create view of staff details at branch B003 excluding salaries. CREATE VIEW Staff3 AS SELECT staffno, fname, lname, position, sex FROM Staff WHERE branchno = B003 ; CREATE INDEX CREATE INDEX myindex on Staff(Name); Staff
Transactions Concurrency Control Normal Banking Your Account = 50,000: Balance (DB) = 50,000 Read it at Branch A: Balance (Teller A) = 50,000 Draw 30,000: Balance (Teller A) = 20,000 Write it back: Balance (DB) = 20,000 Abnormal Banking Your Account = 50,000 Read it at Branch A: Balance (DB) = 50,000 Draw 30,000: Balance (Teller A) = 20,000 Read it at Branch B: Balance (Teller B) = 50,000 (Oops!!) Draw 40,000: Balance (Teller B) = 10,000 Write it back at A: Balance (DB) = 20,000 Write it back at B: Balance (DB) = 10,000 In Your Pocket = 70,000 while Balance is still 10,000 Transactions Atomicity Consistency Isolation Durability Transactions SQL defines transaction model based on COMMIT and ROLLBACK. Transaction is logical unit of work with one or more SQL statements guaranteed to be atomic with respect to recovery. An SQL transaction automatically begins with a transaction-initiating SQL statement (e.g., SELECT, INSERT). Changes made by transaction are not visible to other concurrently executing transactions until transaction completes.
Access Control Give Manager full privileges to Staff table. GRANT ALL PRIVILEGES ON Staff TO Manager WITH GRANT OPTION; Give users Personnel and Director SELECT and UPDATE on column salary of Staff. GRANT SELECT, UPDATE (salary) ON Staff TO Personnel, Director; Give all users SELECT on Branch table. GRANT SELECT ON Branch TO PUBLIC; Revoke privilege SELECT on Branch table from all users. REVOKE SELECT ON Branch FROM PUBLIC; Revoke all privileges given to Director on Staff table. REVOKE ALL PRIVILEGES ON Staff FROM Director; Summary Transforming ER Diagrams into Relational Schema Normalization SQL Concepts Data Types Schema/Table/View Creation Transactions and Access Control