CS Paper 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

Harvey James

Paper 1 03/12/2018
Started at: 9:08 End at 10:37

1.1

B conclusion 2 follows

1.2

C neither

3.a

Value for x Type of test data


25 Integer/Float
1 Integer/Float
-8 Complex
Using python code:

def sqrt(x):
return x**0.5
print(sqrt(-8))
print(type(sqrt(-8)))

3.1

There is no identifiable beginning of the graph, no beginning parent node.

3.2

Vertex
1 2, 3
2 1,4
3 5
4
5

3.3

Where you need to identify quickly which nodes are connected so you can identify the shortest
route to an certain node like with route finding algorithms for moving packets across networks.
Harvey James

3.4

NoCats A B C 1 2 3 4 5
5 2 1 1 1
2 1 2 1
2 2 2 1 2
3 1 1 1 2
3 1 2 1 2
3 2 2 1 2
3 1 3 1 2
3 2 3 1 2
3 3 3 1 2 3
4 1 1 1 2 3
4 2 1 1 2 3
4 3 1 1 2 3
4 4 1 1 2 3 1
Using python:

M = [[0,1,1,0,0],[1,0,1,1,0],[1,1,0,0,1],[0,1,0,0,0],[0,0,1,0,0]]
def cats():
Cat = [1,0,0,0,0]
NoOfCats = 5
for A in range(2, NoOfCats):
B = 1
C = 1
print("1, A:{}, B={}, C={}".format(A,B,C))
while B < A:
if M[A-1][B-1] == 1:
if Cat[B-1] == C:
B = 1
C+=1
else:
B+=1
else:
B+=1
print("2, A:{}, B={}, C={}".format(A,B,C))
Cat[A-1] = C
print("Cat={}".format(Cat))
print(Cat)
cats()
Harvey James

3.5

What order to get the cats to the show

3.6

Where the problem

3.7

4.1

False

4.2

4.3

4.4

O(n2)

4.5

O(log n) could be the time complexity for a binary search

4.6

O(1)

4.7

O(n)

4.8

Because you are going through the list one by one and if the item you are searching for is at the
very end of the list you will go through the entire list.

5.1

“Katherine Hepburn”

5.2

1052 MOD 100 = 52

5.3

The values are stored as hashes of a value using a key. You can only turn the hashes back into
the true values by using the inverse of the key.

5.4
Harvey James

Hash values are added or subtracted to until they fit a free space

5.5

{“client_name”:catalogue_number}

5.1

3*4=12

5.2

12+8*4

5.3

Takes up less space

6.1
Harvey James

Section B
6 Decimal to binary

6.1

Task 1 (reverse order):

Task 2:

6.2

Task 1(reverse order):

Task 2/3:
Harvey James

7.1

In character subclass MakeMove(direction) is not a variable when is should be

4.1

Harvard architecture is Architecture A

4.2

Harvard architecture is used in specific and special computers used for processing large amounts
of data because it is faster for the computer to find the instructions and data when they are
separate rather than in the same place

7.1

Not organised

Not all held in one area or server

7.2

Because multiple functions can be called at a time which allows you to process more at once.

7.3 (in flash)

3.a different types of data /


Harvey James

3.1 trees, graphs, edges, relationships /

3.2 adjacency lists /

3.3 adjacency matrix /

3.5 intractable problems /

3.7 What is the approach taken for intractable problem? ???

4.1 Binary search /

4.4 time complexity of an intractable problem ???

4.5 time complexity’s /

5.2 Hash tables /

5.4 rehashing /

5.5 setting up a dictionary /

5.1 reverse polish notation / don’t understand fully

5.2 infix notation /

5.3 reverse polish notation advantages and disadvantages with infix notation Find

Sec B

7.1 class diagrams and functional programming Sec 12 Ch67 and 68

4.1 Harvard and Von Neumann architecture Ch25

7.1 Big data Ch72

7.2 Parallel, distributed computer architectures Sec 5

Paper 2

SQL databases modifying and more /

Fixed point binary more Appendix A

Results:
Harvey James

38 out of 70?
3.a

3.1 A tree cannot contain cycles

3.2 can do twice if no arrows

3.3 When there aren’t that many edges, or the adjacency matrix is sparse.

3.6 Difficult to solve, not nice solution

3.7 Trial and error, Brute Force, Gut instinct, Best fit

4.2 Failed=True

4.3 L=M

4.4 Intractable problem: O()

5.5 By making the value be a data structure

5.2 (use brackets)

5.3 A hashing algorithm is applied to the key field of each record to transform it into an address.
These are stored in an array. Where there are collisions, or synonyms, the address will be moved
up one space to an empty place in the array if there is any.

5b.2 (12+8) * 4

5b.3 Reverse polish notation is easier for a computer to evaluate or code the algorithm

7.1 Item class is the subclass of Trap, Monster and Character. Should be the other way around

