Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Unit 4_Oracle basics

Uploaded by

aavishkaarclub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 4_Oracle basics

Uploaded by

aavishkaarclub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

What is SQL?

• SQL stands for Structured Query Language


• SQL lets you access and manipulate databases
• SQL became a standard of the American National Standards Institute
(ANSI) in 1986, and of the International Organization for
Standardization (ISO) in 1987

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.

Types of SQL Commands


There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1. Data Definition Language (DDL)
o DDL changes the structure of the table like creating a table, deleting a table, altering a
table, etc.
o All the command of DDL are auto-committed that means it permanently save all the
changes in the database.

Here are some commands that come under DDL:

o CREATE
o ALTER
o DROP
o TRUNCATE

a. CREATE It is used to create a new table in the database.

Syntax:

Skip Ad

1. CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);

Example:

1. CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB


DATE);

b. DROP: It is used to delete both the structure and record stored in the table.

Syntax
1. DROP TABLE table_name;

Example

1. DROP TABLE EMPLOYEE;

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:

To add a new column in the table

1. ALTER TABLE table_name ADD column_name COLUMN-definition;

To modify existing column in the table:

1. ALTER TABLE table_name MODIFY(column_definitions....);

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:

1. TRUNCATE TABLE table_name;

Example:

1. TRUNCATE TABLE EMPLOYEE;


2. Data Manipulation Language
o DML commands are used to modify the database. It is responsible for all form of
changes in the database.
o The command of DML is not auto-committed that means it can't permanently save all
the changes in the database. They can be rollback.

Here are some commands that come under DML:

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:

INSERT INTO TABLE_NAME


(col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);

INSERT INTO TABLE_NAME


VALUES (value1, value2, value3, .... valueN);

For example:

INSERT INTO books (Author, Subject) VALUES ("Sonoo", "DBMS");

b. UPDATE: This command is used to update or modify the value of a column in the
table.

Syntax:

1. UPDATE table_name SET [column_name1= value1,...column_nameN = valueN]


[WHERE CONDITION]

For example:

UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'

c. DELETE: It is used to remove one or more row from a table.

Syntax

DELETE FROM table_name [WHERE condition];

For example

DELETE FROM books

WHERE Author="Sonoo";
3. Data Control Language
DCL commands are used to grant and take back authority from any database user.

Here are some commands that come under DCL:

o Grant
o Revoke

a. Grant: It is used to give user access privileges to a database.

Example

1. GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;

b. Revoke: It is used to take back permissions from the user.

Example

1. REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;


4. Transaction Control Language
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.

These operations are automatically committed in the database that's why they cannot
be used while creating tables or dropping them.

Here are some commands that come under TCL:

o COMMIT
o ROLLBACK
o SAVEPOINT

a. Commit: Commit command is used to save all the transactions to the database.

Syntax:
1. COMMIT;

Example:

DELETE FROM CUSTOMERS


WHERE AGE = 25;
COMMIT;

b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.

Syntax:

1. ROLLBACK;

Example:

DELETE FROM CUSTOMERS

WHERE AGE = 25;


ROLLBACK;

c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling
back the entire transaction.

Syntax:

SAVEPOINT SAVEPOINT_NAME;

5. Data Query Language


DQL is used to fetch the data from the database.

It uses only one command:

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;

Oracle data types


In Oracle, every value has a data type which defines a set of characteristics for the
value. These characteristics cause Oracle to treat values of one data type differently
from values of another.

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.

Oracle Data Types


Oracle String data types

CHAR(size) It is used to store character data within the predefined length. It


can be stored up to 2000 bytes.

NCHAR(size) It is used to store national character data within the predefined


length. It can be stored up to 2000 bytes.
VARCHAR2(size) It is used to store variable string data within the predefined length.
It can be stored up to 4000 byte.

VARCHAR(SIZE) It is the same as VARCHAR2(size). You can also use


VARCHAR(size), but it is suggested to use VARCHAR2(size)

NVARCHAR2(size) It is used to store Unicode string data within the predefined


length. We have to specify the size of NVARCHAR2 data type. It
can be stored up to 4000 bytes.

Oracle Numeric Data Types

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.

Oracle Date and Time Data Types

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.

Introduction to the primary key


A primary key is a column of a combination of columns in a table that uniquely
identifies a row in the table.

The following are rules that make a column a primary key:

• A primary key column cannot contain a NULL value or an empty string.


• A primary key value must be unique within the entire table.
• A primary key value should not be changed over time.

Creating a primary key that consists of one column

The following CREATE TABLE statement creates the purchase_orderstable:

CREATE TABLE purchase_orders (

po_nr NUMBER PRIMARY KEY,

vendor_id NUMBER NOT NULL,

po_status NUMBER(1,0) NOT NULL,

created_at TIMESTAMP WITH TIME ZONE NOT NULL

);

Code language: SQL (Structured Query Language) (sql)

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.

Creating a primary key that consists of multiple columns

The following statement creates the purchase order line items table:

CREATE TABLE purchase_order_items (

po_nr NUMBER NOT NULL,

item_nr NUMBER NOT NULL,

product_id NUMBER NOT NULL,


quantity NUMBER NOT NULL,

purchase_unit NUMBER NOT NULL,

buy_price NUMBER (9,2) NOT NULL,

delivery_date DATE,

PRIMARY KEY (po_nr, item_nr)

);

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.

FOREIGN KEY Constraint

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.

CREATE TABLE Orders (

OrderID int NOT NULL PRIMARY KEY,

OrderNumber int NOT NULL,

PersonID int FOREIGN KEY REFERENCES Persons(PersonID)

);

Oracle unique constraint syntax


A unique constraint is an integrity constraint that ensures the data stored in a
column, or a group of columns, is unique among the rows in a table.

Typically, you apply the unique constraints to columns when you create the table
using the inline constraint syntax as follows:

CREATE TABLE table_name (

...

column_name data_type UNIQUE

...

);

This unique constraint specifies that the values in the column_name is unique across
the whole table.

Oracle unique constraint examples

Let’s create a table named clients for the demonstration:

CREATE TABLE clients (

client_id NUMBER,

first_name VARCHAR2(50) NOT NULL,

last_name VARCHAR2(50) NOT NULL,

company_name VARCHAR2(255) NOT NULL,

email VARCHAR2(255) NOT NULL UNIQUE,

phone VARCHAR(25)

);
The email column has a unique constraint that ensures there will be no duplicate
email.

Introduction to Oracle Check constraint


An Oracle check constraint allows you to enforce domain integrity by limiting the
values accepted by one or more columns.

Creating Check constraint syntax

Typically, you create a check constraint on a column when you create the table:

CREATE TABLE table_name (

...

column_name data_type CHECK (expression),

...

);

Creating Oracle Check constraint examples

The following example creates the parts table whose buy prices are positive:

CREATE TABLE parts (

part_id NUMBER,

part_name VARCHAR2(255) NOT NULL,

buy_price NUMBER(9,2) CHECK(buy_price > 0),

PRIMARY KEY(part_id)

);

Code language: SQL (Structured Query Language) (sql)

Attempting to insert 0 or negative buy price will cause an error:


INSERT INTO parts(part_name, buy_price)

VALUES('HDD',0);

Oracle issued the following error:

SQL Error: ORA-02290: check constraint (OT.SYS_C0010681) violated

Oracle NOT NULL

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.

CREATE TABLE table_name (

...

column_name data_type NOT NULL

...

);

You might also like