SQL Flashcards
(101 cards)
What is SQL?
Structured Query Language is a special-purpose programming language designed for managing data held in a relational database system.
DDL
Data Definition Language: To create and modify the structure of the database
DML
Data Manipulation Language: To perform read, insert, update and delete operations on the data of the database
DCL
Data Control Language: To control the access of the data stored in the database
BigInt Storage in Bytes
8
Int Storage in Bytes
4
SmallInt Storage in Bytes
2
TinyInt Storage in Bytes
1
money Storage in Bytes
8
smallmoney Storage in Bytes
4
Char and Varchar
String data types of either fixed length or variable length
Nchar and Nvarchar
UNICODE string data types of either fixed length or variable length
UNIQUEIDENTIFIER
a 16 byte GUID / UUID
NULL
Means literally “Nothing”, the absence of any value
WHERE field IS NULL | WHERE field IS NOT NULL
SELECT
The select statement is at the heart of most SQL queries. It defines what result set should be returned by the query, and is almost always used in conjunction with the FROM clause, which defines what part(s9 of the database should be queried.
- the wildcard character
It’s used to select all available columns in a table
table.*
It’s used to select all values from a specific table
- wildcard character problems
Excess IO network load, memory use, and so on.
Potencial problem if you add more columns in the tables involved
When to use * in production environment
When using EXISTS, such as select A.col1, A.col2 from A where exists (select * from B where A.ID = b.A_ID). In COUNT(*) can be used.
Aliases
Column aliases are used mainly to shorten code and make column names more readable.
Aliases can be applied to Columns and table as well
WHERE
the condition can be any SQL expression, specified using comparison or logical operator like >, , >=, <=, LIKE, NOT, IN, BETWEEN
CASE
When results need to have some logic applied ‘on the fly’ one can user CASE statement to implement it.
SELECT case when col1 < 50 then ‘under’ else ‘over’ end threshold from tablename
Selecting without Locking the table
Sometimes when tables are used mostly (or only) for reads, indexing does not help anymore and every little bit counts, one might use selects without LOCK to improve performance.
MySQL
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT * FROM TableName;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
AVG
The AVG() aggregate function will return the avergae of values selected