Unit 4_Oracle basics
Unit 4_Oracle basics
SQL Commands
o SQL commands are instructions. It is used to communicate with the database. It is also
used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
o CREATE
o ALTER
o DROP
o TRUNCATE
Syntax:
Skip Ad
Example:
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
1. DROP TABLE table_name;
Example
c. ALTER: It is used to alter the structure of the database. This change could be either
to modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:
EXAMPLE
1. ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
2. ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.
Syntax:
Example:
o INSERT
o UPDATE
o DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row
of a table.
Syntax:
For example:
b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
For example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
Syntax
For example
WHERE Author="Sonoo";
3. Data Control Language
DCL commands are used to grant and take back authority from any database user.
o Grant
o Revoke
Example
Example
These operations are automatically committed in the database that's why they cannot
be used while creating tables or dropping them.
o COMMIT
o ROLLBACK
o SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
1. COMMIT;
Example:
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax:
1. ROLLBACK;
Example:
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling
back the entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
o SELECT
a. SELECT: This is the same as the projection operation of relational algebra. It is used
to select the attribute based on the condition described by WHERE clause.
Syntax:
1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
For example:
1. SELECT emp_name
2. FROM employee
3. WHERE age > 20;
When you create a new table, you specify a data type for each of its columns.
Similarly, when you create a new procedure, you specify a data type for each of its
arguments. The data type defines the allowed values that each column or argument
can store. For example, a DATE column cannot store a value of February 30, because
this is not a valid date.
NUMBER(p, It contains precision p and scale s. The precision p can range from 1 to 38, and
s) the scale s can range from -84 to 127.
FLOAT(p) It is a subtype of the NUMBER data type. The precision p can range from 1 to
126.
DATE It is used to store a valid date-time format with a fixed length. Its range varies
from January 1, 4712 BC to December 31, 9999 AD.
TIMESTAMP It is used to store the valid date in YYYY-MM-DD with time hh:mm:ss format.
);
The purchase_orders table has four columns purchase order number (po_nr), vendor
id (vendor_id), purchase order status (po_status), and the timestamp (created_at) of
which the purchase order is created.
In this table, defined the po_nr column as the primary key by using the PRIMARY KEY
clause.
The following statement creates the purchase order line items table:
delivery_date DATE,
);
In this example, the primary key of the purchase_order_items table consists of two
columns: po_nr and item_nr. It means that the combination of values of these
columns uniquely identifies a purchase order line item.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the
PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with the primary
key is called the referenced or parent table.
);
Typically, you apply the unique constraints to columns when you create the table
using the inline constraint syntax as follows:
...
...
);
This unique constraint specifies that the values in the column_name is unique across
the whole table.
client_id NUMBER,
phone VARCHAR(25)
);
The email column has a unique constraint that ensures there will be no duplicate
email.
Typically, you create a check constraint on a column when you create the table:
...
...
);
The following example creates the parts table whose buy prices are positive:
part_id NUMBER,
PRIMARY KEY(part_id)
);
VALUES('HDD',0);
An Oracle NOT NULL constraint specifies that a column cannot contain NULL values.
The Oracle NOT NULL constraints are inline constraints which are typically used in
the column definition of the CREATE TABLE statement.
...
...
);