4.2 Harvard architecture would be used in navigation systems because it is faster than von
Neumann architecture. This is important as it is used in real time

7.1 The velocity at which it is generated and received and the variety of data

7.2 Can process and run data without changing it meaning it has no side effects

Map or reduce operations can be easily paralysed. One function does not affect another function

7b.3
Harvey James

Revision
Types of data:
An integer is any whole number

A natural number is a whole number used in counting, (0,1,2,3,4,5)

A rational number is any value that can be expressed as a ratio


Harvey James

A irrational number such as pi is one that cannot be expressed as a fraction and which has an
endless series of non-repeating digits.

Trees
A tree is an abstract data type that simulates a hierarchical tree structure. It has a root node at the
top which has subtrees of children each with a parent node, to represent a series of linked nodes.
The uses for trees include manipulating hierarchical data, such as system directories or folder
structures, Making information easy to search or manipulating sorted lists of data.

Graph without cycles as not possible to find a path in the tree which returns to the start node
without traversing an edge twice.

A tree with a root node is defined as a rooted tree, terms:

Nodes contain the tree data

Edge connects two nodes; every node except the root is connected by exactly one edge from
another node in the level above it.

Root is the node that has no incoming edges and starts at the top of the tree

Child is the set of nodes that have incoming edges from the same node

A node is a parent of all the nodes it connects to with outgoing edges

A subtree is the set of nodes and edges comprised of a parent and all descendants of the parent.

A leaf node is a node that has no children

A rooted tree is a special case of a connected graph, a node can only be connected to one parent
node and to its children. It is described as having no cycles because there can be no connection
between children or between branches.

A tree is a connected, undirected graph with no cycles. “Connected” implies that it is always
possible to find a path from a node to any other node, by backtracking if necessary. “No cycles”
means that it is not possible to find a path in the tree which returns to the start node without
traversing an edge twice. A tree does not have to have a root.

A binary tree is a rooted tree in which each node has a maximum of two children. A binary search
tree holds items in such a way that the tree can be searched quickly and easily for a particular
item, new items can be easily added and the whole tree can be printed out in sequence. A binary
search tree is a typical use of a rooted tree.

There are three ways of traversing a tree: Pre-order traversal, In-order traversal and Post-order
traversal.

A pre-order traversal can be used to produce prefix notation, used in functional programming
languages
Harvey James

Post order traversal is used in program compilation to produce reverse polish notation algorithms
for implementing a binary tree.

A binary search tree can be implemented using an array of records, with each node consisting of a
left pointer, data item, right pointer.

What is a tree?

A tree is a connected, undirected graph with no cycles, in that it is not possible to find a path in the
tree which returns to the start node without traversing an edge twice.

Graphs
A graph is a set of vertices or nodes connected by edges or arcs. The edges can be one-way to
two-way. In an undirected graph, all edges are bidirectional, data can move both ways, If the
edges are all one-way, the graph is said to be a directed graph or digraph.

Edges may be weighted to show there is a cost to go from one vertex to another. The weights can
represent the distances between towns or objects for example.

Two possible implementation of a graph are the adjacency matrix and the adjacency list

The adjacency matrix is a square matrix used to represent a finite graph. It can be seen as a two-
dimensional array which can store information about a directed or undirected graph. Each of the
rows and columns represents a node and a value stored in the cell at the intersection of a row and
column indicates that there is an edge connecting the two nodes. Example:

A B C D E F
A 5 4
B 6 3
C 8
D 2
E
F
With this matrix we can see that A and B have an edge connecting them with a value of 5.

The main advantage of an adjacency matrix is that it is very convenient to work with, as adding an
edge or testing for the presence of an edge is very simple and quick. However a sparse/scattered
graph with many nodes and not many edges will leave most of the cells empty-wasting memory.
The larger the graph the more memory space will be wasted. And with a static two-dimensional
array it is harder to add or delete nodes.

The adjacency list is a collection of unordered lists used to represent a finite graph. It is a more
space-efficient way to implement a sparsely connected graph. A list of all the nodes are created
and each node points to a list of all the adjacent nodes to which it is directly linked. The adjacency
list can be implemented as a list of dictionaries, with the key in each dictionary being the node and
the value, the edge weight.

The main advantage of an adjacency list is that is uses much less memory to represent a sparsely
connected graph.

Applications:
Harvey James

Graphs may be used to represent computer networks-with edges between computers representing
bandwidth, Roads between towns-edges representing distance, Tasks in a project, States in a
finite state machine, Webpages and links

PageRank is an algorithm used to rank websites in search engine results by counting the number
and quality of links to the website, which determines how important the website is.

Intractable problems
An intractable problem is one that is difficult to manage or solve. This problem is regarded as
inherently difficult if its solution requires significant resources.

In computing a NP-complete problem, is an computational problem for which there is no efficient


solution algorithm identified. There are many of these problems such as the traveling salesman
problem or graph-covering problems.

