You are on page 1of 19

Chapter 2 Getting Information from a Table

Chapter 5 The Data Dictionary and othet Oracle Topics

Session #1
DAT615 Advanced Database
Programming

ORACLE TABLES

CREATE TABLE TEST


(StudentId CHAR(5) PRIMARY KEY,
Last VARCHAR2 ((15)) NOT NULL,,
First VARCHAR2 (15) NOT NULL,
BirthDate DATE,
MajorId NUMBER (3),
Phone CHAR (10));

DAT615 Advanced Database Programming By 2


Dr. Mudasser F. Wyne

Adding a New Tuple


• DML Statement:
INSERT INTO tablename [(column1, column2, column3,…)]
VALUES (value1, value2, value3,…);

• For example:
INSERT INTO dept (DeptId, Deptname, Location,
EmployeeId)
VALUES (10, ‘Finance’, ’Charlotte’,123);
OR
INSERT INTO dept
VALUES (10, ‘Finance’, ’Charlotte’,123);
DAT615 Advanced Database Programming By 3
Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 1
Null Values
• Explicit Method:
INSERT INTO dept (DeptId, DeptName, Location,
EmployeeId)
VALUES (60, ‘Personnel’, ‘Chicago’, NULL);

• Implicit Method:
INSERT INTO dept (DeptId, DeptNAme)
VALUES (50, ‘Production’);

DAT615 Advanced Database Programming By 4


Dr. Mudasser F. Wyne

SELECT Clause to List Some Columns


• Order of the columns within the Select clause determines
their order within the result table
• Order can be different than order in original table
• Column can be renamed by giving it a column alias
• A Lit
Literall Value
V l can beb included
i l d d – if text
t t or date
d t it should
h ld be
b
within single quote marks.

DAT615 Advanced Database Programming By 5


Dr. Mudasser F. Wyne

SELECT DISTINCT
• SELECT DISTINCT a list of columns
• Get only the columns listed
• Put them in the order they are listed
• You can rename them
• p
Eliminate duplicate rows from the result

• Example:
SELECT DISTINCT Manager_Id
FROM L_Employees
WHERE Employee_id in (201, 208, 210);

DAT615 Advanced Database Programming By 6


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 2
WHERE Clause
• Specifies a condition that is True for all the rows you want
in the result table
• Each condition has a positive and a negative form
• Negative from is always the exact opposite of the positive
form

DAT615 Advanced Database Programming By 7


Dr. Mudasser F. Wyne

WHERE Clause
• EQUAL and other Comparison Conditions
• Equal (=)
• Less than (<)
q ((<=))
• Less than or equal
• Greater than (>)
• Greater than or equal (>=)
• Not Equal (<>)(and others)

DAT615 Advanced Database Programming By 8


Dr. Mudasser F. Wyne

WHERE Clause
• SET INCUSION Test – list of specific values
• IN (In a Set)
• NOT IN (Not In a Set)
• RANGE Test – between two values
• BETWEEN (in a range)
• NOT BETWEEN (not within a range)

DAT615 Advanced Database Programming By 9


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 3
WHERE Clause
• PATTERN MATCHING Test –
• LIKE (matches a pattern)
• NOT LIKE (does not match a pattern)
• NULL Test
• IS NULL (is a null value)
• IS NOT NULL (is not a null value)

DAT615 Advanced Database Programming By 10


Dr. Mudasser F. Wyne

WHERE Clause
SELECT Employee_Id, First_Name, Last_Name, Manager_Id
FROM L_EMPLOYEES
WHERE NOT (Manager_Id = 203);

WHERE Manager_Id < > 203;


WHERE Manager_Id != 203;
WHERE Manager_Id ^= 203;

DAT615 Advanced Database Programming By 11


Dr. Mudasser F. Wyne

WHERE Clause
• BOOLEAN CONNECTORS – Joining simple conditions
together
• AND (both of the conditions are True)
• OR (one or both of the conditions is True)
• NOT (the condition is False)

DAT615 Advanced Database Programming By 12


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 4
LIKE Condition - Wildcards
ORACLE ACCESS Meaning
% (percent sign) * (asterisk) A string of characters of
any length, or none
_ (underscore) ? (question mark) One character

