UNIT 2 notes
UNIT 2 notes
Sequence:
A sequence in SQL is a database object that generates a series of unique numbers, typically
used to create auto-incrementing values like primary keys. SQL sequences are an essential
feature of relational database management systems (RDBMS) used to generate unique
numeric values in a sequential order. These values are widely used for generating primary
keys, unique keys, and other numeric identifiers in databases. SQL sequences offer
flexibility, performance, and ease of use, making them indispensable in managing and
organizing data in large-scale applications.
Syntax
1. Referencing a Sequence
Once a sequence is created, you use it to generate values.
CURRVAL: Returns the current value (Oracle, PostgreSQL only), after NEXTVAL is
called.
Example:
-- Create table
Name VARCHAR(100)
);
-- Create sequence
INCREMENT BY 1;
Altering a Sequence:
You can modify a sequence’s properties, such as its increment value, cache size, min/max
values, etc.
✅ Syntax:
-- Oracle / PostgreSQL
INCREMENT BY 5
MAXVALUE 10000
CACHE 20;
-- SQL Server
INCREMENT BY 2
MAXVALUE 9999;
3. Dropping a Sequence
Snapshot
SQL snapshots are a recent read-only copy of the table from the database or a subset of
rows/columns of a table. The SQL statement that creates and subsequently maintains a
snapshot normally reads data from the source database server.
A snapshot (or materialized view) is a database object that stores the results of a query
physically, unlike a regular view which stores only the query definition.
Example
Complex Snapshot
In complex snapshot, a row may be based on more than one row in a remote table
via GROUP BY operation or result of Multi-Table Join. This consists of joined tables, views, or
grouped and complex SELECT statement queries.
FROM student
UNION ALL
FROM new_student;
Reduced load: Decreases the load on the underlying tables and allows for better
performance when reporting.
Offline access: You can use the snapshot even when the base tables are not available
(depending on refresh frequency).
Example
Syntax -- MySQL
Username VARCHAR(100),
Email VARCHAR(100));
PRIVILEDGES –
The authority or permission to access a named object as advised manner, for example,
permission to access a table. Privileges can allow permitting a particular user to connect to
the database. In, other words privileges are the allowance to the database by the database
object. In SQL, privileges (also called permissions) are rights or authorizations given to users
or roles that determine what actions they can perform on database objects.
Types:
System privileges — A system privilege is the right to perform an activity on a specific type
of object. for example, the privilege to delete rows of any table in a database is system
privilege. There are a total of 60 different system privileges. System privileges allow users to
CREATE, ALTER, or DROP the database objects.
CREATE TABLE
CREATE USER
Granting Priviledge
Granting privileges means giving permission to a user or role to perform specific actions on
database objects (such as tables, views, procedures)
ON object_name
TO user_or_role;
ON Employees
TO John;
This allows user John to read (SELECT) and add (INSERT) data into the Employees table.
Revoking Privileges-
Revoking privileges means taking away previously granted permissions from a user or role to
restrict their access to database objects.
ON object_name
FROM user_or_role;
REVOKE UPDATE
ON Employees
FROM John;
Cascade: The CASCADE option means that any dependent objects or records will be
automatically deleted or dropped when the referenced object is deleted or dropped.
Use Cases:
This will drop the Employees table and any dependent objects (like foreign key constraints or
views.
CustomerID INT,
ON DELETE CASCADE
);
RESTRICT:-
The RESTRICT option prevents the operation from being completed if there are any
dependencies (e.g., other objects or rows that rely on it).
This will only drop the table if no other objects depend on it. If any constraints or views
reference it, the operation fails.
If you don't specify ON DELETE CASCADE, then by default, most SQL systems restrict deletion
of a parent row if dependent rows exist.
TYPES OF VIEW:-
Vertical View:- A vertical view is created by selecting only specific columns (i.e., vertical
slices of the table).
This limits what data (columns) users can see, often used for data security or customized
reporting.
EmployeeID INT,
Name VARCHAR(100),
Department VARCHAR(50),
Email VARCHAR(100));
FROM Employees;
This view hides sensitive fields like Salary and Email.
Horizontal View:-
A horizontal view is created by selecting only specific rows (i.e., horizontal slices of the
table), typically using a WHERE clause.
SELECT *
FROM Employees