0% found this document useful (0 votes)
77 views

0OracleSQL Summary

1. The document discusses Oracle data types and provides examples of creating tables, inserting data, and performing queries using SQL statements. 2. Examples are given for creating tables with different data types, primary keys, foreign keys and constraints. Sample insert and select statements are also shown. 3. Queries demonstrate selecting data from single and multiple tables using joins, formatting output, and updating or deleting records.

Uploaded by

tim02x
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

0OracleSQL Summary

1. The document discusses Oracle data types and provides examples of creating tables, inserting data, and performing queries using SQL statements. 2. Examples are given for creating tables with different data types, primary keys, foreign keys and constraints. Sample insert and select statements are also shown. 3. Queries demonstrate selecting data from single and multiple tables using joins, formatting output, and updating or deleting records.

Uploaded by

tim02x
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture 0: Oracle SQL

Oracle Data Types


A table can have up to 1000 columns in Oracle 10 Oracle supports these basic data types char(n): Fixed-length character string with n characters long. The maximum size for n is 2000 bytes in Oracle 10. Note that a string of type char is always padded on right with blanks to full length of n and therefore can be memory consuming varchar2(n): Variable-length character string. Maximum n is 4000 in Oracle 10 Only the bytes used for a string require storage

number(o, d): Numeric data. o is the overall number of digits, d is number of digits to the right of the decimal point. Use number (without specifying o and d) for floating point data

Lecture 0: Oracle SQL Summary

Oracle Data Types II


date: Date and time. The default format is DD-MONYY, e.g. 12-Feb-05

Other standard data types are converted to Oracle built-in data types. e.g.,
int -> number(38) smallint ->number(38) float(b)->number real->number

Lecture 0: Oracle SQL Summary

Create the Employee Table


1. Now, we look into the create table SQL statements by using the examples in the tutorial. We are going to create 4 tables: Employee (person_name, street, city) Works (person_name, company_name, salary) Company (company_name, city) Manages (person_name, manager_name) 2. Create the Employee table by typing the statement: Notice the syntax of the create table SQL statement

CREATE TABLE Employee ( person_name CHAR(50) PRIMARY KEY, street VARCHAR2(100), city VARCHAR2(50) NOT NULL );
Lecture 0: Oracle SQL Summary 3

Create the Company Table


3. 4. If the statement runs successfully, table created should appear in the SQL*Plus command windows Use the desc command to see the description of the employee table desc Employee 5. Create the Company table: Notice the data types and the primary key / not null constraints

CREATE TABLE Company ( company_name CHAR(100) PRIMARY KEY, city VARCHAR2(50) NOT NULL );
Lecture 0: Oracle SQL Summary 4

Create the Works Table


6. Create the Works table: Notice foreign keys are specified by REFERENCES keyword; also take note how the number data type is used and how the default value of an attribute is set

CREATE TABLE Works ( person_name CHAR(50) NOT NULL REFERENCES Employee(person_name), company_name CHAR(100) NOT NULL REFERENCES Company(company_name), salary NUMBER(10, 2) DEFAULT 0, PRIMARY KEY(person_name, company_name) );

Lecture 0: Oracle SQL Summary

Create the Manages Table


7. Finally, lets create the Manages table: It is an example of how a primary key is declared which consists of several attributes CREATE TABLE Manages ( person_name CHAR(50) NOT NULL REFERENCES Employee(person_name), manager_name CHAR(50) NOT NULL REFERENCES Employee(person_name), PRIMARY KEY(person_name, manager_name) );

Lecture 0: Oracle SQL Summary

Insert Tuples
8. After the tables were created, we check the tables under our account by: select * from tab; 9. Then, we are ready to insert tuples (records) into these tables by the insert into SQL statement. Type the following statement to insert a tuple into the Employee table:

INSERT INTO Employee VALUES('Ken', 'abc street', 'E city');


10. We have already prepared some tuples for insertion. Create the insert2.sql file and run it (just like what we did in lab 2): http://www.cs.ust.hk/~yini/ta231/lab3/insert2.sql

Lecture 0: Oracle SQL Summary

Select Statements I
11. Now, we will have some practice on select SQL statement: 12. To answer the query: A) Find the name of all employees who work for First Bank Corporation We use the following select statement, try it in SQL*Plus:

select person_name from works where company_name = 'First Bank Corporation';