Tractable problems can be solved by computer algorithms that run in polynomial time, O(n)

Intractable problems require times that are exponential functions of the problem size n. Polynomial
time algorithms are considered efficient, and exponential algorithms are considered inefficient.

A problem is NP if its solution can be guessed and verified in polynomial time. Finding an efficient
algorithm for any NP problem implies that an efficient algorithm can be found for all such
problems.

Binary Tree
A binary tree is a rooted tree in which each node has a maximum of two children. A binary search
tree holds items in such a way that the tree can be searched quickly and easily for a particular
item, makes it easy for new items to be added, and allows the whole tree to be printed out in
sequence. It is a typical use of a rooted tree.

For a list you place the first item at the root. Then for each item in the list, visit the root, which
becomes the current node, and branch left is the item is less than the value at the current node,
and right if the item is greater than the value at the current node. Continue down the branch,
applying the rule at each node visited until a leaf node is reached. The item is them placed to the
left or to the right of this node, depending on whether it is less than or greater than the value at the
node. Can be traversed in pre-order, in-order and post-order traversal.

Binary Search
The binary search is a much more efficient method of searching a list for an item than a linear
search, but crucially, the items in the list must be sorted. If they are not sorted, a linear search is
the only option.

Items to be searched are held in a ordered array. Tis array is divided up into three parts, a middle
item, the first part starting at List[0] up to the middle item and the second part after the middle item
Harvey James

and ending with the final item in the list. The middle item is examined to see if it is equal to the
sought item.

If it is not then it must be greater, in which the second half of the array is not important, or less
than in which the first half of the array is not important. The number of items being searched is
halved and process repeats until the last item is examined.

The binary search halves the search are with each execution of the loop-an example of a divide
and conquer strategy. The binary search is O(log n)

The basic concept of the binary search is recursive. The procedure calls itself, eventually
“unwinding” when the procedure ends. Must be a condition that if true causes the program to
terminate the recursive procedure or it will loop forever.

The recursive algorithm for searching a binary tree is similar to the binary search algorithm, except
that instead of looking at the midpoint of a list, or a subset of the list, on each pass, half of the tree
or subtree is eliminated each time its root is examined. O(log n)

Time complexities
O(1)-constant time

O(n)-linear time

O(n2)-polynomial time

O(2n)-Exponential time

O(log n)-logarithmic time

O(n!)-factorial time

Hash tables (ch40)


Accessing a large database needs to be done quickly, without looking through all records. This
can be done by holding an index of the physical address on the file where the data is held.

A hashing algorithm can be applied to the value in the key field of each record to transform it into
an address. Normally there are many more possible keys than actual records that need to be
stored. One common hashing algorithm is to divide the key by the number of available addresses
Harvey James

and take the remainder as the address. For example the algorithm “address = key mod 1000”,
where there are 1000 free spaces allocated to store the records.

When the addresses 631552 and 134552 are put through the algorithm they have the same
address 52. This is called a synonym and they are bound to occur with any hashing algorithm.
Two record keys hashing to the same address is referred to as a collision. A simple way to deal
with collisions is to store the item in the next available free space.

A hash table is a collection of items stored in such a way that they can be quickly located. The
hash table could be implemented as an array or list of given size with a number of empty spaces.

When searching for an item you first apply the hashing algorithm, examine the resulting cell in the
list, if item is there return item, if cell is empty, item is not in table, if another item in that spot keep
moving forward until either the item is found or a blank cell is encountered, when it is apparent the
item is not in the table.

To be as efficient as possible the hashing algorithm needs to be chosen so that it generates the
least number of collisions. This can depend to some extent on the distribution of the items to be
hashed.

There are many other algorithms, the folding method divides the item into equal parts, and adds
up the parts to give the hash value.

A hash function can be created for alphanumeric strings by using the ASCII code for each
character. This is hashing a string

The fuller the hash table becomes the more likely it is that there will be collisions. Needs to be
taken into account when designing the hashing algorithm and deciding on the table size.

Rehashing is the process of finding an empty slot when a collision has occurred. The rehashing
algorithm used above simply looks for the next available spot.

Hash tables are primarily used for efficient lookup, so that for example an index would typically be
organised as a hash table. A hash table could be used to look up a person’s telephone number
given their name. They can be used to store codes and encrypted passwords.

Hash table are an implementation of the data structure called a dictionary.

A dictionary is an abstract data type consisting of associated pairs of items, where each pair
consists of a key and a value. When they key is given a value is returned. Items can be easily
amended, added or removed as required.

Reverse polish notation


Reverse polish notation is a method of writing arithmetic expressions that is particularly suited to
computerised methods of evaluation. Advantages include: it eliminates the need for brackets in
sub-expressions, it produces expressions in a form suitable for evaluation using a stack, it is used
in interpreters based on a stack, for example: postscript and bytecode.
Harvey James

Infix notation is the way we normally write arithmetic expressions, however it is not easy for a
computer to evaluate such an expression directly: (a+b)*c

