Skip to content

Commit d2a1e3c

Browse files
committed
Added latest databse lectures
1 parent cc3fc2d commit d2a1e3c

File tree

5 files changed

+321
-2
lines changed

5 files changed

+321
-2
lines changed

_data/CS258.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ Notes:
1010
order:
1111
- Database_Basics.html
1212
- Relational_Data_Model.html
13-
- Relational_Database_Constraints.html
13+
- Relational_Database_Constraints.html
14+
- Structured_Query_Language.html
15+
- SQL_Creating_A_Schema.html
16+
- SQL_Retrieving_Data.html

modules/CS255/Joes_Notes/Informed_Search.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ For any path $$P''$$ that extends $$P'$$
7575
So $$cost(P) \leq cost(P'')$$ for any path $$P''$$ to a goal
7676

7777
### Solution
78-
>Theorem: A* will always find a solution fi there is one
78+
>Theorem: A* will always find a solution if there is one
7979
8080
The frontier always contains the initial part of a path to a goal
8181

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
2+
> CS258 and this guide is based on PostgreSQL other versions may be different
3+
4+
> This page shows limited select commands
5+
6+
> This page provides links in the titles to the PostgreSQL documentation It is recommenced you visit for a deeper understanding of the commands
7+
8+
## Create
9+
10+
### [CREATE SCHEMA](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1722)
11+
12+
<pre>CREATE SCHEMA <i>schema_name</i> [AUTHORIZATION <i>user_name</i>]</pre>
13+
14+
enters a new schema into the database, a schema contains tables if no schema is specified the public schema is used.
15+
16+
tables within a schema can be referenced with the dot operator
17+
18+
CREATE TABLE _schema.table_ ....
19+
20+
if no schema is specified then `public` is used
21+
22+
<pre>
23+
CREATE TABLE <i>table_name</i> (...) == CREATE TABLE <i>public.table_name</i> (...)
24+
</pre>
25+
26+
**catalogue** named collection of schemas
27+
28+
### [CREATE TABLE](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1738)
29+
30+
<pre>
31+
CREATE TABLE <i>table_name</i> (
32+
<i>column_name</i> <i>data_type</i> <i>constraints</i>
33+
<i>column_name</i> <i>data_type</i> <i>constraints</i>
34+
<i>column_name</i> <i>data_type</i> <i>constraints</i>
35+
...
36+
)
37+
</pre>
38+
39+
#### [CREATE VIEW](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1792)
40+
41+
Can be used to create a virtual table that is the result of a query , the view is virtual and is run on every request
42+
43+
## [Constraints](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=98)
44+
45+
### [NOT NULL](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=101)
46+
47+
Specifies that data in a particular column must not be null
48+
49+
```
50+
CREATE TABLE products(
51+
name text NOT NULL
52+
)
53+
```
54+
55+
### [CHECK](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=99)
56+
57+
A check consists of a boolean expression that must evaluate to true for the data to be valid
58+
59+
```
60+
CREATE TABLE products(
61+
price numeric CHECK (price > 0)
62+
)
63+
```
64+
the constraint can also be given a name this can help with error messages as the mae will be provided
65+
```
66+
CREATE TABLE products(
67+
price numeric CONSTRAINT positive_price CHECK (price > 0)
68+
)
69+
```
70+
71+
A check can also refer to multiple columns by adding it to the end of the table
72+
73+
```
74+
CREATE TABLE products(
75+
price numeric CONSTRAINT positive_price CHECK (price > 0)
76+
discount_price numeric CONSTRAINT positive_price CHECK (discountPrice > 0)
77+
check( discount_price < price)
78+
)
79+
```
80+
81+
Check can oly be assumed to evaluate on update or delete so should only refer to items in the current row not other elements or table
82+
83+
### [PRIMARY KEY](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=103)
84+
85+
Assignees a column as a key. multiple can be used with `PRIMARY KEY (a,b)`
86+
87+
### [UNIQUE](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=101)
88+
89+
Ensures that data is unique among all the rows in the table. multiple can be used with `UNIQUE (a,b)`
90+
91+
### Foreign Key [REFERENCES](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=103)
92+
93+
Ensures that referential integrity is maintained between tables
94+
95+
```
96+
CREATE TABLE orders(
97+
product_id integer REFERENCES products
98+
)
99+
```
100+
101+
can use multiple foreign keys with `FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)`
102+
103+
a foreign key must reference a UNIQUE column or a primary KEY if no column is specified the primary key will be chosen
104+
105+
#### Referential Triggered Actions
106+
107+
When a value that a foreign key is referencing is updated or deleted
108+
109+
```
110+
CREATE TABLE orders(
111+
product_id integer REFERENCES products ON DELETE CASCADE
112+
)
113+
```
114+
115+
116+
Action | ON DELETE | ON UPDATE
117+
- |- |-
118+
RESTRICT | prevents the deletion | prevents the update
119+
CASCADE | any row(s) referencing deleted rows are also deleted | any row(s) referencing updated rows are changed to the new reference
120+
SET NULL | sets the reference to null | sets the reference to null
121+
SET DEFAULT | Sets the reference to the default value | Sets the reference to the default value
122+
123+
## DROP
124+
125+
**!! WARNING these commands can destroy large amounts of data**
126+
127+
## [DROP SCHEMA](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1843)
128+
129+
Removes a schema from the database
130+
131+
```
132+
DROP SCHEMA mystuff
133+
```
134+
135+
## [DROP TABLE](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1849)
136+
137+
Removes a table from the database
138+
139+
```
140+
DROP TABLE mytable
141+
```
142+
143+
144+
## Other
145+
146+
### Default
147+
148+
used to define a default value if none is provided
149+
150+
```
151+
CREATE TABLE meal(
152+
drink text DEFAULT "water"
153+
)
154+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
> CS258 and this guide is based on PostgreSQL other versions may be different
2+
3+
> This page shows limited select commands
4+
5+
> This page provides links in the titles to the PostgreSQL documentation It is recommenced you visit for a deeper understanding of the commands
6+
7+
## Data duplication
8+
9+
SQL Databases allow tables to contain duplicates of tuples. known as **Multi-set** or **bag** semantics
10+
11+
This has some advantages for checking for duplicates of a particular column e.g counting the number fo people who live at a particular address. It is also computationally expensive to remove duplicates and so should only ber done if required.
12+
13+
SQL query results are also tables
14+
15+
16+
17+
## [SELECT](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1927)
18+
19+
Select is one of the only commands used to retrieve data from a database and has a lot of additional commands and extensions to define a full selection.
20+
21+
### Basic
22+
23+
<pre>
24+
SELECT <<i>attribute1</i>,<i>attribute2</i>, ...> FROM <<i>table</i>> WHERE (<i>condition</i>)
25+
</pre>
26+
27+
28+
### Attributes
29+
These are the attributes to select form the table when filtered by the where clause
30+
31+
If using multiple tables `table.attribute` can be used to specify witch table the attribute comes from]
32+
33+
If there are several attributes with a similar or the came name then aliasing can be used with `AS`
34+
```
35+
SELECT shop AS Teashop FROM teas
36+
```
37+
Mathematical expressions can also be used with name aliasing
38+
```
39+
SELECT price*1.38 AS PriceInDollars FROM teas
40+
```
41+
42+
the character `*` can be used to select all the attributes
43+
44+
### Table
45+
46+
Name aliasing can also be used with tables
47+
```
48+
SELECT s.studentID FROM students AS s
49+
```
50+
51+
### WHERE
52+
53+
where is used to filter the table to produces a reduces output and operates on standard SQL condition statements
54+
```
55+
SELECT studentID FROM students WHERE yearOfStudy = 2
56+
```
57+
58+
```
59+
SELECT tea as expensiveTea FROM teas WHERE price > 4.5
60+
```
61+
62+
### ORDER BY
63+
this allows an order to be applied to the output of an SQL result
64+
65+
<pre>
66+
ORDER BY <i>expression</i> [ASC | DESC]
67+
</pre>
68+
69+
```
70+
SELECT tea as expensiveTea FROM teas ORDER BY price DESC
71+
```
72+
73+
### DISTINCT | ALL
74+
75+
It can be desirable to only have unique items in a query result adding `DISTINCT` removes any duplicates
76+
77+
```
78+
SELECT DISTINCT address FROM customers
79+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
SQL or Structured query Language is a high lever **declarative language**
2+
3+
Using SQL the DBMS can be told what to do ant it will find the optimal ways to perform the request
4+
5+
SQL is both a
6+
* Data definition Language
7+
* Data manipulation Language
8+
9+
Formal definition to SQL terms
10+
Relational | SQL
11+
-|-
12+
relation | Table
13+
Tuple | Row
14+
Attribute | Columns
15+
16+
Standards have evolved over time and there are now many different standards. SQL but operate against the chosen DBMS to the standard will be defined form that.
17+
18+
> CS258 and this guide will focus on PostgreSQl
19+
20+
## DataTypes
21+
22+
Data Types are used to describe the domain and provide information on how to interpret attributes. SQL has many different data types the full list ofr PostgreSQL can be found [here](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=179) (p.179)
23+
24+
| Name | Description |
25+
|-|-|
26+
integer | standard integer ~$$ \plusmn 2^{32}$$ *|
27+
bigint | Large range integer ~ $$\plusmn 2^{63}$$ * |
28+
numeric | near exact number $$131072$$ digits before decimal $$16383$$ digits after |
29+
char(n) | Fixed length character
30+
varchar(n)| Variable length character with limit
31+
text | Variable unlimited length
32+
boolean | true or false
33+
real | implementation of IEEE 754
34+
35+
<small>* approximate for exact specification see the documentation</small>
36+
37+
ANSI data types are a standard and may DBMS will recognize ANSI data types
38+
39+
## Comparison Operators
40+
sql uses the standard comparison operators
41+
42+
operator | Desc
43+
-|-
44+
= | Equal to
45+
\> | Greater than
46+
< | Less than
47+
\>= | Greater than or equal to
48+
<= | Less than or equal to
49+
<> | Not equal to
50+
51+
Will return TRUE, FALSE or UNKNOWN
52+
53+
UNKNOWN is returned when a comparison os performed with a NULL value
54+
55+
use
56+
57+
`IS NULL` and `IS NOT NULL` when comparing NULL values
58+
59+
## Logical Operators
60+
61+
operator | desc
62+
-|-
63+
AND | logical and
64+
OR | logical or
65+
NOT | logical not
66+
67+
## Pattern Matching
68+
https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=281
69+
70+
Sql has simple sting pattern matching using the keywords `LIKE` and `NOT LIKE`
71+
72+
pattern | desc
73+
-|-
74+
_ | a single character
75+
% | a sequence of 0 or more characters
76+
'C' | A single character c
77+
78+
if selecting a literal `%` or `_` a `\` can be used ans an escape character
79+
80+
`ILIKE` can be used in postgresQL to make the match case insensitive
81+
82+
### SIMILAR TO
83+
this command is available to use with regular expressions

0 commit comments

Comments
 (0)