Lecture 0: Oracle SQL Summary

Select Statements II
13. B) Find the names and cities of residence of all employees who works for First Bank Corporation

select e.person_name, e.city from employee e, works w where w.company_name = 'First Bank Corporation' and w.person_name = e.person_name;
A join operation is involved in this query. Please notice how to rename tables to make the SQL statement shorter 14. To make the result more readable, we use format commands to change the width of a column (the default width is defined in create table SQL statement)
COL PERSON_NAME FORMAT A12 PERSON_NAME is the column name, the number after A is the new width of the column
Lecture 0: Oracle SQL Summary 9

Sample Output of the Query I


PERSON_NAME -------------------------------------------------CITY -------------------------------------------------Alice A

Out put of query at step 13:

Dick A

Lake A
PERSON_NAME -------------------------------------------------CITY -------------------------------------------------Michael C Lecture 0: Oracle SQL Summary 10

Select Statements III


15. Similarly, change the width of the city column COL CITY FORMAT A4 Now, we run the query again by typing: / The column width has already been changed, but the result format is still not good. It is because wrapping is set to on by default which will wrap the spaces to the next line. To set the wrapping off (truncate the result), use: set wrap off Rerun the query by /, and we will see the well-formated result
Lecture 0: Oracle SQL Summary 11

Sample Output of the Query II


After changing the width:
PERSON_NAME CITY ------------ ---Alice A

After setting the wrapping off:

PERSON_NAME -----------Alice Dick Lake Michael

CITY ---A A A C

Dick

PERSON_NAME CITY ------------ ---Lake A

Michael

PERSON_NAME CITY ------------ ----

Lecture 0: Oracle SQL Summary

12

Select Statements IV
16. Try other select statements: C) Find the name, street address, and cities of residence of all employees who work for First Bank Corporation and earn more than $10,000 per annum select * from employee where person_name in (select person_name from works where company_name = 'First Bank Corporation' and salary > 10000); 17. Format the result by yourself

Lecture 0: Oracle SQL Summary

13

Select Statements V
18. Perform this query: G) Find the names of all employees who earn more than every employee of Small Bank Corporation

select person_name from works where salary > all (select salary from works where company_name = 'Small Bank Corporation');

Lecture 0: Oracle SQL Summary

14

Select Statements VI
19. We can rename a column by using as : select person_name as name, salary * 0.85 as salary_after_tax from works; 20. To format the number data type, we can use: col SALARY_AFTER_TAX FORMAT $99,999 Use 9 to represent a digit.

Rerun the query and see the result 21. The SQL*Plus user interactions (including the commands typed by the user and the results) can be logged into a file. To enable logging and specify the filename as sql.log: spool sql.log
Lecture 0: Oracle SQL Summary 15

Sample Output of the Query III


Original output:
NAME SALARY_AFTER_TAX ------------ ---------------Alice 42500 Cindy 59500 Dick 6800 Jack 11050 Lake 12750 Michael 34000 William 5950

After formatting number


NAME SALARY_AFTER_TAX ------------ ---------------Alice $42,500 Cindy $59,500 Dick $6,800 Jack $11,050 Lake $12,750 Michael $34,000 William $5,950

Lecture 0: Oracle SQL Summary

16

Select Statements VI
22. We rerun the last query by typing / 23. The query result is recorded in the sql.log file 24. Turn off the logging: spool off 25. Invoke pico the see the file !pico sql.log

Lecture 0: Oracle SQL Summary

17

Update Statments
27. Update statement I increase the salary by 10%: Update Works set salary = salary * 1.1; To see the updated result: select person_name, salary from Works; 28. Update statement II further increase the salary by 5% if the salary is less or equal to $15000, otherwise increase by 10% update Works set salary = case when salary <= 15000 then salary *1.05 else salary * 1.10 end; To see the updated result: select person_name, salary from Works;
Lecture 0: Oracle SQL Summary 18

Delete statement
29. Delete statement delete the entries with the highest salary from Works table delete from Works W where W.salary= ( select max(W2.salary) from Works W2 );

To see the updated result: (Cindy is deleted) select person_name from Works;
30. Finally, we dropped the created tables

DROP DROP DROP DROP

TABLE TABLE TABLE TABLE

Manages; Works; Company; Employee;

Lecture 0: Oracle SQL Summary

19

You might also like