2. Oracle SQL*PLUS. 60-539 Winter 2015. Some SQL Commands. To connect to a CS server, do:



Similar documents
Oracle SQL*PLUS (For , Fall 2006) Prepared by: Mr. Nkulu Nkasa

5. CHANGING STRUCTURE AND DATA

A basic create statement for a simple student table would look like the following.

Oracle Database 10g: Introduction to SQL

3.GETTING STARTED WITH ORACLE8i

Programming with SQL

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Oracle Database: SQL and PL/SQL Fundamentals

Database Programming with PL/SQL: Learning Objectives

Database Migration from MySQL to RDM Server

Oracle Database: SQL and PL/SQL Fundamentals NEW

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Oracle Database: Introduction to SQL

Oracle Database 10g Express

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle. Brief Course Content This course can be done in modular form as per the detail below. ORA-1 Oracle Database 10g: SQL 4 Weeks 4000/-

Instant SQL Programming

Oracle Database 12c: Introduction to SQL Ed 1.1

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

SQL. Short introduction

Database Administration with MySQL

How To Create A Table In Sql (Ahem)

IT2305 Database Systems I (Compulsory)


SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Oracle Database: Introduction to SQL

Oracle Database 11g SQL

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: Introduction to SQL

Oracle 10g PL/SQL Training

IT2304: Database Systems 1 (DBS 1)

INSTALLATION INSTRUCTIONS FOR THE STUDENT SAMPLE SCHEMA

Information Systems SQL. Nikolaj Popov

The Relational Model. Why Study the Relational Model?

Database Query 1: SQL Basics

Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

Setting Up Your Team-SQL Database for ORACLE 8.05

David Dye. Extract, Transform, Load

6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

14 Triggers / Embedded SQL

Introduction to the Oracle DBMS

Introduction to Microsoft Jet SQL

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

1 Triggers. 2 PL/SQL Triggers

A Brief Introduction to MySQL

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Lecture 6. SQL, Logical DB Design

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

BCA. Database Management System

Configuring an Alternative Database for SAS Web Infrastructure Platform Services

Procedural Extension to SQL using Triggers. SS Chung

SQL Data Definition. Database Systems Lecture 5 Natasha Alechina

Migrate Topaz databases from One Server to Another

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Using SQL Server Management Studio

CSC 443 Data Base Management Systems. Basic SQL

Mini User's Guide for SQL*Plus T. J. Teorey

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Introduction to SQL and database objects

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Introduction to SQL for Data Scientists

Porting from Oracle to PostgreSQL

Ch.5 Database Security. Ch.5 Database Security Review

news from Tom Bacon about Monday's lecture

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Fundamentals of Database Design

AUTHENTICATION... 2 Step 1:Set up your LDAP server... 2 Step 2: Set up your username... 4 WRITEBACK REPORT... 8 Step 1: Table structures...

Database security tutorial. Part I

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Working with DB2 UDB objects

Oracle For Beginners Page : 1

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

LOBs were introduced back with DB2 V6, some 13 years ago. (V6 GA 25 June 1999) Prior to the introduction of LOBs, the max row size was 32K and the

In This Lecture. Security and Integrity. Database Security. DBMS Security Support. Privileges in SQL. Permissions and Privilege.

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

New SQL Features in Firebird 3

New Security Options in DB2 for z/os Release 9 and 10

ORACLE 10g Lab Guide

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

SQL Introduction Chapter 7, sections 1 & 4. Introduction to SQL. Introduction to SQL. Introduction to SQL

Optimizing the Performance of the Oracle BI Applications using Oracle Datawarehousing Features and Oracle DAC

CS143 Notes: Views & Authorization

FileMaker 11. ODBC and JDBC Guide

MOC 20461C: Querying Microsoft SQL Server. Course Overview

3. Relational Model and Relational Algebra

There are five fields or columns, with names and types as shown above.

SQL Server An Overview

Advance DBMS. Structured Query Language (SQL)

Transcription:

60-539 Winter 2015 Some SQL Commands 1 Using SSH Secure Shell 3.2.9 to login to CS Systems Note that if you do not have ssh secure shell on your PC, you can download it from www.uwindsor.ca/softwaredepot. Login with your UWin account and download ssh and install on your PC so you can connect remotely to any of the CS servers sol.cs.uwindsor.ca or luna.cs.uwindsor.ca. You need to have an account on these servers and also an Oracle database account to be able to create and query databases. To request for an account, contact, the CS technician, Rob Mavrinac (mavrinac@uwindsor.ca) or ext 4410. To connect to a CS server, do: 1. Launch the ssh secure shell 2. Click on Quick Connect 3. Enter Host Name eg: cs.uwindsor.ca 4. Enter User Name eg: smith 5. Port Number: 22 (default) 6. Authentication Method: <profile Setting> (default) 7. Then press ENTER 8. Enter Password eg: XXXXXX When connected to the computer science system cs.uwindsor.ca or a specific cs server, such as alpha.cs.uwindsor.ca, you can use SQL*PLUS Oracle s interactive Interface to the database server. SQL statements can be issued at the SQL> prompt and file containing SQL statements can be executed from within SQL*PLUS. 2. Oracle SQL*PLUS Oracle s SQL*PLUS program provides a convenient interactive environment with the Oracle Database Server. The user may type the commands directly at the SQL> prompt or have SQL*PLUS execute commands residing in operating system files. Entering and Exiting Oracle SQL*PLUS To enter the SQL*PLUS environment, the sqlplus program should be executed as follows, where <user id> is the oracle user identification and <password> is the associated password: sqlplus <userid> sqlplus The Oracle userid and password may be different from the userid and password to get access to the server (cs.uwindsor.ca/alpha.cs.uwindsor.ca) 1

If the sqlplus program is invoked with only <userid>, the program prompts the user for the password. To Exit the SQL*PLUS environment, the exit or quit command must be entered at the SQL> prompt. SQL> quit ** 3. Basic DDL, DML, DCL operations for SQL (structured query lang): A. DDL 1. Create tables [Create Table ] 2. Destroy tables [Drop Table ] 3. Alter tables [Alter Table.] 4. Truncate tables [Truncate Table Student] 5. Rename tables [Rename oldname To newname] Examples: 1 CREATE TABLE student (stuid VARCHAR2(20), name VARCHAR2 (20), gpa NUMBER(5,2), CONSTRAINT student_pk PRIMARY KEY (stuid)); When creating a table, the constraints defined on the table and its attributes can be defined using the general format: Constraints can be named as in the following. Create table tabl ( col1 VARCHAR2(10), col2 NUMBER(4) NOT NULL, col3 NUMBER(6), col4 DATE DEFAULT SYSDATE, col5 VARCHAR2(5), col6 NUMBER, col7 VARCHAR2(5) CONSTRAINT tab1_pk PRIMARY KEY ( col1) CONSTRAINT tab1_zipcode_fk FOREIGN KEY ( col13) REFERENCES zipcode(zip), CONSTRAINT tab1_uk UNIQUE ( col5, col6 ), CONSTRAINT tabl1_col6_chk CHECK ( cl6 < 100)); 2. DROP TABLE Student CASCADE CONSTRAINTS 3. Altering Tables The characteristic of an already created table can be changed using the ALTER TABLE command in conjunction with the ADD, DROP and MODIFY clauses. E.g 2

ALTER TABLE toy ADD (manufacturer VARCHAR2(30) NOT NULL ) or Alter table TABLE toy ADD (manufacturer VARCHAR2(30) constraint toy_maun_nn NOT NULL ) Constraints can be added, dropped, disabled or enabled through the Alter table command. E.g. ALTER TABLE Toy ADD PRIMARY KEY (description) Alter TABLE toy DROP CONSTRAINT toy_pk Eg. Alter Table Student Modify (studid NUMBER (9)) Alter Table Student ADD CONSTRAINT stu_pk PRIMARY KEY(stuid) 4. The Truncate Command: deletes all rows from a table, like the Delete command, but does not allow a Where clause and it automatically issues a COMMIT. Eg. Truncate Table Student 5. Rename command can be used to rename tables and views as in: Rename Student To New_Student B: Data Manipulation Language (DML) commands allow us to query, insert, update or delete data and include the following: 6. Insert Data into Tables [Insert Into.] 7. Delete Data from Tables [Delete from ] 8. Update Data in Tables [Update. ] 9. Merge tables [Merge Into tablename ] 10. Query Tables [Select from where ] 11. Find the structure of DB, relation, view, index, etc.[querying catalogue with select * from tab; select * from cat; Desc Table; etc.] 3

6. Insert Into Student (Studid, Name, gpa) Values ( 53666, Jones S, 3.4) Commit 7 A DELETE statement may delete rows in other table if the foreign key constraint specifies the ON DELETE CASCADE option, which causes a child row (e.g., in enroll) to be automatically deleted when a parent row (e.g., in Student) is deleted. Eg. Delete from Student Where gpa <= 2.0 8. E.g. Update Student Set gpa = 3.4 Where Studid = 53831 9. The Merge command allows us to perform combined Insert, Update, Delete operations using the syntax: Merge Into tablename Using {query tablename} On (condition) [When Matched Then Update set_clause [Delete condition]] [When Not Matched Then Insert values_clause] This can be used for example to merge all student rows not found in the current Student table but that are in another Student_c table with the following: Merge Into Student s Using (Select Studid, Name, gpa) c On (s.studid = c.studid) When Matched Then Update Set s.name= c.name, s.gpa = c.gpa When Not Matched Then Insert (s.studid, s.name, s.gpa) Values (c.studid, c.name, c.gpa) 10. Query the Table using a wide variety of Select statements. 11. Query the meta data or catalogue with such select instructions as querying catalogue with select * from tab; select * from cat; Desc Table; etc. 4

