You are on page 1of 10

Cursors are a mechanism to explicitly enumerate

through the rows of a result set, rather than


retrieving it as such.

they are typically a thing to be avoided within SQL


Server stored procedures if at all you can write a query
without the use of cursors, you give the optimizer a
much better chance to find a fast way to implement it.
DECLARE
@product_name VARCHAR(MAX),
@list_price DECIMAL;

DECLARE cursor_product CURSOR


FOR SELECT
product_name,
list_price
FROM
production.products;

OPEN cursor_product;

FETCH NEXT FROM cursor_product INTO


@product_name,
@list_price;

WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @product_name + CAST(@list_price AS varchar);
FETCH NEXT FROM cursor_product INTO
@product_name,
@list_price;
END;

CLOSE cursor_product;

DEALLOCATE cursor_product;
Clustered indexes They don't have to be unique but it
certainly is encouraged.

A clustered index defines the order in which data is


physically stored in a table.

Table data can be sorted in only way, therefore, there can be


only one clustered index per table.

In SQL Server, the primary key constraint automatically


creates a clustered index on that particular column.

The only time the data rows in a table are stored in sorted
order is when the table contains a clustered index. When a
table has a clustered index, the table is called a clustered
table

Create nonclustered indexes

A nonclustered index is a data structure that improves the


speed of data retrieval from tables.

Unlike a clustered index, a nonclustered index sorts and


stores data separately from the data rows in the table

Unique indexes – enforce the uniqueness of values in one or more


columns.

CREATE INDEX ix_customers_name ON sales.customers(last_name, first_name);


A WHERE-mondatot a sor műveletekben használják, és egyetlen
sorban alkalmazzák, míg a HAVING záradékot az oszlop műveletekben
használják, és az összesített sorokra vagy csoportokra is alkalmazható.

As a rule of thumb, use WHERE before GROUP


BY and HAVING after GROUP BY. It is a rather primitive rule, but it is
useful in more than 90% of the cases.

Az összesített funkciók, mint a min, összeg, max, avg soha nem


jelennek meg a WHERE záradékkal együtt. Ezzel szemben ezek a
funkciók a HAVING záradékban jelennek meg.
Gyakori adattipusok:
CHAR(N) N karakter hosszú fix hosszon tárolt karaktersorozat.
VARCHAR (N) N karakter hosszú változó hosszon tárolt karaktersorozat.

SZÁMTÍPUS (M,D) M számjegyből álló szám, amiből D van a tizedesvessző után


PL.:
SMALLINT -32768 és 32767 közötti egész
INT -2147483648 és 2147483647 közötti egész
FLOAT lebegőpontos szám
DOUBLE

DÁTUM
DATE ÉÉÉÉ-HH-NN
DATETIME ÉÉÉÉ-HH-NN óó:pp:mm
ENUM felsorolás tipus. pl.:ENUM(érték1,érték2,érték3…)

MEGSZORÍTÁSOK

-elsődleges kulcs megszorítás


-egyediség megszorítás (Unique)
-Not Null
-Külső kulcs megadása

ON UPDATE NO ACTION

ON UPDATE CASCADE

ON UPDATE SET NULL:


ON UPDATE SET DEFAULT

CONSTRAINT fk_group FOREIGN KEY (group_id)


REFERENCES procurement.vendor_groups(group_id)

SELECT
city,
COUNT (*)
FROM
sales.customers
WHERE
state = 'CA'
GROUP BY
city
HAVING
COUNT (*) > 10
ORDER BY

city;
In that case, WHERE is used to filter rows before grouping,
and HAVING is used to exclude records after grouping.

AGGREGATED/ összesítő függvények


COUNT

SUM

MIN and MAX

AVG

SELECT year,
COUNT(*) AS count
FROM tutorial.aapl_historical_stock_price
GROUP BY year;

SELECT
firstname,
lastname
FROM
sales.customers
ORDER BY
first_name DESC;
When should indexes be avoided?
 Indexes should not be used on small tables.

 Tables that have frequent, large batch updates


or insert operations.

 Indexes should not be used on columns that


contain a high number of NULL values.

 Columns that are frequently manipulated


should not be indexed.

WHERE-mondatot a sor műveletekben használják, és


egyetlen sorban alkalmazzák

HAVING záradékot az oszlop műveletekben használják,


és az összesített sorokra vagy csoportokra is
alkalmazható.
Clustered
A clustered index sorts and stores the data rows of
the table or view in order based on the index key.

This type of index is implemented as a B-tree


structure that supports fast retrieval of the rows,
based on their key values.

By default, SQL Server creates a clustered index when


you create a new table with a primary key

Nonclustered
A nonclustered index can be defined on a table or
view with a clustered index or on a heap.
Each row in the index contains the key value and a
row locator.
This locator points to the data row in the
clustered index or heap having the key value.
The rows in the index are stored in the order of
the key values,
but the data rows are not guaranteed to be in any
particular order unless they are in a clustered
index.

Unique
A unique index ensures that the key contains no
duplicate values and therefore every row in the
table or view is in some way unique.
Full-text
A special type of token-based functional index that
is built and maintained by the Microsoft Full-Text
Engine for SQL Server. It provides efficient
support for sophisticated word searches in
character string data.

Spatial A spatial index provides the ability to


perform certain operations more efficiently on
spatial objects (spatial data)

Filtered An optimized nonclustered index,


especially suited to cover queries that select from
a well-defined subset of data. It uses a filter
predicate to index a portion of rows in the table.
A well-designed filtered index can improve query
performance, reduce index maintenance costs, and
reduce index storage costs compared with full-table
indexes.

XML A shredded, and persisted, representation of


the XML binary large objects (BLOBs) in the xml
data type column.

You might also like