Java - Adv Tech Rest2.0
Java - Adv Tech Rest2.0
Table created.
1
Display Table structure
Example:
SQL> DESC EMPLOYEE1
Name Null? Type
----------------------------------------------------- -------- --------------------
EMP_NAME VARCHAR2(25)
CITY VARCHAR2(20)
SALARY NUMBER(6)
1 row created.
1 row created.
2
('ahmet','ankara', 2000);
1 row created.
SQL> insert into employee1
values
('Can','izmir', 2400);
1 row created.
1 row created.
1 row created.
6 rows selected.
EMP_NAME SALARY
------------------------- ---------
ali 2400
ali 2400
ahmet 2000
kasim 2400
hakan 2000
3
Can 2400
6 rows selected.
SQL> Select emp_name, city from employee1;
EMP_NAME CITY
------------------------- --------------------
ali ankara
ali ankara
ahmet ankara
kasim ankara
hakan izmir
Can izmir
6 rows selected.
4
Lab# 3
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
5
SQL> SELECT * FROM STUDENT1;
SQL> INSERT INTO STUDENT1
VALUES
('INDS01', 'MUSTAFA', 'ATILIM', 'ANKARA');
INSERT INTO STUDENT1
*
ERROR at line 1:
ORA-00001: unique constraint (REHAN.SYS_C0010350) violated
6 rows selected.
SQL> COMMIT;
Commit complete.
Table created.
6
Insert Data
1 row created.
*Error will genearted Because, INDS11 IS NOT IN THE PRIMARY KEY TABLE.
1 row created.
7
Lab #4
How to add a PRIMARY KEY to the table after creating a table without PRIMARY KEY.
Table created.
1 row created.
1 row created.
Table altered.
For example:
8
Enable a Primary Key
The syntax for enabling a primary key is:
For example:
Table created.
9
DATE DATATYPE- DATE DATATYPE IS USED TO STORE DATE IN THE TABLE.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
NAME DATE_OF_B
-------------------- ---------
ALI 26-AUG-06
MEHMET 24-JUL-06
JOHN 28-AUG-06
AHMET 10-SEP-06
Distinct Command
10
The SQL DISTINCT clause is used together with the SQL SELECT keyword, to return a dataset with
unique entries for certain database table column.
The result of the SQL DISTINCT expression above will look like this:
LastName
Smith
Goldfish
Brown
where Command
NAME DATE_OF_B
-------------------- ---------
ALI 26-AUG-06
But we can use any of the following comparison operators in conjunction with the SQL WHERE
clause:
11
3 name != 'ALI';
NAME DATE_OF_B
-------------------- ---------
MEHMET 24-JUL-06
JOHN 28-AUG-06
AHMET 10-SEP-06
SELECT *
FROM Customers
WHERE DOB > '1/1/1970';
SELECT *
FROM Customers
WHERE DOB >= '1/1/1970';
SELECT *
FROM Customers
WHERE DOB < '1/1/1970';
Note the LIKE syntax is different with the different RDBMS (SQL Server syntax used above). Check
the SQL LIKE article for more details.
SELECT *
FROM Customers
WHERE DOB BETWEEN '1/1/1970' AND '1/1/1975';
Lab#5
12
DISPLAY Columns
The columns to be selected from a table are specified after the keyword SELECT.
For the order by clause is used and which has one or more attributes
listed in the select clause as parameter. desc command specifies a
descending order and asc specifies an ascending order (this is also the
default order). For example, the query
Next
Set Conditions: <column> [not] in (<list of values>)
Exampl1e:
SELECT ∗ from DEPT where DEPTNO in (10,30);
Example3 : •
SELECT EMPNO, ENAME, SAL from EMP
where SAL between 1000 and 3500;
Aggregate Functions
Aggregate functions are statistical functions such as count, min, max etc.
They are used to compute a single value from a set of attribute values of a
column:
13
SELECT count(∗) from EMP;
Ex1: How many different type of job are save in the EMP table.
SELECT count(distinct JOB) from EMP;
Ex3: Find out the difference between the minimum and maximum salary.
SELECT max(SAL) - min(SAL) from EMP;
1 row created.
1 row created.
14
Lab#6
Student Should create followin tables.
Example Table STUDENTS students must put other name of same table
INSERT INTO courses VALUES (101, 'CIS3400', 'DBMS I', 'FALL', 1997, 'B+');
INSERT INTO courses VALUES (101, 'CIS3100', 'OOP I', 'SPRING', 1999, 'A-');
INSERT INTO courses VALUES (101, 'MKT3000', 'Marketing', 'FALL', 1997,
'A');
INSERT INTO courses VALUES (102, 'CIS3400', 'DBMS I', 'SPRING', 1997,
'A-');
INSERT INTO courses VALUES (102, 'CIS3500', 'Network I', 'SUMMER', 1997,
'B');
INSERT INTO courses VALUES (102, 'CIS4500', 'Network II', 'FALL', 1997,
'B+');
INSERT INTO courses VALUES (103, 'MKT3100', 'Advertizing', 'SPRING', 1998,
'A');
INSERT INTO courses VALUES (103, 'MKT3000', 'Marketing', 'FALL', 1997,
'A');
INSERT INTO courses VALUES (103, 'MKT4100', 'Marketing II', 'SUMMER', 1998,
'A-');
15
Average GPA of all students:
SELECT AVG(gpa)
FROM students;
AVG(GPA)
----------
3.12857143
Average GPA of Finance and CIS students:
SELECT AVG(gpa)
FROM students
WHERE major = 'CIS' OR major = 'Finance';
AVG(GPA)
----------
3.14
NAME GPA
------------------------- ---------
Tom 3.5
Another option is to enclose some text in quotes and concatenate that text with the output
of the SQL statement:
SQL> SELECT 'The student with the highest GPA is ' || name
2 FROM students
3 WHERE gpa =
4 ( SELECT MAX(gpa) FROM students );
'THESTUDENTWITHTHEHIGHESTGPAIS'||NAME
-------------------------------------------------------------
The student with the highest GPA is Tom
16
Tom Finance 3.5
Sam Marketing 3.25
Note the two aliases given to the students table: s1 and s2. These allow us to refer to
different views of the same table.
You may wish to sort the output based on the GPA. In this case, the output is ordered by GPA
in decending order (highest GPA will come first, etc.):
Table 1
FNAME Varchar2
MI Varchar2
LNAME Varchar2
SSN Number
BDATE date
ADDRESS Varchar2
Sex Varchar2
SALARY NUMBER
SUPERSSN NUMBER
DNO NUMBER
17
JOYCE A ENGLISH 453453453 31-JUL-62 5631 RICE, HOUSTON, TX F 25000 333445555
5
AHMAD V JABBAR 987987987 29-MAR-59 980 DALLAS, HOUSTON, TX M 25000 987654321
4
JAMES E BORG 888665555 10-NOV-27 450 STONE, HOUSTON, TX M 55000
1
Table 2
Table 3
DNUMBER NUMBER
DLOCATION VARCHAR2
DNUMBER DLOCATION
------- ---------------
1 HOUSTON
4 STAFFORD
5 BELLAIRE
5 SUGARLAND
5 HOUSTON
Table 4
ESSN NUMBER(10)
DEPENDENT_NAME VARCHAR2(20)
SEX VARCHAR2(5)
BDATE DATE
RELATIONSHIP VARCHAR2(20)
18
Now we will do some queries.
FNAME LNAME
-------- --------
JOHN SMITH
FRANKLIN WONG
RAMESH NARAYAN
JOYCE ENGLISH
JAMES BORG
List each employee name and the location they work in. List them in order of location and
name:
Results:
DLOCATION DNAME FNAME LNAME
--------------- --------------- -------- --------
BELLAIRE RESEARCH JOYCE ENGLISH
BELLAIRE RESEARCH RAMESH NARAYAN
BELLAIRE RESEARCH JOHN SMITH
BELLAIRE RESEARCH FRANKLIN WONG
HOUSTON HEADQUARTERS JAMES BORG
HOUSTON RESEARCH JOYCE ENGLISH
HOUSTON RESEARCH RAMESH NARAYAN
HOUSTON RESEARCH JOHN SMITH
HOUSTON RESEARCH FRANKLIN WONG
STAFFORD ADMINISTRATION AHMAD JABBAR
STAFFORD ADMINISTRATION JENNIFER WALLACE
STAFFORD ADMINISTRATION ALICIA ZELAYA
SUGARLAND RESEARCH JOYCE ENGLISH
SUGARLAND RESEARCH RAMESH NARAYAN
SUGARLAND RESEARCH JOHN SMITH
SUGARLAND RESEARCH FRANKLIN WONG
16 rows selected.
SELECT MAX(employee.salary)
FROM employee, dept_locations
19
WHERE employee.dno = dept_locations.dnumber
AND dept_locations.dlocation = 'HOUSTON';
MAX(EMPLOYEE.SALARY)
--------------------
55000
To obtain the Cartesian Product of two tables, use a SELECT statement with no WHERE
clause:
SELECT *
FROM department, dept_locations ;
DLOCATION
---------------
BELLAIRE
HOUSTON
STAFFORD
SUGARLAND
List the Department name and the total salaries for each department:
Results:
DNAME SUM(EMPLOYEE.SALARY)
--------------- --------------------
ADMINISTRATION 93000
HEADQUARTERS 55000
RESEARCH 133000
20
We can also use a Column Alias to change the title of the columns
Results:
DNAME TOTALSALARIES
--------------- -------------
ADMINISTRATION 93000
HEADQUARTERS 55000
RESEARCH 133000
8 rows selected.
7 rows selected.
From the tree we can see that Mary tutors Bill, Sue and Jane. In turn, Sue tutors Sam. Finally,
Sam tutors both Tom and Alex.
Subqueries using the IN operator are used whenever the value of a column should be
found in a set of values. The set of values can be explicitly listed (as in the first example)
or they can be created on the fly using a subquery.
21
SELECT employee.fname, department.dname
FROM employee, department
WHERE employee.dno = department.dnumber
AND department.dname IN ('HEADQUARTERS', 'RESEARCH');
FNAME DNAME
-------- ---------------
JAMES HEADQUARTERS
JOHN RESEARCH
JOYCE RESEARCH
RAMESH RESEARCH
FRANKLIN RESEARCH
SELECT employee.fname
FROM employee
WHERE employee.dno IN
(SELECT dept_locations.dnumber
FROM dept_locations
WHERE dept_locations.dlocation = 'STAFFORD');
FNAME
-------
ALICIA
JENNIFER
AHMAD
In the above case, the subquery returns a set of tuples. The IN clause returns true when a
tuple matches a member of the set.
Subqueries using EXISTS. EXISTS will return TRUE if there is at least one row resulting
from the subquery.
The above query shows all employees names and salaries where there is at least one
person who makes more money (the first exists) and at least one person who makes
less money (second exists).
22
Subqueries with NOT EXISTS. NOT EXISTS will return TRUE if there are no rows
returned by the subquery.
The above query shows all employees for whom there does not exist an employee who
is paid less. In other words, the highest paid employee.
The HAVING clause is similar to the WHERE clause. The difference is that WHERE is
used to filter individual rows while HAVING is used to filter groups according to the
GROUP BY clause.
DNAME AVG(SALARY)
--------------- -----------
HEADQUARTERS 55000
RESEARCH 33250
DNAME COUNT(EMPLOYEE.DNO)
--------------- -------------------
ADMINISTRATION 3
RESEARCH 4
23
Change the last name of an Employee:
UPDATE employee
SET lname = 'SMITH'
WHERE lname = 'JONES';
UPDATE employee
SET salary = salary * 1.05
WHERE fname = 'JOYCE' AND lname = 'ENGLISH' ;
UPDATE EMPLOYEE
SET SALARY = SALARY * 1.02
WHERE TO_NUMBER( ( SYSDATE - bdate) / 365) >= 50;
PL NOTES
SYSDATE
------------------------
Mon 27-Nov-2006 10:24:00
SQL> BEGIN
2 dbms_output.put_line('Welcome to PL/SQL');
3 END;
4 /
Welcome to PL/SQL
Program - 2
SQL> begin
2 dbms_output.put_line('The next line is blank');
3 dbms_output.put_line('');
4 dbms_output.put_line('The above line should be blank');
24
5 end;
6 /
The next line is blank
The above line should be blank
Table created.
1 row created.
SQL> DECLARE
2
3 BEGIN
4
5
6 INSERT INTO PL1 VALUES('B3', 'AHMET');
7 INSERT INTO PL1 VALUES('B4', 'CAN');
8 INSERT INTO PL1 VALUES('B5', 'MUSTAFA');
9
10 END;
11 /
ID NAME
-------------------- --------------------
A1 MERT
A2 AHMET
B1 ALI
B3 AHMET
B4 CAN
B5 MUSTAFA
6 rows selected.
This program will Display User Name:
Program 4
DECLARE
user_name VARCHAR2(8) := user;
BEGIN
dbms_output.put_line('Welcome to PL/SQL, ' || user_name || '!');
25
END;
/
Program 5
Procedure created.
Program 6
SQL> DECLARE
2 x NUMBER;
3 BEGIN
4 x := 72600;
5 dbms_output.put_line('The variable X = ');
6 dbms_output.put_line(x);
7 END;
8 /
The variable X =
72600
SQL> DECLARE
2 x NUMBER;
3 y NUMBER;
4 z number;
5
6 BEGIN
7 x := 72600;
8 y := 5000;
9 z := x+y;
10 dbms_output.put_line('The sum of x +y = ');
11 dbms_output.put_line(z);
12 END;
13 /
26
OUTPUT
The sum of x +y =
77600
Procedure created.
27