0% found this document useful (0 votes)
16 views2 pages

Oracle SQL Tutorial for Beginners

The document provides a tutorial on Oracle SQL, covering the creation of a database schema, management of tables, and performing CRUD operations. It includes examples for creating users, tables, and sequences, as well as executing insert, select, update, and delete statements. The tutorial serves as a foundational guide for working with databases in Oracle SQL.

Uploaded by

smmuhad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Oracle SQL Tutorial for Beginners

The document provides a tutorial on Oracle SQL, covering the creation of a database schema, management of tables, and performing CRUD operations. It includes examples for creating users, tables, and sequences, as well as executing insert, select, update, and delete statements. The tutorial serves as a foundational guide for working with databases in Oracle SQL.

Uploaded by

smmuhad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

An Najah University

Databases Fundamentals

Oracle SQL Tutorial


1. Create a Database Schema
A schema in Oracle is created by creating a user. Below are the steps:

CREATE USER my_schema IDENTIFIED BY my_password;

GRANT CONNECT, RESOURCE TO my_schema;


ALTER USER my_schema QUOTA UNLIMITED ON USERS;

CONNECT my_schema/my_password;

2. Create and Manage Tables


You can create, modify, and manage tables in Oracle. Below are examples:

-- Create Table
CREATE TABLE employees (
emp_id NUMBER PRIMARY KEY,
emp_name VARCHAR2(50) NOT NULL,
department VARCHAR2(30),
salary NUMBER(10, 2),
hire_date DATE DEFAULT SYSDATE
);

-- Add Column
ALTER TABLE employees ADD phone_number VARCHAR2(15);

-- Modify Column
ALTER TABLE employees MODIFY salary NUMBER(12, 2);

-- Drop Column
ALTER TABLE employees DROP COLUMN phone_number;

3. Perform CRUD Operations (Create, Read, Update, Delete)


CRUD refers to Create, Read, Update, and Delete operations. Examples are shown below:

Create (Insert Data)


Insert rows into the table using the INSERT statement.

INSERT INTO employees (emp_id, emp_name, department, salary)


VALUES (1, 'John Doe', 'HR', 5000);

1
An Najah University
Databases Fundamentals

Read (Retrieve Data)


Retrieve data using the SELECT statement.

SELECT * FROM employees;

SELECT emp_name, department FROM employees;

SELECT emp_name, salary FROM employees


WHERE department = 'HR';

SELECT emp_name AS "Employee Name", salary AS "Monthly Salary" FROM


employees;

Update (Modify Data)


Update rows using the UPDATE statement.

UPDATE employees SET salary = 5500 WHERE emp_id = 1;

UPDATE employees SET salary = salary * 1.10;

Delete (Remove Data)


Delete rows using the DELETE statement.

DELETE FROM employees WHERE emp_id = 2;

DELETE FROM employees; -- Deletes all rows (Use carefully!)

4. Use Auto-Generated IDs with Sequences


Oracle uses sequences to auto-generate unique IDs for tables.

CREATE SEQUENCE emp_seq START WITH 1 INCREMENT BY 1;

INSERT INTO employees (emp_id, emp_name, department, salary)


VALUES (emp_seq.NEXTVAL, 'Charlie Green', 'IT', 8000);

Common questions

Powered by AI

CRUD operations—Create, Read, Update, Delete—are foundational to database management. In Oracle, 'Create' involves inserting new records, which initiates data growth and utilization (e.g., INSERT INTO employees). 'Read' retrieves data, enabling data analysis and reporting (e.g., SELECT * FROM employees). 'Update' modifies data to reflect changes, maintaining accuracy and currency (e.g., UPDATE employees SET salary = 5500 WHERE emp_id = 1). 'Delete' removes data, essential for clearing obsolete or unnecessary records, thereby optimizing storage and improving performance (e.g., DELETE FROM employees WHERE emp_id = 2).

