Restore Database
- bak
- Database Backup file
- SQlskills
- Restore database
- Right click
database
folder and click [Restore Database] - Add your file and click [OK] button
- Right click
Table
- Create new table in GUI
- Right click
table
folder and click [new]-[table] - Input your data
- Input table name, for example,
dbo.~
- Right click
- Change Design in GUI
- Right click your table and click [design]
- You can edit your table design in GUI
- Change Row in GUI
- Right click your table and click [edit rows in top 200]
- You can edit your table row in GUI
- Select rows
- Right click your table and click [select rows in top 1000]
- You can read your rows in top 1000
SELECT FROM WHERE
- SELECT
- define column what you want to see in table
- FROM
- define table you want to read
- WHERE
- define condition
=
isequal
AND
is&&
andOR
is||
%
means every length word, and_
is only one characterIS NULL
means this data can be null, andIS NOT NULL
is vice versa.
Don’t use=
when you want tonull
check, it’s always null!
- Compile order
FROM
→WHERE
→SELECT
ORDER BY
- ORDER BY
- Sort the table with specified condition
- TOP
- defines how many rows to read
- DESC and ASC
DESC
: sort data by descendingASC
: sort data by ascending
- PERCENT
- is used with
TOP
- defines how many rows with calculating percent to read
- is used with
- OFFSET # ROWS FETCH NEXT # ROWS ONLY
OFFSET
defines how many rows to readFETCH NEXT
means read rows after #th rows
Calculate and String
- Calculate
- You can use arithmetic operations, for example,
+
,-
,/
and*
ROUND
is to round value- You can also use Triangle function, for example,
COS
,SIN
andTAN
- String
- To read string that each character is 2-Byte, you should input
N
in front of the string SUBSTRING
slices the string, but start point is1
TRIM
erases blank in the string- You can link several strings by
+
- To read string that each character is 2-Byte, you should input
- SELECT # AS
- define column name by context after the
AS
- define column name by context after the
DATETIME
- Create new table in GUI
- Save table name
DateTimeTest
- Save table name
- INSERT in GUI
- Right click the table and click [Table Scripting]-[INSERT]-[New Query editor window]
- Date Syntax
- YYYYMMDD
- YYYYMMDD hh:mm
- YYYYMMDD hh:mm:ss
- YYYYMMDD hh:mm:ss.nnn
- GETUTCDATE
- Get time by UCT Criteria
- CAST(# AS DATETIME)
- You can change type of string to
DATETIME
- You can change type of string to
- DATEADD
- Adds date between DATETIME values
- DATEDIFF
- Sub date between DATETIME values
- DATEPART
- Extract value by type in DATETIME value
- You can just write type what you want to extract
CASE
- CASE WHEN ELSE END
- smillar with switch
WHEN
defines condition andTHEN
defines resultELSE
specifies result when there is not match condition with this value
Aggregation function
GROUP BY
- GROUP BY
- makes group by specified column
- You can make group by several columns
- HAVING
- define condition in
GROUP BY
- define condition in
INSERT
- INSERT
- Adds row in table
- in VALUE, You should put data that is not null
- if data can be null, you don’t need to fill the data
DELETE
- DELETE
- deletes specified row
UPDATE
- UPDATE
- changes specified data in
SET
SET
defines how to change the data
- changes specified data in
SUBQUERY
- Double SELECT statement
- Select query from the select
- You can make input value from SELECT statement
- You can use
SELECT
instead ofVALUES
- IN
- substitutes the result value of the subquery to the main query and output the result after the condition comparison.(Sub query → MAIN query)
- EXISTS
- The results of the main query are substituted to the subquery to output the results after the condition comparison(MAIN query → Sub query)
- Make new table with
INSERT INTO
- seems like copy context and pate to the table
UNION
- UNION
- merges two select
- Duplicate values are shown only once
- result columns are always same in
SELECT
statements
- UNION ALL
- selects dulicate values
- INTERSECT
- shows intersect results of two
SELECT
statements
- shows intersect results of two
- EXCEPT
- shows the result of subtracting the second
SELECT
statement from the firstSELECT
statement
- shows the result of subtracting the second
JOIN
ON
- specifies the condition of
JOIN
- specifies the condition of
- INNER JOIN
- Combine two tables horizontally
- OUTER JOIN
- Combine data even if it exists on either side
- LEFT JOIN
- RIGHT JOIN
- LEFT JOIN
- If there is information on the left, show it no matter what
- If there is no information on the right, the right information is NULL
- RIGHT JOIN
- If there is information on the right, show it no matter what
- If there is no information on the left, the left information is NULL
DECLARE
- DECLARE
- declares variable
- DECLARE TABLE
- declares table
- SET
- input value in variable
- You can set value from
SELECT
GO
- GO
- Set Valid Range for Variables
- A set of commands that are analyzed and executed in a single batch
IF-ELSE
- IF-ELSE
- same with if-else statement in C#
- PRINT
- prints content on console
- BEGIN-END
- You can several process in statement
WHILE
- WHILE
- same with while statement in C#
- BREAK
- same with break statement in C#
- CONTINUE
- same with continue statement in C#
WINDOW FUNCTION
- WINDOW FUNCTION
- always use with
OVER
- always use with
- ROW_NUMBER
- shows number of row
- RANK
- shows the ranking
- DENSE_RANK
- shows rankings including ties
- NTITLE
- show the percentage
- PARTITION
- divide by reference value and process
- LAG
- show previous value
- LEAD
- show next value
- FIRST_VALUE
- show the biggest value between previous values and current value
- LAST_VALUE
- show the smalliest value between previous values and current value
- ROWS BETWEEN AND
- specifies the range of rows to compare
UNBOUNDED PRECEDING
: from all previous row in partitionCURRENT ROW
: from/to current rowUNBOUNDED FOLLOWING
: to all previous row in partition
JOIN
Merge
- Sort Merge
- Merge Join has condition
- outer has to be unique
- One-to-Many(PK, unique)
- Random Access X → Clustered Scan and sorting
- Merge Join in C#
- Case: Outer is clustered
- is the best way to Merge Join
Nested Loop
- Nested Loop
- access outer table which is accessed first, and then access randomly in inner table
- if there is not index in inner table, time complexity O(N^2) is really heavy
- Nested Loop is great for ranging process
- Nested Loop in C#
- Forcing to use Nested Loop
- use
OPTION(LOOP JOIN)
to force using Nested Loop
- use
- use
OPTION(FORCE ORDER)
to force ordering
Hash
Sorting
Decrease Sorting
- Reason
- O(NLogN) → DB has too many data
- if size is to big to cover useable memory, then database search disk also
- So, you need to know when the sorting is excuteing
- SORT MERGE JOIN
- in algorithm, before to Merge, Sort is caused
- ORDER BY
- the database have to Sort by
ORDER BY
- if you can use
INDEX
exactly, you don’t habe to sort probably
- the database have to Sort by
- GROUP BY
- to sum up
- if you can use
INDEX
exactly, you don’t habe to sort probably
- DISTINCT
- to remove duplication
- UNION
- to remove duplication
- if you can use
UNION ALL
exactly, you don’t habe to sort probably
- RANKING WINDOWS FUNCTION
- to sum up
- MIN MAX
- to sum up