SQL Query Extras MIS 433. Rerunning the last Query n Type the forward slash “/” to rerun the last query that was entered.

Slides:



Advertisements
Similar presentations
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Advertisements

Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
Introduction to Structured Query Language (SQL)
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
ORACLE Using ORACLE 8 SQL using ORACLE 8 PL/SQL using ORACLE 8.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands 
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Chapter 6 Additional Database Objects
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file) Jason C. H. Chen, Ph.D. Professor.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
SQL (DDL & DML Commands)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
1 Reports. 2 Objectives  Use concatenation in a query  Change column headings and formats  Add a title to a report  Group data in a report  Include.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
Oracle Command Spool Spool C:\temp\Lab9.lst Select Hotel_no, room_no, type, price From Room Order by Hotel_no; Spool Off.
1 Chapter 6 Database Administration. 2 Introduction Database administration The process of managing a database Database administrator A person or an entire.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
8 Producing Readable Output with SQL*Plus. 8-2 Objectives At the end of this lesson, you should be able to: Produce queries that require an input variable.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
Copyright  Oracle Corporation, All rights reserved. 8 Producing Readable Output with SQL*Plus.
1 Chapter 2: Creating and Modifying Database Objects.
Chapter 14 Formatting Readable Output. Chapter Objectives  Add a column heading with a line break to a report  Format the appearance of numeric data.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
Chapter 12 Additional Database Objects. Chapter Objectives  Define the purpose of a sequence and state how it can be used by an organization  Explain.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Use of Data Security Commands Security: Information is vital to success, but when damaged or in the wrong hands, it can threaten success. OREACLE provides.
Introduction to Teradata Client Tools. 2 Introduction to Teradata SQL  OBJECTIVES :  Teradata Product Components.  Accessing Teradata – Database /
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Oracle PL/SQL SQL*Plus. EDIT Opens notepad with the buffer contents To use type: –Edit –Ed Opens notepad with the buffer contents To use type: –Edit –Ed.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
A Guide to SQL, Sixth Edition 1 Chapter 7 Reports.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
Fundamentals of DBMS Notes-1.
Chapter 5 Introduction to SQL.
SQL and SQL*Plus Interaction
Using SQL*Plus.
Chapter Twenty Producing Readable Output
Using SQL*Plus.
ORACLE SQL Developer & SQLPLUS Statements
ORACLE.
Using SQL*Plus.
IST 318 Database Administration
Producing Readable Output with iSQL*Plus
Presentation transcript:

SQL Query Extras MIS 433

Rerunning the last Query n Type the forward slash “/” to rerun the last query that was entered

Aliases for Field Names n You can replace the name of the field name with an Alias. For example: SQL> Select first as FirstName, last as LastName, total_commission as “Total Commission” from sales_rep FIRSTNAM LASTNAME Total Commission Mary Jones 2150 William Smith Miguel Diaz 2150 Note : Spaces are not allowed unless a double quote is used. In addition, the width of the column is defined by the field width not the alias

Ordering Calculated Columns n If you want to use a calculated column to sort your output, you can use the column number to specify a column to order the output SQL> select city, state, balance*1.05 from customer order by 3 CITY ST BALANCE* Grant MI Ada MI

Prompting for user Input n Use the PROMPT command to request input from a user. n Use the Accept command to accept input PROMPT Enter a title up to 30 characters long. ACCEPT MYTITLE PROMPT 'Title: ' TTITLE LEFT MYTITLE SKIP 2 SELECT * FROM CUSTOMER WHERE BALANCE>1000

Prompting for Input n Run the command file: SQL> START PROMPT1 Enter a title up to 30 characters long. Title: Department Report as of 1/1/95 n SQL*Plus displays the following output: Department Report as of 1/1/95 DEPTNO DNAME LOC ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON

Prompts for Variable Input n You can also pass information to a query: SQL> SELECT last, city, state FROM CUSTOMER where city = ‘&city’

Prompts for Variable Input n The prompt will ask for the City Name SQL> select last, city, state from customer where city = '&city' Enter value for city: Grant old 2: where city = '&city' new 2: where city = 'Grant' LAST CITY ST Samuels Grant MI Williams Grant MI Martin Grant MI

Storing Query Statements n SQL statements may be saved and used over again. The commands used are SAVE, GET, and START –Run a query, then type SAVE FILENAME No extension is needed (it will be *.sql) SQL> get cust-bal 1 Select * from customer 2* where balance>500 SQL> save cust-bal Created file cust-bal

Retrieving Query Statements n Saved SQL statements may be retrieved. The commands used are GET or START –Get: retrieves the saved statement and displays it on the screen. SQL> get cust-bal 1 Select * from customer 2* where balance>500; –Start: runs the saved statement. SQL> start cust-bal