C. The Data control Language commands 12. The grant command is used to give a database user access to a system privilege. Privileges can be granted individually or through a role. Example privileges on Tables are Select, Insert, Update, Delete, References, alter, Index, All. Roles are several privileges collected under one role name. Some pre-defined roles are CONNECT, RESOURCE, DBA. The CONNECT role include CREATE SESSION privilege, create tables, views and sequences, etc. 13. The Revoke Command is used to take away privileges. Other SQL features: Outer Joins, Savepoint, Commit, Rollback The OUTER join operator (+) is used to indicate that NULLs are generated for no matching rows. E.g., Select c.course_no, s.course_no, s.section_id, c.description, s.start_date_time from course c, section s where c.course_no = s.course_no(+) UNION Select c.course_no, s.course_no, s.section_id, c.description, s.start_date_time from course c, section s where c.course_no (+) = s.course_no The first select statement performs an OUTER join on section table and the second select statement performs an outer join on the course table. SAVEPOINT command allows you to save the result of the DML transactions temporarily. Commit command: makes the change to the data permanent and allows other users to see the data. SQL DDL and DCL statements implicitly complete with an automatic commit but DML statements are controlled by the transaction issuing them and need to be committed explicitly. ROLLBACK command can then refer back to a particular SAVEPOINT and roll back the transaction up to that point. E.g. SAVEPOINT zip222 ROLLBACK TO SAVEPOINT zip222 DDL commands can not be rolled back and they COMMIT any previously issued DML statements. 5

Common Data Types Data type Use 1 VARCHAR2 For character date and requires column length up to 4000 chars 2 CHAR Requires fixed column length up to 2000 chars 3 LONG Stores up to 2 gigabytes of data 4 CLOB Stores up to 4 gigabytes of data 5 NUMBER Can store up to 38 decimal digits of precision. It does not require a precision and scale necessarily. Used to specify real value as e.g NUMBER ( 5,2) for numbers, -999.99 to 999.99 6 DATE Stores the century, year, month, day, hour, minute and second in internal format, can be displayed using different format masks. 7 BLOBS, RAW,LONG RAW,BFILE Each of these data types points to a binary operating system files. INDEXES To optimize performance, you store indexes in separate tablespaces residing on different physical drives. To create an index and place it in a tablespace called index_tx, do the following CREATE INDEX sect_location_i ON section ( location) TABLESPACE index_tx Periodically, the Oracle B-tree index needs to be re-built as: ALTER INDEX stu_zip_fk_i REBUILD 14. User Privileges, Roles and synonyms To establish a connection to the database using sqlplus, the user must be granted certain privileges. A user name is created with the CREATE USER command To create a new user first login as the user SYSTEM, default password is manager You can login with the CONNECT(CONN) command with parameter userid/password as in. CONN system/manager You can also include host database string as in CONN system/manager@scratchy The host string is found in the tnsnames.ora file listing the server name and the datrabase instance name. 6

The following creates a user MUSIC with password LISTEN CREATE user music IDENTIFIED By listen DEFAULT TABLESPACE user_data TEMPORARY TABLESPACE temp_data Although the user music is created, the user cannot start a SQLPlus session because they lack their CREATE SESSION privilege to login to the database. A privilege is given to a user with GRANT command A role is a collection of privileges and a user granted a role acquires all the privileges defined within the role. The following grants some system privileges defined in the Oracle roles CONNECT and RESOURCE GRANT CONNECT,RESOURCE to music Another oracle role is DBA for all system privileges You can grant users the privileges to extend privileges they have to other WITH ADMIN option as in: GRANT CONNECT TO music WITH ADMIN OPTION Privileges can be taken away with the REVOKE command as in: REVOKE RESOURCE FROM music A user s password can be changed as: ALTER USER music IDENTIFIED BY tone DEFAULT TABLESPACE users A user is dropped with the following command DROP USER music CASCADE Object privileges can be granted on objects as tables, view, sequences to other users as in : GRANT SELECT ON course TO music GRANT ALL ON course TO music WITH GRANT OPTION With the second command music is the grantee but can become a granter if the privilege is passed on to another user 7