You are on page 1of 16

WELCOME ALL

SQL – STRUCTURAL QUERY LANGUAGE

• Computer language
• IBM implemented this language, originally called sequel.

Purpose?
• To store, Manipulate, and retrive data in relational database.
TOPIC TO BE COVERED…

• Constraints
• Index
• Views
• Alias
CONSTRAINTS

• Limitations / Restrictions.
• Rules implemented on table.

When to be specified?
• Constraints can be specified when table is created.
CREATE TABLE statement.

• After the table is created with alter


ALTER TABLE statement.
CONSTRAINTS TYPES

• Column constraints.
• Table constraints.

Commonly using constraints.


•NOT NULL - Ensures that a column cannot have a NULL value
•UNIQUE - Ensures that all values in a column are different
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in
a table
•DEFAULT - Sets a default value for a column if no value is specified
•CREATE INDEX - Used to create and retrieve data from the database very quickly
EXAMPLE FOR CONSTRAINTS.

• CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255) NOT NULL,
    Age int
);
• ALTER TABLE Persons
MODIFY Age int NOT NULL;
INDEX STATEMENT

• Reference.
• Quick lookup table.
• Retrieve data from the database.
• CREATE INDEX statement is used to create indexes in tables
• The users cannot see the indexes,
• Used to speed up searches/queries.
• Therefore: Check the syntax for creating indexes in your database.

CREATE UNIQUE INDEX index_name


ON table_name (column1, column2, ...);

• The DROP INDEX statement is used to delete an index in a table.


DROP INDEX table_name.index_name;

ALTER TABLE table_name


DROP INDEX index_name;
• You can check the different indexes present in a particular table given by the
user or the server itself and their uniqueness. 
select * from USER_INDEXES;

• You can use the system stored procedure sp_rename to rename any index in
the database.
EXEC sp_rename
index_name,
new_index_name,
N'INDEX';
VIEW STATEMENT

• view is a virtual table based on the result-set of an SQL statement.


• A view is created with the CREATE VIEW statement.
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

• We can query the view above as follows:


SELECT * FROM table_name;
•  View named DetailsView from the table StudentDetails.
Query:
CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;

• To see the data in the View, we can query the view in the same manner as we
query a table.
SELECT * FROM DetailsView;
• shows up-to-date data!
• The database engine recreates the view, every time a user queries it

• The SELECT statement is used to select data from a database.


• The data returned is stored in a result table, called the result-set.
ALIAS STATEMENT

• assumed identity.
• used to give a table, or a column in a table, a temporary name.
• An alias only exists for the duration of that query.
• An alias is created with the AS keyword.
• Alias Column Syntax
• SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
SELECT column_name AS alias_name
FROM table_name;

• Alias Table Syntax


SELECT column_name(s)
FROM table_name AS alias_name;
• Alias for Tables Example
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName='Around the Horn' AND c.CustomerID=o.CustomerID;
Without Alias
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName='Around the Horn' AND
Customers.CustomerID=Orders.CustomerID;
Thank You

You might also like