SQL Server

02 Apr 2020 - Jake Na

Microsoft SQL 서버에 대하여 기본 학습이 필요하여 이 문서를 작성합니다. 학습내용은 sqlservertutorial.net에서 제공되는 샘플 DB와 설명을 기초하였으니 자세한 내용이 필요할 경우 해당 사이트를 참조하기 바랍니다.

SQL Express 2019 설치 및 구성

Sample DB 다운로드 및 SQL Server DB 업로드

BikeStores DB 생성, 스키마 생성, 테이블 정의 및 Sample Data 업로드 수행

Table, View, Indexed View

CREATE VIEW product_master WITH SCHEMABINDING AS
  SELECT
    product_id,
    product_name,
    model_year,
    list_price,
    brand_name,
    category_name
  FROM
    production.products p
  INNER JOIN production.brands b ON b.brand_id = p.brand_id
  INNER JOIN production.categories c ON c.category_id = p.category_id;
CREATE UNIQUE CLUSTERED INDEX ucidx_product_id
ON production.product_master(product_id);

Indexes

Clustered Indexes vs. NonClustered Indexes

Rename Indexes, Diable/Enable Indexes, Unique Indexes, Drop Indexes

Indexes with included columns

CREATE UNIQUE INDEX ix_cust_email_inc
       ON sales.customers(email)
       INCLUDE(first_name,last_name);

Filtered Indexes

CREATE INDEX ix_cust_phone ON sales.customers(phone) WHERE phone IS NOT NULL;

Indexes on computed columns

SELECT
    first_name,
    last_name,
    email
FROM
    sales.customers
WHERE
    SUBSTRING(email, 0, CHARINDEX('@', email, 0)) = 'garry.espinoza';
ALTER TABLE sales.customers
ADD email_local_part AS SUBSTRING(email, 0, CHARINDEX('@', email, 0));
;;
CREATE INDEX ix_cust_email_local_part ON sales.customers(email_local_part);

Stored Procedures

A basic guide to SP, Parameters, Variables, Output Parameters

Control-of-Flow Statements : BEGIN…END, IF ELSE, WHILE, BREAK/CONTINUE

Cursors, Handling Exceptions, Dynamic SQL

User-defined Functions

User-defined scalar functions

Table varialbes

Table-valued functions

Removing user-defined functions

Triggers

Creating a trigger in SQL Server

Creating an INSTEAD OF trigger

Creating a DDL trigger

Enabling/Diabling triggers

Viewing the definition of a trigger

Listing all triggers in SQL Server

Removing triggers

Functions

Aggregate Functions

Date Functions

String Functions

System Functions

Window Functions