SQL Introduction
Oracle Data Types
VARCHAR2(n) → Variable-length string
NUMBER(p,s) → Numeric values (p = precision, s = scale)
DATE → Stores date and time
Datatype. Example Value
CHAR(5) ‘ ABC '
VARCHAR2(10) 'Anu'
NUMBER(5,2) 123.45
DATE 21-AUG-2025
CREATE TABLE Command
Used to create a new table with columns and datatypes.
Syntax:
CREATE TABLE table_name (
column1 datatype [constraint],
column2 datatype [constraint],
...
);
Example:
CREATE TABLE Student (
RollNo NUMBER(5) PRIMARY KEY,
Name VARCHAR2(50),
Age NUMBER(3),
DOB DATE
);
Inserting Data into Table
Syntax:
INSERT INTO table_name (col1, col2, ...) VALUES (val1, val2, ...);
Example:
INSERT INTO Student (RollNo, Name, Age, DOB)
VALUES (101, 'Anu', 22, '01-JAN-2003');
Runtime insertion
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (&value1, &value2, &value3, ...);
Example:
INSERT INTO Student (RollNo, Name, Age, Department)
VALUES (&RollNo, '&Name', &Age, '&Department');
. ALTER Command
Category: DDL (Data Definition Language)
Purpose: Used to change the structure of a
table (not the data).
Affects: Columns, constraints, table schema
Alter:Syntax
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE table_name
MODIFY column_name new_datatype;
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
Alter :Example
-- Add new column
ALTER TABLE Student ADD Email
VARCHAR(50);
-- Change column datatype
ALTER TABLE Student MODIFY Name
VARCHAR(100);
-- Drop a column
ALTER TABLE Student DROP COLUMN Age;
UPDATE Command
Category: DML (Data Manipulation
Language)
Purpose: Used to change the data
stored in the table.
Affects: Row values inside the table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 =
value2, ...
WHERE condition;
Update: Example
-- Update one student’s email
UPDATE Student
WHERE RollNo = 101;
-- Increase marks for all students
UPDATE Student
SET Marks = Marks + 5;
Viewing Data in the Table
Retrieve data using SELECT.
Syntax:
SELECT * FROM table_name;
SELECT column1, column2 FROM table_name;
Example:
SELECT * FROM Student;
SELECT Name, Age FROM Student;
Sorting Data in a Table
Use ORDER BY to arrange rows.
Syntax:
SELECT * FROM table_name ORDER BY column ASC;
SELECT * FROM table_name ORDER BY column DESC;
Example:
SELECT * FROM Student ORDER BY Age ASC;
SELECT * FROM Student ORDER BY Marks DESC;
Creating a Table from a
Table
Create a new table by copying structure/data from another.
Syntax:
CREATE TABLE new_tablename AS SELECT * FROM old_tablename;
Example:
CREATE TABLE Top_Students AS SELECT * FROM Student WHERE
Marks > 80;
Inserting Data into a Table
from Another Table
Copy data from one table to another.
Syntax:
INSERT INTO new_table SELECT * FROM old_table;
Example:
INSERT INTO Backup_Student SELECT * FROM
Student;
Delete Operations
Remove rows from a table.
Syntax:
DELETE FROM table_name WHERE condition;
DELETE FROM table_name;
-- deletes all rows
Example:
DELETE FROM Student WHERE Age < 18;
DELETE FROM Student;
-- deletes all rows
Updating the Contents of a
Table
Modify existing records.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
Example:
UPDATE Student SET Marks = 90 WHERE Name = 'joe';
UPDATE Student SET Age = 21, Marks = 85 WHERE RollNo =
101;
Modifying the Structure of
Tables
Use ALTER TABLE to add, modify, or drop columns.
Syntax:
ALTER TABLE table_name ADD column datatype;
ALTER TABLE table_name MODIFY column datatype;
ALTER TABLE table_name DROP COLUMN column;
Example:
ALTER TABLE Student ADD Address VARCHAR(50);
ALTER TABLE Student MODIFY Marks DECIMAL(5,2);
ALTER TABLE Student DROP COLUMN Address;
Renaming Tables
Change table name.
Syntax:
RENAME old_table TO new_table;
Example:
RENAME Student TO Student_Info;
Destroying Tables
Permanently remove a table.
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE Student_Info;
TRUNCATE Command
TRUNCATE is used to delete all rows from a table.
The structure of the table remains, only the data is removed.
Faster than DELETE because it does not log each row deletion.
Syntax
TRUNCATE TABLE table_name;
Example
TRUNCATE TABLE Student;
This will remove all student records but the Student table still exists (you can insert new data again).
TRUNCATE is a DDL command
Because it does not operate row by row like DELETE (which is DML).
Instead, it deallocates all the data pages of the table, resetting it quickly.
After TRUNCATE, the table structure remains, but all data is gone.
Data Constraints in SQL
Types of Data Constraints
Rules applied on columns to ensure valid
data.
Integrity-Oriented (IO) → Primary Key,
Foreign Key, Unique
Business Rule Constraints → NOT NULL,
CHECK, DEFAULT
Primary Key Constraint
Ensures a column/combination of columns
uniquely identifies each row.
Syntax:
CREATE TABLE Student (
RollNo NUMBER PRIMARY KEY,
Name VARCHAR2(50)
);
Foreign Key Constraint
Ensures values in a column match values in another table’s primary key.
Simple Example of Foreign Key
Create Department table (Parent table):
CREATE TABLE Department (
Dept_ID INT PRIMARY KEY,
Dept_Name VARCHAR(50)
);
Create Student table (Child table):
CREATE TABLE Student (
RollNo INT PRIMARY KEY,
Name VARCHAR(50),
Dept_ID INT,
FOREIGN KEY (Dept_ID) REFERENCES Department(Dept_ID)
);
Unique Key Constraint
Ensures all values in a column are unique
(but allows NULLs).
Syntax:
CREATE TABLE Employee (
EmpID NUMBER PRIMARY KEY,
Email VARCHAR2(100) UNIQUE
);
NULL Value Concept
NULL means “no value” (different from 0 or empty
string).
NOT NULL Constraint
Ensures a column cannot be left empty.
Syntax:
CREATE TABLE Product (
P_ID NUMBER PRIMARY KEY,
Name VARCHAR2(50) NOT NULL
);
CHECK Constraint
👉 Restricts values in a column based on a condition.
Syntax:
CREATE TABLE Employee (
EmpID NUMBER PRIMARY KEY,
Age NUMBER CHECK (Age >= 18)
);
DEFAULT Value Concept
👉 Assigns a default value when no value is provided.
Syntax:
CREATE TABLE Orders (
OrderID NUMBER PRIMARY KEY,
Status VARCHAR2(20) DEFAULT 'Pending'
);