Week 2 - SQL Intro Flashcards
What are the two sublanguages?
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Manipulation Language (DML)
- Queries can be written intuitively
- Works with the content of the database (rows/tuples).
- Used to insert, update, delete, and query data within tables.
Data Definition Language (DDL)
- Deals with the schema or “shape” of the database.
- Used to define and modify the structure of tables and their relationships.
What is the most widely used relational query language?
SQL
What two aspects are languages defined by?
Syntax (formal rules of the language, grammar)
Semantics (meaning of words, sentences, expressions)
Information in a Relational Database is held using what data structure
Relations
What are relations?
A mathematical concept representing a subset of a Cartesian product of sets.
What properties does a table have?
Name
Unchanging set of columns (attributes - named and typed)
Time varying set of rows (these are the data entries)
State table vocab and its corresponding relation vocab
TABLE ==> RELATION
Table Name ==> Relation Name
Column Name ==> Set Name
Column Datatype ==> Set
Row ==> n-tuple
All rows ==> The relation
Describe tables in terms of relations
A table is essentially a concrete implementation of a relation in a relational database.
Examples of DML commands
INSERT, DELETE, UPDATE, SELECT.
Examples of DDL commands
CREATE, ALTER, DROP.
What is the key difference between DDL and DML in terms of separation of concerns?
DDL focuses on the meta-information about the database (e.g., column names and types), while DML manipulates the actual data stored in tables.
Practical Advice for creating tables
Think carefully about what you need now, and what you might need in the future.
Discuss your schema with end-user.
What are the semantics of this SQL?
CREATE TABLE student(
name VARCHAR(255),
id INTEGER,
exam1 INTEGER,
exam2 INTEGER);
[[student]] = [[VARCHAR(255)]] x [[INTEGER]] x [[INTEGER]] x [[INTEGER]]
List numeric datatypes for SQL
Integer: INT
Real: DECIMAL, NUMERIC, FLOAT, DOUBLE
List string datatypes for SQL:
Fixed length: CHAR(n)
Varying length: VARCHAR(n)
List time datatypes for SQL:
DATE, DATETIME
(Side note: Format depends on your DBMS. For example, MySQL uses YYYY-MM-DD whereas Oracle uses DD-MMM-YYYY)
Give examples of a relation between smaller sets
Prices must be positive
NULL can cause crashes
Give an example of a constraint between columns
AskPrice > BidPrice
Give an example of a constraint within columns
Primary key must be unique
Give an example of a constraint between tables
Foreign & Primary keys are related
What are the two types of constraints that can be defined in SQL?
Column constraints (within columns)
Table constraints (between columns or tables)
Provide SQL examples of column constraints
AskPrice DOUBLE CHECK (AskPrice >= 0)
Ticker VARCHAR(255) NOT NULL
Provide SQL examples of table constraints
CHECK (AskPrice >= BidPrice)
PRIMARY KEY(Ticker)
What is a function? (in terms of mathematics)
A relation where every input maps to exactly one output.
Describe how primary keys behave like a mathematical function
Each primary key value corresponds to exactly one row.
What is a composite primary key?
A primary key consisting of two or more columns. The combination of values in these columns must be unique for each row in the table.
What do foreign keys do?
Establish a relation between tables.
What is a foreign key?
A set of attributes in table A that refers to a primary key in table B.