Intro to SQL Flashcards

1
Q

Database

A
An organized collection of stored and persisted data.
A database (or db for short) is an organized collection of stored and persisted data. Databases are responsible for storing data, organized in ways that are useful to us programmers.

Where are databases? Databases are stored on any compatible machine, whether it’s a dedicated database server, or even on our own laptops.

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

SQL

A

A query language specifically designed to talk to relational databases. Used in a sentence: “I added 50 new records to the database using SQL statements,” “I forgot the SQL syntax to create a database and create a table in that database”

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

Postgres

A

A specific relational database. Synonyms: PostgreSQL, psql, pg. Used in a sentence: “I heard that postgres is a database that’s a little different from mySQL, but not very different and I could learn both,” “When talking to my database, I got an error saying that the psql connection was bad, so I restarted my Postgres services”

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

Database entity

A

A single concept that needs to be stored in a table and database, such as a kind of person, product, thing, or object. “When I started my project, I considered all of the database entities I needed to store, such as users, posts, and comments.”

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

Primary key

A

An attribute dedicated to being a unique identifier for each row in a table. In a sentence: “Even though two rows had the same name, they had different primary keys,” “I was able to retrieve the correct row each time because I queried using primary keys.”

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

How does SQL Communicates With Relational Databases?

A

SQL is a query language. It’s a declarative programming language specifically designed to talk to relational databases.
We will learn SQL syntax and write SQL statements. Relational databases will interpret the SQL statements and give us back the records we need.
We read that SQL is a declarative programming language. Essentially, this means we use the language to express what we would like to have done, but not how to do it. We can ask a database for all kinds of data with SQL, but we will never have to tell it how to iterate over a table, or how to find a certain row. On the other hand, Python is considered an imperative language. We have to tell it how to do everything! If we want to find an item in a list, we have to write the exact steps of how to do that!

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

Relational Databases

A

Relational databases are a kind of database.

Relational databases organize data in the form of many tables. Relational databases contain many tables of data, where the columns are attributes and the rows are records.

The family tree of relational database systems is very large indeed. Generally speaking, these database systems all operate similarly and carry out the same responsibilities. They usually differ in how they are designed, ways that they are used, and various lower-level implementation details.
This is a short list of currently supported relational database systems:

    MySQL
    PostgreSQL
    SQLite
    Microsoft SQL Server
    Microsoft Azure SQL Database
    Apache Hive
    MariaDB
    Oracle
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Using Postgres

A

PostgreSQL, also known as Postgres (and often seen as psql), is a free and open-source relational database system. PostgreSQL is:

  • *Free and open-source
  • *Built-in compatibility with Heroku, a popular platform used often for free Flask app deployment (Flask is a Python package used to build web applications)
  • *It is easy to install on macOS
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Parts of a Database

A

Databases structure and store data that is related to one or more projects.

One database can store data for many different database entities. A database entity is any single concept that needs to be stored, usually a kind of person, product, thing, or object (a noun!). For example, imagine a micro-loan system. One single database for this webapp could have the entities “Borrower,” “Lender,” “Loan,” “Loan Request,” and “Repayment.”

There are several components of a database. The following is an overview of a few key concepts:

* *Schema
* *Tables
   - Records and Attributes
    - Primary Keys (and IDs)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Schema

A

A database schema is a set of programming statements that describe the organization of one database.

The schema usually defines:

    The name of each table
    The attributes (columns) of each table
    The data types of each column
    Any rules and constraints for each column or table
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Data Types and Constraints in SQL

A

SQL databases enforce data type and other constraints on columns of tables.

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

Some data types available in Postgres are..

A

boolean, integer, varchar (Text. The user may define a maximum lengther), text (Text, The user does not define a maximum length), numeric (a number that allows floats with precision), timestamp (Date and time, including time zone), and json (JSON)

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

Constraints

A

Constraints are rules on a column or table that can reject invalid data from ever entering the database. Some example constraints that can be defined on a column or table are:

* *A value cannot be a null value (similar to None in Python). This implies that a value is required.
* *Integers must be greater than 0.
* *A value must be unique compared to the other values in the same column and table.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Tables

A

Databases can have one or more tables. Each table usually represents one entity.

The columns of the table are the attributes of the entity, and each row is a record of one entity.

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

text vs. varchar

A

The text data type is used for strings. What is varchar? The varchar data type is great for text that isn’t expected to be long. It also helps with db performance.

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

Result Set

A

A set of rows from a database, as well as metadata about the query such as the column names, and the types and sizes of each column. Used in sentence: “My query returned a result set with 1000 rows.”

17
Q

Query

A

A request to access data from a database. Used in a sentence: “I wrote a query to find the most popular books in the library.”

18
Q

Removing Records With DELETE

A

!!Can Delete Multiple Records At Once!! All relational databases including Postgres support deleting one or multiple records at once. This can be dangerous because a mistake could cause unintentional deletions!
To avoid unintentional data loss, it’s a good idea to start with a SELECT query to identify the data we wish to delete.
Without WHERE, All Rows Are Deleted

If you omit the WHERE clause, ALL records will be deleted!

19
Q

Result Set

A

A set of rows from a database, as well as metadata about the query such as the column names, and the types and sizes of each column. How to Use in a Sentence: My query returned a result set with 1000 rows.

20
Q

Query

A

A request to access data from a database. How to Use in a Sentence: “I wrote a query to find the most popular books in the library.”

21
Q

Compound Condition

A

A condition formed from multiple simple comparisons combined with one of the SQL logical operators: AND, OR, or NOT. How to Use in a Sentence: “I can use a compound condition to restrict my query results to students who are sophomores and taking World History.” “Let’s expand our query result to students who are either sophomores or juniors by using a different compound condition.”