Week 3 Flashcards

1
Q

SQL Sublanguages (5)

A
  1. DDL - data definition language
  2. DML - data manipulation language
  3. DQL - data query language
  4. DCL - data control language
  5. TCL - transaction control language
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define DDL

A
  • data definition language - statements used to create tables and databases as well as defined properties
  • create, alter, drop, truncate
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define DML

A
  • data manipulation language - statements used to insert or remove data from tables
  • insert, update, delete
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define DQL

A
  • data query language - statements used to query data from a table
  • select
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define DCL

A
  • data control language - statements used to control who can access data
  • grant, revoke
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define TCL

A
  • transaction control language - statements used to commit and restore data through transaction. Transactions group a set of tasks into a single execution unit.
  • commit, rollback, savepoint
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Numeric SQL Datatypes (7)

A
  1. INT
  2. TINYINT
  3. BIGINT
  4. DECIMAL
  5. NUMERIC
  6. FLOAT
  7. REAL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Date/Time SQL Datatypes (5)

A
  1. DATE
  2. TIMESTAMP
  3. DATETIME
  4. TIME
  5. YEAR
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Character/String SQL Datatypes (5)

A
  1. CHAR
  2. NCHAR
  3. VARCHAR
  4. NVARCHAR
  5. NTEXT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Binary SQL Datatypes (3)

A
  1. BINARY
  2. VARBINARY
  3. IMAGE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Miscellaneous SQL Datatypes (4)

A
  1. CLOB
  2. BLOB
  3. XML
  4. JSON
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

“Industry etiquette” for writing SQL

A
  • use UPPERCASE to refer to keywords, and lowercase for non-SQL specific entities (like tables or column names)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define database schema

A
  • Refers to the structure of the database - the columns of each table, their data types, and any constraints on them
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define constraints and list (7)

A
  • allow us to enforce the schema by ensuring consistency and integrity of the data in the table
    1. PRIMARY KEY
    2. FOREIGN KEY
    3. NOT NULL
    4. UNIQUE
    5. CHECK
    6. DEFAULT
    7. AUTO INCREMENT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain PRIMARY KEY constraint

A
  • uniquely identifies a record in a table

- inherently composed of two other constraints - unique and not null

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

What are the properties a transaction must follow (4)?

A

ACID

  1. Atomicity - all or nothing (transaction must fully complete or rollback)
  2. Consistency - data remains in a consistent state after the transaction
  3. Isolation - transactions do not interfere with one another when running
  4. Durability - data will persist even in case of catastrophic failure
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Explain the different isolation levels (4).

What read phenomena do each prevent?

A
  1. Serializable - slowest, prevents phantom reads
  2. Repeatable Reads - prevents non-repeatable reads, allows phantom reads
  3. Read Committed - prevents dirty reads, allows non-repeatable reads
  4. Read Uncommitted - fastest, allows dirty reads
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is RDBMS?

A
  • relation database management system
  • A database is a collection of data, and the management system is a set of programs to store and access that data in a quick and effective manner
  • Developed to handle large amounts of data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What vendors are supported by RDS?

A
  • MySQL
  • Oracle
  • SQLServer
  • Postgres
  • Amazon Aurora
  • MariaDB
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Relational vs non-relational databases

A
  • non-relational does no store data within tables that relate to each other, thus does not use SQL to interact with the database
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Explain FOREIGN KEY constraint

A
  • signifies that a column represents a reference to the primary key of another table. This allows us to create relationships between tables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Explain NOT NULL constraint

A
  • enforces that all records must have a field for the column on which this constraint is applied
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Explain UNIQUE constraint

A
  • records cannot be inserted if another record already has the same value for the column on which this is declared (similar to NOT NULL)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Explain CHECK constraint

