0% found this document useful (0 votes)
3 views

3 Table Indexes and View

Uploaded by

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

3 Table Indexes and View

Uploaded by

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

TABLE INDEXES and VIEW

Fsoft Academy

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use


Lesson Objectives

This is a sample text that you can edit.


01 Create IndexesYou
to can
improve
change query
font(size,retrieval speed
color, name),
or apply any desired formatting.

02 Create, maintain, and use View

03 Demo

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 2


Agenda

1. Table Indexes

2. View

3. Naming Convention and Styles

4.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 3


Section1

TABLE INDEXES

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 4


Why use indexes?
 An index in database is similar to an index in a book
 Indexes in database help speed up search queries. Allow find data in a table
without scanning the entire table.
1 2 3 4 5

Late
9 8 7 6

I want bananas …

Result:

1 2 3 4 5
Quick
9 8 7 6

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 5


Table Indexes
CREATE TABLE dbo.PhoneBook (
LastName varchar(50) NOT NULL,
FirsName varchar(50) NOT NULL,
PhoneNumber varchar(50) NOT NULL
);

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 6


Table Indexes
CREATE TABLE dbo.PhoneBook ( Result:
LastName varchar(50) NOT NULL, 783-555-0110
FirsName varchar(50) NOT NULL,
PhoneNumber varchar(50) NOT NULL
);

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 7


Table Indexes
 There are 2 types of major Indexes:
Clustered
• Data is stored in the order on the clustered index
• Only 1 clustered index per table
• Usually the Primary Key
• Sort and store the data rows in the table based on their key value.
Non-clustered
• Data is not stored in the order on the non clustered index
• Have a structure completely separate from the data rows.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 8


Clustered Index
CREATE CLUSTERED INDEX IX_PhoneBook_CI ON dbo.PhoneBook (LastName, FirstName)

Index

Data

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 9


Clustered Index

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 10


Non - Clustered Index

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 11


Creating an Index
 Create a new index:
CREATE INDEX index_name
ON table_name (column1_name, column2_name, …)

 Deleting an Index

DROP INDEX table_name.index_name

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 12


Section2

VIEWS

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 13


What is a view?
 A View is a logical or virtual table. The fields in a view are fields from one or
more real tables in the database.
 There are two major reasons you might want to use views:
 Views allow you to limit the data users can access
 Views reduce complexity for end users.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 14


Creating a view
CREATE VIEW View_Name [list of column names]
AS
SELECT…
Table: DEPARTMENT
Table: EMP
Example:

CREATE VIEW view_EmployeeByDpt


AS
SELECT ID, NAME, AGE, DEPT_NAME
FROM EMP, DEPARTMENT
WHERE EMP.DEP_ID = DEPARTMENT.DEPT_ID

view_EmployeeByDpt
SELECT * FROM view_EmployeeByDpt

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 15


Deleting a view
 Syntax:

DROP VIEW View_Name

 Example:

DROP VIEW view_EmployeeByDpt

view_EmployeeByDpt

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 16


Section3

Naming convention and styles

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 17


NAMING CONVENTION AND STYLES
1.Use UPPER CASE for all T-SQL constructs, excepts Types

Correct:

SELECT MAX([Salary]) FROM dbo.[EmployeeSalary]

Incorrect:

SELECT max([Salary]) from dbo.[EmployeeSalary]

2.Use lower case for all T-SQL Types and Usernames

Correct:

DECLARE @MaxValue int

Incorrect:

DECLARE @MaxValue INT

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 18


NAMING CONVENTION AND STYLES
3.Use Pascal casing for all UDO’s

Correct:
CREATE TABLE dbo.EmployeeSalary
(
EmployeeSalaryID INT
)
Incorrect:
CREATE TABLE dbo.Employeesalary
(
EmployeesalaryID int
)
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 19
NAMING CONVENTION AND STYLES
4.Avoid abbreviations and single character names
Correct:
DECLARE @RecordCount int
Incorrect:
DECLARE @Rc int

5.UDO naming must confer to the following regular expression


([a-zA-Z][a-zA-Z0-9]).
Do not use any special or language dependent characters to name objects. Constraints
can use the underscore character.
Correct:
CREATE TABLE dbo.[EmployeeSalary]
Incorrect:
CREATE TABLE dbo.[Employee Salary]

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 20


NAMING CONVENTION AND STYLES
6.Use the following prefixes when naming objects

usp_: User stored procedures DF_: Default constraints


ufn_: User defined functions PK_: Primary Key constraints
view_: Views
FK_: Foreign Key constraints
IX_: Indexes
CHK_: Check constraints

UNI_: Unique constraints

Correct:
CREATE PROCEDURE dbo.usp_EmployeeSelectAll
Incorrect:
CREATE PROCEDURE dbo.EmployeeSelectRetired –-without preffixed

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 21


NAMING CONVENTION AND STYLES
7.Name tables in the singular form
Correct:
CREATE TABLE dbo.[Employee]
Incorrect:
CREATE TABLE dbo.[Employees]

8.Tables that map one-to many, many-to-many relationships should be named by


concatenating the names of the tables in question, starting with the most central
table’s name.
Correct:
CREATE TABLE dbo.[EmployeeSalary]

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 22


Summary
 Table Indexes
 Why use indexes?
 Create, maintain and use index

 View
Create, maintain and use view

 Naming convention
 Demo

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 23


THANK YOU!

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use

You might also like