The computer wants the operands and the operators in the sequence: a b + c *

In reverse polish notation the operator follows the operands-which is the logical sequence for a
computer.

In order to translate from infix to reverse polish notation we need to define the order of precedence
of operators. Shown in increasing order to precedence:

+=)

*/

^ (exponentiation)

~ (unary minus, -3 + 2)

Infix: (6*7)+4 the operator is written between the operands

Postfix: 6 7 * 4 + the operator follows the operands

A computer will use a fairly complex algorithm using a stack to translate from infix to reverse polish
notation. However it is quite simple to do it manually with knowledge of the rules of precedence
and a few simple rules.

Starting from the left hand side of the expression, allocated numbers to operands and operators as
follows: If the next symbol is an operand, allocate the next number to it. If it is an operator move to
the next operand. Ignore parentheses except in so far as they affect the order of calculation. Using
the rules of precedence, decide which is the next operation that should be performed, and as soon
as its operands have been allocated numbers, back up and allocate it the next number. E.g:

A+B*C

1 5 2 4 3

A + B * C

Working from the left, allocate 1 to a, 2 to b. Multiplication is done before addition, so keep going
and allocate 3 to c. Then back up and allocate 4 to * and finally 5 to +

Write down the tokens in the order of the numbers you have allocated: a b c * +

Once a compiler has translated an arithmetic expression into reverse polish notation, each symbol
in the expression may be held in a string or array. The expression may then be evaluated using a
stack, scanning the elements of the string from left to right as follows: If the next token is an
operand place it on the stack, if the next token is an operator, remove the required number of
operands from the stack, perform the operation, and put the result back on the stack.
Harvey James

Section 12 OOP and Functional programming


Programming languages have been evolving ever since the development of assembly languages.
High level languages such a Basic and Pascal are known as procedural languages, and a
program written in one of these languages is written using a series of step by step instructions on
how to solve the problem. This is usually broken down into a number of smaller modules, and the
Harvey James

program then consists of a series of calls to procedures or functions, each of which may in turn
call other procedures or functions.

In this method of programming, the world is viewed as a collection of objects, An object might be a
person, animal, place, concept or event, for example. It could be something more abstract like a
bank account or a data structure as a stack or queue that the programmer wished to implement.
An object-oriented program is composed of a number of interacting objects, each of which is
responsible for its own data the operations on that data. Program code in an object-oriented
program creates the objects and allows the objects to communicate with each other by sending
messages and receiving answers. All the processing that is carried out in the program is done by
objects.

Each object will have its own attributes. The attributes of a car might include its make, engine size,
colour, ext. The attributes of a person could include first name, last name and date of birth. An
object has a state. A radio for example may be on or off, turned to a particular station, set to a
certain volume. A bank account may have a particular balance, like £54.20. An object has
behaviours. These are the actions that can be performed by an object; for example, a cat can
walk, pounce, catch mice, purr, and meow and so on.

A class is a blueprint or template for a object, and it defines the attributes and behaviours of
objects in that class. An attribute is data that is associated with the class, and a method is a
functionality of the class – something that it can do or that can be done with it.

For example, a stock control system might be used by a bookshop for recording the items that it
receives into stock from suppliers and sells to customers. The only information that the stock class
will hold in this simplified system is the stock ID number, stock category, description, and quantity
in stock. Part of a sample definition of a class named StockItem is defined below. Program coding
will vary

As a general ruel, instance variables or attributes are declared private and most methods public,
so that other classes may use methods belonging to another class but may not see or change
their attributes. This principle of information hiding where other classes cannot directly access the
attributes of another class when they are declared private, is an important feature of object-orient
programming. A constructor is used to create objects in a class. In the above example the
constructor is called the StockItem; in many programming languages the constructor must have
the same name as the class.

Once the class and its constructor have been defined, and each of the methods coded we can
start creating and naming actual objects. The creation of a new object is know as instantiation
Multiple instance of a class can be created which each share identical methods and attributes, but
the values of those attributes will be unique to each instance. This means that multiple enemy
objects can be created in a computer game by programming just one zombie class, with health,
position and speed attributes; but each individual zombie could operate independently with
different attribute values. Suppose we want to create a new stock item called book1 The type of
variable to assign to book1 has to be stated. This will be the class name, StockItem. The word
new is typically used like in java, to instantiate a new object in the class.
Harvey James

Book1 is called a reference type variable or simply a reference variable. Reference variable are
named memory locations in which you can store information. However a reference variable does
not hold the object-it hold a pointer or reference to where the object itself is stored.

A variable reference diagram shows in graphic form the new stockitem object referenced by the
variable book1. In the diagram reference variable are shown as circles and primitive data types
are shown as rectangles.

Messages can be categorised as either “getter” or “setter” messages. In some languages “getter”
messages are written as a function which return an answer and setter messages as procedures
which change the state of an object.