Storing and Printing Query Results n Options –Send query results to a file to edit with a word processor –To store the results of a query in a file, enter the SPOOL command: SPOOL file_name –SQL*Plus continues to spool information to the file until spooling is turned off: SPOOL OFF

Pausing Output n You can pause output at the end of each screen full of information. SQL> set pause on n You can turn the pause off. SQL> set pause off

Changing Line Lengths n Normally your output will be set to 80 characters. You can change the line length to a longer setting using the following command: SQL> set linesize 132 –This command will change the line size to 132 characters. n A similar command changes the length of the page(the default is 22 or 24 lines) SQL> set pagesize 66

Changing Field Lengths in Output n Sometimes you will find that the fields for character strings are much longer than are needed in your output. You can use the substring function to shorted a field’s length. SQL> select substr(city,1,7) as City from customer CITY Lansing Grant etc.

Spooling to the Printer n To print query results, spool the results to a file. n Instead of typing SPOOL OFF, enter the command to output to the system printer: SPOOL OUT n SQL*Plus stops spooling to the file and copies the contents of the spooled file to default printer.

Altering tables: Do’s and Don’ts n Restricted Actions –Changing a column’s data type, size, and default values is allowed only if there are no data in the columns being modified. –Adding a primary key is allowed only if current field values are unique. –Adding UNIQUE and CHECK CONDITION constraints to a column is allowed only if current field values match the added condition. –Adding a foreign key is allowed only if current field values are NULL or exist in the referenced table. –Changing a column name is NOT allowed. –Deleting a column (e.g., ALTER TABLE table_name DROP COLUMN column_name;

Altering tables: Do’s and Don’ts n Unrestricted Actions –Adding a new column to a table. –Deleting a primary key constraint (also removes any foreign key references to the field in other tables). –Deleting a foreign key constraint.

Options for Altering a Table ALTER TABLE Table_nameADD column-definition MODIFY column-nameDEFAULT value DROP DEFAULT ADDprimary-key-definition foreign-key-definition null check-constraint DROP CONSTRAINT column-nameCASCADE RESTRICT

Examples of Altering Tables ALTER TABLE Part MODIFY (warehouse_number INTEGER) ALTER TABLE Part ADD CONSTRAINT item_class check in (‘HW’, ‘AP’, ‘SG’) ALTER TABLE Part MODIFY (item_class DEFAULT ‘HW’;) ALTER TABLE Part DROP CONSTRAINT item_class

Options for Alter Table n Use ALTER TABLE / ADD to… –Add a new column –Add a new foreign key or check condition constraint n Use ALTER TABLE / MODIFY to… –Modify a column’s data type or size –modify a column to add a NOT NULL constraint –Modify a column to add a default value

Creating an Auto-Number Sequence n Sequences are sequential lists of numbers that are generated automatically by SQL. –Used to create a unique key value for a table when no “natural” key value exists. –Used for numeric fields only

Sequence Syntax CREATE SEQUENCE [INCREMENT by ] [START WITH ] [MAXVALUE ] [MINVALUE ] {for decreasing sequences} [ORDER] {ensures proper ordering} [CACHE]{sequence numbers are cached to improve performance}

An Example of Sequence SQL> CREATE SEQUENCE customer_sequence start with 623 nomaxvalue nocache; SQL> INSERT INTO customer VALUES (customer_sequence.nextval,’Bolean',’Robert', ’4431 Sycamore', 'Lansing', 'MI', '49224', , 1322,'03');

Renaming a table n You can change the name of a table RENAME oldtablename TO newtablename

You can Reformat Columns n Changing a Column Heading –To produce a report from sales_rep with new headings specified for various columns, enter the following commands: clear columns COLUMN slsrep_number HEADING 'Sales Rep #' format a12 COLUMN Last HEADING 'Last Name' format a12 COLUMN First HEADING 'First Name' format a12 COLUMN Street HEADING 'Street Address' format a15 Select slsrep_number, first, last, street, total_commission as Commission from sales_rep

Reformatting Columns n SQL*Plus displays the following output: Sales Rep # First Name Last Name Street Address COMMISSION Mary Jones 123 Main William Smith 102 Raymond Miguel Diaz 419 Harper 2150 – Note : The new headings will remain in effect until you enter different headings, reset each column's format, or exit from SQL*Plus.

Word Wrap for Text Output n WRAP or WORD_WRAP can be used at the end of a Column Format statement to shorten the column length but not lose information. SQL> column street heading 'ST' format a6 word_wrap SQL> select city, street from customer 2 where city='Grant'; CITY ST Grant 215 Pete Grant 519 Watson Grant 419 Chip