(not available) # (pound sign) One digit (numeric)

(not available) [c-m] (square brackets Range of characters


with a dash)
(not available [!c-m] Outside a range of
characters
\% or \_ [*] or [?] or [#] Character literally, not a
wildcard
DAT615 Advanced Database Programming By 13
Dr. Mudasser F. Wyne

ORDER BY Clause
• Syntax –
• ORDER BY a list of column names
• ORDER BY a list of numbers
• Sort Order Options for each column
• ASC means ascending order (default)
• DESC means descending order
• Example
ORDER BY last_name, first_name

DAT615 Advanced Database Programming By 14


Dr. Mudasser F. Wyne

Example of a SELECT statement


SELECT EMPLOYEE_ID,
LAST_NAME,
CREDIT_LIMIT
FROM L_EMPLOYEES
L EMPLOYEES
WHERE CREDIT_LIMIT > 20.00
ORDER BY LAST_NAME;

DAT615 Advanced Database Programming By 15


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 5
RESTRICTING DATA
QUERY List last name of all the employees who draw salary from 35K to 40K.

SELECT Lname
FROM EMPLOYEE
WHERE Salary BETWEEN 35000 AND 40000;

FNAME LNAME SSN SALARY SEX

John Smith 123456789 30000 M


EMPLOYEE

Franklin Wong 333445555 40000 M

Alicia Zelaya 999887777 25000 F

Jennifer Wallace 987654321 43000 F

Ramesh Narayan 666888444 38000 M

DAT615 Advanced Database Programming By 16


Dr. Mudasser F. Wyne

RESTRICTING DATA
QUERY List last name of all the employees who draw salary 35K, 25K and 40K.

SELECT Lname
FROM EMPLOYEE
WHERE Salary IN ( 35000, 40000, 25000 ) ;

FNAME LNAME SSN SALARY SEX

John Smith 123456789 30000 M


EMPLOYEE

Franklin Wong 333445555 40000 M

Alicia Zelaya 999887777 25000 F

Jennifer Wallace 987654321 43000 F

Ramesh Narayan 666888444 38000 M

DAT615 Advanced Database Programming By 17


Dr. Mudasser F. Wyne

RESTRICTING DATA
QUERY List last name of all the employees who do not draw any salary.

SELECT Lname
FROM EMPLOYEE
WHERE Salary IS Null ;

FNAME LNAME SSN SALARY SEX

John Smith 123456789 30000 M

Franklin Wong 333445555 40000 M

Alicia Zelaya 999887777 25000 F

Jennifer Wallace 987654321 43000 F

Ramesh Narayan 666888444 null M

DAT615 Advanced Database Programming By 18


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 6
EQUIJOIN
SYNTAX

SELECT List_of_Attributes
FROM Relation_names
WHERE join condition(s);

• Column names (both tables) are separated by commas


• Table names separated by commas
• Joining condition includes attributes from each table
• Conditions can be added with AND, OR, NOT operators

DAT615 Advanced Database Programming By 19


Dr. Mudasser F. Wyne

JOIN Operations
JOIN

• Joining condition is a must to combine tuples from two


tables
• For joining n tables we need n-1
n 1 join conditions
• Join condition on Primary key and Foreign Key

DAT615 Advanced Database Programming By 20


Dr. Mudasser F. Wyne

SQL JOIN Operation


Query: List last name of the employees who manage departments. Also list the name
of the corresponding department.
DEPARTMENT

DNAME DNUMBER MGRSSN MGRSTARTDATE


Research 5 333445555 1988-05-22

Administration 4 987654321 1995-01-01

Headquarters 1 888665555 1981-06-19

FNAME LNAME SSN SALARY SUPERSSN SEX


EMPLOYEE

John Smith 123456789 30000 333445555 M

Franklin Wong 333445555 40000 888665555 M

Alicia Zelaya 999887777 25000 987654321 F

Jennifer Wallace 987654321 43000 888665555 F

Ramesh Narayan 666888444 38000 333445555 M

DAT615 Advanced Database Programming By 21


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 7
Query: List last name of the employees who manage departments. Also
list the name of the corresponding department.

DEPARTMENT
DNAME DNUMBER MGRSSN MGRSTARTDATE

EMPLOYEE
FNAME LNAME SSN SALARY SUPERSSN SEX

SELECT DEPARTMENT.Dname, EMPLOYEE.Lname


FROM DEPARTMENT, EMPLOYEE
WHERE DEPARTMENT.MGRSSN = EMPLOYEE.SSN;

DAT615 Advanced Database Programming By 22


Dr. Mudasser F. Wyne

More on JOINS !!!


ADDITIONAL CONDITIONS

• More than one condition using logical operator (AND, OR)

NON--EQUIJOIN
NON

• The join condition other than EQUAL

DAT615 Advanced Database Programming By 23


Dr. Mudasser F. Wyne

JOIN Operations
CARTESIAN PRODUCT

• Does not have a WHERE clause


• No join condition is required
• Joins each tuple in first table with each and every tuple in
the second table
• Gives all possible combinations of tuples from two tables
• It is not considered as a join operation

SELECT Attributenames
FROM tablename1, tablename2

DAT615 Advanced Database Programming By 24


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 8
Attribute ALIASING
SELECT Attributenname [AS] alias . . .
• Changes the attribute name to a different column heading
• Keyword used is AS
SELECT Fname AS “Name”, Lname AS “Family_Name”
FROM EMPLOYEE ;

DAT615 Advanced Database Programming By 25


Dr. Mudasser F. Wyne

ALIASING
• Changes the attribute name to a different column heading
• TTITLE CENTER 'Lab Exercise # BY WYNE'

SELECT Fname “Name”, Lname “Family_Name”


FROM EMPLOYEE ;

DAT615 Advanced Database Programming By 26


Dr. Mudasser F. Wyne

ALIASING
Relation/Table Alias:
• Appear in the FROM clause of the SELECT query
• Alias names can be used in SELECT clause
• It can be 1 to 30 character
SELECT e.Lname,
L s.Dname
D
FROM EMPLOYEE e, DEPARTMENT s
WHERE e.SSN = s.MGRSSN;

DAT615 Advanced Database Programming By 27


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 9
ALIASING

SELECT Employee_Id AS Employee_Number


Phone_Number AS Extension
Last_Name,,
‘Excellent Worker’ AS Evaluation
10 AS Rating
FROM L_EMPLOYEES;

DAT615 Advanced Database Programming By 28


Dr. Mudasser F. Wyne

RELATION INFORMATION
Oracle keeps track of all the tables created by users using its
own system database (SQL statements)
• To view users tables
SELECT table_name FROM user_tables;
or
SELECT * FROM user_tables;
• To view the relation’s structure
DESCRIBE relation_name;
• To view all the tuples in a relation
SELECT * FROM relation_name;

DAT615 Advanced Database Programming By 29


Dr. Mudasser F. Wyne

TABLE INFORMATION
• To view the constraint information

SELECT constraint_name, column_name


FROM user_cons_columns where TABLE_NAME =
‘EMPLOYEE’;

SELECT constraint_name, constraint_Type


FROM user_constraints where TABLE_NAME =
‘DEPT’;

DAT615 Advanced Database Programming By 30


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 10
Altering Table Statements
General Syntax:
ALTER TABLE TableName
ADD/MODIFY/DROP attribute DataType;

ALTER TABLE TableName


ADD/DROP CONSTRAINT ConstraintName
ConstraintType
(column, …) [REFERENCES TableName (ColumnName)];

DAT615 Advanced Database Programming By 31


Dr. Mudasser F. Wyne

Altering Table Statements


For example
ALTER TABLE student ADD SocSecNum CHAR(9);

ALTER TABLE student MODIFY Zip VARCHAR2(10);

ALTER TABLE student ADD CONSTRAINT student_facultyid_fk


FOREIGN KEY (FacultyId) REFERENCES faculty (FacultyId);

DAT615 Advanced Database Programming By 32


Dr. Mudasser F. Wyne

Updating Existing Tuples


• The DML statement UPDATE is used to modify existing
data.
• Only one table can be updated at a time
General Syntax:
UPDATE ttablename
bl
SET column1 = newvalue [, column2 = newvalue,…]
[ WHERE condition ];
UPDATE employee
SET ssn = 786345012
WHERE ssn = 123456789;

DAT615 Advanced Database Programming By 33


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 11
Constraints: Enable/Disable
• Syntax:
ALTER TABLE tablename
DISABLE CONSTRAINT constraintname [CASCADE];

ALTER TABLE tablename


ENABLE CONSTRAINT constraintname;

ALTER TABLE student


DISABLE CONSTRAINT student_studentid_pk CASCADE;

DAT615 Advanced Database Programming By 34


Dr. Mudasser F. Wyne

Commit and Rollback commands


• COMMIT –
• Changes to a Table are often Temporary until
• COMMIT command issued
• This Is the same as a SAVE command
• ROLLBACK –
• Deletes all changes since last COMMIT or SAVE
• Commit make all changes public
• To Keep or Not To Keep – That Is the Question

DAT615 Advanced Database Programming By 35


Dr. Mudasser F. Wyne

AUTOCOMMIT
• AUTOCOMMIT is issued when –
• Oracle is exited
• A New Table is created
j is created
• A Database Object
• Oracle AUTOCOMMIT property –
Checkbox for Autocommit

DAT615 Advanced Database Programming By 36


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 12
Transactions
• A way to package together several changes to the data in
one or more tables
• Changes made using INSERT, UPDATE, DELETE
• Either all will process successfully or all will fail
• Used to ensure the data is consistent
• To enable transactions, uncheck Autocommit

DAT615 Advanced Database Programming By 37


Dr. Mudasser F. Wyne

Example of a Transaction
• Transferring of $5000 from Savings account to Checking
account
• Two SQL statements
• Decrease balance of Savings account by $5000
• Increase balance of Checking account by $5000
• Issue a Commit prior to First SQL statement
• If Second SQL fails, issue a Rollback
• If Second SQL works, issue a Commit

DAT615 Advanced Database Programming By 38


Dr. Mudasser F. Wyne

Modifying Data Through a View


• Common practice to Change data through a View when
• Working with a Large Database
• Many Users are accessing the Database at the same time
• y, the DBA changes
Generally, g Tables and the Users change
g
Views
• A View can be used for Security reasons

DAT615 Advanced Database Programming By 39


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 13
Changing Data Through a View
• Generally, you can only change the data that can be seen
through the View
• Two exceptions to this rule:
• You can only delete rows that can be seen through the
View (but the entire row is deleted)
• You can insert a new row (the entire row)

DAT615 Advanced Database Programming By 40


Dr. Mudasser F. Wyne

Changing a View Exceptions

Rows restricted to the Columns restricted to


ones in the View the ones in the View

I
Insert
t N
No Y
Yes

Update Yes Yes

Delete Yes No

DAT615 Advanced Database Programming By 41


Dr. Mudasser F. Wyne

Changing Data Through a View


• Only ‘Update-able Views’ can be changed
• It only contains data from one table
• It contains some or all of the columns and rows from the
table
• It does not summarize the data or condense it by using
SELECT DISTINCT. (The data in each cell of the view
comes from the data in only one cell of the base table)

DAT615 Advanced Database Programming By 42


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 14
Steps of Changing Data
Through a View
• Step 1 – Beginning Table (Data stored here)
• Step 2 – Beginning View (Select statements)
• Step 3 – Change the Data
((Insert,, Delete,, Update
p that appears
pp in view))
• Step 4 – Ending View (from Users perspective)
• Step 5 – Ending Table (resulting data)

DAT615 Advanced Database Programming By 43


Dr. Mudasser F. Wyne

Views with the ‘Check Option’


• If the Check Option is not used, you are allowed to make
changes through the view even if the effect of those
changes cannot be seen in the view
• Check Option limits the Insert and Update command
• If the change removes the data from the View, the
change cannot be made
• You can only make changes that you can ‘View’

DAT615 Advanced Database Programming By 44


Dr. Mudasser F. Wyne

Oracle SQL Command page

• Links - Home Link, Logout Link, Help Link


• User Identification and Breadcrumbs
• A t
Autocommit it checkbox
h kb
• Number of rows to Display
• Buttons – Save button, Run button
• Area to enter SQL commands
• Options – Results, Explain, Describe, more …
• Results Area

DAT615 Advanced Database Programming By 45


Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 15
Finding Information about
Tables and Views
• Data Dictionary
• A set of Tables that contains all the information about
the structure of the database
• Names of the Tables, their column names, primary keys,
names of their views, the select statements that define
the views, and mucho more
• Select statements can be used to retrieve Data
Dictionary data
• Used extensively by the DBA
• Access does not have a data dictionary

DAT615 Advanced Database Programming By 46


Dr. Mudasser F. Wyne

Oracle Data Dictionary


Information to Get Data Dictionary Table Data Dictionary Columns
Table Names User_tables or Table_name
All_tables
View Names User_views or View_name
All views
All_views
View Definition User_views or Text
All_views
Columns of tables and User_tab_columns or Column_name
views All_tab_columns
Primary Keys of User_constraints and (see Section 5-17)
tables user_cons_columns or
all_constraints and
all_cons_columns

DAT615 Advanced Database Programming By 47


Dr. Mudasser F. Wyne

Additional
Find The Names of all relations/views:
• SELECT table_name/view_name
• FROM user_tables/user_view;
List all relations yyou are ppermitted to use;;
• SELECT table_name
• FROM all_tables;
Find SELECT statement that Defines a view;
• SELECT view_name, text
• FROM user_views
• WHERE view_name = ‘SHIPPING_DEPT_VIEW’;
DAT615 Advanced Database Programming By 48
Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 16
How to Find the Names of the Columns
in a Table or View
• DESCRIBE table_name1;
• Or
• DESC table_name1;

• Hint: Do not confuse DESCribe with DESCending in the


ORDER BY clause – a good habit is to spell out
DESCRIBE

DAT615 Advanced Database Programming By 49


Dr. Mudasser F. Wyne

How to Find the Primary Key


of a Table
• Two Rules
• Nulls are NOT allowed in a Primary Key
• No Two Rows may have the same Primary Key

DAT615 Advanced Database Programming By 50


Dr. Mudasser F. Wyne

How to Find the Primary Key


of a Table
• Step 1 –
SELECT table_name,
constraint_type,
constraint_name
FROM user_constraints
WHERE table_name = ‘L_FOODS’;
• Step 2 –
SELECT *
FROM user_cons_columns
WHERE table_name = ‘L_FOODS’;
DAT615 Advanced Database Programming By 51
Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 17
Logging in To iSQL*PLUS
iSQL*PLUS

• Enter the iSQL*Plus URL in your web browser's Location


or Address field. The iSQL*Plus URL looks like:
• http://machine_name.domain:port/isqlplus
• http://127.0.0.1:5560/isqlplus/
http://localhost:5560/isqlplus/
• iSQL*Plus uses HTTP port 5560 by default.
• If iSQL*Plus is not available on port 5560, read the
$ORACLE_HOME/install/portlist.ini file on the computer
running the iSQL*Plus Application Server to find the port
on which iSQL*Plus is running.

DAT615 Advanced Database Programming By 52


Dr. Mudasser F. Wyne

• This is the LOGIN screen


DAT615 Advanced Database Programming By 53
Dr. Mudasser F. Wyne

• The screen after you login.


• You can write your SQL queries under “enter statements” and when
you hit “execute” button, it will execute the query.
• Other buttons are self explanatory.
DAT615 Advanced Database Programming By 54
Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 18
Create a New Account
• Log in to your account that you have created under
SYSTEM user name.
CREATE USER DAT615 PROFILE "DEFAULT"
IDENTIFIED BY whatever ACCOUNT UNLOCK;
GRANT CONNECT TO DAT615;
GRANT CREATE SESSION TO DAT615;
GRANT CREATE TABLE TO DAT615;
GRANT CREATE VIEW TO DAT615;
GRANT CREATE SEQUENCE TO DAT615;
ALTER USER DAT615 QUOTA 30 M ON USERS;
DAT615 Advanced Database Programming By 55
Dr. Mudasser F. Wyne

DAT615 Advanced Database Programming By


Dr. Mudasser F. Wyne 19

You might also like