An object encapsulates both its state and its behaviours or methods. All the data and methods of
each object are wrapped up into a single entity so that the attributes and behaviours of one object
cannot affect the way in which another object functions.

Encapsulation is a fundamental principle of object-oriented programming and is very powerful. It


means for example that in a large project different programmers can work on different classes and
not have to worry about how the other parts of the system may affect any code they write. They
can also use methods from any other classes without having to know how they work.

Information hiding is where the details of an objects instance variables are hidden so that other
objects must use messages to interact with that objects state.

Classes can inherit data and behaviour from a parent class in much the same way that children
can inherit characteristics from their parents. A “child” class in object oriented programing is
referred to as a subclass, and a parent as a superclass.

Inheritance diagram:
Harvey James

SQL Querying Databases


--DISTINCT returns distinct results
--SELECT DISTINCT column1, column2, ... FROM table_name;
Harvey James

--single line comments


/* multi
line
comments*/

--ORDER BY allows you to sort records, sorts records in ascending order


by default, use DESC to sort in descending order.
ORDER BY column1, column2, ...

--INSERT INTO statement is used to insert new records in a table.


--Two ways:
--First way specifies both column names and values to be inserted
INSERT INTO table_name (column1, column2, …)
VALUES (value1, value2, ...);
--Second way does not however make sure that the order of the values is
the same order as the columns in the table.
INSERT INTO table_name
VALUES (value1, value2, ...);
--It is also possible to only insert data in specific columns
--Following SQL statement will insert a new record but only in specific
columns.
INSERT INTO Customers (CustomerName, City, Country)
VALUES (‘Cardinal’, ‘Stavanger’, ‘Norway’);
--any columns with no values will have a default value of NULL unless It
is specified that these columns cannot be NULL

--A field with a NULL value is a field with no value


--If a field in a table is optional, it is possible to insert a new
record or update a record without adding a value to this field. Then the
field will be saved with a NULL value
Use IS NULL and NOT NULL operators to test for NUL values
SELECT column_names FROM table_name WHERE column_name IS NULL;
SELECT column_names FROM table_name WHERE column_name IS NOT NULL;

--The UPDATE statement is used to modify the existing records in a table


UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;
--The WHERE clause determines how many records will be updated
--If you omit the WHERE clause, all records will be updated
UPDATE Customers SET ContactName = ‘Jaun’;
--All contact names will have the value of ‘Jaun’

--The DELETE statement is used to delete existing records in a table


DELETE FROM table_name WHERE condition;
--Can delete rows in a table without deleting the table
--table structure, attributes and indexes will be intact
DELETE FROM table_name;

--MySQL uses LIMIT to limit the amount of records returned


SELECT column_names FROM table_name WHERE condition LIMIT number;
--MIN() function returns smallest value of selected column
SELECT MIN(column_name) FROM table_name WHERE condition;
--MAX() function returns largest value of selected column
Harvey James

SELECT MAX(column_name) FROM table_name WHERE condition;


--COUNT() function returns the number of rows that matches a criteria
SELECT COUNT(column_name) FROM table_name WHERE condition;
--AVG() function returns the average value of a numeric column
SELECT AVG(column_name) FROM table_name WHERE condition;
--SUM() function returns the total sum of a numeric column
SELECT SUM(column_name) FROM table_name WHERE condition;

--LIKE operator is used in a WHERE clause to search for a specified


pattern in a column
-- % represents zero, one or multiple characters
-- _ represents a single character
SELECT column1, column2,... FROM table_name WHERE columnN LIKE pattern;
‘a%’ Finds any values that start with “a”
‘%a’ Ends with “a”
‘%or%’ has “or” in any position
‘_r%’ have “r” in second position
‘a_%_%’ starts with “a” and there are at least 3 characters in length
‘a%o’ start with “a” ends with “o”
‘[abc]%’ start with either ‘a’ or ‘b’ or ‘c’
‘[!abc]%’ does not start with either ‘a’ or ‘b’ or ‘c’

A wildcard character is used to substitute any other characters in a


string. Used with the LIKE operator, which is used in a WHERE clause to
search for a specified pattern in a column.

--The IN operator allows you to specify multiple values in a WHERE


clause, it is a shorthand for multiple OR conditions
SELECT column_names FROM table_name WHERE column_name
IN (value1, value2, ...);
--or:
SELECT column_names FROM table_name WHERE column_name
IN (SELECT STATEMENT);

--The BETWEEN operator selects values within a given range. The values
can be numbers, text, or dates. It is inclusive, begin and end values
are included
SELECT column_names FROM table_name WHERE column_name BETWEEN value1 AND
value2;
--Can use NOT aswel:
SELECT column_names FROM table_name WHERE column_name NOT BETWEEN value1
AND value2;
--can be done between numbers,strings,letters,dates

--SQL aliases are used to give a table, or a column in a table, a


