Discovering SQL

508 676 0
Discovering SQL

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info www.it-ebooks.info ffirs.indd ii 3/15/2011 1:29:22 PM DISCOVERING SQL INTRODUCTION xxv CHAPTER Drowning in Data, Dying of Thirst for Knowledge CHAPTER Breaking and Entering: Structured Information 29 CHAPTER A Thing You Can Relate To — Designing a Relational Database 79 CHAPTER Overcoming the Limitations of SQL 103 CHAPTER Grouping and Aggregation 137 CHAPTER When One Is Not Enough: A Query Within a Query 155 CHAPTER You Broke It; You Fix It: Combining Data Sets 173 CHAPTER What Else Is There, and Why? 207 CHAPTER Optimizing Performance 231 CHAPTER 10 Multiuser Environment 251 CHAPTER 11 Working with Unstructured and Semistructured Data 287 CHAPTER 12 Not by SQL Alone 329 APPENDIX A Installing the Library Database 353 APPENDIX B Installing RDBMSs Software 375 APPENDIX C Accessing RDBMSs 377 APPENDIX D Accessing RDBMSs with the SQuirreL Universal SQL Client 379 INDEX 381 www.it-ebooks.info ffirs.indd i 3/15/2011 1:29:21 PM www.it-ebooks.info ffirs.indd ii 3/15/2011 1:29:22 PM Discovering SQL www.it-ebooks.info ffirs.indd iii 3/15/2011 1:29:22 PM www.it-ebooks.info ffirs.indd iv 3/15/2011 1:29:22 PM Discovering SQL A HANDS-ON GUIDE FOR BEGINNERS Alex Kriegel www.it-ebooks.info ffirs.indd v 3/15/2011 1:29:22 PM Discovering SQL Published by Wiley Publishing, Inc 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2011 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-118-00267-4 ISBN: 978-1-118-09279-8 (ebk) ISBN: 978-1-118-09277-4 (ebk) ISBN: 978-1-118-09278-1 (ebk) Manufactured in the United States of America 10 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose No warranty may be created or extended by sales or promotional materials The advice and strategies contained herein may not be suitable for every situation This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services If professional assistance is required, the services of a competent professional person should be sought Neither the publisher nor the author shall be liable for damages arising herefrom The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002 Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available in electronic books Library of Congress Control Number: 2011922790 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc and/or its affiliates, in the United States and other countries, and may not be used without written permission All other trademarks are the property of their respective owners Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book www.it-ebooks.info ffirs.indd vi 3/15/2011 1:29:25 PM To Liana www.it-ebooks.info ffirs.indd vii 3/15/2011 1:29:25 PM www.it-ebooks.info ffirs.indd viii 3/15/2011 1:29:25 PM 10 x APPENDIX D ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT The two tabs on the left, Drivers and Aliases, allow you to configure the client for communication with the relational database of your choice Let’s start with Drivers fi rst Here’s a step-by-step procedure to register the JDBC drivers for Oracle, IBM DB2, Microsoft SQL Server, PostgreSQL, and MySQL RDBMSs Connecting to Microsoft Access and Hyper Structured Query Language Database (HSQLDB) embedded with OpenOffice BASE requires additional steps Make sure to check this book’s accompanying websites for the additional materials The developers of the application provided a rather comprehensive list of JDBC drivers’ templates in lieu of the drivers that they are not allowed to distribute To shorten the list of drivers to display only those loaded, click the last button, as shown in Figure D-12 (the adjacent button would display the full list again) FIGURE D-12 As you can see in Figure D-13, there are quite a few drivers that you can use to connect to different RDBMSs, with some listing more than one JDBC driver template Let’s go through the process of adding and configuring JDBC drivers for Microsoft SQL Server 2008 www.it-ebooks.info both03.indd 10 3/22/2011 2:04:25 PM Configuring SQuirreL Universal SQL Client x 11 FIGURE D-13 Once you’ve located a driver’s template for your particular RDBMS (Microsoft SQL Server 2008 in this case), select it and choose the Modify Driver option from the right-click menu, as shown in Figure D-14 FIGURE D-14 www.it-ebooks.info both03.indd 11 3/22/2011 2:04:25 PM 12 x APPENDIX D ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT If you are unfamiliar with Java world conventions, here is a basic overview as related to JDBC drivers’ functionality Java classpath is an environmental variable that holds information about directories in which JVM will look for Java classes requested by the application If your JDBC JAR files were copied to any of the directories listed in the classpath, they would have been picked by the application Alternatively, you can add additional classpath to the configuration by clicking the Extra Class Path tab, shown in Figure D-15 FIGURE D-15 Once on the Extra Class Path tab, click the Add button to navigate to the location of the JDBC JAR files you’ve just downloaded/installed, as shown in Figure D-16 The Name field on the screen shown in Figure D-15 is but a label for your driver, so make sure to add some descriptive name www.it-ebooks.info both03.indd 12 3/22/2011 2:04:26 PM Configuring SQuirreL Universal SQL Client x 13 FIGURE D-16 After navigating to the file (in our case, it was installed in C:\Discovering SQL\database connection\sqljdbc_3.0\enu), select the file name sqljdbc4.jar, as shown in Figure D-17, and click Open FIGURE D-17 If the required driver class does not appear in the drop-down box with the label Class Name, click the List Drivers button Successful registration will look like that shown in Figure D-18 www.it-ebooks.info both03.indd 13 3/22/2011 2:04:26 PM 14 x APPENDIX D ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT FIGURE D-18 After you click OK, the driver will be registered, and the confirmation message highlighted in green will appear in the bottom pane of the application’s window, as shown in Figure D-19 FIGURE D-19 www.it-ebooks.info both03.indd 14 3/22/2011 2:04:26 PM Configuring SQuirreL Universal SQL Client x 15 The same process should be repeated for every additional JDBC driver you’ve decided to register with your SQuirreL Universal SQL Client application The following several steps show how to register drivers for other RDBMSs Once you’ve added classpath and listed drivers, make sure that the driver class has been successfully loaded, which is indicated by the success message highlighted in green in the bottom pane of the application Table D-2 lists the libraries (fi les) which contain the drivers along with connection strings (URL) to be used for SQuirreL configuration TABLE D-2: Connection URLs for JDBC RDBMS JDBC DRIVER JAR FILE CONNECTION URL Oracle ojdbc14.jar jdbc:oracle:thin:@localhost:1521:XE IBM DB2 db2jcc4.jar jdbc:db2://localhost:50000/library Microsoft SQL Server 2008 sqljdbc4.jar jdbc:sqlserver:// localhost:1443;integrated security = SSPI PostgreSQL postgresql-9.0-801 jdbc4.jar jdbc:postgresql://localhost:5432/ template1 MySQL mysql-connector-java5.1.13-bin.jar jdbc:mysql://localhost:3306 Oracle Thin Driver To register Oracle Thin JDBC driver, add a classpath to point to the ojdbc14.jar fi le downloaded from Oracle’s site (refer to Table D-2), or later version The configuration is shown in Figure D-20 FIGURE D-20 www.it-ebooks.info both03.indd 15 3/22/2011 2:04:27 PM 16 x APPENDIX D ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT IBM DB2 IBM supplies JDBC drivers with its installation, as well as makes them available for separate download In our case, the required driver can be located in the C:\Program Files\IBM\SQLLIB\java directory At least two fi les must be present in the directory for the driver to register successfully: db2jcc4.jar and db2jcc_license_cu.jar The former contains the classes needed to connect to the IBM DB2 database (see Figure D-21) FIGURE D-21 PostgreSQL Driver The latest, as of the time of writing, the PostgreSQL JDBC driver available for download at the URL listed in Table D-1, is contained in a single fi le postgresql-9.0-801.jdbc4.jar The registration example is presented in Figure D-22 www.it-ebooks.info both03.indd 16 3/22/2011 2:04:27 PM Download from Wow! eBook Configuring SQuirreL Universal SQL Client x 17 FIGURE D-22 MySQL The latest MySQL JDBC driver available for download is contained in the mysql-connector-java5.1.13-bin.jar fi le The sample configuration is shown in Figure D-23 FIGURE D-23 www.it-ebooks.info both03.indd 17 3/22/2011 2:04:27 PM 18 x APPENDIX D ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT Creating Aliases for RDBMS Connections Once you have all drivers registered within your SQuirreL application, you are ready to connect to your RDBMS The beauty of using a universal client is that you can connect to any RDBMS of your choice (that is, for which you have JDBC driver registered) or all of them at once Connections to specific instances of RDBMSs are added though the Aliases tab By creating an alias and adding it to the list, you create a connection to a specific RDBMS instance based upon one of the RDBMS-specific JDBC drivers you have just registered You add an alias by clicking the blue plus sign, as shown in Figure D-24 FIGURE D-24 Nothing prevents you from creating as many alias-connections as you want to as many RDBMSs as you need, including multiple connections to the same instance of the database Configuring an Alias for Microsoft SQL Server 2008 On the alias configuration screen shown in Figure D-25, select an appropriate driver for the RDBMS you want to connect to (Microsoft SQL Server in this case) www.it-ebooks.info both03.indd 18 3/22/2011 2:04:27 PM Configuring SQuirreL Universal SQL Client x 19 FIGURE D-25 The next steps consist of specifying the alias name (a free-form label), URL, the address by which the driver will locate your RDBMS server (refer to Table D-2 for list of connection string URLs for each respective database), and user ID and password The URL specified on the screen presented in Figure D-26 assumes SQL Server is being set up with Windows integrated security For access utilizing User ID and password, you’d have to supply valid values (and make sure they are set in your SQL Server, as well) If your system is not configured correctly, you might see this error: “This driver is not configured for integrated security authentication.” The fastest (albeit not in the spirit of best practices) way to remedy the situation would be to copy the sqljdbc_auth dll fi le to the same directory where your Microsoft SQL Server JDBC FIGURE D-26 driver sqljdbc4.jar is located (refer to Figure D-19) The authentication fi le sqljdbc_auth.dll is located in directory \auth\x86 (for 32 bit operating system) or \auth\x64 (for the 64-bit OS) right under the one holding sqljdbc4.jar, as shown in Figure D-27 www.it-ebooks.info both03.indd 19 3/22/2011 2:04:27 PM 20 x APPENDIX D ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT FIGURE D-27 Test your connection by clicking the Test button and then the Connect button on the pop-up screen shown in Figure D-28 If you have configured everything properly, the application will report its status by displaying the message shown in Figure D-29 Alternatively, a window with an error message will be displayed to provide clues about what might have gone wrong In that case, feel free to contact us via e-mail at discoveringSQL@ agilitator.com FIGURE D-28 FIGURE D-29 Now you are ready to connect to your Microsoft SQL Server 2008 database and begin running the examples and exercises in the book Configuring an Alias for Oracle 10g XE The default database (in the Oracle sense; not to be confused with a database term used in any other RDBMS context) installed with Oracle Express will be called XE, and the user is almost equivalent to what other RDBMSs call a database The configuration shown in Figure D-30 creates an alias to connect to the Oracle database using the SYSTEM user account specified during installation www.it-ebooks.info both03.indd 20 3/22/2011 2:04:28 PM Configuring SQuirreL Universal SQL Client x 21 FIGURE D-30 Once connected, you can run SQL commands to create the user LIBRARY; see Appendix C for more details A word of caution: SQuirreL saves user credentials, both passwords and user ID(s) in clear text; the warning is right there To add an additional level of security, you may choose not to store credentials, despite all the convenience, but instead type them in when connecting to the RDBMS server (see later in the chapter) Configuring an Alias for IBM DB2 LUW 9.7 Keep in mind that users in DB2 are created on the operating system level Therefore, your user ID and password would be the same as those used for logging onto your computer A sample configuration is presented in Figure D-31 Notice that in order to connect to the DB2 instance with the SQuirreL Universal SQL Client you have to specify a database, which means that it has to be there before you can connect to the instance to create it You would either have to resort to using the DB2 graphical admin tool to create the database, or run a command-line script (see Appendices A and C for examples) Alternatively, you can specify a preinstalled SAMPLE database to connect to and look around FIGURE D-31 www.it-ebooks.info both03.indd 21 3/22/2011 2:04:28 PM 22 x APPENDIX D ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT Configuring an Alias for PostgreSQL The PostgreSQL RDBMS comes with the pgAdmin III utility which is an excellent tool to connect and administer PostgreSQL database Still, SQuirreL might come in handy; the Figure D-32 shows the configuration for the PostgreSQL database connection alias You may have noticed that the connection string points to the TEMPLATE1 built-in database that serves as a blueprint for every other database created in the PostgreSQL environment It is conveniently created during the installation process; otherwise, you’d have to run the PostgreSQL-specific tool intdb command (see vendor’s documentation for more details) We are going to take a closer look at this in Appendix A when installing our sample Library database FIGURE D-32 Configuring an Alias for MySQL MySQL comes with a built-in command-line tool that many users consider an adequate interface, yet you’d appreciate the versatility and usefulness of the graphical user interface (GUI) provided by SQuirreL SQL Client Figure D-33 represents a configuration for connecting to the MySQL database as the ROOT administrative user Unlike PostgreSQL or IBM DB2, it allows you to connect to a server; no database is required FIGURE D-33 The aliases can be modifi ed at any time, either through a right-click menu or through a graphical toolbar at the top of the pane Tooltips help you learn the environment; just hover the mouse pointer over a button on the toolbar You may wonder about the JDBC connection to Microsoft Access The good news is that there are JDBC drivers for Access; the bad news is that they are all commercial Nevertheless, you still can www.it-ebooks.info both03.indd 22 3/22/2011 2:04:29 PM Configuring SQuirreL Universal SQL Client x 23 connect to Access mdb and accdb files through the JDBC.ODBC bridge driver listed on your drivers tab This would require an additional Open Database Connectivity (ODBC) data source to be configured on the computer If you are interested to learn more, check out www.wrox.com and www agilitator.com for more resources Running SQL Samples Against RDBMSs With so much effort put into installing and configuring SQuirreL Universal SQL Client, we are ready to reap the rewards Connecting to the database for which you have already created an alias is a snap Double-click the alias, and click Connect (if you not store user ID or password as alias properties, this would be the time to type them in) As shown in Figure D-34, the connection opens a new pane on the right hand, with four tabs: Objects, SQL, Hibernate, and Monitor For the purposes of this book, only the SQL tab is necessary FIGURE D-34 As you can see, you can open a connection to several RDBMSs (or several connections to the same RDBMS) Figure D-34 shows that both SQL Server and MySQL connections are open, each in its own set of tabs, with only one displayed at a time You can switch between the environments with the click of a mouse The lower pane displays the status of the executed queries, while the middle will display the data sets returned A query typed into the SQL pane gets executed by clicking the button with a running figure on the toolbar (alternatively you can use the shortcut Ctrl+Enter) www.it-ebooks.info both03.indd 23 3/22/2011 2:04:29 PM 24 x APPENDIX D ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT Despite being in development for quite some time, the tool might not handle all quirks a particular RDBMS might throw at it…yet For instance, it cannot handle binary data types, and the date data type might be a challenge sometimes Be patient; whenever new updates are released, the tool will notify you and also help with installation Download from Wow! eBook Figure D-35 illustrates all three concepts: It inserts a record into the Library’s BOOKS table, and retrieves with a SELECT query (not shown on the screen, but it is part of the batch typed into the SQL pane); the results of the execution are displayed in the lower pane FIGURE D-35 The SQuirreL Universal SQL Client tool packs a lot of features and certainly deserves a book of its own This appendix is but a brief introduction into how to use this tool to learn SQL using different RDBMSs, and not having to learn this RDBMS-specific set of tools www.it-ebooks.info both03.indd 24 3/22/2011 2:04:29 PM ... IBM DB2 LUW Oracle Microsoft SQL Server Microsoft Access PostgreSQL MySQL HSQLDB and OpenOffice BASE 2 2 3 3 4 4 6 7 8 What Is SQL? The SQL Standard Dialects of SQL Not the Only Game in Town... Microsoft SQL Server 2008 MySQL PostgreSQL Microsoft Access 2007/2010 Open Office BASE with HSQLDB 377 377 377 378 378 378 378 APPENDIX D: ACCESSING RDBMSS WITH THE SQUIRREL UNIVERSAL SQL CLIENT... Synonyms Auto-Incremented Values Identity Columns Microsoft SQL Server IBM DB2 PostgreSQL MySQL Microsoft Access OpenOffice BASE with HSQLDB Who Am I: Finding One’s IDENTITY Sequences Comparing

Ngày đăng: 27/03/2019, 16:49

Mục lục

  • WroxBooks

    • Discovering SQL

      • CONTENTS

      • INTRODUCTION

      • CHAPTER 1: DROWNING IN DATA, DYING OF THIRST FOR KNOWLEDGE

        • Data Deluge and Informational Overload

          • Database Management Systems (DBMSs)

            • Storage Capacity

            • Number of Users

            • Security

            • Performance

            • Scalability

            • Costs

            • Recording Data

              • Oral Records

              • Pictures

              • Written Records

              • Printed Word

              • All of the Above

              • Analog versus Digital Data

              • To Store or Not to Store?

              • Relational Database Management Systems

                • IBM DB2 LUW

                • Oracle

                • Microsoft SQL Server

                • Microsoft Access

                • PostgreSQL

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

  • Đang cập nhật ...

Tài liệu liên quan