You are on page 1of 2

Primary Key

The primary key constraint uniquely identifies each record in a database table.
PKs must contain unique values and cannot contain null values.

Foreign Key
Is a key used to link two tables together.
Is a field in one table that refers to the primary key in another table.

Composite Key
Is a combination of two or more columns in a table that can be used to uniquely identify each
row in a table.

SQL
SQL is a standard language for storing, manipulating and retrieving data in databases.

SQL Joins
INNER JOIN Returns records that have matching values in both tables
LEFT JOIN Returns all records from the left table, and the matched records from the right
table
RIGHT JOIN Returns all records from the right table, and the matched records from the left
table
FULL JOIN Returns all records when there is a match in either left or right table
Index
An index is a data structure that improves the speed of data retrieval operations on a
database.

Tips for performance


If my query has joins
I check if those tables have index on their primary keys and foreign keys.
If the query returns lots of data, then I create index on filtered columns.
If there are aggregate functions, then I create index on aggregate columns.
If the query contains an order by, then I create index on ordered columns.

Other tips are:


Avoid correlated subqueries, because this kind of queries tend to run row by row.
Avoid select asterisk, instead, include the specific columns that the query needs.
Temporary tables usually increase the complexity in a query. If the code can be written in a
simple way, I suggest to avoid temp tables.
Use EXISTS for checking if a record exists, instead of COUNT. While COUNT scans the
entire table, counting up all entries matching your condition, EXISTS will exit as soon as it
sees the result it needs.

You might also like