temporary name. They are often used to make column or table names more
readable. They only exist for the duration of the query
SELECT column_name AS alias_name FROM table_name;
SELECT column_name FROM table_name AS alias_name;
--A JOIN clause is used to combine rows from one or more tables, based
on a related column between them.
--(INNER) JOIN returns records that have matching values in both tables
--LEFT (OUTER) JOIN returns all records from left table and matched
records from the right
Harvey James

--RIGHT (OUTER) JOIN returns all records from right table and matched
records from the left
--FULL (OUTER) JOIN returns all records when there is a match in either
left or right table

SELECT column_names FROM table1 JOIN table2 ON table1.column =


table2.column;
--can join multiple tables at once

--INNER JOIN keyword selects records that have matching values in both
table
SELECT column_names FROM table1 INNER JOIN table2 ON table1.column_name
= table2.column_name;

--LEFT JOIN keyword returns all records from the left table, and matched
records from the right table. The result is NULL from the right side if
there is no match.
SELECT column_names FROM table1 LEFT JOIN table2 ON table1.column_name =
table2.column_name;

--RIGHT JOIN keyword returns all records from the right table and the
matched records from the left table. The result is NULL from the left
side, when there is no match.
SELECT column_names FROM table1 RIGHT JOIN table2 ON table1.column_name
= table2.column_name;

--FULL OUTER JOIN keyword returns all records when there is a match in
either left or right table records.
SELECT column_names FROM table1 FULL OUTER JOIN table2 ON
table1.column_name = table2.column_name;

--A self JOIN is a regular join, but the table is joined with itself.
SELECT column_names FROM table1 T1, table1 T2 WHERE condition;

--The UNION operator is used to combine the result-set of two or more


SELECT statements. Each SELECT statement within UNION must have the same
number of columns, the columns must also have similar data types and the
columns in each must also be in the same order
SELECT column_names FROM table1
Harvey James

UNION
SELECT column_names FROM table2;
--The UNION operator selects only distinct values by default. To allow
duplicate values, use UNION ALL
SELECT column_names FROM table1
UNION ALL
SELECT column_names FROM table2;

--The GROUP BY statement is often used with aggregate functions to group


the result-set by one or more columns.
SELECT column_names FROM table_name WHERE condition
GROUP BY column_names
ORDER BY column_names;

--The HAVING clause was added to SQL because the WHERE keyword could not
be used with aggregate functions.
SELECT column_names FROM table_name WHERE condition GROUP BY
column_names HAVING condition ORDER BY column_name;

--The EXISTS operator is used to test for the existence of any record in
a subquery, It returns true if one or more records are returned.
SELECT column_names FROM table_name WHERE EXISTS (SELECT column_name
FROM table_name WHERE condition);

--The ANY and ALL operators are used with a WHERE or HAVING clause
--The ANY operator returns true if any of the subquery values meet the
condition
SELECT column_names FROM table_name WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE condition);
--The ALL operator returns true if all of the subquery values meet the
condition
SELECT column_names FROM table_name WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);

--The SELECT INTO statement copies data from one table into a new table.
--Copy all columns into a new table:
SELECT * INTO newtable [IN externaldb] FROM oldtable WHERE condition;
--Copy some columns into a new table:
SELECT column1, column2, ... INTO newtable [IN externaldb] FROM oldtable
WHERE condition;

--The INSERT INTO SELECT statement copies data from one table and
inserts it into another table. It requires that data types in source and
target tables match. The existing records in the target table are
unaffected.
--Copy all columns from one table to another table:
INSERT INTO table2 SELECT * FROM table1 WHERE condition;
Harvey James

--Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, ...) SELECT column1, column2, ...
FROM table1 WHERE condition;

--The CASE statement goes through conditions and returns a value when
the first condition is met. So, once a condition is true is will stop
reading and return the result. If no conditions are true, it returns the
value in the ELSE clause. If there is no ELSE part, and no conditions
are true, it returns NULL.
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN result
ELSE result
END;

--The MySQL IFNULL() function lets you return an alternative value if an


expression is NULL:
SELECT IFNULL(value_to_check, default_value) FROM table_name;
--We can also use the COALESCE() function:
SELECT COALESCE(value_to_check, default_value) FROM table_name;

--A stored procedure is a prepared SQL code that you can save, so the
code can be reused over and over again. So if you have an SQL query that
you write over and over again, save it as a stored procedure and then
call it to execute it. You can pass parameters to a stored procedure, so
that the stored procedure can act based on the parameter values that is
passed.
--Creates a stored procedure: (parameter is optional)
-CREATE PROCEDURE procedure_name @parameter_name data_type
AS sql_statement
GO;
--Executes a stored procedure:
EXEC procedure_name;
--Example:
CREATE PROCEDURE SelectALLCustomers @City nvarchar(30)
AS SELECT * FROM Customers WHERE City = @City
GO;

