
4730ch01.qxd 3/28/2000 9:33 AM Page 1 CHAPTER 1 SQL AND DATA CHAPTER OBJECTIVES In this chapter, you will learn about: ✔ Data, Databases, and the Definition of SQL Page 2 ✔ A Case Study Schema Diagram Page 9 ✔ Referential Integrity and Table Relationships Page 15 ✔ The SQL*Plus Environment Page 20 hat is SQL? SQL (pronounced sequel) is an acronym for Structured WQuery Language, a standardized language used to access and manip- ulate data. Before you begin to use SQL, however, you must learn about data and how it is designed and organized for use in a database. You will then learn how to use SQL to access that data. Organizing data into a schema diagram is necessary for good software de- velopment. Before you begin to write SQL, it is important you first under- stand the components of a schema diagram. Also in this chapter, you will be introduced to Oracle’s SQL*Plus tool, which gives you access to the Oracle database. After you have performed the exercises in this chapter, you will have a good grasp of the basics of data, databases, database design, and SQL*Plus, and you will be well on your way to unleashing the power of your Oracle database. 1 4730ch01.qxd 3/28/2000 9:33 AM Page 2 2 Lab 1.1: Data, Databases, and the Definition of SQL LAB LAB 1.1 1.1 DATA, DATABASES, AND THE DEFINITION OF SQL LAB OBJECTIVES After this lab, you will be able to: ✔ Identify and Group Data ✔ Define a Database and Its Use ✔ Define SQL Data is all around you—you make use of it every day. Your hair may be brown; your flight leaves from gate K10; you try to get up in the morning at 6:30 A.M. Storing this data in related groups and making the connec- tions among them are what databases are all about. A database is a collection of grouped data that can be shared among users. For example, database systems organize and maintain patient data in a hospital, bank accounts in a bank, or inventory in a warehouse. A Database Management System (DBMS) is software that organizes the data in a meaningful way. Among its functions are controlling access to data, managing one or more concurrent users, and providing security. A Rela- tional Database Management System (RDBMS) provides all of this func- tionality. In addition, it organizes groups of data into tables, and each table consists of columns and rows. These columns and rows are specifi- cally designed to uniquely identify rows and eliminate duplication of data, providing the power of the relational database. SQL is used to communicate specifically with a relational database. It is an industry-wide standard, based on rules developed in the 1970’s by IBM’s E.F. Codd, making it portable across all relational database products on the market today. It is an interactive language that comes in three flavors: DML, or data manipulation language; DDL, or data definition 4730ch01.qxd 3/28/2000 9:33 AM Page 3 Lab 1.1: Data, Databases, and the Definition of SQL 3 language; and DCL, or data control language. DML, DDL, and DCL com- mands help a user query, manipulate, define, and control the data in a LAB RDBMS. 1.1 LAB 1.1 EXERCISES 1.1.1 IDENTIFY AND GROUP DATA a) Give three examples of types of data. b) What groupings of data do you use in your daily life? 1.1.2 DEFINE A DATABASE AND ITS USE a) Give an example of a database system you use outside of the workplace, and explain how it helps you. b) How is data organized in a relational database? 1.1.3 DEFINE SQL a) What is SQL and why is it useful? 4730ch01.qxd 3/28/2000 9:33 AM Page 4 4 Lab 1.1: Data, Databases, and the Definition of SQL The following two questions will pose a challenge to anyone not yet fa- LAB miliar with SQL. For question b, think about what the command on the 1.1 left is trying to accomplish. b) Try to match each of the SQL commands on the left with a verb from the list on the right. 1. CREATE _____ a. manipulate 2. INSERT _____ b. define 3. GRANT _____ c. control c) Why do you think it is important to control access to data in a database? LAB 1.1 EXERCISE ANSWERS 1.1.1 ANSWERS a) Give three examples of types of data. Answer: The answer to this question will vary depending on your choices. A circle, square, and triangle are all data about geometrical shapes. Your mother, fa- ther, and sister is data about your immediate family members. Fiction, comedy, cook- book, and computer are all data about types of books. b) What groupings of data do you use in your daily life? Answer: The answer to this question will vary depending on your situation. I use my address book daily. It contains addresses and phone numbers of friends and relatives. I also keep a running to-do list of tasks at work, which groups together the tasks I have completed, as well as separately grouping those tasks I have yet to do. When grouping data, each piece of data should be related to the others. A person’s physical appearance is typically described by more than just brown hair; they may also have green eyes, be six feet tall, and be of the 4730ch01.qxd 3/28/2000 9:33 AM Page 5 Lab 1.1: Data, Databases, and the Definition of SQL 5 female sex. In my address book, I group together a person’s name, their address, and telephone number. I may keep a separate address book for LAB my business contacts that would group together the person’s name, com- 1.1 pany name, work telephone number, fax number, and e-mail address. 1.1.2 ANSWERS a) Give an example of a database system you use outside of the work- place, and explain how it helps you. Answer: The answer to this question will vary depending on your situation. When I’m in a record store, I often use a computerized information kiosk to search for information about an album, such as where it is located in the store. Another example is an ATM machine, where I can inquire about my account balance. b) How is data organized in a relational database? Answer: Data is organized by placing like pieces of information together in a table, or- ganized into columns and rows. I FOR EXAMPLE The data found in a library is typically organized in several ways to facili- tate finding a book. Searching for a book by title might yield the follow- ing excerpt of data: Title Author ISBN# Genre Location_ID Computer’s Jeff Smith 0-11-124456-2 Computer D11 Life, A Desk Work Robert Jones 0-11-223754-3 Fiction H24 Let’s Go to the Mark Porter 0-11-922256-8 Juvenile J3 Beach From Here to There Gary Mills 0-11-423356-5 Fiction H24 This group of information contains data specific to books. The data is or- ganized into columns and rows; the columns represent a type of data (title vs. genre), while the rows contain data. A table in a database is orga- nized the same way. You might call this table book as it contains infor- mation related to books only. Each intersection of a column and row in a table represents a value. Searching for a book by location might yield this excerpt of data: 4730ch01.qxd 3/28/2000 9:33 AM Page 6 6 Lab 1.1: Data, Databases, and the Definition of SQL LAB Location_ID Floor Section Shelf 1.1 D11 1 3 1 H24 2 2 3 J3 3 1 1 This set of columns and rows represents another database table called lo- cation, with information specific to locations in a library. The advantage to storing information about books and their locations separately is that information is not repeated unnecessarily, and maintenance of the data is much easier. For instance, two books have the same location_id, H24. If the floor, sec- tion, and shelf information were also stored in the book table, this infor- mation would be repeated for each of the two book rows. If the floor of location_id H24 changes, both of the rows in book would have to change. Instead, by storing the location information separately, the floor information only has to change once in the location table. The two tables have a common column between them, namely loca- tion_id. In a relational database, SQL can be used to query information from more than one table at a time, making use of the common column they contain by performing a join. The join allows you to query both the book and location tables to return a list of books listing the floors, sec- tions, and shelves where they are located. You will learn how to write joins in Chapter 6, “Equijoins.” Each of these columns contains a different kind of data, which can be classified by a datatype. For instance, values in the location_id column of the location table contain both letters and numbers, also called alphanu- meric data, and could be stored in a column of datatype VARCHAR2(10). This means up to 10 alphanumeric characters (letters or numbers) may be stored in this column. Another datatype, the CHAR datatype, also stores alphanumeric data, but is a fixed-length datatype and pads any unused space in the column with blanks until it reaches the defined column length.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages26 Page
-
File Size-