Foundations Flashcards
What is SQL?
a programming language designed to manage data stored in relational databases
What is a relational database?
a type of database that stores and provides access to data points that are related to one another
What is a table?
a collection of data organized into rows and columns
What is a column?
a set of data values of a particular type
What is a row?
a single record in a table
What are common data types?
integer, text, date, real
What is an integer data type?
a positive or negative whole number
What is a text data type?
a text string
What is a date data type?
the date formatted as YYYY-MM-DD
What is a real data type?
a decimal value
What is a statement?
text that the database recognizes as a valid command, always ends in a semicolon (;)
What does CREATE TABLE do?
a clause also known as a command; performs specific tasks in SQL. conventionally written in capital letters.
What is the point of the tablename in a statement?
refers to the name of the table that the command is applied to
What is a parameter?
a list of columns, data types, or values that are passed to a clause as an argument
How do we write statements?
they can be written all on one line, or split up across multiple lines if it makes it easier to read
What does CREATE do?
allows us to create a new table in the database
What is an example of creating a table?
CREATE TABLE celebs (
id INTEGER,
name TEXT,
age INTEGER,
);
What does INSERT do?
it inserts a new row into a table
When do we use INSERT?
when we want to add new records
What is an example of an INSERT?
INSERT INTO celebs (id, name, age)
VALUES (1, ‘Justin Beiber’, 29);
What does INSERT INTO do?
a clause that adds the specified row or rows
What does SELECT do?
used to fetch data from a database
What does SELECT return?
a new table called the result set
What is an example of a SELECT statement?
SELECT name FROM celebs;
What is the use of the *?
a special wildcard character that we can use to refer to “all”
What does ALTER TABLE do?
adds a new column to a table
What is an example of an ALTER TABLE?
ALTER TABLE celebs
ADD COLUMN twitter_handle TEXT;
What does NULL mean?
a special value in SQL that represents missing or unknown data
What does CTE stand for?
Common Table Expression
What is a CTE?
essentially a named subquery, it functions as a virtual table that only its main query can access
Why do we use CTEs?
it helps save space (and money) in our database because we are only allowed a certain amount of helper tables
What does UPDATE do?
edits a row in a table
What is an example of an UPDATE statement?
UPDATE celebs
SET twitter_handle = ‘@taylorswift13’
WHERE id=4;
What does DELETE do?
(FROM) deletes one or more rows from a table
What is an example of a DELETE statement?
DELETE FROM celebs
WHERE twitter_handle IS NULL;
What does IS NULL represent?
a condition in SQL that returns true when the value is NULL and false otherwise
What are constraints?
add information about how a column can be used, are invoked after specifying the data type for a column
What is a primary key?
columns can be used to uniquely identify the row
What happens when we attempt to insert a row with an identical value to a row already in a table?
it will result in a constraint violation
What does UNIQUE represent?
columns have a different value for every row (tables can have many different unique columns)