SQL databases
--Data types:
/*
CHAR(size) fixed length string, 255 characters max
VARCHAR(size) variable length string, 255 characters max
Harvey James

TINYTEXT string with max length 255 characters


TEXT String with max length 65,535 characters
BLOB for Binary Large Objects, 65,535 bytes of data
MEDIUMTEXT string with max length 16,777,215
MEDIUMBLOB for BLOBs, 16,777,215 bytes of data
LONGTEXT string with max length 4,294,967,295
LONGBLOB for BLOBs, 4,294,967,295 bytes of data
ENUM(x,y,z,…) can enter list of possible values, 65535 list size
Values stored in order you enter
SET Can store up to 64 items and more than one choice

TINYINT(size) 1 byte -128 to 127 signed, 0 to 255 UNSIGNED


SMALLINT(size) 2 bytes -32768 to 32767 signed, 0 to 65535 UNSIGNED
MEDIUMINT(size)3 bytes 0 to 224-1 UNSIGNED
INT(size) 4 bytes 0 to 232-1 UNSIGNED
BIGINT(size) 5 bytes 0 to 240-1 UNSIGNED
FLOAT(size,d) small number with a floating decimal point
d = max number of digits to right of the decimal point
DOUBLE(size,d) large number with a floating decimal point
DECIMAL(size,d)DOUBLE stored as a string, allows for fixed decimal point

DATE() A date. FORMAT:YYYY-MM-DD


DATATIME() A data and time combination. YYYY-MM-DD HH:MI:SS
TIMESTAMP() Values are stored as number of seconds since Unix epoch
(‘1970-01-01 00:00:00’ UTC) YYYY-MM-DD HH:MI:SS
TIME() HH:MI:SS
YEAR() A year in two-digit or four-digit format
1901 to 2155 or 70 to 69 representing 1970 to 2069
*/

--The CREATE DATABASE statement is used to create a new SQL database.


CREATE DATABASE databasename;

--The DROP DATABASE statement is used to drop an existing SQL database.


DROP DATABASE databasename;
Harvey James

--The CREATE TABLE statement is used to create a new table in a database


CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
--The column parameters specify the names of the columns of the table.
--The datatype parameter specifies the type of data the column can hold
--A copy of an existing table can also be created using CREATE TABLE
--The new table gets the same column definitions. All columns or
specific columns can be selected
--If you create a new table using an existing table, the new table will
be filled with the existing values from the old table.
CREATE TABLE new_table_name AS
SELECT column1, column2, ...
FROM existing_table_name
WHERE ....;

--DROP TABLE statement is used to drop an existing table in a database


DROP TABLE table_name;

--TRUNCATE TABLE statement is used to delete the data inside a table,


but not the table itself.
TRUNCATE TABLE table_name;

--ALTER TABLE statement is used to add, delete, or modify columns in an


existing table and can also be used to add and drop various constraints
on an existing table.
--To add a column:
ALTER TABLE table_name
ADD column_name datatype;
--To drop a column:
ALTER TABLE table_name
DROP COLUMN column_name;
--To change the data type of a column in a table:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

--SQL constraints are used to specify rules for data in a table


--Constraints can be specified when the table is created with the CREATE
TABLE or after the table has been created with the ALTER TABLE
statement.
CREATE TABLE table_name (
Column1 datatype constraint,
Harvey James

Column2 datatype constraint,


Column3 datatype constraint,
....
);

