0OracleSQL Summary
0OracleSQL Summary
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
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
CREATE TABLE Employee ( person_name CHAR(50) PRIMARY KEY, street VARCHAR2(100), city VARCHAR2(50) NOT NULL );
Lecture 0: Oracle SQL Summary 3
CREATE TABLE Company ( company_name CHAR(100) PRIMARY KEY, city VARCHAR2(50) NOT NULL );
Lecture 0: Oracle SQL Summary 4
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) );
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:
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 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
Dick A
Lake A
PERSON_NAME -------------------------------------------------CITY -------------------------------------------------Michael C Lecture 0: Oracle SQL Summary 10
CITY ---A A A C
Dick
Michael
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
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');
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
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
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
19