1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu summary of SQL and SQL plus docx

18 547 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 104,24 KB

Nội dung

17 Summary of SQL and SQL*Plus Foreign Key Table Row Column Primary Key 17Ć2 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary of SQL and SQL*Plus The SQL and SQL*Plus module of the Introduction to Oracle course covered relational database concepts, the SQL command language, and SQL*Plus commands to execute and format SQL commands Database Terminology Concept Description Table A table is the basic storage structure of an RDBMS, consisting of one or more columns and zero or more rows Row A row is a combination of column values in a table; for example, the information about one department in the table S_DEPT A row is sometimes called a “record.” Column A column represents one kind of data in a table; for example, the department name in the example table S_DEPT It is described with a column name and holds data of a specific type and size Field At the intersection of a row and a column, you find a field The field can contain data If there is no data in the field, it is said to contain a null value Primary key A primary key is the column or set of columns that uniquely identifies each row in a table; for example a department number It must contain a value Foreign key A foreign key is a column or set of columns that refers to a primary key in the same table or in another table You create them to enforce relational database design rules Summary of SQL and SQL*Plus 17Ć3 17Ć4 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary of SQL and SQL*Plus continued Structured Query Language, SQL*Plus, and PL/SQL commands are used to access and manipulate data stored in an Oracle database SQL*Plus, SQL, and PL/SQL Language or Tool Description SQL A command language for communication with the Oracle7 Server from any tool or application Oracle SQL contains many extensions SQL*Plus An Oracle tool that recognizes and executes SQL and PL/SQL statements and contains its own command language PL/SQL An Oracle procedural language for writing application logic and manipulating data outside the database SQL Commands There are many commands available in SQL The table below describes the commands covered in this course Command Description SELECT Retrieves data from the database Most commonly used command INSERT UPDATE DELETE Enters new rows, changes existing rows, and removes unwanted rows from tables in the database, respectively Collectively known as Data Manipulation Language (DML) commands CREATE ALTER DROP RENAME TRUNCATE Sets up, changes, and removes data structures from tables Collectively known as Data Definition Language (DDL) commands COMMIT ROLLBACK SAVEPOINT Manage the changes made by DML statements Changes to the data can be grouped together into logical transactions GRANT REVOKE Gives or removes access rights to both the Oracle database and the structures within it Collectively known as Data Control Language (DCL) commands Summary of SQL and SQL*Plus 17Ć5 17Ć6 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary of SQL and SQL*Plus continued Oracle Data Dictionary The data dictionary is a set of tables that the user can access through views The data dictionary holds all the data about the database Whenever the database is in operation, the data dictionary is updated and maintained by the Oracle7 Server Write a SELECT statement to view the contents of the data dictionary views The following are some frequently accessed data dictionary tables: D DICTIONARY D USER_OBJECTS D OBJ (alias for USER_OBJECTS) D USER_CONSTRAINTS D USER_SEQUENCES SQL*Plus Commands SQL*Plus commands may be divided into the following main categories: Category Purpose Environment Affects the general behavior of SQL statements for the session Format Formats query results File manipulation Saves, loads, and runs script files Execution Sends SQL or PL/SQL commands from SQL buffer to Oracle7 Server Edit Modifies SQL commands in the buffer Interaction Allows users to create and pass variables to SQL statements, print variable values, and print messages to the screen Miscellaneous Various commands to connect to the database, manipulate the SQL*Plus environment, and display column definitions Summary of SQL and SQL*Plus 17Ć7 17Ć8 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice Overview This practice has you build a set of database tables for a video application Once you create the tables, you will insert, update, and delete records in a video store database, and generate a report The database contains only the essential tables Practice Contents D Creating tables and sequences based on a database design D Modifying data in the tables D Modifying a table definition D Creating a view D Writing scripts containing SQL and SQL*Plus commands D Generating a simple report Note: If you want to build the tables, you can execute the buildtab.sql script in SQL*Plus If you want to drop the tables, you can execute the dropvid.sql script in SQL*Plus Then, you can execute the buildvid.sql script in SQL*Plus to create and populate the tables Summary of SQL and SQL*Plus 17Ć9 17Ć10 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 17 Create the tables based on the entity relationship diagram on the previous page and the table instance charts below Choose the appropriate datatypes and be sure to add the integrity constraints a Table name: MEMBER Column Name MEMBER_ ID Key Type PK Null/ Unique NN, U LAST_ FIRST_ NAME NAME ADDRESS CITY PHONE NN JOIN_DATE NN Default Value System date Datatype number char char char char char Length 10 25 25 100 30 15 b date Table name: TITLE Note: Do not create the PRICE column while creating the TITLE table You would be modifying the TITLE table, in Exercise 7, to add the PRICE column Column Name TITLE_ ID Key Type PK Null/ Unique NN, U TITLE DESCRIP– TION NN NN CATEGORY G, PG, R, NC17, NR Check RATING RELEASE_ DATE DRAMA, COMEDY, ACTION, CHILD, SCIFI, DOCUMENTA RY Default Value DRAMA Datatype number char char char char Length 10 60 400 20 Summary of SQL and SQL*Plus date 17Ć11 Practice 17 continued 1.—continued c Table name: TITLE_COPY Column Name COPY_ID TITLE_ID Key Type PK PK, FK Null/ Unique NN, U NN, U STATUS NN Check AVAILABLE, DESTROYED, RENTED, RESERVED FK Ref Table title FK Ref Col title_id Datatype number number char Length 10 10 15 d Table name: RENTAL Column Name BOOK_ DATE MEMBER_ ID COPY_ID ACT_RET_ DATE EXP_RET_ DATE Key Type PK FK PK, FK PK, FK Null/ Unique NN,U NN NN,U NN,U Default Value System date TITLE_ID days after book date FK Ref Table member title_copy title_copy FK Ref Col member_id copy_id title_id number number 10 10 Datatype Length 17Ć12 date date date number 10 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 17 continued 1.—continued e Table name: RESERVATION Column Name RES_DATE MEMBER_ID TITLE_ID Key Type PK PK, FK PK, FK Null/Unique NN,U NN,U NN,U FK Ref Table member title FK Ref Col member_id title_id number number 10 10 Datatype date Length Verify that the tables and constraints were created properly by checking the data dictionary Create sequences to uniquely identify each row in the MEMBER table and the TITLE table a Member number for the MEMBER table, start with 101, not allow caching of the values b Title number for the TITLE table, start with 92, no caching c Verify the existence of the sequences in the data dictionary Summary of SQL and SQL*Plus 17Ć13 Practice 17 continued Add data to the tables Create a script for each set of data to add a Add movie titles to the TITLE table Write a script to enter the movie information Save the script as p17q4a.sql Use the sequence to uniquely identify each title Remember that single quotation marks in a character field must be specially handled Verify your additions Title Description Willie and Christmas Too All of Willie’s friends made a G Christmas list for Santa, but Willie has yet to add his own wish list CHILD 05-OCT-95 Alien Again Yet another installment of science fiction history Can the heroine save the planet from the alien life form? R SCIFI 19-MAY-95 The Glob A meteor crashes near a small American town and unleashes carnivorous goo in this classic NR SCIFI 12-AUG-95 My Day Off With a little luck and a lot of ingenuity, a teenager skips school for a day in New York PG COMEDY 12-JUL-95 Miracles on Ice A six-year-old has doubts about Santa Claus But she discovers that miracles really exist PG DRAMA 12-SEP-95 Soda Gang After discovering a cache of drugs, a young couple find themselves pitted against a vicious gang NR ACTION 01-JUN-95 17Ć14 Rating Category Release date Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 17 continued 4.—continued b Add data to the MEMBER table Write a script named p17q4b.sql to prompt users for the information Execute the script Be sure to use the sequence to add the member numbers First name Last name Address City Phone Join date Carmen Velasquez 283 King Street Seattle 206-899-6666 08-MAR-90 LaDoris Ngao Modrany Bratislava 586-355-8882 08-MAR-90 Midori Nagayama 68 Via Centrale Sao Paolo 254-852-5764 17-JUN-91 Mark Quick-To-See 6921 King Lagos Way 63-559-7777 07-APR-90 Audry Ropeburn 86 Chu Street Hong Kong 41-559-87 18-JAN-91 Molly Urguhart 3035 Laurier Blvd Quebec 418-542-9988 18-JAN-91 Summary of SQL and SQL*Plus 17Ć15 Practice 17 continued 4.—continued c Add the following movie copies in the TITLE_COPY table: Title Copy number Status Willie and Christmas Too Available Alien Again Available Rented The Glob Available My Day Off Available Available Rented Miracles on Ice Available Soda Gang Available d Add the following rentals to the RENTAL table: Title Copy number Customer Date return Date returned Date rented expected 92 101 days ago day ago 93 101 day ago day from now 95 102 days ago Today 97 106 days ago days ago days ago days ago Create a view named TITLE_AVAIL to show the movie titles and the availability of each copy and its expected return date if rented Query all rows from the view 17Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 17 continued Make changes to data in the tables a b Enter two reservations One reservation is for Carmen Velasquez who wants to rent “Interstellar Wars.” The other is for Mark Quick-To-See who wants to rent “Soda Gang” c Add a new title The movie is “Interstellar Wars”, which is rated PG, and classified as a SCIFI movie The release date is 07-JUL-77 The description is “Futuristic interstellar action movie Can the rebels save the humans from the evil Empire?” Be sure to add a title copy record for two copies Customer Carmen Velasquez rents the movie “Interstellar Wars”, copy Remove her reservation for the movie Record the information about the rental Allow the default value for the expected return date to be used Verify the rental was recorded by using the view you created Make a modification to one of the tables a Add a PRICE column to the TITLE table to record the purchase price of the video The column should have a total length of eight digits and two decimal places Verify your modification b Create a script named p17q7b.sql to update each video with a price according to the following list: Title Price Willie and Christmas Too 25 Alien Again 35 The Glob 35 My Day Off 35 Miracle on Ice 98 Soda Gang 35 Interstellar Wars 29 c Ensure that in the future all titles will contain a price value Verify the constraint Create a report titled Customer History Report This report will contain each customer’s history of renting videos Be sure to include the customer name, movie rented, dates of the rentals, and duration of rentals Count up the total number of rentals for all customers for the reporting period Save the script in a file named p17q8.sql Summary of SQL and SQL*Plus 17Ć17 17Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder ... rules Summary of SQL and SQL* Plus 17Ć3 17Ć4 Introduction to Oracle: SQL and PL /SQL Using Procedure Builder Summary of SQL and SQL* Plus continued Structured Query Language, SQL* Plus, and PL /SQL. .. Oracle: SQL and PL /SQL Using Procedure Builder Summary of SQL and SQL* Plus The SQL and SQL* Plus module of the Introduction to Oracle course covered relational database concepts, the SQL command language,... dropvid .sql script in SQL* Plus Then, you can execute the buildvid .sql script in SQL* Plus to create and populate the tables Summary of SQL and SQL* Plus 17Ć9 17Ć10 Introduction to Oracle: SQL and PL/SQL

Ngày đăng: 09/12/2013, 17:15

TỪ KHÓA LIÊN QUAN

w