/*
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table.
This ensures the accuracy and reliability of the data in the table. If
there is any violation between the constraint and the data action, the
action is aborted. Constraints can be column level or table level.
Column level constraints apply to a column, and table level constraints
apply to the whole table.
Following constraints are commonly used:

NOT NULL ensures that the column cannot be NULL


UNIQUE ensures that all values in a column are different
PRIMARY KEY a combination of NOT NULL and UNIQUE, unique id each row
FOREIGN KEY uniquely identifies a row/record in another table
CHECK ensures that all values in a column satisfies a condition
DEFAULT sets a default value when no value specified
INDEX used to create and retrieve data very quickly

Can have many UNIQUE constraints per table but only one PRIMARY KEY
PRIMARY KEY may consists of single or multiple fields, composite key
Table containing the foreign key is called the child table, and the
table containing the candidate key is called the referenced or parent
table. FOREIGN KEY is used to link two table together. The FOREIGN KEY
constraint is used to prevent actions that would destroy links between
tables. The FOREIGN KEY constraint also prevents invalid data from being
inserted into the foreign key column, because it has to be one of the
value contained in the table it points to. CHECK constraint used to
limit value range that can be in a column.
*/
--MySQL:
CREATE TABLE Persons (
....
PersonID PRIMARY KEY (ID,LastName) --composite primary key
Age int CHECK (Age>=18) DEFAULT 18
);
--For alter:
ALTER TABLE Persons
ADD PRIMARY KEY (ID);
--Foreign key
CREATE TABLE Orders (
....
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
--To create an index
CREATE INDEX index_name ON table_name (col1, col2, ...);
--To create an unique index
CREATE UNIQUE INDEX index_name ON table_name (col1, col2, ...);
Harvey James

--Auto-increment allows a unique number to be generated automatically


when a new record is inserted into a table. Often this is the primary
key field that we would like to be created automatically every time a
new record is inserted. Default start value is 1.
CREATE TABLE Persons (
ID int NOT NULL AUTO_INCREMENT=100; --will start at 100
);

/*
As long as your data contains only the date portion, your queries will
work as expected. However, if a time portion is involved, it gets more
complicated.
MySQL has the following data types for dates and times:
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MI:SS
TIMESTAMP YYYY-MM-DD HH:MI:SS
YEAR YYYY or YY

A view is a virtual table based on the result-set of an SQL statement.


A view contains rows and columns, just like a real table. The fields in
a view are fields from one or more real tables in the database. Views
always show up-to-date data. The database engine recreates the data,
using the view’s SQL statement, every time the user queries a view.
*/
CREATE VIEW view_name AS SELECT col1, col2, ...
FROM table_name WHERE condition;
--A view can be updated using CREATE OR REPLACE VIEW command:
CREATE OR REPLACE VIEW view-name AS SELECT col1, col2, ...
FROM table_name WHERE condition;
--View can also be dropped
DROP VIEW view_name;

/*
SQL injection is a code injection technique that has the potential to
destroy your database. It is one of the most common web hacking
techniques. SQL injection is the placement of malicious code in SQL
statements via web page input.
Harvey James

SQL injection usually occurs when you ask a user for input, like their
uername/userid, and instead of a name/id, the user gives you an SQL
statement that you will unknowingly run on your database.
The following example creates a SELECT statement by adding a variable to
a select string. The variable is fetched from user input:
txtUserId = getRequestString(“UserId”);
txtSQL = “SELECT * FROM Users WHERE UserId = “ + txtUserId;

The original purpose of the code above is to create an SQL statement to


select a user, with a given user id. If there is nothing to prevent a
user from entering “wrong” input, the user can enter “smart” input like:
‘105 OR 1=1’
Then the SQL statement will look like this:
SELECT * FROM Users WHERE UserId = 105 or 1=1;
This statement is valid and will return ALL rows from the “Users” table,
since ‘OR 1=1’ is always TRUE
If the users table contains names and passwords then the statement would
be like:
SELECT UserId, Name, Password FROM Users WHERE UserId = 105 or 1=1;
A hacker can get access to all the user names and passwords in a
database.

Example of a user login on a web site:


uName = getRequestString("username");
uPass = getRequestString("userpassword");
sql = 'SELECT * FROM Users WHERE Name ="' + uName + '" AND Pass ="' +
uPass + '"'
A hacker might get access to user names and passwords in a database by
simply inserting “ OR “”=” into the user name or password text box.
The code at the server could make a valid statement like:
SELECT * FROM Users WHERE Name=”” or “”=”” AND Pass=”” or “”=””
Returns all rows as OR “”=”” is always true

Most databases support batched SQL statement.


A batch of SQL statements is a group of two or more SQL statements,
separated by semicolons.
An example of an userID entry on a website:
txtUserId = getRequestString("UserId");
txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;
With the input as:
105; DROP TABLE Suppliers
Would make a valid statement that can remove a part of your database:
SELECT * FROM Users WHERE UserId = 105; DROP TABLE Suppliers;

To protect a web site from SQL injection, you can use SQL parameters.
SQL parameters are values that are added to an SQL query at execution
time, in a controlled manner. Parameters are represented in the SQL
statement by a @ marker. The SQL engine checks each parameter to ensure
that it is correct for its column and are treated literally, and not as
part of the SQL to be executed.
Harvey James

If you want your web site to be able to store and retrieve data from a
database, your web server should have access to a database-system that
uses the SQL language. You may need to look for SQL hosting plans with
your ISP. The most common SQL hosting databases are MS SQL Server,
Oracle, MySQL, and MS Access.
MS SQL Server is popular for websites with high traffic, very powerful,
robust and full featured SQL database system.
Oracle is a popular database software for websites with high traffic,
powerful, robust, full featured SQL database system, but is run by
assholes who only want your money.
MySQL is a popular choice for web sites, powerful, robust and full
featured SQL database system. Inexpensive alternative to expensive
Mirosoft and Oracle solutions
Access is great for simple databases, not suited for high-traffic, and
not as powerful as the rest.

Bitwise operators:
& AND
| OR
^ exclusive OR

Compound operators
+= Add equals
-= Subtract equals
*= Multiple equals
/= Divide equals
%= Modulo equals
&= AND equals
^-= exclusive equals
|*= OR equals

See https://www.w3schools.com/sql/sql_ref_mysql.asp for full list of


functions
*/

Inter-dimensional programming, quantum-bitwise operations and glitch-matrix computing


Harvey James

You might also like