Using DELETE without a WHERE clause in Oracle SQL results in the deletion of all rows from a table. This operation can be beneficial for quickly clearing test data or resetting a table for fresh data input. However, it carries significant risks, such as the irreversible loss of important data if backups are not available, and potential disruption to applications and reports dependent on the data. Caution and clear data management strategies must accompany such powerful operations to mitigate these risks .

Sequences for auto-generated IDs in Oracle ensure unique, incrementing primary keys across transactions, which is a best practice because it guarantees uniqueness without manual input, reducing human errors. By using CREATE SEQUENCE and calling emp_seq.NEXTVAL during inserts, each new entry receives a unique identifier, facilitating data integrity. This method prevents potential issues such as duplicate keys and concurrent conflicts from parallel transactions, which could arise if IDs were managed manually or through application logic .

The GRANT statement in Oracle SQL is used to assign specific privileges to users, ensuring controlled access to resources and actions in a database. By granting roles like CONNECT and RESOURCE, specific capabilities are legally shared, enforcing security by preventing unauthorized operations. This structured approach ensures users have the necessary, but not excessive, privileges to perform their roles efficiently while restricting potential misuse or data breaches .

In Oracle, creating a database schema involves creating a user, granting privileges, setting quotas, and connecting to the schema. Creating a user is done through the command CREATE USER my_schema IDENTIFIED BY my_password;, which establishes a unique schema and associated credential. Granting privileges with GRANT CONNECT, RESOURCE TO my_schema; is critical as it allows the user to connect and utilize resources within the database. Setting a quota using ALTER USER my_schema QUOTA UNLIMITED ON USERS; ensures the schema has the necessary space to grow and store data effectively. Finally, connecting via CONNECT my_schema/my_password; verifies the setup, allowing the user to begin creating and managing tables .

Modifying a column's data type in Oracle SQL requires careful consideration of existing data compatibility and potential schema impacts. For instance, changing salary from NUMBER(10, 2) to NUMBER(12, 2) allows for larger values but must ensure no data truncation and that stored values fit the new format. Impacts include necessary application logic adjustments and anticipated data range expansions. Backing up data and testing changes in non-production environments are critical precautionary measures .

Default values such as SYSDATE in Oracle SQL provide benefits like ensuring that fields capture current system timestamps automatically for record creation, ensuring immediate data completeness without manual intervention. However, potential drawbacks include lack of flexibility if future updates necessitate specific date inputs and possible inconsistencies if the database timezone differs from application settings, impacting accurate time records .

Using ALTER USER to assign quotas in Oracle SQL impacts database storage by controlling the space allotment available to different users or schemas, which helps prevent individual users from overconsuming resources. Granting unlimited quotas strategically enables necessary operations within specified constraints, while maintaining a balance across system-wide storage demands. This practice is significant because it optimizes database performance, prevents resource monopolization, and ensures fair resource distribution among multiple schemas or users .

Altering table structures in Oracle SQL offers flexibility by allowing changes to existing table definitions without recreating them. Adding columns with ALTER TABLE employees ADD phone_number VARCHAR2(15) allows for the expansion of data attributes stored. Modifying column definitions, e.g., ALTER TABLE employees MODIFY salary NUMBER(12, 2), permits adjustments to accommodate broader data ranges. Dropping columns using ALTER TABLE employees DROP COLUMN phone_number helps simplify tables by removing unnecessary fields. These operations enable timely updates to schema structures in response to evolving data requirements .

In Oracle SQL, the table creation syntax supports data integrity by allowing for specification of data types, primary keys, and default values. For example, the declaration emp_id NUMBER PRIMARY KEY ensures that each ID is unique and cannot be null, maintaining the uniqueness of records. The use of NOT NULL constraints like in emp_name VARCHAR2(50) NOT NULL ensures that certain fields must always have a value. Default attribute settings such as hire_date DATE DEFAULT SYSDATE ensure meaningful data is automatically populated without manual input .

You might also like