A
  • provides a way of performing validation on values before records are entered into the table
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Explain DEFAULT constraint
- allows setting default values on columns for records that are inserted into the table
26
Explain AUTO INCREMENT constraint
- allows a unique number to be generated automatically when a new record is inserted into a table
27
What is a candidate key? What is a composite key? What is a surrogate key?
candidate - multiple columns that together create a primary key to uniquely identify rows composite - the actual combination of columns to use as the primary keys surrogate - a column that can uniquely identify a record (can act as Primary Key)
28
Multiplicity relationship: one to one
- each entity in a table only relates to a single entity in another table - to enforce the one to one aspect, use a foreign key to relate to the primary key on another table; putUNIQUE constraint on the foreign key column
29
Multiplicity relationship: one to many / many to one
- one entity can belong to, own, or otherwise relate to multiple other entities - to create this in the database, we add the foreign key only on the many side of the relationship
30
Multiplicity relationship: many to many
- implies a one-to-many relationship in both directions on the entities - we cannot provide a direct link between the tables in the database - instead, use a 3rd table, called a junction table, to connect them. the junctiontable will have 2 foreign keys and will be the many side of both relations
31
what are junction tables?
- direct link between tables in a many to many relationship | - also known as bridge table
32
what is normalization? normal forms (3)?
- The process of reducing redundancy in a database 1. 1NF 2. 2NF 3. 3NF "swearing to tell the truth, the whole truth, and nothing but the truth"
33
How would you describe AWS? What is "the cloud" or "cloud computing" and why is it so popular now?
- AWS is leading cloud platform - Provides resources and services - Business benefits: no need to manage infrastructure; useful tooling; worldwide distribution/deployment - Downsides: locked into cloud provider
34
Define Infrastructure, Platform, and Software as a Service
1. IaaS: direct access to hardware; most control 2. PaaS: hardware abstracted; developer provides the software to run 3. SaaS: software abstracted
35
What's the difference between a Region and an Availability Zone (AZ)?
Regions cover a geographic area, all over the world AZs are individual data centers; 3 AZs per region for redundancy and fault tolerance
36
How are you charged for using AWS services? Does it vary by service?
By uptime, data stored, data exchanged
37
Different ways to interact with AWS services?
- Web console - AWS CLI / REST API - AWS SDK (software development kit)
38
What is RDS?
relational database service - a PaaS offering that loads RDBMS software based on your configuration
39
Explain the AWS service - Route 53
- DNS (domain name service) for registering domain names like '.com' or '.net' addresses - Works to resolve 'somesite.come' into an IP address where your server is located
40
Explain the AWS service - VPN
- Virtual Private Network - can place your cloud resources in a VPN for network security purposes
41
Explain the AWS service - IAM
- Identity and Access Management - manage users, roles, and policies to provide access to your cloud resources
42
Explain the AWS service - ELB
- Elastic load balancer - distribute incoming requests between different services
43
What is JDBC?
- Java Database Connectivity | - A Java API used to execute queries on various databases. Uses JDBC drivers to connect with the database
44
What are the core interfaces / classes in JDBC (8)?
1. DriverManager class - to make a connection with a database driver 2. DataSource interface - for retrieving connections, an alternative to DriverManager 3. Connection interface - represents a physical connection with a database 4. SQLException class - a general exception thrown when something goes wrong when accessing the database 5. Statement interface - used for executing static SQL statements 6. PreparedStatement interface - represents pre-compiled SQL statements 7. CallableStatement interface - used to execute stored procedures 8. ResultSet interface - represents data returned from the database
45
What is a stored procedure and how would you call it in Java?
A stored procedure is an executable block of code that is written in PL/SQL and stored in the Oracle database. A stored procedure is called from a Java class using a CallableStatement object. When the procedure is called, its name and any relevant parameters are sent over the JDBC connection to the DBMS, which executes the procedure and returns the results (if applicable) via the connection.
46
What is the difference between Statement and PreparedStatement?
PreparedStatements are pre-compiled by the JVM. The database doesn't have to compile the SQL each and every time it is executed. PreparedStatement can be parameterized, which can make the SQL more readable. Furthermore, PreparedStatement will properly escape reserved characters to prevent SQL injection attacks
47
Steps to executing an SQL query using JDBC (6)?
1. Register the driver using `.forName()` (or let `DriverManager` detect and load automatically from classpath) 2. Create the connection (`DriverManager.getConnection(url,username,password)`) 3. Create a statement for executing the SQL query (`Statement st = conn.createStatement()`); 4. Execute the SQL query (`ResultSet rs = st.executeQuery(String sql)`) 5. Use `ResultSet` to get values returned (`rs.getInt(1)`, etc) 6. Close the connection (`conn.close()`)
48
How to execute stored procedures using JDBC?
Use the `Callable` statement interface
49
Which interface is responsible for transaction management?
The `Connection` interface - can `commit`, `rollback`, and `savepoint`
50
What is Maven?
A build automation and dependcy management tool for Java applications
51
What is the Maven lifecycle (8)?
Validate => project is correct and all necessary information is available Compile => compiles project source code Test => tests all compiled code Package => packages all compiled code to WAR/JAR file Integration => performs all integration tests on WAR/JAR Verify => runs checks on the results of integration tests Install => installs WAR/JAR to local repository Deploy => copies final WAR/JAR to the remote repository
52
Where / when does Maven retrieve dependencies from? Where are they stored locally?
Maven first looks to see if the dependency is in the local repo under `.m2` directory. If not, it will download the necessary .jar file(s) from the remote central Maven repository into the `.m2` directory
53
What is the POM and what is the pom.xml?
POM stands for project object model and is the model used by Maven to understand project attributes and dependencies. The pom.xml is the xml document which lists those attributes and dependencies
54
The first normal form (1NF) enforces that a table must:
- Have a primary key - Each column should be as granular as possible (e.g. "Name" column should be broken up into: "First Name", "Last Name", "Middle Name", etc..)
55
To be in second normal form (2NF), a table must:
- must already be in 1NF - Cannot have columns that are dependent on only one part of the key - If there are no composite primary keys, you are automatically in 2NF
56
To get to third normal form (3NF), a table must:
- must already be in 2NF - Not have transitive dependencies - This means that if column C relates to column B which relates to column A which is the primary key, this is not in 3NF because C is related to the primary key but indirectly (it is a transitive dependency)
57
Explain Query Clause WHERE
operates on individual records
58
Explain Query Clause HAVING
used with GROUP BY and aggregate functions to operate on groups ofrecords added to SQL because the WHERE keyword cannot be used with aggregate functions.
59
Explain Query Clause ORDER BY
sorts the results set on a particular column (ascending or descending)
60
Explain Query Clause GROUP BY
used with an aggregate function to combine records together
61
what is an alias in SQL?
- used to give a table, or a column in a table, a temporary name. - often used to make column names more readable. - only exists for the duration of that query. - created with the `AS` keyword
62
What are the different joins (8)?
1. INNER JOIN 2. OUTER JOIN 3. LEFT (OUTER) JOIN 4. RIGHT (OUTER) JOIN 5. FULL (OUTER) JOIN 6. CROSS JOIN 7. SELF JOIN 8. NATURAL JOIN
63
Describe INNER JOIN
only joins records that match based on join predicate
64
Describe OUTER JOIN
joins all records in both tables, will have null as value if doesn'texist
65
Describe LEFT [OUTER] JOIN
joins all records in left table, plus any records that match joinpredicate on right
66
Describe RIGHT [OUTER] JOIN
joins all records in right table, plus any records that match joinpredicate on left
67
Describe FULL [OUTER] JOIN
Returns all rows from both tables specified including the ones which had null values on either side
68
Describe NATURAL JOIN
joins tables based on columns that have the same name in both tables
69
Describe CROSS JOIN
cartesian product of records in both tables, i.e. each record in one table paired with each in another (results in M x N records)
70
Describe SELF JOIN
joins a table with itself, FK points to PK of same table - e.g. employees table has reportsTo column for referencing manager in same table
71
What are joins?
- Used to combine two or more tables, joins are a database technique used in SELECT statements. - Joins are normally performed comparing primary keys to foreign keys, however, they can be performed with any type of column, as long as the types match. - Joins are a way of denormalizing a set of tables in order to aggregate or process some query.
72
What is the difference between joins and set operators?
Joins combine tables horizontally Set operators combine results sets vertically
73
What are the 4 set operators?
UNION (ALL) - combines results sets, removes duplicates; like UNION but allows duplicates INTERSECT - returns the records that are in common between the two results sets MINUS - Removes from the first result set any rows that appear in the second result set and returns what remains EXCEPT - Same as MINUS, but for SQLServer instead of Oracle
74
What is SQL?
- Structured Query Language | - allows us to interact with and manipulate relational databases
75
How is data structured in a SQL database?
Data is inserted as records, or rows, in a table. A database can have many tables that are related to each other. Each table has different columns, each with its own data type and possibly constraints.
76
What is an ERD? How is it useful?
Entity relationship diagram - used to model and show relationships between different tables in the database
77
Explain the concept of referential integrity
- Every foreign key in the database points to a valid, existing record - If this is broken, we have what is called an "orphan record"
78
What is a cascade delete?
- Setting that allows referenced records to be deleted as well, if record being deleted has a foreign key - Ensures referential integrity, prevents orphaned records
79
What is the difference between DELETE, DROP, and TRUNCATE commands?
DROP removes a table entirely; DELETE will remove specific rows (unless no WHERE clause); TRUNCATE will empty a table of all data but leave the schema intact DROP and DELETE can be rolled back; TRUNCATE cannot
80
What are some SQL clauses you can use with SELECT statements (8)?
1. WHERE 2. HAVING 3. AND 4. OR 5. GROUP BY 6. ORDER BY 7. LIMIT 8. LIKE
81
What is an index?
allow databases to speed up the process of retrieving data (like an index of a book, know where to go for info)
82
What is the purpose of a view? What about an index?
View is like a stored/cached query, useful for removing sensitive fields when giving access to a table Index increases performance of queries when done on the column with the index
83
What's the difference between a clustered and non-clustered index?
Clustered - written to disk, only one allowed per table Non-clustered - uses references in memory, multiple allowed
84
What is a trigger? Give the syntax for creating a trigger.
Trigger is a special type of stored procedure that runs when an event happens in the database
85
How would you setup a primary key that automatically increments with every INSERT statement?
- Postgres >10: use the IDENTITY keyword - Oracle 11g: use trigger and sequence together so that sequence increments on insert - Oracle 12c: IDENTITY keyword now exists
86
What is the difference between scalar and aggregate functions? Give examples of each
Scalar - operates on a single value, e.g. UPPER, LOWER, TRIM, CONCAT, DATE Aggregate - operates on multiple values, e.g. MIN, MAX, AVG, SUM
87
What's the difference between implicit and explicit cursors?
- Implicit cursor created automatically for you when you run a query - Cursor initially points to before the first record in a resultset - Explicit cursors must be created manually but give more control
88
Write a query to find all data in the table `Employees`
SELECT * FROM employees
89
Write a query to find employees with a salary over 75
SELECT * FROM employees WHERE salary > 75
90
Write a query to find employees whose first name contains an 'e' or whose last name begins with 'S'\
SELECT * FROM employees WHERE firstName LIKE 'e' OR lastName LIKE 'S'
91
Write a query to find the first name of all employees who do not work in accounting
SELECT firstName FROM employees WHERE dept != 'Accounting'
92
Write a query to find the highest paid salesperson
SELECT * FROM employees ORDER BY salary DESC LIMIT 1
93
Write a query to combine the resultsets of any two previous queries
Must use UNION or UNION ALL
94
Steps for creating a database connection
1. Register the JDBC driver 2. Open a connection using: - Database URL - Username - Password 3. Execute some SQL statement using either: - Statement - PreparedStatement - CallableStatement 4. Retrieve the results that are returned in a ResultSet object
95
Explain JDBC String
The database URL is an address pointing to the database to be used The String is made up of the JDBC URL, your username, and password Connection conn = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD)
96
Explain Autocommit Mode
By default, when a connection is created it is in auto-commit mode, so every SQL statement acts as a transaction and is committed immediately after execution. In order to manually group statements into a transaction, simply call: Connection conn = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);conn.setAutoCommit(false);
97
Explain Data Access Objects (design pattern)
- logically separate the code that accesses the database into Data Access Objects - to use: define an interface which declares methods through which the database will be queried. Then, concrete implementation classes can implement the interface and contain the data access logic to return the required data
98
Explain PL/SQL - Triggers
stored programs, which are automatically executed or fired when some events occur
99
Explain PL/SQL - Cursors
holds the rows (one or more) returned by a SQL statement
100
Explain PL/SQL - Procedures
do not return a value directly; mainly used to perform an action
101
Explain PL/SQL - Sequences
an object that is used to generate a number sequence this can be useful when you need to create a unique number to act as a primary key