1 creating and maintaining database objects part 1 database systems
TRANSCRIPT
![Page 1: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/1.jpg)
1
Creating and MaintainingDatabase Objects
Part 1
Database Systems
![Page 2: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/2.jpg)
2
Script: text file that contains a sequence of SQL commands
Running a script:SQL> START path_to_script_file;
Path cannot contain any blank spaces
SQL Scripts
![Page 3: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/3.jpg)
3
Syntax:INSERT INTO tablename VALUES
(column1_value, column2_value, …);
You must insert a value or a NULL placeholder for every field
Fields must be entered in the order they appear in the table when you issue the DESC command
Inserting a Value Into EveryField in a Record
![Page 4: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/4.jpg)
4
Inserting a Value Into EveryField in a Record
• Example:
![Page 5: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/5.jpg)
5
Command to insert values for selected record fields:
INSERT INTO tablename
(column1_name, column2_name, …)
VALUES
(column1_value, column2_value, …);
Inserting Selected Table Fields
![Page 6: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/6.jpg)
6
Example:
Inserting Selected Table Fields
![Page 7: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/7.jpg)
7
Date values must be converted from characters to dates using the TO_DATE function and a format mask
Example:
Inserting Date Values
![Page 8: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/8.jpg)
8
Must be enclosed in single quotes Is case-sensitive To insert a string with a single
quote, type the single quote twice Example:'Mike''s Motorcycle Shop'
Inserting Text Data
![Page 9: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/9.jpg)
9
Transaction Logical unit of work consisting of one or
more SQL DML commands INSERT, UPDATE, DELETE
All transaction commands must succeed or none can succeed
Transaction results are not visible to other users until they are “committed” to the database
Until a transaction is committed, it can easily be “rolled back” (undone)
Transactions
![Page 10: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/10.jpg)
10
A transaction starts when you type one or more commands in SQL*Plus
A transaction ends when you issue either the COMMIT or ROLLBACK command
SQL>COMMIT;
SQL>ROLLBACK;
Transactions
![Page 11: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/11.jpg)
11
Committing and Rolling Back Data
• COMMIT• Makes transaction command changes
permanent in the database and visible to other users
• ROLLBACK• Rolls back transaction command
changes and restores database to its state before the transaction
![Page 12: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/12.jpg)
12
Used to mark individual sections of a transaction
You can roll back a transaction to a savepoint
Savepoints
![Page 13: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/13.jpg)
13
Syntax:UPDATE tablename
SET column1 = new_value,
column2 = new_value, …
WHERE search_condition;
Records can be updated in only one table at a time
Can update multiple records if they all match the search condition
Updating Records
![Page 14: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/14.jpg)
14
Format:WHERE fieldname operator expression
Operators Equal (=) Greater than, Less than (>, <) Greater than or Equal to (>=) Less than or Equal to (<=) Not equal (< >, !=, ^= LIKE BETWEEN IN NOT IN
Search Conditions
![Page 15: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/15.jpg)
15
WHERE s_name = ‘Sarah’
WHERE s_age > 18
WHERE s_class <> ‘SR’
Text in single quotes is case sensitive
Search Condition Examples
![Page 16: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/16.jpg)
16
Syntax:DELETE FROM tablenameWHERE search_condition;
Deletes multiple records if search condition specifies multiple records
If search condition is omitted, all table records are deleted
You can’t delete a record if it contains a primary key value that is referenced as a foreign key
Deleting Records
![Page 17: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/17.jpg)
17
Truncating Tables
• Removes all table data without saving any rollback information• Advantage: fast way to delete table
data• Disadvantage: can’t be undone
• Syntax:TRUNCATE TABLE tablename;
![Page 18: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/18.jpg)
18
Sequential list of numbers that is automatically generated by the database
Used to generate values for surrogate keys
Sequences
![Page 19: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/19.jpg)
19
Syntax:CREATE SEQUENCE sequence_name
[optional parameters];
Example:CREATE SEQUENCE f_id_sequence
START WITH 200;
Creating Sequences
![Page 20: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/20.jpg)
20
Viewing Sequence Information
• Query the SEQUENCE Data Dictionary View:
![Page 21: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/21.jpg)
21
Pseudocolumns
• Acts like a column in a database query
• Actually a command that returns a specific values
• Used to retrieve:• Current system date • Name of the current database user• Next value in a sequence
![Page 22: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/22.jpg)
22
Pseudocolumn Examples
PseudocolumnName
Output
CURRVAL Most recently retrieved sequence value
NEXTVAL Next value in a sequence
SYSDATE Current system date from database server
USER Username of current user
![Page 23: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/23.jpg)
23
Retrieving the current system date:SELECT SYSDATEFROM DUAL;
• Retrieving the name of the current user:
SELECT USERFROM DUAL;
DUAL is a system table that is used with pseudocolumns
Using Pseudocolumns
![Page 24: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/24.jpg)
24
Accessing the next value in a sequence:
sequence_name.NEXTVAL
Inserting a new record using a sequence:
INSERT INTO my_faculty VALUES
(f_id_sequence.nextval, ‘Professor Jones’);
Using PseudocolumnsWith Sequences
![Page 25: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/25.jpg)
25
• Permissions that you can grant to other users to allow them to access or modify your database objects
• Granting object privileges:GRANT privilege1, privilege2, …ON object_nameTO user1, user 2, …;
• Revoking object privileges:REVOKE privilege1, privilege2, …ON object_nameFROM user1, user 2, …;
Object Privileges
![Page 26: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/26.jpg)
26
Examples of Object Privileges
Object Type Privilege Description
Table, Sequence
ALTER Allows user to change object’s structure using the ALTER command
Table, Sequence
DROP Allows user to drop object
Table, Sequence
SELECT Allows user to view object
Table INSERT, UPDATE, DELETE
Allows user to insert, update, delete table data
Any database object
ALL Allows user to perform any operation on object
![Page 27: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/27.jpg)
27
Granting and Revoking Object Privileges
![Page 28: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/28.jpg)
28
Syntax:
SELECT column1, column2, …
FROM tablename
WHERE search_condition;
Retrieving Data From a Single Table
![Page 29: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/29.jpg)
29
To retrieve every column in a table:SELECT * FROM …
To retrieve every record in a table, omit the search conditionSELECT column1, column2, …
FROM tablename;
Retrieving Data From a Single Table
![Page 30: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/30.jpg)
30
Qualifying Table Names
• If you retrieve data from a table that is owned by another user, you must qualify the table name by prefacing it with the owner’s name
![Page 31: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/31.jpg)
31
Sometimes queries retrieve duplicate records
To suppress duplicate outputs, use the DISTINCT qualifier:SELECT DISTINCT column1, column2, …
FROM ...
Suppressing Duplicate Records
![Page 32: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/32.jpg)
32
Combining search conditions AND: both conditions must be true OR: either condition can be true
Combining AND and OR in a single operation AND comparisons are evaluated first Always use parentheses to force
conditions to be evaluated in the correct order
Using Multiple Search Conditions
![Page 33: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/33.jpg)
33
NULL: not defined Use IS NULL search condition
SELECT s_name, s_class
FROM my_students
WHERE s_class IS NULL;
Searching for NULL Records
![Page 34: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/34.jpg)
34
Use IS NOT NULL operator
SELECT s_name, s_age
FROM my_students
WHERE s_class IS NOT NULL;
Searching for NOT NULL Records
![Page 35: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/35.jpg)
35
Using the IN and NOT IN Operators
• IN retrieves all values where the search column value matches a set of values
SELECT *
FROM enrollment
WHERE grade IN (‘A’, ‘B’);
![Page 36: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/36.jpg)
36
Using the IN and NOT IN Operators
• NOT IN retrieves all values where the search column value matches a set of values
SELECT *
FROM enrollment
WHERE grade NOT IN (‘A’, ‘B’);
![Page 37: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/37.jpg)
37
Using the LIKE Operator
• Performs inexact searches by matching part of a character string
WHERE fieldname LIKE character_string;
![Page 38: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/38.jpg)
38
Using the LIKE Operator
• Character string must be in single quotes and use wildcard characters• % represents multiple wildcard characters• _ represents a single wildcard character• Wildcard characters can be placed at
beginning or end of string
• Examples:WHERE s_class LIKE ‘_R’;WHERE s_name LIKE ‘J%’;
![Page 39: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/39.jpg)
39
Use the ORDER BY clause Specify sort key, which is
column by which output is sorted
SELECT s_name, s_age
FROM my_students
ORDER BY s_age;
Sorting Query Output
![Page 40: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/40.jpg)
40
Default sort order Numerical: ascending Character: A - Z Date: oldest - newest To force the sort order: use ASC or
DESC Example
SELECT s_name, s_ageFROM my_studentsORDER BY s_age DESC;
Sorting Query Data
![Page 41: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/41.jpg)
41
Arithmetic operations on retrieved data Addition (+) Subtraction (-) Multiplication (*) Division (/)
Example:SELECT inv_id, qoh*price
FROM inventory;
Using Calculations in Queries
![Page 42: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/42.jpg)
42
ABS - absolute value CEIL – rounds a number up to the next integer FLOOR – rounds a number down to the
previous integer MOD – returns the remainder of a number and
a divisor POWER - raises a number to an exponent ROUND - rounds a number SQRT – returns the square root of a value TRUNC - truncates a number to the nearest
whole number
Single-RowNumber Functions
![Page 43: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/43.jpg)
43
Example:SELECT s_name,
TRUNC((SYSDATE - s_dob)/365)
FROM my_students;
Using Single-RowNumber Functions
![Page 44: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/44.jpg)
44
CONCAT – joins 2 character strings INITCAP – returns a string with the initial letter only
uppercase LENGTH – returns the length of a string LPAD, RPAD – returns a string with a specific number
of characters added on the left or right side LTRIM, RTRIM – returns a string with all instances of a
specific character trimmed from the left or right side REPLACE – replaces all instances of a character with
another character UPPER/LOWER – returns a string in all upper/lower
case letters
Single-RowCharacter Functions
![Page 45: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/45.jpg)
45
Example:SELECT UPPER(s_name)
FROM my_students;
Using Single-RowCharacter Functions
![Page 46: 1 Creating and Maintaining Database Objects Part 1 Database Systems](https://reader033.vdocuments.site/reader033/viewer/2022061612/5697bf791a28abf838c821eb/html5/thumbnails/46.jpg)
End of Lecture