|
| 1 | +title: Database Week 1 |
| 2 | +author: |
| 3 | + name: Unmesh Joshi |
| 4 | + url: https://unmeshjoshi.wordpress.com |
| 5 | +output: week1.html |
| 6 | +controls: true |
| 7 | + |
| 8 | +-- |
| 9 | + |
| 10 | +# Database 101 |
| 11 | + |
| 12 | +-- |
| 13 | + |
| 14 | +### What is a Database ? |
| 15 | +* Definition : Organized collection of data and rules about its manipulation |
| 16 | +* Client-server architecture : E.g. (Relational) DBMS |
| 17 | +* Files as database |
| 18 | +* Data structure/object as database |
| 19 | +```js |
| 20 | +const capitals = [ |
| 21 | + "Amsterdam", |
| 22 | + "Delhi", |
| 23 | + "Damascus", |
| 24 | + "Madrid"]; |
| 25 | +``` |
| 26 | +-- |
| 27 | + |
| 28 | +### Relations = Table |
| 29 | + |
| 30 | +* What is a relation (in the following sentences)? |
| 31 | +* Delhi is the capital of India |
| 32 | +* Amsterdam is the capital of Netherlands |
| 33 | +* Damascus is the capital of Syria |
| 34 | + |
| 35 | +Dan, 29, works in Amazon and lives in Seattle. His friend Ben who just celebrated |
| 36 | +his 24th birthday works in Facebook and lives in Redmond. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### DBMS implementations |
| 41 | + |
| 42 | +* ** MySQL ** |
| 43 | +* PostgreSQL |
| 44 | +* MongoDB (NoSQL) |
| 45 | +* Cassandra (NoSQL) |
| 46 | +-- |
| 47 | + |
| 48 | +### MySQL |
| 49 | + |
| 50 | +* MySQL server (runs as a service, default port: 3306) |
| 51 | +* mysql: monitor / terminal / client (to connect to the server and execute stuff) |
| 52 | +* mysqladmin: Administering a MySQL Server |
| 53 | + |
| 54 | +-- |
| 55 | + |
| 56 | +### Create a user, grant privileges, create database |
| 57 | + |
| 58 | +``` |
| 59 | +# Create a new user with privileges |
| 60 | +mysql> GRANT ALL PRIVILEGES ON *.* TO 'hyfuser'@'localhost' IDENTIFIED BY 'hyfpassword'; |
| 61 | +
|
| 62 | +Query OK, 0 rows affected, 1 warning (0.01 sec) |
| 63 | +
|
| 64 | +# Create a database to work with |
| 65 | +
|
| 66 | +mysql>create database todo_app |
| 67 | +
|
| 68 | +``` |
| 69 | +--- |
| 70 | + |
| 71 | +### Create Table in MySQL |
| 72 | + |
| 73 | +## Collection of rows and columns |
| 74 | +## SYNTAX |
| 75 | +``` |
| 76 | +CREATE TABLE table_name (column_name, column_type [, column2_name, column2_type]); |
| 77 | +``` |
| 78 | + |
| 79 | +* INT(N) type |
| 80 | +* DATE, DATETIME and TIMESTAMP, (set time_zone = '+03:00') |
| 81 | +* BLOB (LOAD_FILE(filename)) |
| 82 | +-- |
| 83 | + |
| 84 | +### Fill up the table in MySQL: INSERT rows |
| 85 | +A row (aka record or tuple) represents a single, implicitly structured data item in the table. |
| 86 | + |
| 87 | +## SYNTAX |
| 88 | +``` |
| 89 | +INSERT INTO table_name VALUES(value1, value2 [,value3,...]); |
| 90 | +``` |
| 91 | +* INSERT INTO table_name VALUES(...values...) |
| 92 | +* INSERT INTO table_name (column names) VALUES(..values...) |
| 93 | +* INSERT INTO table_name SET column_name = {expr | DEFAULT} |
| 94 | + |
| 95 | +-- |
| 96 | + |
| 97 | +### See the content : SELECT |
| 98 | + |
| 99 | +* FROM clause : multiple tables |
| 100 | +* WHERE clause : Multiple conditions(AND, OR, NOT) Operators ( =, <>, BETWEEN, LIKE, IN) |
| 101 | +* Aggregation : SUM, AVG, COUNT |
| 102 | +* Joins : Natural join, inner join, left outer and right outer join |
| 103 | +## Simple SYNTAX for a single table |
| 104 | +``` |
| 105 | +SELECT */column_name FROM TABLE |
| 106 | +WHERE condition1 AND/OR |
| 107 | + condition; |
| 108 | +``` |
| 109 | +-- |
| 110 | + |
| 111 | +### INSERT and SELECT together |
| 112 | + |
| 113 | +``` |
| 114 | +INSERT INTO 'employees' ('shop_id', 'gender', 'name', 'salary') |
| 115 | +SELECT 3, |
| 116 | + LEFT(gender, 1), |
| 117 | + CONCAT_WS(' ', first_name, last_name), |
| 118 | + salary |
| 119 | +FROM transferred_ employees |
| 120 | +WHERE transfer_date > '2008-01-01'; |
| 121 | +``` |
| 122 | +-- |
| 123 | + |
| 124 | +### Uniqueness and Keys |
| 125 | + |
| 126 | +* Super key : set of columns that uniquely identify a row |
| 127 | +* Candidate key : minimal super key that can uniquely identify a row |
| 128 | +* Primary key : choice of candidate key chosen by database designer : cannot be null |
| 129 | +-- |
| 130 | + |
| 131 | +### Relations |
| 132 | + |
| 133 | +* One to One : Account to Employee |
| 134 | +* One to Many : Department to Employee |
| 135 | +* Many to Many : Project to Employee |
| 136 | +-- |
| 137 | + |
| 138 | +### Foreign key |
| 139 | + |
| 140 | +* A field (or collection of fields) in one table that uniquely identifies a row of another table or the same table |
| 141 | +* In simpler words, the foreign key is defined in a second table, but it refers to the primary key or a unique key in the first table |
0 commit comments