SQL VISUAL QUICKSTART GUIDE- P8 pdf

10 428 1
SQL VISUAL QUICKSTART GUIDE- P8 pdf

Đang tải... (xem toàn văn)

Thông tin tài liệu

Other normal forms Higher levels of normalization exist, but the relational model doesn’t require (or even mention) them. They’re useful in some cases to avoid redundancy. Briefly, they are: ◆ Boyce-Codd normal form is a more rigor- ous version of 3NF. BCNF deals with tables that have multiple candidate keys, composite candidate keys, or candidate keys that overlap. A table is in BCNF if every determinant is a candidate key. (A determinant column is one on which some of the columns are fully functionally dependent.) ◆ Atable in fourth normal form is in BCNF and has no multivalued dependencies (MVDs). An MVD occurs when in a table containing at least three columns, one column has multiple rows whose values match a value of a single row of one of the other columns. Suppose that employees can be assigned to multiple projects and each employee can have multiple skills. If you stuff all this information into a single table, you must use all three attributes as the key because nothing less can identify a row uniquely. The relationship between emp_id and proj_id is an MVD because for each pair of emp_id / skill_id values in the table, only emp_id (independent of skill_id ) determines the associated set of proj_id values. The relationship between emp_id and skill_id also is an MVD because the set of skill values for an emp_id / proj_id pair always depends on only emp_id . To transform a table with MVDs to 4NF, move each MVD pair to a new table. ◆ Atable in fifth normal form is in 4NF and has no join dependencies, which are gen- eralizations of MVDs. The aim of 5NF is to have tables that can’t be decomposed further into any number of smaller tables. The redundancies and anomalies that 5NF cures are rare and unintuitive. In real databases, you’ll see 1NF, 2NF, 3NF, and occasionally 4NF. 4NF and even 3NF tables almost always are 5NF too. 50 Chapter 2 Normalization Denormalization The increased number of tables that normalization generates might sway you to denormalize your database to speed queries (because having fewer tables reduces computationally expen- sive joins and disk I/O). This common technique trades off data integrity for performance and presents a few other problems. A denormalized database: ◆ Usually is harder to understand than a normalized one ◆ Usually makes retrievals faster but updates slower ◆ Increases the risk of inserting inconsistent data ◆ Might improve the performance of some database applications but hurt that of others (because users’ table-access patterns change over time) The need for denormalization isn’t a weakness in the relational model but reveals a flawed implementation of the model in DBMSs. A common use for denormalized tables is as perma- nent logs of data copied from other tables. The logged rows are redundant, but because they’re only INSERT ed (never UPDATE d), they serve as an audit trail immune to future changes in the source tables. The Sample Database Pick up an SQL or database-design book, and probably you’ll find a students/courses/ teachers, customers/orders/products, or authors/books/publishers database. In a bow to convention, most of the SQL examples in this book use an authors/books/publishers sample database named books . Here are some things that you should know about books : ◆ Recall from “Tables, Columns, and Rows” earlier in this chapter that a database appears to the user as a collection of tables (and nothing but tables). books contains five tables that contain infor- mation about authors, titles they’ve pub- lished, their publishers, and their royal- ties. Figure 2.21 depicts the tables and relationships in books by using the graphical conventions introduced earlier in this chapter. ◆ The SQL statements in Chapters 10 and later modify data in books (rather than just retrieve data). Unless I note otherwise, each new section in a chapter starts with a pristine copy of books . In other words, assume that database changes made in one section don’t carry over to the next section. ◆ Some of the concepts mentioned in this section, such as data types and nulls, are covered in the next chapter. ◆ books is a teaching tool; its structure and size don’t approach the complexity of real production databases. ◆ To create the sample database on your own DMBS, see “Creating the Sample Database” later in this chapter. 51 The Relational Model The Sample Database authors au_id au_fname au_lname phone address city state zip title_authors title_id au_id au_order royalty_share publishers pub_id pub_name city state country titles title_id title_name type pub_id pages price sales pubdate contract royalties title_id advance royalty_rate Figure 2.21 The sample database books . The table authors The table authors describes the books’ authors. Each author has a unique identifier that’s the primary key. Table 2.3 shows the structure of the table authors , and Figure 2.22 shows its contents. 52 Chapter 2 The Sample Database Table 2.3 authors Table Structure Column Name Description Data Type Nulls? Keys au_id Unique author identifier CHAR(3) PK au_fname Author first name VARCHAR(15) au_lname Author last name VARCHAR(15) phone Author telephone number VARCHAR(12) Yes address Author address VARCHAR(20) Yes city Author city VARCHAR(15) Yes state Author state CHAR(2) Yes zip Author zip (postal) code CHAR(5) Yes au_id au_fname au_lname phone address city state zip A01 Sarah Buchman 718-496-7223 75 West 205 St Bronx NY 10468 A02 Wendy Heydemark 303-986-7020 2922 Baseline Rd Boulder CO 80303 A03 Hallie Hull 415-549-4278 3800 Waldo Ave, #14F San Francisco CA 94123 A04 Klee Hull 415-549-4278 3800 Waldo Ave, #14F San Francisco CA 94123 A05 Christian Kells 212-771-4680 114 Horatio St New York NY 10014 A06 Kellsey 650-836-7128 390 Serra Mall Palo Alto CA 94305 A07 Paddy O'Furniture 941-925-0752 1442 Main St Sarasota FL 34236 Figure 2.22 The contents of the table authors . The table publishers The table publishers describes the books’ publishers. Every publisher has a unique identifier that’s the primary key. Table 2.4 shows the structure of the table publishers , and Figure 2.23 shows its contents. 53 The Relational Model The Sample Database Table 2.4 publishers Table Structure Column Name Description Data Type Nulls Keys pub_id Unique publisher identifier CHAR(3) PK pub_name Publisher name VARCHAR(20) city Publisher city VARCHAR(15) state Publisher state/province CHAR(2) Yes country Publisher country VARCHAR(15) pub_id pub_name city state country P01 Abatis Publishers New York NY USA P02 Core Dump Books San Francisco CA USA P03 Schadenfreude Press Hamburg NULL Germany P04 Tenterhooks Press Berkeley CA USA Figure 2.23 The contents of the table publishers . The table titles The table titles describes the books. Every book has a unique identifier that’s the pri- mary key. titles contains a foreign key, pub_id , that references the table publishers to indicate a book’s publisher. Table 2.5 shows the structure of the table titles , and Figure 2.24 shows its contents. 54 Chapter 2 The Sample Database Table 2.5 titles Table Structure Column Name Description Data Type Nulls? Keys title_id Unique title identifier CHAR(3) PK title_name Book title VARCHAR(40) type Subject of the book VARCHAR(10) Yes pub_id Publisher identifier CHAR(3) FK publishers(pub_id) pages Page count INTEGER Yes price Cover price DECIMAL(5,2) Yes sales INTEGER Yes pubdate Date of publication DATE Yes contract SMALLINT Nonzero if author(s) signed contract Lifetime number of copies sold title_id title_name type pub_id pages price sales pubdate contract T01 1977! history P01 107 21.99 566 2000-08-01 1 T02 200 Years of German Humor history P03 14 19.95 9566 1998-04-01 1 T03 Ask Your System Administrator computer P02 1226 39.95 25667 2000-09-01 1 T04 But I Did It Unconsciously psychology P04 510 12.99 13001 1999-05-31 1 T05 Exchange of Platitudes psychology P04 201 6.95 201440 2001-01-01 1 T06 How About Never? biography P01 473 19.95 11320 2000-07-31 1 T07 I Blame My Mother biography P03 333 23.95 1500200 1999-10-01 1 T08 Just Wait Until After School children P04 86 10.00 4095 2001-06-01 1 T09 Kiss My Boo-Boo children P04 22 13.95 5000 2002-05-31 1 T10 Not Without My Faberge Egg biography P01 NULL NULL NULL NULL 0 T11 Perhaps It's a Glandular Problem psychology P04 826 7.99 94123 2000-11-30 1 T12 Spontaneous, Not Annoying biography P01 507 12.99 100001 2000-08-31 1 T13 What Are The Civilian Applications? history P03 802 29.99 10467 1999-05-31 1 Figure 2.24 The contents of the table titles . The table title_authors Authors and books have a many-to-many relationship, because an author can write multiple books and a book can have multiple authors. title_authors is the junction table that associates the tables authors and titles ; see “Relationships” earlier in this chapter. title_id and au_id together form a composite primary key, and each column separately is a foreign key that references titles and authors , respectively. The non- key columns indicate the order of the author’s name on the book’s cover (always 1 for a book with a sole author) and the fraction of total royalties that each author receives (always 1.0 for a book with a sole author). Table 2.6 shows the structure of the table title_authors , and Figure 2.25 shows its contents. 55 The Relational Model The Sample Database title_id au_id au_order royalty_share T01 A01 1 1.00 T02 A01 1 1.00 T03 A05 1 1.00 T04 A03 1 0.60 T04 A04 2 0.40 T05 A04 1 1.00 T06 A02 1 1.00 T07 A02 1 0.50 T07 A04 2 0.50 T08 A06 1 1.00 T09 A06 1 1.00 T10 A02 1 1.00 T11 A03 2 0.30 T11 A04 3 0.30 T11 A06 1 0.40 T12 A02 1 1.00 T13 A01 1 1.00 Figure 2.25 The contents of the table title_authors . Table 2.6 title_authors Table Structure Column Name Description Data Type Nulls? Keys title_id Title identifier CHAR(3) PK, FK titles(title_id) au_id Author identifier CHAR(3) PK, FK authors(au_id) au_order Author name order on book cover SMALLINT royalty_share Author fractional royalty share DECIMAL(5,2) The table royalties The table royalties specifies the royalty rate paid to all the authors (not each author) of each book, including the total up-front advance against royalties paid to all authors (again, not each author) of a book. The royalties primary key is title_id . The table royalties has a one-to-one relation- ship with titles , so the royalties primary key also is a foreign key that references the titles primary key. Table 2.7 shows the structure of the table royalties , and Figure 2.26 shows its contents. 56 Chapter 2 The Sample Database title_id advance royalty_rate T01 10000.00 0.05 T02 1000.00 0.06 T03 15000.00 0.07 T04 20000.00 0.08 T05 100000.00 0.09 T06 20000.00 0.08 T07 1000000.00 0.11 T08 0.00 0.04 T09 0.00 0.05 T10 NULL NULL T11 100000.00 0.07 T12 50000.00 0.09 T13 20000.00 0.06 Figure 2.26 The contents of the table royalties . Table 2.7 royalties Table Structure Column Name Description Data Type Nulls? Keys title_id Unique title identifier CHAR(3) PK, FK titles(title_id) advance Up-front payment to author(s) DECIMAL(9,2) Yes royalty_rate Fraction of revenue paid author(s) DECIMAL(5,2) Yes Creating the Sample Database To create (or re-create) the database books on your own DMBS, visit www.fehily.com , click the Downloads link for this book, and then follow the onscreen instructions. Creating books is a two-step process: 1. Use your DBMS’s built-in tools to create a new, blank database named books . 2. Run an SQL script that creates tables within books and populates them with data. Listing 2.1 shows a standard (ANSI) SQL script that creates the sample-database tables and inserts rows into them. ✔ Tip ■ If you’re using Microsoft Access, you don’t two-step— you simply open an .mdb file in Access. 57 The Relational Model Creating the Sample Database Listing 2.1 This standard SQL script, books_standard.sql , creates the tables in the sample database books and populates them with data. The file that you download at the companion website includes versions of this script changed to run on specific DBMSs. DROP TABLE authors; CREATE TABLE authors ( au_id CHAR(3) NOT NULL, au_fname VARCHAR(15) NOT NULL, au_lname VARCHAR(15) NOT NULL, phone VARCHAR(12) , address VARCHAR(20) , city VARCHAR(15) , state CHAR(2) , zip CHAR(5) , CONSTRAINT pk_authors PRIMARY KEY (au_id) ); INSERT INTO authors VALUES('A01','Sarah','Buchman','718-496-7223', '75 West 205 St','Bronx','NY','10468'); INSERT INTO authors VALUES('A02','Wendy','Heydemark','303-986-7020', '2922 Baseline Rd','Boulder','CO','80303'); INSERT INTO authors VALUES('A03','Hallie','Hull','415-549-4278', '3800 Waldo Ave, #14F','San Francisco','CA','94123'); (listing continues on next page) Listing 58 Chapter 2 Creating the Sample Database Listing 2.1 continued INSERT INTO authors VALUES('A04','Klee','Hull','415-549-4278', '3800 Waldo Ave, #14F','San Francisco','CA','94123'); INSERT INTO authors VALUES('A05','Christian','Kells','212-771-4680', '114 Horatio St','New York','NY','10014'); INSERT INTO authors VALUES('A06','','Kellsey','650-836-7128', '390 Serra Mall','Palo Alto','CA','94305'); INSERT INTO authors VALUES('A07','Paddy','O''Furniture','941-925-0752', '1442 Main St','Sarasota','FL','34236'); DROP TABLE publishers; CREATE TABLE publishers ( pub_id CHAR(3) NOT NULL, pub_name VARCHAR(20) NOT NULL, city VARCHAR(15) NOT NULL, state CHAR(2) , country VARCHAR(15) NOT NULL, CONSTRAINT pk_publishers PRIMARY KEY (pub_id) ); INSERT INTO publishers VALUES('P01','Abatis Publishers','New York','NY','USA'); INSERT INTO publishers VALUES('P02','Core Dump Books','San Francisco','CA','USA'); INSERT INTO publishers VALUES('P03','Schadenfreude Press','Hamburg',NULL,'Germany'); INSERT INTO publishers VALUES('P04','Tenterhooks Press','Berkeley','CA','USA'); DROP TABLE titles; CREATE TABLE titles ( title_id CHAR(3) NOT NULL, title_name VARCHAR(40) NOT NULL, type VARCHAR(10) , pub_id CHAR(3) NOT NULL, pages INTEGER , price DECIMAL(5,2) , sales INTEGER , pubdate DATE , contract SMALLINT NOT NULL, CONSTRAINT pk_titles PRIMARY KEY (title_id) ); INSERT INTO titles VALUES('T01','1977!','history','P01', 107,21.99,566,DATE '2000-08-01',1); INSERT INTO titles VALUES('T02','200 Years of German Humor','history','P03', 14,19.95,9566,DATE '1998-04-01',1); INSERT INTO titles VALUES('T03','Ask Your System Administrator','computer','P02', 1226,39.95,25667,DATE '2000-09-01',1); INSERT INTO titles VALUES('T04','But I Did It Unconsciously','psychology','P04', 510,12.99,13001,DATE '1999-05-31',1); INSERT INTO titles VALUES('T05','Exchange of Platitudes','psychology','P04', 201,6.95,201440,DATE '2001-01-01',1); INSERT INTO titles VALUES('T06','How About Never?','biography','P01', 473,19.95,11320,DATE '2000-07-31',1); (listing continues on next page) Listing 59 The Relational Model Creating the Sample Database Listing 2.1 continued INSERT INTO titles VALUES('T07','I Blame My Mother','biography','P03', 333,23.95,1500200,DATE '1999-10-01',1); INSERT INTO titles VALUES('T08','Just Wait Until After School','children','P04', 86,10.00,4095,DATE '2001-06-01',1); INSERT INTO titles VALUES('T09','Kiss My Boo-Boo','children','P04', 22,13.95,5000,DATE '2002-05-31',1); INSERT INTO titles VALUES('T10','Not Without My Faberge Egg','biography','P01', NULL,NULL,NULL,NULL,0); INSERT INTO titles VALUES('T11','Perhaps It''s a Glandular Problem','psychology','P04', 826,7.99,94123,DATE '2000-11-30',1); INSERT INTO titles VALUES('T12','Spontaneous, Not Annoying','biography','P01', 507,12.99,100001,DATE '2000-08-31',1); INSERT INTO titles VALUES('T13','What Are The Civilian Applications?','history','P03', 802,29.99,10467,DATE '1999-05-31',1); DROP TABLE title_authors; CREATE TABLE title_authors ( title_id CHAR(3) NOT NULL, au_id CHAR(3) NOT NULL, au_order SMALLINT NOT NULL, royalty_share DECIMAL(5,2) NOT NULL, CONSTRAINT pk_title_authors PRIMARY KEY (title_id, au_id) ); INSERT INTO title_authors VALUES('T01','A01',1,1.0); INSERT INTO title_authors VALUES('T02','A01',1,1.0); INSERT INTO title_authors VALUES('T03','A05',1,1.0); INSERT INTO title_authors VALUES('T04','A03',1,0.6); INSERT INTO title_authors VALUES('T04','A04',2,0.4); INSERT INTO title_authors VALUES('T05','A04',1,1.0); INSERT INTO title_authors VALUES('T06','A02',1,1.0); INSERT INTO title_authors VALUES('T07','A02',1,0.5); INSERT INTO title_authors VALUES('T07','A04',2,0.5); INSERT INTO title_authors VALUES('T08','A06',1,1.0); INSERT INTO title_authors VALUES('T09','A06',1,1.0); INSERT INTO title_authors VALUES('T10','A02',1,1.0); INSERT INTO title_authors VALUES('T11','A03',2,0.3); INSERT INTO title_authors VALUES('T11','A04',3,0.3); INSERT INTO title_authors VALUES('T11','A06',1,0.4); INSERT INTO title_authors VALUES('T12','A02',1,1.0); INSERT INTO title_authors VALUES('T13','A01',1,1.0); DROP TABLE royalties; CREATE TABLE royalties ( title_id CHAR(3) NOT NULL, advance DECIMAL(9,2) , royalty_rate DECIMAL(5,2) , CONSTRAINT pk_royalties PRIMARY KEY (title_id) ); (listing continues on next page) Listing . an SQL or database-design book, and probably you’ll find a students/courses/ teachers, customers/orders/products, or authors/books/publishers database. In a bow to convention, most of the SQL. blank database named books . 2. Run an SQL script that creates tables within books and populates them with data. Listing 2.1 shows a standard (ANSI) SQL script that creates the sample-database tables. Access. 57 The Relational Model Creating the Sample Database Listing 2.1 This standard SQL script, books_standard .sql , creates the tables in the sample database books and populates them with data.

Ngày đăng: 05/07/2014, 05:20

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Introduction

  • About SQL

  • About This Book

  • What You’ll Need

  • Chapter 1: DBMS Specifics

    • Running SQL Programs

    • Microsoft Access

    • Microsoft SQL Server

    • Oracle

    • IBM DB2

    • MySQL

    • PostgreSQL

    • Chapter 2: The Relational Model

      • Tables, Columns, and Rows

      • Primary Keys

      • Foreign Keys

      • Relationships

      • Normalization

      • The Sample Database

      • Creating the Sample Database

      • Chapter 3: SQL Basics

        • SQL Syntax

Tài liệu cùng người dùng

Tài liệu liên quan