Database Flashcards

1
Q

Types of Database

A

1) SQL - Structured Query Language

2) NoSQL - Not only Structured Query Language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

SQL characteristic

A

1// Pre defined Structure
2// Doesn’t like content to be going beyond whats defined - or else it will assign the other values to Null unless its specified not todo so.
3// Is great for keeping relations among tables.
4// Can scale vertically only.
5// As the tables scales vertically, sorting through information becomes harder and slower.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

NoSQL characteristic

A

1// Doesn’t need a rigid structure. It is flexible.
2// No great at having relations among various tables and complex relationship makes NoSQL slow.
3// Its scalability is worlds better horizontally as you can easily break and divide the tables without any concerns of structure or slowing down.
4// The technology is pretty new - so its constantly developing and changing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The most important thing to do in Database

A

CRUD

Create, Read, Update and Destroy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

SQL - Create Table

A

CREATE TABLE table_name (
column1 data-type, // amount INT
column2 data-type, // name STRING
….
)

As you note here, most operations and keywords are in ALL CAPS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

SQL - data-type

A

There are many, check them out here - https://www.w3schools.com/sql/sql_datatypes.asp

Commonly used one are INT, STRING, MONEY (// for currency), CHAR(size) (// to limit the letters getting input), TEXT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

SQL - PRIMARY KEY

A

PRIMARY KEY (column_name) is specified to make a row uniquely identify from the rest of the row.

Primary Key is assigned to one of the row that have been initialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

SQL - NOT NULL

A

While initializing a column, we can force the value to be NOT NULL.

Example -
CREATE TABLE prices ( id INT NOT NULL, amount MONEY NOT NULL, name STRING, PRIMARY KEY (id) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

SQL - INSERT INTO

A

After initializing the table, we can insert data into the table using INSERT INTO.

For example -
INSERT INTO table_name VALUES (col1_val, col2_val, col3_val)

OR

INSERT INTO table_name (col1, col2, col3) VALUES (col1_val, col2_val, col3_val)

The second way of writing the code is chosen when we need to insert data in specific columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

SQL - Read the table

A

SELECT command is used for reading database.

For example-
SELECT * FROM table_name // => Shows the whole table under the table_name

SELECT id, amount FROM table_name // => Shows the id and amount column under table_name

SELECT if, amount FROM table_name WHERE conditions // => Shows the id, amount column under table _name and condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

SQL - UPDATE

A

UPDATE table_name SET column_name=value WHERE conditions

Without ‘WHERE’, everything in the column will be set to ‘value’. So, WHERE is important.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

SQL - Update the Table colums

A

ALTER TABLE table_name ADD new_column_name data-type

Changes the table layout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

SQL - Delete

A

DELETE FROM table_name //=> deletes everything from the table if no WHERE is specified
WHERE column=value //=> delete the row that satisfies the condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

SQL - Relation between tables

A

We use FOREIGN KEY to establish some relation between the tables

For example -
CREATE TABLE table_name (
         quantity INT,
         customer _name STRING,
         FOREIGN KEY (quantity)  REFERENCES price_table(price)
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

SQL - JOIN tables

A

After having relations among tables, we can join the tables to make a new table.

SELECT table1_name.table1_column_name, table2_name.table2_column_name,
FROM table_where_join_occurs
INNER JOIN table1_name_to_join ON table_name.foreignkey = table_name.primarykey

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

MongoDB

A

NoSQL Database program

Has its own documentation - https://docs.mongodb.com/manual/

17
Q

MongoDB - Start server

A

mongosh - Mongoshell to interact with Mongo servers.

Writing ‘mongosh’ in the command line will initiate the server and enter the mongo command line interface.

18
Q

mongosh commands

A

show dbs - show databases saved in local disk
use ‘database_name’ - initiate a database
show collections - show the name of tables present in a database
db - show the current database we are working in.

19
Q

Mongosh - Start Collection

A

db. collection.insertOne({json_type_file_syntax_here})
db. collection.insertMany( { }, { }, { } )

if a collection is not present already - it will create the collection when we pass this command.

20
Q

Mongosh - Read Collection

A

Read everything in a collection - db.collection.find( )

Read something particular - db.colllection.find( {query here} , {projection here} )

We can use different query operator to widen our read operations. Projection means what we want the o/p to show. If we have something like {name: 1, address: 0} then the o/p will show the name but not the address corresponding to the name. So projection takes only 0 and 1 value for ‘no’ and ‘yes’.

21
Q

Mongosh - Update Document

A

db.collection.updateOne( {query here} , {$set: {new document column} } )

22
Q

Mongosh - Delete document

A

db.collection.deleteOne( {query here} )

23
Q

Mongosh - Relation between collection

A

Establishing relation between data is much more simplistic in Mongo.

We simply have a whole array of data embed into a bigger array to show relation.

For example -
We have a bookstore - and every book has got reviews.

Then to the database bookstore - we have a collection of Books - inside which each document has a unique book name and have author’s information as an object in an array. And there is another document inside the same document - which also lists the multiple review of the book.
SO -> Bookstore > Book [ ] > { name: , author : [ ] > { name: , address: } , reviews [ ] > {stars: , reviewer name: } }

24
Q

Native MongoDB driver for Nodejs

A
npm install mongodb
const MongoClient = require('mongodb').MongoClient;

Documentation - https://docs.mongodb.com/drivers/node/current/